2 qq 30909659 qq_30909659 于 2017.09.17 11:03 提问

C#不能点击其他窗口上已知句柄的按钮 2C
c#

我想用SendMessage向"4GB Patch"这个对话框里面的"OK"按钮发送鼠标单击的消息,但是现在的情况是窗口和按钮的句柄都正确,但是按钮就是无法被按下,请各位大神看看是哪里出了问题。感谢!

PS.打开exe程序以后会弹出一个文件选择框,选择完文件以后才会弹出"4GB Patch"的对话框。虽然按道理说可以用遍历打开的程序然后关闭,但是我觉得效率可能在大部分情况下都没有查找窗口句柄来得快,这个程序只是用来测试的,不是我最终的程序,所以不要纠结于实用性和其他代码设计的合理性。

     public partial class Form1 : Form
    {
        public const int WM_LBUTTONDOWN = 0x0201;
        public const int WM_LBUTTONUP = 0x0202;
        [DllImport("User32.dll")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam);
        [DllImport("User32.dll")]
        public static extern IntPtr FindWindowEx(IntPtr parent, IntPtr childe, string strclass, string FrmText);
        [DllImport("User32.dll", EntryPoint = "SetForegroundWindow")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Process process = new Process();
            process.StartInfo.FileName = "C:/Users/yangy/Desktop/4gb_patch.exe";
            process.Start();
                       Process process = new Process();
            process.StartInfo.FileName = "C:/Users/yangy/Desktop/4gb_patch.exe";
            process.Start();
            for (int i = 0; i < 1;)
            {
                if (FindWindow(null, "4GB Patch") != null)
                {
                    IntPtr window = FindWindow(null, "4GB Patch");
                    IntPtr button = FindWindowEx(window, IntPtr.Zero, "Button", "OK");
                    SetForegroundWindow(window);
                    SetForegroundWindow(button);
                    SendMessage(button, WM_LBUTTONDOWN, IntPtr.Zero, null);
                    SendMessage(button, WM_LBUTTONUP, IntPtr.Zero, null);
                }
                                else
                                {
                                    i = 2;
                                }
            }
        }
    }

3个回答

caozhy
caozhy   Ds   Rxr 2017.09.17 11:24

用spy++看你的按钮句柄,和你调试到IntPtr button = FindWindowEx(window, IntPtr.Zero, "Button", "OK");后得到的是否一致
直接发送wm_click试试看

qq_30909659
qq_30909659 我的问题里提到了这个代码查到的句柄是正确的,就是通过spy++核对的
2 个月之前 回复
steel_1991
steel_1991   2017.09.19 17:55
        SendMessage(handle, (uint)MessageType.WM_LBUTTONDOWN, new IntPtr((int)0x0001), new IntPtr(new IntPtr(((UInt32)y << 16) | (UInt32)x)));
        Thread.Sleep(10);
        SendMessage(handle, (uint)MessageType.WM_LBUTTONUP, new IntPtr((int)0x0000), new IntPtr(new IntPtr(((UInt32)y << 16) | (UInt32)x)));


                    x和y是相对按钮的坐标   0x0001 表示鼠标左键。 0x0000表示没有其他键
steel_1991
steel_1991   2017.09.19 17:57

[DllImport("user32")]
private static extern bool PostMessage(IntPtr handle, uint Msg, IntPtr wParam, IntPtr lParam);
推荐使用postmessage

Csdn user default icon
上传中...
上传图片
插入图片