C# 如何点击";是”;带代码的订单应用程序中的按钮

C# 如何点击";是”;带代码的订单应用程序中的按钮,c#,windows,winforms,user32,C#,Windows,Winforms,User32,我有密码 private const int WM_CLOSE = 16; private const int BN_CLICKED = 245; [DllImport("User32.dll")] public static extern Int32 FindWindow(String lpClassName, String lpWindowName); [DllImport("user32.dll", CharSet = CharSet.Auto)]

我有密码

 private const int WM_CLOSE = 16;
    private const int BN_CLICKED = 245;
    [DllImport("User32.dll")]
    public static extern Int32 FindWindow(String lpClassName, String lpWindowName);

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern int SendMessage(int hWnd, int msg, int wParam, IntPtr lParam);

    [DllImport("user32.dll", SetLastError = true)]
    public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);

    [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
    private static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);



 public void Click(string _btnTitle)
    {
        int hwnd = 0;
        IntPtr hwndChild = IntPtr.Zero;

        //Get a handle for the Calculator Application main window
        // foreach (Process p in Process.GetProcesses())
        //{   
        hwnd = FindWindow(null, FrmTitle);            
        if (hwnd != 0)
        {
            hwndChild = FindWindowEx((IntPtr)hwnd, IntPtr.Zero, "Button", _btnTitle);
            SendMessage((int)hwndChild, BN_CLICKED, 0, IntPtr.Zero);
        }
    }
我无法单击应用程序消息框上的“是”按钮:(


有人得到提示吗?Tks

您没有发送正确的消息

在调用SendMessage()时,尝试使用
BM\u CLICK
(0x00F5)。如果
hwndChild
是按钮的窗口句柄,而不是容器对话框,则应该可以使用


BN_单击的
不起作用,因为那是一个通知代码,不是一条消息。

只是为了确保“hwnd”和“hwndChild”确实有值?您是否检查以确保hwndChild返回一个有效的按钮?我在过去的生活中广泛使用了这种技术(配置管理3k+工作站),通常最大的工作是“爬树”我通常会使用应用程序手动查找我想要的窗口,然后编写代码来搜索、查找和发送我想要的消息。(如果您的实际代码比示例更复杂,您可能需要获取“parent”窗口在实际“侦听”“单击”消息之前处于正确状态。您是否使用spy++或其他工具确保您的hwndChild是您认为应该的窗口?此外,FrmTitle是否实际是表单?您不应该传递其句柄吗?此代码以应用程序的主要形式工作,但在Messagebox中:(你没有试过寻找字符串文字<代码>和是<代码>,而不是<代码>是的<代码>?好的答案,而且是正确的!我已经从你的答案中删除了这条线,表达了对问题的年龄的关注,因为这不是我们认为堆栈溢出很重要的问题。如果你有一个好的答案,你应该分享它,就像你做的那样!我们总是从谷歌获得新的流量,你的答案仍然可以帮助很多人。