的標題說明了一切,我有這樣的代碼:重複FindWindow函數每次出現的窗口/打開
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
const UInt32 WM_CLOSE = 0x0010;
和這裏就是我加入Form1_Load
:
IntPtr windowPtr = FindWindowByCaption(IntPtr.Zero, "Untitled - Notepad");
if (windowPtr == IntPtr.Zero)
{
MessageBox.Show("Window not found");
return;
}
SendMessage(windowPtr, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
所以我添加的代碼上面的Form1_Load
函數,它實際上工作,它關閉記事本,當我打開我的程序,但我的問題是,如何使功能重複,如關閉記事本,每當它打開,不僅在Form1_Load
?
[編寫自動化等待窗口被創建(並解僱它)](http://blogs.msdn.com/b/oldnewthing/archive/2014/02/17/10500645.aspx) – GSerg
@GSerg我試過了,它什麼也沒做,它沒有關閉它,嘗試用記事本,運行和很多其他的... – user3542613
它的運行方式與運行對話框一樣,只要用''Run''字面替換此對話框的標題顯示在您的Windows語言中。對於記事本,由於它沒有此代碼按下的取消按鈕,因此您需要編寫不同的結束代碼。然而,我的觀點是,這是您在創建新窗口時收到通知的方式。您可以使用現有的代碼作爲事件處理程序。 – GSerg