我查询了网上很多资料。最后代码如下。
[DllImport("user32.dll")]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr PostMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam); private void button1_Click(object sender, EventArgs e)
{
Process p = Process.Start(textBoxNewFormLocation.Text);
IntPtr ActiveWindowHandle = p.MainWindowHandle;
IntPtr hwnd_button = FindWindowEx(ActiveWindowHandle, new IntPtr(0), "WindowsForms10.BUTTON.app3", "button4");
const int BM_CLICK = 0x00F5;
Message msg = Message.Create(hwnd_button, BM_CLICK, new IntPtr(0), new IntPtr(0));
PostMessage(msg.HWnd, msg.Msg, msg.WParam, msg.LParam);
}问题是第2步获取按钮句柄总是失败,获取的值为0.
是我语法有误么?
请指教。
[DllImport("user32.dll")]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr PostMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam); private void button1_Click(object sender, EventArgs e)
{
Process p = Process.Start(textBoxNewFormLocation.Text);
IntPtr ActiveWindowHandle = p.MainWindowHandle;
IntPtr hwnd_button = FindWindowEx(ActiveWindowHandle, new IntPtr(0), "WindowsForms10.BUTTON.app3", "button4");
const int BM_CLICK = 0x00F5;
Message msg = Message.Create(hwnd_button, BM_CLICK, new IntPtr(0), new IntPtr(0));
PostMessage(msg.HWnd, msg.Msg, msg.WParam, msg.LParam);
}问题是第2步获取按钮句柄总是失败,获取的值为0.
是我语法有误么?
请指教。
IntPtr hwnd_button = FindWindowEx(ActiveWindowHandle, null, "button4");
FindWindowEx(ActiveWindowHandle, new IntPtr(0), null, "button4");
不是正确的类的名字,当为NULL的时候系统会忽略这个参数,所在让其为null就行了.
把这个button移到一个tabcontrol上,马上所有命令都没用了。