This time I will show how to maximize and minimize windows. I will be using the WindowFinder class that I introduced in the blog Finding specific windows .
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
namespace Minimizing_and_maximizing_windows
{
class Program
{
[DllImport("user32.dll" )]
public static extern bool ShowWindowAsync (int hWnd, int nCmdShow);
public enum SW : int
{
HIDE = 0 ,
SHOWNORMAL = 1 ,
SHOWMINIMIZED = 2 ,
SHOWMAXIMIZED = 3 ,
SHOWNOACTIVATE = 4 ,
SHOW = 5 ,
MINIMIZE = 6 ,
SHOWMINNOACTIVE = 7 ,
SHOWNA = 8 ,
RESTORE = 9 ,
SHOWDEFAULT = 10
}
static void Main(string [] args)
{
Finding_specific_windows.WindowFinder wf = new Finding_specific_windows.WindowFinder();
wf.FindWindows(0 , null , new Regex("- (Windows|Microsoft) Internet Explorer" ), new Regex("iexplore" ), new Finding_specific_windows.WindowFinder.FoundWindowCallback(foundWindow));
Console.Read();
}
static bool foundWindow(int handle)
{
if (new Random().Next(0 , 2 ) == 0 )
{
ShowWindowAsync(handle, (int )SW.SHOWMAXIMIZED);
Console.WriteLine("Window maximized" );
}
else
{
ShowWindowAsync(handle, (int )SW.MINIMIZE);
Console.WriteLine("Window minimized" );
}
return true ;
}
}
}
And the result:
Mark S. Rasmussen
I'm the CTO at
iPaper where I cuddle with databases, mold code and maintain the overall technical & team responsibility. I'm an avid speaker at user groups & conferences. I love life, motorcycles, photography and all things technical. Say hi on
Twitter , write me an
email or look me up on
LinkedIn .