Floating tool windows

From HashVB
Jump to: navigation, search
float
 This article is based on Visual Basic 6. Find other Visual Basic 6 articles.

There are several occasions where you may want a tool window to float above your app but not other apps without being a modal dialog. It is possible to go insane trying to come up with a workaround for this unless you know the proper way to do it. The way to do it is by setting the floating window's owner. What this means in real terms is that you just have to call SetWindowLong with the GWL_HWNDPARENT constant to change its owner (note: HWNDPARENT is a misnomer, as you are actually setting the window's owner, not its parent). The code is very simple and is as follows.

Declarations

The following API declarations are required:

 Private Const GWL_HWNDPARENT = (-8)
 Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

Setting the window's owner

This is just one line of code:

 SetWindowLong myForm.hwnd, GWL_HWNDPARENT, Me.hwnd

Unsetting the window's owner

In order that your app doesn't crash, you have to unset the window's owner on exit, which is pretty much the same as setting the owner:

 SetWindowLong myForm.hwnd, GWL_HWNDPARENT, 0

(Where myForm is the form that you wish to float above your app)

Simple as that