2010-04-30 80 views
9

在窗口的形式,我做了一個按鈕,我試圖讓它發送F1到一個特定的窗口(如Firefox,我的電腦等)發送擊鍵程序

我問題是:

  • 如何通過窗口的名稱進行操作? (例如「Mozilla Firefox」)
  • 我該如何使用進程的名稱來執行此操作? (如firefox.exe)

回答

14

通過窗口名稱:

[DllImport("User32.dll")] 
static extern IntPtr FindWindow(string lpClassName, string lpWindowName); 
[DllImport("User32.dll")] 
static extern int SetForegroundWindow(IntPtr hWnd); 

IntPtr ptrFF = FindWindow(null, "Mozilla Firefox"); 
SetForegroundWindow(ptrFF); 
SendKeys.SendWait("{F1}"); 

通過進程名稱:

Process proc = Process.GetProcessesByName("firefox")[0]; 
IntPtr ptrFF = proc.Handle; 
SetForegroundWindow(ptrFF); 
SendKeys.SendWait("{F1}"); 
+0

和進程名? – 2010-04-30 11:36:44

+0

@或Betzalel - 更新過程名稱。 – 2010-04-30 11:37:52

+0

謝謝,想通了 – 2010-04-30 12:17:54