Creating TOPMOST or "Floating" Window in Visual Basic

ID Number: Q84251

1.00

WINDOWS

Summary:

You can create a "floating" window such as that used for Windows 3.1

Clock by using the SetWindowPos Windows API call.

More Information:

A floating (or TOPMOST) window is a window that remains constantly

above all other windows, even when it is not active. Examples of

floating windows are the Find dialog box in WRITE.EXE, and CLOCK.EXE

(when Always on Top is selected from the Control menu).

There are two methods to produce Windows that "hover" or "float," one

of which is possible in Visual Basic:

Call SetWindowPos, specifying an existing non-topmost window and

HWND_TOPMOST as the value for the second parameter (hwndInsertAfter):

Use the following declarations:

Declare Function SetWindowPos Lib "user" (ByVal h%, ByVal hb%,

ByVal x%, ByVal y%, ByVal cx%, ByVal cy%, ByVal f%) As Integer

Global Const SWP_NOMOVE = 2

Global Const SWP_NOSIZE = 1

Global Const FLAGS = SWP_NOMOVE Or SWP_NOSIZE

Global Const HWND_TOPMOST = -1

Global Const HWND_NOTOPMOST = -2

To set the form XXXX to TOPMOST, use:

success% = SetWindowPos (XXXX.hWnd, HWND_TOPMOST, 0, 0, 0, 0, FLAGS)

REM success% <> 0 When Successful

To reset the form XXXX to NON-TOPMOST, use:

success% = SetWindowPos (XXXX.hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, FLAGS)

REM success% <> 0 When Successful

Note: This attribute was introduced in Windows Version 3.1, so

remember to make a GetVersion() API call to determine whether the

application is running under Windows 3.1.

Reference(s):

"Microsoft Windows 3.1 Programmer's Reference, Volume 2, Functions,"

page 892

Additional reference words: WIN31 FLOAT TOPMOST NOTOPMOST SETWINDOWPOS