Click here to Skip to main content
6,821,293 members and growing! (18,847 online)
Email Password   helpLost your password?
Desktop Development » Dialogs and Windows » General     Intermediate License: The Code Project Open License (CPOL)

Find and Minimize or Maximize or Restore a Window

By Mohan Kumar

Find and minimize or maximize or restore a window.
VB, .NET, Win2K, WinXP, Win2003, Vista, TabletPC, Embedded, WinForms, Dev
Revision:2 (See All)
Posted:19 Dec 2007
Views:11,030
Bookmarked:10 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
3 votes for this article.
Popularity: 1.30 Rating: 2.71 out of 5
1 vote, 33.3%
1

2

3

4
2 votes, 66.7%
5

Introduction

GetWindowPlacement, SetWindowPlacement, and FindWindow: using these API functions, we can minimize or maximize or restore a window.

Using the functions

  • 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 matches the specified strings. This function does not search child windows. This function does not perform a case-sensitive search.

    FindWindow(string lpClassName,string lpWindowName)

Usage

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"
                 wp.showCmd = SW_SHOWMINIMIZED
            Case "Maximize"

                 wp.showCmd = SW_SHOWMAXIMIZED

            Case "Restore"
                wp.showCmd = SW_SHOWNORMAL
        End Select
        SetWindowPlacement(app_hwnd, wp)

    End Sub
End Class
'Use this Class
'This is sample for Minimize the notepad window

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Mohan Kumar


Member
I’m Mohan Kumar from Chennai and hold a Masters Degree in Information Technology. I have worked with Opcion Technologies and Atlas systems in Various Technologies.Currently i'm associated with congnizant Technology Solutions.I am a very good fan of Microsoft Technologies. I love Programming, and I am eager to learn New things Curious about Microsoft Products, Interested in Knowledge sharing.I am glad that I have received couple of awards from community Credit and Dotnetspider for submitting .Net related articles and answers. My famous slogan "Awake! Arise! Stop not till the goal is reached.
Occupation: Software Developer
Company: Cognizant Technology Solution
Location: India India

Other popular Dialogs and Windows articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Layout  Per page   
  (Refresh) 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.

PermaLink | Privacy | Terms of Use
Last Updated: 19 Dec 2007
Editor: Smitha Vijayan
Copyright 2007 by Mohan Kumar
Everything else Copyright © CodeProject, 1999-2010
Web20 | Advertise on the Code Project