在C#中向windows窗体发送消息示例
// define
[DllImport("User32.dll")]
public static extern IntPtr FindWindow(String lpClassName, String lpWindowName);
[DllImport("User32.dll")]
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
const int WM_LBUTTONDOWN = 0x201;
const int WM_LBUTTONUP = 0x0202;
// sample, u should use spy++ to find windows class name and control class name
IntPtr hwndWin = FindWindow("TfrmMain", "window title");
if (hwndWin.Equals(IntPtr.Zero) == false)
{
IntPtr hwndBtn = FindWindowEx(hwndWin, IntPtr.Zero, "TButton", "control text");
if (hwndBtn.Equals(IntPtr.Zero) == false)
{
SendMessage(hwndBtn, WM_LBUTTONDOWN, IntPtr.Zero, IntPtr.Zero);
SendMessage(hwndBtn, WM_LBUTTONUP, IntPtr.Zero, IntPtr.Zero);
}
}