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