SIGN UP MEMBER LOGIN:    
ARTICLE

Find, Minimize, Maximize, Restore the window

Posted by Mohan Kumar Articles | Windows Services in C# February 05, 2008
This article explains you how to find, minimize, maximize, restore the window using win API.
Reader Level:

Using the API function

GetWindowPlacement,SetWindowPlacement and FindWindow. Using these API function, we can minimize or maximize or restore the window.

GetWindowPlacement

The GetWindowPlacement function retrieves the show state and the restored, minimized, and maximized positions of the specified window.

GetWindowPlacement (ByVal hwnd As Integer, ByRef lpwndpl As WINDOWPLACEMENT) As Integer

SetWindowPlacement

The SetWindowPlacement function sets the show state and the restored, minimized, and maximized positions of the specified window.

SetWindowPlacement (ByVal hwnd As Integer, ByRef lpwndpl As WINDOWPLACEMENT) As Integer

FindWindow

The FindWindow function retrieves a handle to the top-level window whose class name and window name match the specified strings. This function does not search child windows. This function does not perform a case-sensitive search.

FindWindow(string lpClassName,string lpWindowName)

Sample code for minimize the notepad

Public
Class Minimize 

    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer

    Private Declare Function GetWindowPlacement Lib "user32" (ByVal hwnd As Integer, ByRef lpwndpl As WINDOWPLACEMENT)As Integer

    Private Declare Function SetWindowPlacement Lib "user32" (ByVal hwnd As Integer, ByRef lpwndpl As WINDOWPLACEMENT) As Integer

    Private Const SW_SHOWMINIMIZED As Short = 2

    Private Const SW_SHOWMAXIMIZED As Short = 3

    Private Const SW_SHOWNORMAL As Short = 1

 

    Private Structure POINTAPI

        Dim X As Integer

        Dim Y As Integer

    End Structure

 

    Private Structure RECT

        Dim Left_Renamed As Integer

        Dim Top_Renamed As Integer

        Dim Right_Renamed As Integer

        Dim Bottom_Renamed As Integer

    End Structure

 

    Private Structure WINDOWPLACEMENT

        Dim length As Integer

        Dim flags As Integer

        Dim showCmd As Integer

        Dim ptMinPosition As POINTAPI

        Dim ptMaxPosition As POINTAPI

        Dim rcNormalPosition As RECT

    End Structure 

 

    Public Sub windowAction(ByVal classname As String, ByVal action As String)

 

        Dim app_hwnd As Integer

        Dim wp As WINDOWPLACEMENT

        app_hwnd = FindWindow(classname, vbNullString)

        wp.length = Len(wp)

        GetWindowPlacement(app_hwnd, wp)

 

        Select Case action

 

            Case "Minimize"

                'For Minimize

                wp.showCmd = SW_SHOWMINIMIZED

            Case "Maximize"

 

                'For Maximize.

                wp.showCmd = SW_SHOWMAXIMIZED

 

            Case "Restore"

                'For Restore

                wp.showCmd = SW_SHOWNORMAL

        End Select

        SetWindowPlacement(app_hwnd, wp) 

    End Sub

End Class

Maximize the notepad

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim test As New Minimize

    test.windowAction("Notepad", "Maximize")

End Sub 

 

Restore the notepad

 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim test As New Minimize

    test.windowAction("Notepad", "Restore")
End Sub

Minimize the notepad

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim test As New Minimize

    test.windowAction("Notepad", "Minimize ")

End Sub

Login to add your contents and source code to this article
share this article :
post comment
 

You are using Notepad as the window name. Where does this come from? Can I open a window minimized and when the page is finished loading maximize the window? Thanks, Sam

Posted by Sam Pinizzotto Mar 28, 2011
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Gauge for SharePoint
Become a Sponsor