How to Determine the Restored State of a Minimized Form

Last reviewed: June 21, 1995
Article ID: Q110620
The information in this article applies to:

- Standard and Professional Editions of Microsoft Visual Basic for

  Windows, version 3.0

SUMMARY

It is not possible within Visual Basic to determine programatically whether an iconized form will be restored to a maximized or normal state without using the Windows API (application programming interface) function GetWindowPlacement(). This article gives an example of how to call this function from a Visual Basic application.

MORE INFORMATION

The following program demonstrates how to determine whether a minimized form will be restored as maximized or normal. The program requires two forms with a Label and a Command control on Form 1.

  1. Start a new project in Visual Basic. Form1 is created by default.

  2. From the File menu, choose New Form to create Form2.

  3. From the File menu, choose New Module to create a .BAS file and enter the following code in the (General) (Declarations) section:

    Type RECT

          left As Integer
          top As Integer
          right As Integer
          bottom As Integer
    
    End Type

    Type POINTAPI

          x As Integer
          y As Integer
    
    End Type

    Type WINDOWPLACEMENT

          length As Integer
           flags As Integer
          showCmd As Integer
          ptMinPosition As POINTAPI
          ptMaxPosition As POINTAPI
          rcNormalPosition As RECT
    
    End Type

    Global Const WPF_RESTORETOMAXIMIZED = &H0002

       ' Enter the following Declare statement as one, single line:
    
       Declare Function GetWindowPlacement Lib "User"
          (ByVal hWnd As Integer, lpwndpl As WINDOWPLACEMENT) As Integer
    
       Function is_max (hWnd As Integer) As Integer
          Dim wp As WINDOWPLACEMENT
          Dim rtn As Integer
          wp.length = Len(wp) ' Initialize size
          rtn = GetWindowPlacement(hWnd, wp)
          If wp.flags = WPF_RESTORETOMAXIMIZED Then
             is_max = True
          Else
             is_max = False
          End If
       End Function
    
       NOTE: The value of wp.length must be initialized or the call to
       GetWindowPlacement will return an error. There is no mention of this in
       the Microsoft Windows "Programmer s Reference," but it is described as
       a documentation error in the following article in the Microsoft
       Knowledge Base:
    
       ARTICLE-ID: Q89569.
       TITLE     : DOCERR: GetWindowPlacement Function Always Returns an Error
    
    

  4. Add a Label control and Command control to Form1.

  5. Add the following code to the Command_Click procedure of Form1:

       Sub Command1_Click ()
          If is_max((Form2.hWnd)) Then
             Label1.Caption = "Form 2 will be Maximized"
          Else
             Label1.Caption = "Form 2 will be Normalized"
          End If
       End Sub
    
       NOTE: Form2.hWnd cannot be passed directly to a function as a parameter.
       It must be enclosed in an extra set of parentheses or stored in a
       temporary variable.
    
    

  6. From the File menu, choose Save Project to save the forms and project.

  7. Run your application by choosing Start from the Run menu or by pressing the F5 function key. Minimize Form2, click the Command button on Form1, and observe the message displayed in the Label control. Restore Form2, maximize it, and then minimize it again. Click the Command button on Form1, and again observe the message displayed in the Label control.


Additional reference words: 3.00
KBCategory: kbinterop kbprg kbcode
KBSubcategory: APrgDataODBC


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 21, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.