使用API關閉特定應用程式

在此舉例開啟了一個IE瀏覽器視窗
//關閉IE
[DllImport("user32.dll")]
public static extern int FindWindow(string strclassName, string strWindowName);

[DllImport("user32.dll")]
private static extern int SendMessage(
  int hWnd,   // handle to destination window
  int Msg,    // message
  int wParam, // first message parameter
  int lParam // second message parameter
);

const int WM_KEYDOWN = 0x0100;
const int WM_KEYUP = 0x0101;
const int WM_SYSCOMMAND = 0x0112;
const int SC_CLOSE = 0xF060;

private void closeIE()
{
  //dd 為開啟網頁的Title
  int a = FindWindow("IEFrame", "dd - Microsoft Internet Explorer");
  if (a > 0)
  {
    SendMessage(a, WM_SYSCOMMAND, SC_CLOSE, 0);
  }
}

留言