Thursday, May 12, 2005

On Top of The World...

I was facing an issue for quite some time with Visual Basic 6. I needed to make an MDI child window "On Top" of other MDI child windows. The obvious solution was to use the win32 API function SetWindowPos. I made a small function which will make a window "On Top" and back to "normal".

Public Sub MakeAlwaysOnTop(frm As Form, SetOnTop As Boolean)
Dim lflag as long
If SetOnTop Then
lflag = HWND_TOPMOST
Else
lflag = HWND_NOTOPMOST
End If
SetWindowPos frm.hwnd, lflag, 0,0, 0, 0, _
SWP_NOMOVE Or SWP_NOSIZE
End Sub

This works fine for normal windows but NOT for MDI child windows. If I minimize the MDI parent form, the "On Top" form will not get minimized but it will still stay "On Top" of any other application that might be running. Not a good solution. If I make the MDI Child property of the form to False, then it doesnt work at all (it simply doesnt stay on top).
I was surprised to find that almost all of the Google search results pointed to using the above method only. I am sure that many people would have faced the same problem. Then I remembered reading something on the web regarding setting the parent of a form at runtime.

So I tried this code in the Form_Load of my form after setting its MDI Child property to False.

Private Sub Form_Load()
SetParent Me.hwnd, fMainForm.hwnd
MakeAlwaysOnTop Me, True
End Sub

That did the trick :-)
Now the window stayed on top of other MDI child windows and automatically minimized when I minimized the MDI parent.
Big thanks to the person from whom i got the clue for using SetParent API function.

1 Comments:

Blogger Tejas Patel said...

That's remarkable achievement for you. congratulations ;)

5/12/2005 06:04:00 PM  

Post a Comment

Subscribe to Post Comments [Atom]

<< Home