Microsoft KB Archive/84251

From BetaArchive Wiki
Knowledge Base


How To Create a Topmost or Floating Window in Visual Basic

Article ID: 84251

Article Last Modified on 6/29/2004



APPLIES TO

  • Microsoft Visual Basic 2.0 Standard Edition
  • Microsoft Visual Basic 3.0 Professional Edition
  • Microsoft Visual Basic 2.0 Professional Edition
  • Microsoft Visual Basic 3.0 Professional Edition
  • Microsoft Visual Basic 1.0 Standard Edition



This article was previously published under Q84251

SUMMARY

You can create a "floating" window such as that used for the Microsoft Windows version 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).


NOTE: Visual Basic forms that are TopMost Windows when they are in either Normal or Maximized positions will lose the Topmost setting when the Visual Basic program is Minimized. Visual Basic destroys the old window and creates a new one with the Icon in the Window. Therefore, in order for TopMost to stay in effect, the user needs to reset TopMost in the Resize event.

There are two methods to produce windows that "hover" or "float," one of which is possible in Visual Basic for Windows. This method is described below:

Use the following declarations:

Declare Function SetWindowPos Lib "User" (ByVal hWnd As Integer, ByVal
hWndInsertAfter As Integer, ByVal X As Integer, ByVal Y As Integer, ByVal
cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer) As Integer
' The above Declare statement must appear on one line.

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
'Put global constants in bas module.
                


To set the form XXXX to TOPMOST, use the following code:

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 the following code:

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, version 3.1.


REFERENCES

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


Additional query words: top most Win31 Float Topmost Notopmost Setwindowpos

Keywords: kbhowto kbwndw kbcode KB84251