PowerBuilder – Have Window Stay on Top

Posted on Thursday, December 16th, 2010 at 8:20 pm in

One common application technique is to have any notification windows stay on top of all others, even if subsequent windows are opened on top of them (and the notification window loses focus). This can easily be achieved via an external API call: SetWindowPos. From the MSDN: “Changes the size, position, and Z order of a child, pop-up, or top-level window. These windows are ordered according to their appearance on the screen. The topmost window receives the highest rank and is the first window in the Z order.”

FUNCTION boolean SetWindowPos(ulong hWnd, int hwinpos, int x, int y, int cx, int cy, int swp) LIBRARY "user32.dll"

The values of hwinpos can be:
HWND_BOTTOM
(HWND)1

Places the window at the bottom of the Z order. If the hWnd parameter identifies a topmost window, the window loses its topmost status and is placed at the bottom of all other windows.

HWND_NOTOPMOST
(HWND)-2

Places the window above all non-topmost windows (that is, behind all topmost windows). This flag has no effect if the window is already a non-topmost window.

HWND_TOP
(HWND)0

Places the window at the top of the Z order.

HWND_TOPMOST
(HWND)-1

Places the window above all non-topmost windows. The window maintains its topmost position even when it is deactivated.

The next two parameters are the left and top position of the window.

The next two parameters are the width and height in pixels of the window.

The last parameter is a combination of 1 + 2
1 – retain current size
2 – retain current position

Using my system tray popup window as an example, I added the API definition as a local external function, put a checkbox at the bottom of the window (with checked=true), and placed the following in the open event:

ulong ul_handle
ul_handle = handle(THIS)
IF cbx_1.checked THEN
	SetWindowPos(ul_handle,-1,0,0,0,0,3)// -1 puts this at top of all others always, 3 says ignore position and size
ELSE
	SetWindowPos(ul_handle,1,0,0,0,0,3)// 1 put at bottom of z order
	bringtotop = TRUE
END IF

And the following in the clicked event of the checkbox control

ulong ul_handle
ul_handle = handle(Parent)
IF cbx_1.checked THEN
	SetWindowPos(ul_handle,-1,0,0,0,0,3)// -1 puts this at top of all others always, 3 says ignore position and size
ELSE
	SetWindowPos(ul_handle,1,0,0,0,0,3)// 1 put at bottom of z order
	bringtotop = TRUE
END IF

Now when unchecked:

You may wish to provide another way to bring the window back to the top so the users don’t have to move or close things.

Top