Many of you will have tried to create floating tool windows, that always appear on the top of the screen, like find & replace dialogs, which let you edit the text as you go. It would seem that the only way to do this is to use the SetWindowPos API call, using code like below
Public Declare
Function SetWindowPos Lib "user32" (ByVal hWnd As Long, ByVal
hWndInsertAfter As Long, ByVal x As Long, y, ByVal cx As Long, ByVal cy As Long,
ByVal wFlags As Long) As Long
Public Const
HWND_TOPMOST = -1
Public Const SWP_NOMOVE = &H2
Public Const SWP_NOSIZE = &H1
Public Const TOPMOST_FLAGS = SWP_NOMOVE Or SWP_NOSIZE
Public Sub
MakeTopMost(hWnd As Long)
SetWindowPos hWnd, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS
End Sub
However, if you want to create a window like a find dialog, that window must only appear on top of your own program. It's no good having a find dialog that floats on top of everything! In fact, there is a very easy way to make a window appear on top of your program, which is so obvious I have missed it for ages. What's more, the VB help doesn't tell you it either. To make a window appear on top of your program, all you need to do is:
frmFind.Show
vbModeless, frmMain
where frmFind is the floating window, and frmMain is the parent window. vbModeless sets that other windows in your application can be opened at the same time. The frmMain sets the parent window, which makes the window appear on top. If you don't specify this, the window will not appear on top. Amazingly simple, and amazingly easy to miss!
Comments