How to Create a Modeless Dialog or Form in Visual BasicID: Q114775
|
This article contains information on how to create a modeless dialog or
form in Visual Basic. An example of a modeless dialog is the toolbar in
Microsoft Excel version 5.0 and Word version 6.0. A modeless form
always displays on top of the parent form, but is not bounded by the
parent form nor does it stay on top of all other applications as does
SetWindowPos and HWND_TOPMOST.
For additional information on SetWindowPos, please see the following
article in the Microsoft Knowledge Base:
Q84251 : How to Create a Topmost or Floating Window in Visual Basic
The Windows API SetWindowWord() is used with the value GWW_HWNDPARENT to
change the parent of the form that you want to be a modeless dialog.
SetWindowWord returns the original parent's window handle that we need to
restore when the form is unloaded to avoid a General Protection Fault.
Sub Form_Load()
Form2.Show 'display the child form
Form1.Caption = "Parent"
Form2.Caption = "Child"
End Sub
' Enter the following Declare statement as one, single line:
Declare Function SetWindowWord Lib "User" (ByVal hwnd As Integer,
ByVal Index As Integer, ByVal wNewWord As Integer) As Integer
Const GWW_HWNDPARENT = (-8)
Dim OriginalParenthWnd As Integer
Sub Form_Load()
' Set parent for the toolbar to display on top of:
OriginalParenthWnd = SetWindowWord(Me.hWnd, GWW_HWNDPARENT, Form1.hWnd)
Me.Width = Form1.Width \ 3 ' Scale child form
Me.Height = Form1.Height \ 3
End Sub
Sub Form_Unload()
Dim ret As Integer
' Return the original parent handle to avoid a GP Fault
ret = SetWindowWord(Me.hWnd, GWW_HWNDPARENT, OriginalParenthWnd)
End Sub
Additional query words: 2.00 3.00
Keywords :
Version :
Platform :
Issue type :
Last Reviewed: June 10, 1999