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

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试试看

    评论

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名