Click here to Skip to main content
15,881,709 members
Articles / Programming Languages / C#
Article

Set Window Action - (Minimize / Maximize)

Rate me:
Please Sign up or sign in to vote.
3.64/5 (25 votes)
22 Sep 2008CPOL 48.6K   706   15   5
Application to minimize / maximize a window.

Introduction

In this article, I am trying to minimize / maximize a running application window.

Using the code

The GetWindowPlacement, FindWindow, and SetWindowPlacement functions from user32.dll are used to manage the window operations.

The FindWindow function is used to retrieve the handle of the window whose class name and window name match the specified string. This function won't search for the child window (FindWindowEx is used for the child window).

WINDOWPLACEMENT

The WINDOWPLACEMENT structure is used to hold information about the placement of the window on the screen. The GetWindowPlacemnt and SetWindowPlacement functions are used to set and get the WINDOWPLACEMENT of the specific window based on the provided handle.

C#
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("user32.dll")]
static extern bool SetWindowPlacement(IntPtr hWnd, 
                   ref WINDOWPLACEMENT lpwndpl);        
private struct POINTAPI
{
    public int x;
    public int y;
}

private struct RECT
{
   public int left;
   public int top;
   public int right;
   public int bottom;
}

private struct WINDOWPLACEMENT
{
   public int length;
   public int flags;
   public int showCmd;
   public POINTAPI ptMinPosition;
   public POINTAPI ptMaxPosition;
   public RECT rcNormalPosition;
}

void  WindowAction(string className)
{
    System.IntPtr app_hwnd;
    WINDOWPLACEMENT wp = new WINDOWPLACEMENT();
    app_hwnd = FindWindow(className, null);
    GetWindowPlacement(app_hwnd,ref wp);
    wp.showCmd = 2; // 1- Normal; 2 - Minimize; 3 - Maximize;
    SetWindowPlacement(app_hwnd,ref wp);    
}

The WindowAction function gets the class name (e.g.: Notepad) as a parameter and minimizes it. But, we can avoid using FindWindow, which is native code, by calling a combination of Process.GetProcessByName and Process.MainWindowHandle.

C#
private void minWindow(){
    Process[] processes = Process.GetProcessesByName("Notepad");
    foreach (Process p in processes)
    {
        System.IntPtr app_hwnd;
        WINDOWPLACEMENT wp = new WINDOWPLACEMENT();
        app_hwnd = p.MainWindowHandle;
        GetWindowPlacement(app_hwnd, ref wp);
        wp.showCmd = 2;
        SetWindowPlacement(app_hwnd, ref wp);
    }
}

The above snippet is used to minimize the Notepad application using the Process.GetProcessName function.

License

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


Written By
Software Developer (Senior) Proteans Software Solutions Pvt
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionChange position of Minimized window Pin
jymitra11-Dec-19 20:38
jymitra11-Dec-19 20:38 
QuestionDoubt with System Tray Pin
tacho17-Nov-09 0:14
tacho17-Nov-09 0:14 
AnswerRe: Doubt with System Tray Pin
Joshua3212-Apr-10 17:16
Joshua3212-Apr-10 17:16 
QuestionHow to figure out the window I am in Pin
dataporter4-Oct-08 5:56
dataporter4-Oct-08 5:56 
AnswerRe: How to figure out the window I am in Pin
T R Muralidharan5-Oct-08 20:33
T R Muralidharan5-Oct-08 20:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

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