----------------------------------------------------------------------
--- Knud van Eeden --- 12 December 2009 - 11:20 pm -------------------

Computer: Editor: TSE: Window: Top: Always: Operation: Set: How to: Given the title of the window, how to keep this window always on top?

---

Use e.g. the Microsoft Windows API 'FindWindow' to get the window handle, given the title of the window.
Then use Microsoft Windows API 'SetWindowPos' to set and keep the window with that handle on the top of all other windows.

---

Steps: Overview:

 1. -E.g. create the following program:

--- cut here: begin --------------------------------------------------
// filenamemacro=setwitti.s
--- cut here: end ---------------------------------------------------- --- cut here: begin --------------------------------------------------
// PROC PROCWindowSetKeepAlwaysOnTopGivenTitle( STRING titleS )
--- cut here: end ---------------------------------------------------- --- cut here: begin --------------------------------------------------
FORWARD INTEGER PROC FNWindowGetFindWindowTitleExactNotToWindowHandleI( STRING s1 ) FORWARD PROC Main() FORWARD PROC PROCWindowSetCentral( INTEGER i1, STRING s1 ) FORWARD PROC PROCWindowSetKeepAlwaysOnTop( INTEGER i1 ) FORWARD PROC PROCWindowSetKeepAlwaysOnTopGivenTitle( STRING s1 ) FORWARD PROC PROCWindowSetKeepAlwaysOnTopGivenTitleCentral( STRING s1, STRING s2 ) FORWARD PROC PROCWindowSetKeepAlwaysOnTopUndo( INTEGER i1 ) FORWARD STRING PROC FNStringGetWindowHandleToWindowTitleS( INTEGER i1 ) // --- MAIN --- // // // define Microsoft Windows dll constants // #define GW_HWNDFIRST 0 #define GW_HWNDLAST 1 #define GW_HWNDNEXT 2 #define GW_HWNDPREV 3 #define GW_OWNER 4 #define GW_CHILD 5 #define GW_MAX 5 // // get the necessary functions from given Microsoft Windows dll // DLL "<user32.dll>" INTEGER PROC FindWindow( INTEGER reserved, STRING lpszDir : CStrVal) : "FindWindowA" INTEGER PROC SetWindowPos( INTEGER hwnd, INTEGER flag, INTEGER x, INTEGER y, INTEGER width, INTEGER height, INTEGER more_flags ) END // DLL "<user32.dll>" INTEGER PROC GetDesktopWindow() INTEGER PROC GetWindow( INTEGER windowHandleI, INTEGER cmd ) INTEGER PROC _GetWindowText( INTEGER windowHandleI, VAR STRING s:strptr,INTEGER count ) : "GetWindowTextA" INTEGER PROC SetForegroundWindow( INTEGER windowHandleI ) END // PROC Main() STRING s1[255] = "Untitled - Notepad" IF ( NOT ( Ask( "window: set: keep: always: on: top: given: title: titleS = ", s1, _FIND_HISTORY_ ) ) ) RETURN() ENDIF PROCWindowSetKeepAlwaysOnTopGivenTitle( s1 ) END <F12> Main() // --- LIBRARY --- // // library: window: set: keep: always: on: top: given: title <description>given title of window, keep that window always on top</description> <version>1.0.0.0.19</version> (filenamemacro=setwitti.s) [kn, ri, sa, 12-12-2009 22:32:57] PROC PROCWindowSetKeepAlwaysOnTopGivenTitle( STRING titleS ) // e.g. // // e.g. // define Microsoft Windows dll constants // e.g. // // e.g. #define GW_HWNDFIRST 0 // e.g. #define GW_HWNDLAST 1 // e.g. #define GW_HWNDNEXT 2 // e.g. #define GW_HWNDPREV 3 // e.g. #define GW_OWNER 4 // e.g. #define GW_CHILD 5 // e.g. #define GW_MAX 5 // e.g. // // e.g. // get the necessary functions from given Microsoft Windows dll // e.g. // // e.g. DLL "<user32.dll>" // e.g. INTEGER PROC FindWindow( INTEGER reserved, STRING lpszDir : CStrVal) : "FindWindowA" // e.g. INTEGER PROC SetWindowPos( INTEGER hwnd, INTEGER flag, INTEGER x, INTEGER y, INTEGER width, INTEGER height, INTEGER more_flags ) // e.g. END // e.g. // // e.g. DLL "<user32.dll>" // e.g. INTEGER PROC GetDesktopWindow() // e.g. INTEGER PROC GetWindow( INTEGER windowHandleI, INTEGER cmd ) // e.g. INTEGER PROC _GetWindowText( INTEGER windowHandleI, VAR STRING s:strptr,INTEGER count ) : "GetWindowTextA" // e.g. INTEGER PROC SetForegroundWindow( INTEGER windowHandleI ) // e.g. END // e.g. // // e.g. PROC Main() // e.g. STRING s1[255] = "Untitled - Notepad" // e.g. IF ( NOT ( Ask( "window: set: keep: always: on: top: given: title: titleS = ", s1, _FIND_HISTORY_ ) ) ) RETURN() ENDIF // e.g. PROCWindowSetKeepAlwaysOnTopGivenTitle( s1 ) // e.g. END // e.g. // e.g. <F12> Main() // PROCWindowSetKeepAlwaysOnTopGivenTitleCentral( titleS, "keepalwaysontop" ) // END // library: window: set: keep: always: on: top: given: title: central <description></description> <version>1.0.0.0.2</version> (filenamemacro=setwitce.s) [kn, am, fr, 16-07-2010 18:42:58] PROC PROCWindowSetKeepAlwaysOnTopGivenTitleCentral( STRING titleS, STRING caseS ) // e.g. // // e.g. // define Microsoft Windows dll constants // e.g. // // e.g. #define GW_HWNDFIRST 0 // e.g. #define GW_HWNDLAST 1 // e.g. #define GW_HWNDNEXT 2 // e.g. #define GW_HWNDPREV 3 // e.g. #define GW_OWNER 4 // e.g. #define GW_CHILD 5 // e.g. #define GW_MAX 5 // e.g. // // e.g. // get the necessary functions from given Microsoft Windows dll // e.g. // // e.g. DLL "<user32.dll>" // e.g. INTEGER PROC FindWindow( INTEGER reserved, STRING lpszDir : CStrVal) : "FindWindowA" // e.g. INTEGER PROC SetWindowPos( INTEGER hwnd, INTEGER flag, INTEGER x, INTEGER y, INTEGER width, INTEGER height, INTEGER more_flags ) // e.g. END // e.g. // // e.g. DLL "<user32.dll>" // e.g. INTEGER PROC GetDesktopWindow() // e.g. INTEGER PROC GetWindow( INTEGER windowHandleI, INTEGER cmd ) // e.g. INTEGER PROC _GetWindowText( INTEGER windowHandleI, VAR STRING s:strptr,INTEGER count ) : "GetWindowTextA" // e.g. INTEGER PROC SetForegroundWindow( INTEGER windowHandleI ) // e.g. END // e.g. // // e.g. PROC Main() // e.g. STRING s1[255] = "Untitled - Notepad" // e.g. IF ( NOT ( Ask( "window: set: keep: always: on: top: given: title: titleS = ", s1, _FIND_HISTORY_ ) ) ) RETURN() ENDIF // e.g. PROCWindowSetKeepAlwaysOnTopGivenTitleCentral( s1, "keepalwaysontop" ) // e.g. END // e.g. // e.g. <F12> Main() // INTEGER windowHandleI = FNWindowGetFindWindowTitleExactNotToWindowHandleI( titleS ) // IF ( windowHandleI == 0 ) // Warn( Format( "window title", " ", "'", titleS, "'", " ", "not found among the titles of all currently open windows. Please check the (must be exact same) title of the window(s), and or make sure the involved application is running" ) ) // RETURN() // ENDIF // CASE caseS // WHEN "keepalwaysontop" // PROCWindowSetKeepAlwaysOnTop( windowHandleI ) // WHEN "" // PROCWindowSetKeepAlwaysOnTopUndo( windowHandleI ) // OTHERWISE // Warn( Format( "PROCWindowSetKeepAlwaysOnTopGivenTitleCentral()", ":", " ", caseS, " ", "unknown case" ) ) // RETURN() // ENDCASE // END // library: window: get: find: window: title: to: window: handle <description>convert given window title to window handle (=integer representing window)</description> <version>1.0.0.0.8</version> (filenamemacro=getwiwti.s) [kn, ri, su, 10-09-2006 00:18:28] INTEGER PROC FNWindowGetFindWindowTitleExactNotToWindowHandleI( STRING titleS ) // e.g. // // e.g. // define Microsoft Windows dll constants // e.g. // // e.g. #define GW_HWNDFIRST 0 // e.g. #define GW_HWNDLAST 1 // e.g. #define GW_HWNDNEXT 2 // e.g. #define GW_HWNDPREV 3 // e.g. #define GW_OWNER 4 // e.g. #define GW_CHILD 5 // e.g. #define GW_MAX 5 // e.g. // // e.g. // get the necessary functions from given Microsoft Windows dll // e.g. // // e.g. DLL "<user32.dll>" // e.g. INTEGER PROC GetDesktopWindow() // e.g. INTEGER PROC GetWindow( INTEGER windowHandleI, INTEGER cmd ) // e.g. INTEGER PROC _GetWindowText( INTEGER windowHandleI, VAR STRING s:strptr,INTEGER count ) : "GetWindowTextA" // e.g. INTEGER PROC SetForegroundWindow( INTEGER windowHandleI ) // e.g. END // e.g. // e.g. PROC Main() // e.g. Message( FNWindowGetFindWindowTitleExactNotToWindowHandleI( "Untitled - Notepad" ) ) // e.g. gives a large integer number (e.g. 36834218) if a window with that title is found, otherwise 0 // e.g. END // e.g. // e.g. <F12> Main() // INTEGER windowHandleI = 0 // INTEGER top_hwnd = GetDesktopWindow() // windowHandleI = GetWindow( top_hwnd, GW_CHILD ) // WHILE ( windowHandleI <> 0 ) // IF ( Pos( Lower( titleS ), Lower( FNStringGetWindowHandleToWindowTitleS( windowHandleI ) ) ) ) // RETURN ( windowHandleI ) // Window title found // ENDIF // windowHandleI = GetWindow( windowHandleI, GW_HWNDNEXT ) // ENDWHILE // Warn( Format( "window title", " ", titleS, " ", "not found among the titles of currently open windows. Please check the (exact) title of the windows, or run the involved application" ) ) // RETURN ( 0 ) // Window title not found // END // library: window: set: top: keep: always <version>1.0.0.0.2</version> (filenamemacro=setwikal.s) [kn, ri, mo, 02-05-2005 21:37:51] PROC PROCWindowSetKeepAlwaysOnTop( INTEGER windowHandleI ) // e.g. // // e.g. // get the necessary functions from given Microsoft Windows dll // e.g. // // e.g. DLL "<user32.dll>" // e.g. INTEGER PROC SetWindowPos( INTEGER hwnd, INTEGER flag, INTEGER x, INTEGER y, INTEGER width, INTEGER height, INTEGER more_flags ) // e.g. END // e.g. // e.g. PROC Main() // e.g. PROCWindowSetKeepAlwaysOnTop( GetWinHandle() ) // e.g. END // e.g. // e.g. <F12> Main() // PROCWindowSetCentral( windowHandleI, "keepalwaysontop" ) // END // library: window: set: keep: always: on: top: undo <description></description> <version>1.0.0.0.1</version> (filenamemacro=setwitun.s) [kn, am, fr, 16-07-2010 18:41:33] PROC PROCWindowSetKeepAlwaysOnTopUndo( INTEGER windowHandleI ) // e.g. // // e.g. // get the necessary functions from given Microsoft Windows dll // e.g. // // e.g. DLL "<user32.dll>" // e.g. INTEGER PROC SetWindowPos( INTEGER hwnd, INTEGER flag, INTEGER x, INTEGER y, INTEGER width, INTEGER height, INTEGER more_flags ) // e.g. END // e.g. // e.g. PROC Main() // e.g. PROCWindowSetKeepAlwaysOnTop( GetWinHandle() ) // e.g. END // e.g. // e.g. <F12> Main() // PROCWindowSetCentral( windowHandleI, "keepalwaysontopundo" ) // END // library: string: get: window: text <description>convert given window handle (=integer representing window) to that window's title</description> <version>1.0.0.0.2</version> (filenamemacro=getstwte.s) [kn, ri, su, 10-09-2006 00:37:10] STRING PROC FNStringGetWindowHandleToWindowTitleS( INTEGER hwnd ) // e.g. #define GW_HWNDFIRST 0 // e.g. #define GW_HWNDLAST 1 // e.g. #define GW_HWNDNEXT 2 // e.g. #define GW_HWNDPREV 3 // e.g. #define GW_OWNER 4 // e.g. #define GW_CHILD 5 // e.g. #define GW_MAX 5 // e.g. // e.g. DLL "<user32.dll>" // e.g. INTEGER PROC GetDesktopWindow() // e.g. INTEGER PROC GetWindow( INTEGER hwnd, INTEGER cmd ) // e.g. INTEGER PROC _GetWindowText( INTEGER hwnd, VAR STRING s:strptr,INTEGER count ) : "GetWindowTextA" // e.g. INTEGER PROC SetForegroundWindow( INTEGER hwnd ) // e.g. END // e.g. // e.g. PROC Main() // e.g. Message( FNStringGetWindowHandleToWindowTitleS( GetDesktopWindow() ) ) // gives e.g. ..."" // e.g. END // e.g. // e.g. <F12> Main() // STRING s[255] = Format( "":SIZEOF( s ):CHR( 0 ) ) // INTEGER len = _GetWindowText( hwnd, s, SIZEOF( s ) ) // RETURN ( IIF( len > 0, s[1:Pos( CHR( 0 ), s ) - 1], "" ) ) // END // library: window: set: central <description></description> <version>1.0.0.0.2</version> (filenamemacro=setwisce.s) [kn, am, fr, 16-07-2010 18:40:20] PROC PROCWindowSetCentral( INTEGER windowHandleI, STRING caseS ) // e.g. // // e.g. // get the necessary functions from given Microsoft Windows dll // e.g. // // e.g. DLL "<user32.dll>" // e.g. INTEGER PROC SetWindowPos( INTEGER hwnd, INTEGER flag, INTEGER x, INTEGER y, INTEGER width, INTEGER height, INTEGER more_flags ) // e.g. END // e.g. // e.g. PROC Main() // e.g. PROCWindowSetCentral( GetWinHandle(), "keepalwaysontop" ) // e.g. END // e.g. // e.g. <F12> Main() // INTEGER hWndInsertAfterI // CASE caseS // WHEN "keepalwaysontop" // hWndInsertAfterI = -1 // WHEN "keepalwaysontopundo" // hWndInsertAfterI = 1 // OTHERWISE // Warn( Format( "PROCWindowSetCentral()", ":", " ", caseS, " ", "unknown case" ) ) // RETURN() // ENDCASE // SetWindowPos( windowHandleI, hWndInsertAfterI, 0, 0, 0, 0, 3 ) // END
--- cut here: end ---------------------------------------------------- 2. -Save this program (e.g. as setwitti.s) 3. -Compile the program 4. -Make sure the application of which the window has to be on top is *running* now (e.g. in this example that will be Microsoft Windows Notepad) 5. -Run the program 6. -The application's window will now stay on top of all windows 7. -Troubleshooting: -If the *same* application (e.g. notepad) is already running, and its window is already on top, then when starting the application again (e.g. notepad), the first window will remain on top. And the second window will behave as usual, that is not stay on top. 8. -Tested successfully on Microsoft Windows XP Professional (service pack 3), running Microsoft Windows notepad TSE v4.x === Book: see also: === Diagram: see also: === File: see also: === File: version: control: see also: === Help: see also: === Image: see also: === Internet: see also: === Podcast: see also: === Record: see also: === Screencast: see also: === Table: see also: === Video: see also: === <version>1.0.0.0.1</version> ----------------------------------------------------------------------
Share |
This web page is created and maintained using the Semware TSE text editor