Example for closing Notepad
//Declartion
[DllImport("user32.dll")]
public static extern int FindWindow(string lpClassName,string lpWindowName);
DllImport("user32.dll")]
Public static extern int SendMessage(int hWnd,uint Msg,int wParam,int lParam);
public const int WM_SYSCOMMAND = 0x0112;
public const int SC_CLOSE = 0xF060;
private void button1_Click(object sender, EventArgs e)
{
// retrieve the handler of the window
int iHandle = FindWindow("Notepad", "Untitled - Notepad");
if (iHandle > 0)
{
// close the window using API
SendMessage(iHandle, WM_SYSCOMMAND, SC_CLOSE, 0);
}
}
private void button2_Click(object sender, EventArgs e)
{
//closing google window
int googleHandle = FindWindow("IEFrame", "Google - Microsoft Internet Explorer provided by Cognizant");
if (googleHandle > 0)
{
// close the window using API
SendMessage(googleHandle, WM_SYSCOMMAND, SC_CLOSE, 0);
}
}