How to Determine Display State of a VB Form, Modal or Modeless

ID: Q77316


The information in this article applies to:


SUMMARY

The Show method in the Visual Basic for Windows language can display a form either as modal or modeless. No direct support exists in the language to determine the display state of the form without maintaining global variables that contain the display state of the form. However, the Windows API function GetWindowLong can be used to check the display state of the form.


MORE INFORMATION

When Visual Basic for Windows displays a modal form (.Show 1), all other forms will be modified to contain the Window Style WS_DISABLED. The Windows API function GetWindowLong can be used to return the Window Style of another form to check for the WS_DISABLED style.

The following code demonstrates this process:

Add the following to the General Declarations section of Form1 and Form2:


DefInt A-Z
Global Const GWL_STYLE = (-16)
Global Const WS_DISABLED = &H8000000
Declare Function GetWindowLong& Lib "user" (ByVal hWnd, ByVal nIndex) 

Form1.Frm


Sub Form_Click ()
  ' Flip between "Modeless" and "Modal" display states.
  Static ShowStyle
  Unload form2
  form2.Show ShowStyle
  ShowStyle = (ShowStyle + 1) Mod 2
End Sub 

Form2.Frm


Sub Form_Paint ()
  ' Get the Window Style for Form1.
   WinStyle& = GetWindowLong(Form1.hWnd, GWL_STYLE)
   If WinStyle& And WS_DISABLED Then
      ' The WS_DISABLED style is set on "FORM1" when "FORM2"
      ' is displayed with the Modal flag (Show 1).
      Print "Modal    - Show 1"
   Else
      ' The WS_DISABLED style is not set on "FORM1" when "FORM2"
      ' is displayed with the Modeless flag (Show or Show 0).
      Print "Modeless - Show"
   End If
End Sub 


Keywords          : kbcode kbWndw PrgOptTips 
Version           : 1.0 2.0 3.0
Platform          : WINDOWS 
Issue type        : kbhowto 

Last Reviewed: June 16, 1999