Click here to Skip to main content
14,176,928 members
Click here to Skip to main content
Add your own
alternative version


15 bookmarked
Posted 22 Sep 2008
Licenced CPOL

Set Window Action - (Minimize / Maximize)

, 22 Sep 2008
Rate this:
Please Sign up or sign in to vote.
Application to minimize / maximize a window.


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).


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.

[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);

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;
    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.

private void minWindow(){
    Process[] processes = Process.GetProcessesByName("Notepad");
    foreach (Process p in processes)
        System.IntPtr app_hwnd;
        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.


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


About the Author

T R Muralidharan
Software Developer (Senior) Proteans Software Solutions Pvt
India India
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionDoubt with System Tray Pin
tacho17-Nov-09 0:14
membertacho17-Nov-09 0:14 
AnswerRe: Doubt with System Tray Pin
Joshua3212-Apr-10 17:16
memberJoshua3212-Apr-10 17:16 
QuestionHow to figure out the window I am in Pin
dataporter4-Oct-08 5:56
memberdataporter4-Oct-08 5:56 
AnswerRe: How to figure out the window I am in Pin
T R Muralidharan5-Oct-08 20:33
memberT 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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01 | 2.8.190526.1 | Last Updated 22 Sep 2008
Article Copyright 2008 by T R Muralidharan
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid