RF103T 2017-09-17 03:03 采纳率: 0%
浏览 2005
已结题

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条回答

  • threenewbee 2017-09-17 03:24
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料