導入命名空間
using System.Runtime.InteropServices;
添加代碼
[DllImport ( "user32.dll", EntryPoint = "FindWindow", SetLastError = true )]
private static extern IntPtr FindWindow( string lpClassName, string lpWindowName );
[DllImport ( "user32.dll", EntryPoint = "FindWindowEx", SetLastError = true )]
private static extern IntPtr FindWindowEx( IntPtr hwndParent, uint hwndChildAfter, string lpszClass, string lpszWindow );
[DllImport ( "user32.dll", EntryPoint = "SendMessage", SetLastError = true, CharSet = CharSet.Auto )]
private static extern int SendMessage( IntPtr hwnd, uint wMsg, int wParam, int lParam );
[DllImport ( "user32.dll", EntryPoint = "SetForegroundWindow", SetLastError = true )]
private static extern void SetForegroundWindow( IntPtr hwnd );
IntPtr hwndCalc = FindWindow(null, "窗體標題"); //查找計算器的句柄
if (hwndCalc != IntPtr.Zero)
{
IntPtr hwndThree = FindWindowEx(hwndCalc, 0, null, "確定"); //獲取按鈕的句柄
SendMessage(hwndThree, 0xF5, 0, 0); //鼠標點擊的消息,對於各種消息的數值,大家還是得去API手冊
}