Microsoft KB Archive/178401

= PRB: Message Box Appears Behind Form Set as Top-Most Window =

Article ID: 178401

Article Last Modified on 5/12/2003

-

APPLIES TO


 * Microsoft Visual Basic 5.0 Learning Edition
 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



This article was previously published under Q178401



SYMPTOMS
A Visual Basic form can be displayed as the top z-ordered (or top-most) window by using the SetWindowPos API function. Under Windows 95 or Windows 98, if the top-most window then displays a message box, the message box may display behind the top-most window. The user cannot continue or exit the program because the user cannot close the message box.



RESOLUTION
Do not display a message box from a form set as the top-most window. If you must use a message box from the form, unload the form first before displaying the message box.



STATUS
This behavior is by design.



Steps to Reproduce Behavior
 Start a new Standard EXE project in Visual Basic. Form1 is created by default.  Add the two CommandButtons to Form1 form. Set the following Form1 form properties:   Form1 Form Property            Setting ---  BorderStyle                    0-None StartUpPosition               2-CenterScreen   Copy the following code to the Code window of Form1 form: Option Explicit

Private Declare Function SetWindowPos Lib "user32" _ (ByVal hwnd As Long, _        ByVal hWndInsertAfter As Long, _         ByVal x As Long, _         ByVal y As Long, _         ByVal cx As Long, _         ByVal cy As Long, _         ByVal wFlags As Long) As Long Const HWND_TOPMOST = -1 Const SWP_NOMOVE = &H2 Const SWP_NOSIZE = &H1

Private Sub Form_Load Dim lngWindowPosition As Long Command1.Caption = "Show Message Box" Command2.Caption = "End Program" LngWindowPosition = SetWindowPos(Form1.hwnd, _                                          HWND_TOPMOST, _                                           0, _                                           0, _                                           0, _                                           0, _                                           SWP_NOMOVE Or SWP_NOSIZE) End Sub

Private Sub Command1_Click MsgBox "From Message Box", vbSystemModal End Sub

Private Sub Command2_Click End End Sub  Save the Project and compile the project into an executable file. This problem only occurs from an EXE file. Run the executable file.</li> Click Show Message Box to display the message box.</li> Click on the desktop or another application to de-activate Project1.</li> Click on the form of Project1 to activate Project1.</li> Click OK to dismiss the message box.</li></ol>

NOTE: You may need to repeat steps 6-9 several times to reproduce the problem where the Message Box is displayed behind the top-most window and cannot be dismissed.

Additional query words: topmost top most modal

Keywords: kbapi kbprb KB178401

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.