Make a TopMost Window

Miscellaneous Forums/Win32 Discussion/Make a TopMost Window

Does anyone know exactly how to make a window topmost so you can't have any windows over it? I've tried this:

hWnd=QueryObject(Window,1)
Bits=api_GetWindowLong(hWnd,-16)
Bits=Bits Or 8
api_SetWindowLong(hWnd,-16,Bits)


This works for regular things like WS_SYSMENU but not for extended bits (WS_EX_TOPMOST=8). Anyone have any ideas?

I've used this in BMax for a little tool window thingy...
'Extern "win32"
	'Function GetParent(hWnd)
	'Function SetWindowPos(hWnd, hWndInsertAfter, X, Y, cx, cy, uFlags)
'End Extern

'Const SWP_NOSIZE	= $1
'Const SWP_NOMOVE	= $2

'Const HWND_TOPMOST	= -1
'Const HWND_NOTOPMOST	= -2

...

If ontop
  ontop = False
  UncheckMenu(ontopItem)
  SetWindowPos(hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE)
Else
  ontop = True
  CheckMenu(ontopItem)
  SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE)
EndIf

...

...something to be going on with.

That's good, but I want to be able to have Windows keep the window ontop rather than doing it myself. I'm also trying to learn how to add an extended bit to the window style.

but I want to be able to have Windows keep the window ontop rather than doing it myself.
That's exactly what the code above does. I just ripped it straight out of a MaxGUI app and there's a lot of extraneous stuff there. Apologies if that was confusing.

I've doctored some old B3D code to do the job (should be easier to understand and get running in B+)...
;---------------------------------------------------------------------------------
; User32.decls
;==============
;
;.lib "user32.dll"
;
;FindWindow%( class$,Text$ ):"FindWindowA"
;GetWindowLong%(hwnd%, nIndex%) : "GetWindowLongA"
;GetSystemMetrics%(nIndex%) : "GetSystemMetrics"
;MoveWindow%(hwnd%, x%, y%, nWidth%, nHeight%, bRepaint%) : "MoveWindow"
;SetWindowPos%(hWnd%, hWndInsertAfter%, x%, y%, cx%, cy%, uFlags%) : "SetWindowPos"
;SetWindowLong%(hwnd%, nIndex%, dwNewLong%) : "SetWindowLongA"
;ShowWindow%(hwnd%, nCmdShow%) : "ShowWindow"
;ReleaseCapture%() : "ReleaseCapture"
;SendMessage%(hWnd%, Msg%, wParam%, lParam%) : "SendMessageA"
;
;---------------------------------------------------------------------------------

Const title$ = "blitzapp"

Const SM_CXSCREEN		= 0
Const SM_CYSCREEN		= 1
Const SM_CXVSCROLL		= 2
Const SM_CYHSCROLL		= 3
Const SM_CYCAPTION		= 4
Const SM_CXBORDER		= 5
Const SM_CYBORDER		= 6
Const SM_CXDLGFRAME		= 7
Const SM_CYDLGFRAME		= 8
Const SM_CYVTHUMB		= 9
Const SM_CXHTHUMB		= 10
Const SM_CXICON			= 11
Const SM_CYICON			= 12
Const SM_CXCURSOR		= 13
Const SM_CYCURSOR		= 14
Const SM_CYMENU			= 15
Const SM_CXFULLSCREEN	= 16
Const SM_CYFULLSCREEN	= 17
Const SM_CYKANJIWINDOW	= 18
Const SM_MOUSEPRESENT	= 19
Const SM_CYVSCROLL		= 20
Const SM_CXHSCROLL		= 21
Const SM_DEBUG			= 22
Const SM_SWAPBUTTON		= 23
Const SM_RESERVED1		= 24
Const SM_RESERVED2		= 25
Const SM_RESERVED3		= 26
Const SM_RESERVED4		= 27
Const SM_CXMIN			= 28
Const SM_CYMIN			= 29
Const SM_CXSIZE			= 30
Const SM_CYSIZE			= 31
Const SM_CXFRAME		= 32
Const SM_CYFRAME		= 33
Const SM_CXMINTRACK		= 34
Const SM_CYMINTRACK		= 35
Const SM_CXDOUBLECLK	= 36
Const SM_CYDOUBLECLK	= 37
Const SM_CXICONSPACING	= 38
Const SM_CYICONSPACING	= 39
Const SM_MENUDROPALIGNMENT=40
Const SM_PENWINDOWS		= 41
Const SM_DBCSENABLED	= 42
Const SM_CMOUSEBUTTONS	= 43
Const SM_CMETRICS		= 44
Const SM_CXSIZEFRAME	= SM_CXFRAME
Const SM_CYSIZEFRAME	= SM_CYFRAME
Const SM_CXFIXEDFRAME	= SM_CXDLGFRAME
Const SM_CYFIXEDFRAME	= SM_CYDLGFRAME

Const GWL_STYLE		= -16
Const GWL_EXSTYLE	= -20

Const WS_OVERLAPPED 	= $0
Const WS_POPUP			= $80000000
Const WS_CHILD			= $40000000
Const WS_MINIMIZE		= $20000000
Const WS_VISIBLE		= $10000000
Const WS_DISABLED		= $8000000
Const WS_CLIPSIBLINGS	= $4000000
Const WS_CLIPCHILDREN	= $2000000
Const WS_MAXIMIZE		= $1000000
Const WS_CAPTION		= $C00000
Const WS_BORDER			= $800000
Const WS_DLGFRAME		= $400000
Const WS_VSCROLL		= $200000
Const WS_HSCROLL		= $100000
Const WS_SYSMENU		= $80000
Const WS_THICKFRAME		= $40000
Const WS_GROUP			= $20000
Const WS_TABSTOP		= $10000
Const WS_MINIMIZEBOX	= $20000
Const WS_MAXIMIZEBOX	= $10000

Const WS_TILED 				= WS_OVERLAPPED
Const WS_ICONIC 			= WS_MINIMIZE
Const WS_SIZEBOX 			= WS_THICKFRAME
Const WS_OVERLAPPEDWINDOW 	= (WS_OVERLAPPED Or WS_CAPTION Or WS_SYSMENU Or WS_THICKFRAME Or WS_MINIMIZEBOX Or WS_MAXIMIZEBOX)
Const WS_TILEDWINDOW 		= WS_OVERLAPPEDWINDOW
Const WS_POPUPWINDOW 		= (WS_POPUP Or WS_BORDER Or WS_SYSMENU)
Const WS_CHILDWINDOW 		= WS_CHILD

Const WS_EX_DLGMODALFRAME 	= $1
Const WS_EX_NOPARENTNOTIFY	= $4
Const WS_EX_TOPMOST			= $8
Const WS_EX_ACCEPTFILES		= $10
Const WS_EX_TRANSPARENT		= $20

Const SW_HIDE			= 0
Const SW_SHOWNORMAL		= 1
Const SW_NORMAL			= 1
Const SW_SHOWMINIMIZED	= 2
Const SW_SHOWMAXIMIZED	= 3
Const SW_MAXIMIZE		= 3
Const SW_SHOWNOACTIVATE	= 4
Const SW_SHOW			= 5
Const SW_MINIMIZE		= 6
Const SW_SHOWMINNOACTIVE= 7
Const SW_SHOWNA			= 8
Const SW_RESTORE		= 9
Const SW_SHOWDEFAULT	= 10
Const SW_MAX			= 10

Const SWP_NOSIZE		= $1
Const SWP_NOMOVE		= $2
Const SWP_NOZORDER		= $4
Const SWP_NOREDRAW		= $8
Const SWP_NOACTIVATE	= $10
Const SWP_FRAMECHANGED	= $20
Const SWP_SHOWWINDOW	= $40
Const SWP_HIDEWINDOW	= $80
Const SWP_NOCOPYBITS	= $100
Const SWP_NOOWNERZORDER	= $200
Const SWP_DRAWFRAME		= SWP_FRAMECHANGED
Const SWP_NOREPOSITION	= SWP_NOOWNERZORDER

Const HWND_TOPMOST		= -1
Const HWND_NOTOPMOST	= -2


window_w = 800;GetSystemMetrics(SM_CXSCREEN)
window_h = 600;GetSystemMetrics(SM_CYSCREEN)

Graphics window_w, window_h, 0, 2
SetBuffer BackBuffer()

AppTitle title$

; Find this window
blitz_hnd = SystemProperty("AppHWND")
;FindWindow("Blitz Runtime Class", title$) ; Use this for pre v1.88


; Set window as borderless
SetWindowLong(blitz_hnd, GWL_STYLE, WS_VISIBLE)

; Set window as topmost
SetWindowPos(blitz_hnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)

; Centre window on desktop
MoveWindow(blitz_hnd, (GetSystemMetrics(SM_CXSCREEN) - window_w) / 2, (GetSystemMetrics(SM_CYSCREEN) - window_h) / 2, window_w, window_h, 1)

; --------------------------------------------------- test code ---------------------------------------------


Repeat
	Cls
	
	Rect 0, 0, window_w, window_h, 0
	Text window_w / 2, window_h / 2, MilliSecs(), True, True
	
	Flip
Until GetKey()

End