Click here to Skip to main content
Click here to Skip to main content

Set Window Action - (Minimize / Maximize)

By , 22 Sep 2008
 

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.

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

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)

About the Author

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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionDoubt with System Traymembertacho17 Nov '09 - 0:14 
Thank you for the code. I have another question.
 
How can maximize other application that is minimized in system tray?
 
When use your code, the method MainWindowHandle return me zero
AnswerRe: Doubt with System TraymemberJoshua3212 Apr '10 - 17:16 
This is because there is no window handle, This is because there is no window. When it's minimized to tray, that really just means, Destroy the window and create a tray icon. You get 0 as a pointer because there isn't a window anywhere. I'm pretty sure you can't maximize something from tray sorry.
QuestionHow to figure out the window I am inmemberdataporter4 Oct '08 - 5:56 
If I have console app or a windows app that is running as part of a batch file, how can I determine which window I started in so that I can minimize or maximize that cmd window? I have figured out how to minimize all cmd windows or the window if I know the title of it, but I want to min or max the one that spawned my program. Hope I made myself clear. Any ideas? Thanks in advance!Dave
AnswerRe: How to figure out the window I am inmemberT R Muralidharan5 Oct '08 - 20:33 
Dave,
 
to get the parent process id and name you can use the following snippet.
 
            Console.WriteLine("Please Enter the name of the process you'd like the parent for:");
            string procName = Console.ReadLine();
            Process[] procList = Process.GetProcessesByName(procName);
            PerformanceCounter myProcID = new PerformanceCounter("Process", "ID Process", procName);
            PerformanceCounter myParentID = new PerformanceCounter("Process", "Creating Process ID", procName);
 
            float parentPID = myParentID.NextValue();
            Console.WriteLine("Parent for {0}: PID: {1}  Name: {2}", procName, parentPID, Process.GetProcessById((int)parentPID).ProcessName);
 
            for (int i = 1; i < procList.Length; i++)
            {
                PerformanceCounter myMultiProcID = new PerformanceCounter("Process", "ID Process", procName + "#" + i);
                PerformanceCounter myParentMultiProcID = new PerformanceCounter("Process", "ID Process", procName + "#" + i);
                parentPID = myParentMultiProcID.NextValue();
                Console.WriteLine("Parent for {0}#{1}: PID: {2}  Name: {3}", procName, i, parentPID, Process.GetProcessById((int)parentPID).ProcessName);
            }
 
Hope this helps you to solve your problem.
 
- Muralidharan T R

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

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