4

How do I programmatically maximize a program that I have currently running on my pc. For example if I have WINWORD.exe running in task manager. How do I maximize it?

In my code I have tried:

private void button1_Click(object sender, EventArgs e)
{
    this.WindowState = FormWindowState.Maximised;
}

Unfortunately that only displays my application. I would like it to maximise another exe, BUT if it cannot find it then i want to it to exit.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
PriceCheaperton
  • 5,071
  • 17
  • 52
  • 94
  • 1
    https://msdn.microsoft.com/en-us/library/system.windows.automation.windowpattern.setwindowvisualstate(v=vs.110).aspx – Hans Passant Jul 17 '16 at 20:16
  • Since I knew [`ShowWindow`](https://msdn.microsoft.com/en-us/library/windows/desktop/ms633548(v=vs.85).aspx) method so I didn't look for a duplicate post. But, you will find the link posted by @PeterDuniho really useful. It also contains another way to find the window using [`FindWindow`](https://msdn.microsoft.com/en-us/library/windows/desktop/ms633499(v=vs.85).aspx). Also it covers minimizing and closing the window. Also if you find it useful, you can vote for it to make it more useful for future readers. – Reza Aghaei Jul 17 '16 at 21:17
  • I also added a simple example for [WindowPattern.SetWindowVisualState](https://msdn.microsoft.com/en-us/library/system.windows.automation.windowpattern.setwindowvisualstate(v=vs.110).aspx) Thanks @HansPassant. – Reza Aghaei Jul 17 '16 at 21:51
  • @PeterDuhino I edited the post and added also an example of using [`WindowPattern.SetWindowVisualState`](https://msdn.microsoft.com/en-us/library/system.windows.automation.windowpattern.setwindowvisualstate(v=vs.110).aspx) which is not an option in the linked post. Since usually users don't read the answers in a duplicate question, I believe we don't need to keep it as duplicate :) – Reza Aghaei Jul 18 '16 at 12:39

1 Answers1

13

Using ShowWindow

You can set windows state using ShowWindow method. To do so, you first need to find the window handle and then using the method. Then maximize the window this way:

private const int SW_MAXIMIZE = 3;
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
private void button1_Click(object sender, EventArgs e)
{
    var p = System.Diagnostics.Process.GetProcessesByName("WINWORD").FirstOrDefault();
    if(p!=null)
    {
        ShowWindow(p.MainWindowHandle, SW_MAXIMIZE);
    }
}

Using WindowPattern.SetWindowVisualState

Also as another option (based on Hans's comment), you can use SetWindowVisualState method to set state of a window. To so so, first add a reference to UIAutomationClient.dll and UIAutomationTypes.dll then add using System.Windows.Automation; and maximize the window this way:

var p = System.Diagnostics.Process.GetProcessesByName("WINWORD").FirstOrDefault();
if (p != null)
{
    var element = AutomationElement.FromHandle(p.MainWindowHandle);
    if (element != null)
    {
        var pattern = element.GetCurrentPattern(WindowPattern.Pattern) as WindowPattern;
        if (pattern != null)
            pattern.SetWindowVisualState(WindowVisualState.Maximized);
    }
}
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398