Microsoft KB Archive/192254

= BUG: SetWindowPos API Does Not Set Topmost Window in VB =

Article ID: 192254

Article Last Modified on 5/13/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 Q192254



SYMPTOMS
When using the SetWindowPos API in Visual Basic to set the Topmost window flag, these known problems may occur:


 * 1) When an application is running in the Visual Basic IDE, clicking on the Desktop or My Computer icon causes the Topmost window setting to be turned off. This does not occur when the application is a compiled EXE. Please see the REFERENCES section below for more information.
 * 2) Setting the Topmost flag for a parent form does not set it for that form's children. This conflicts with the documented behavior of the SetWindowPos function.
 * 3) When calling SetWindowPos with the NoTopmost flag for a Visual Basic form, all forms in the project are set to NoTopmost.



STATUS
Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article.



MORE INFORMATION
It is possible to use the SetWindowPos API function to make a Visual Basic Form stay topmost in the z-order (always on top), even when focus is on another window. However, the problems listed above may result.

Steps to Reproduce Behavior
 Start Visual Basic and create a standard EXE project. Form1 is created by default. From the Project menu, add another Form to the project. It should be called Form2.  From the Project menu, add a Basic Module (.BAS) to the project and add the following code: ' SetWindowPos Function 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

' SetWindowPos Flags Public Const SWP_NOSIZE = &H1 Public Const SWP_NOMOVE = &H2 Public Const HWND_TOPMOST = -1 Public Const HWND_NOTOPMOST = -2

  Add two CommandButtons to Form2 and add the following code to its Code Window: Private Sub Form_Load Command1.Caption = "Make Topmost" Command2.Caption = "Unload Form2" End Sub

Private Sub Command1_Click Call SetWindowPos(Me.hWnd, HWND_TOPMOST, 0, 0, 0, 0, _                          SWP_NOSIZE Or SWP_NOMOVE) End Sub

Private Sub Command2_Click Call SetWindowPos(Me.hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, _                          SWP_NOSIZE Or SWP_NOMOVE) Unload Me     End Sub

  Now add a CommandButton to Form1 and add the following code to its Code Window: Private Sub Form_Load Command1.Caption = "Show Form2" Call SetWindowPos(Me.hWnd, HWND_TOPMOST, 0, 0, 0, 0, _                          SWP_NOSIZE Or SWP_NOMOVE) End Sub

Private Sub Command1_Click Form2.Show vbModeless, Me     End Sub

 Compile the program and run it as an EXE (outside the Debugger).

RESULTS: Form1 appears as a Topmost window. Pressing the "Show Form2" button makes Form2 visible, but you will notice that Form2 (a child of Form1) is not the topmost window. In fact, you may need to move Form1 to see Form2. This behavior is contrary to the product documentation. Now press the "Make Topmost" button of Form2. This should make Form2 a topmost window. Pressing the "Unload Form2" button will cause Form2 to clear its Topmost flag before closing. This causes Form1's Topmost flag to be cleared as well.

NOTE: Form1 may remain topmost in the z-order until you switch to another window and then switch back. This can give the impression that Form1 is still a topmost window, but the topmost flag has been cleared.</li></ol>

<div class="references_section">