繁体   English   中英

C#找不到Java应用程序按钮

[英]C# Can't Find Java application button

我正在尝试将按钮单击发送到另一个应用程序,在本例中为另一个Java应用程序。 我正在使用FindWindow()。 我可以使用SendKeys.SendWait()将密钥发送到应用程序窗口,但是当我尝试单击“注册”按钮时,Findwindowex()为按钮指针返回0。 我唯一的想法是,也许FindWindowEx()不喜欢父子句柄相同,但是在这种情况下,没有子窗口句柄。 任何帮助将不胜感激。

    [DllImport("user32.dll")]
    private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    [DllImport("user32.dll")]
    private static extern bool SetForegroundWindow(IntPtr hWnd);

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

    [DllImport("user32.dll", CharSet=CharSet.Auto)]

    public static extern int SendMessage(int hWnd, int msg, int wParam, IntPtr lParam);
    public void Start()
    {
        IntPtr zero = IntPtr.Zero;
        for (int i = 0; (i < 60) && (zero == IntPtr.Zero); i++)
        {
            Thread.Sleep(500);
            zero = FindWindow(null, "EDM Autosync Client Login");
        }
        if (zero != IntPtr.Zero)
        {
            SetForegroundWindow(zero);
            SendKeys.SendWait("username");
            SendKeys.SendWait("{TAB}");
            SendKeys.SendWait("password");
            SendKeys.SendWait("{ENTER}");
            SendKeys.Flush();
        }
    }

    public void register()
    {
        IntPtr zero = IntPtr.Zero;
        IntPtr hwndChild = IntPtr.Zero;
        int BN_CLICKED = 245;
        int WM_CLOSE = 16;

        for (int i = 0; (i < 60) && (zero == IntPtr.Zero); i++)
        {
            Thread.Sleep(500);
            zero = FindWindow(null, "Autosync Connection Registration");
        }
        if (zero != IntPtr.Zero)
        {
            SetForegroundWindow(zero);
            SendKeys.SendWait("{TAB}");
            SendKeys.SendWait("{TAB}");
            SendKeys.SendWait("{TAB}");
            SendKeys.SendWait("10.75.12.10");
            SendKeys.SendWait("{TAB}");
            SendKeys.SendWait("{TAB}");
            SendKeys.SendWait("username");
            SendKeys.SendWait("{TAB}");
            SendKeys.SendWait("password");
            SendKeys.SendWait("{TAB}");
            SendKeys.SendWait("{TAB}");
            SendKeys.Flush();
            for (int i = 0; (i < 60) && (zero == IntPtr.Zero); i++)
            {
                Thread.Sleep(500);
                hwndChild = FindWindowEx(zero, IntPtr.Zero, "Button", "Register");
            }
            SendMessage((int)hwndChild, BN_CLICKED, 0, IntPtr.Zero);
        }
    }

如果lpClassNameNULL FindWindow将仅按lpWindowName (窗口的标题)搜索窗口。 如果特定窗口的类是可变的,这很有用,

我的问题是,您给正确的窗口标题吗?

您可以使用流程浏览器找到它-https: //technet.microsoft.com/zh-cn/sysinternals/bb896653.aspx

让我知道这是否为您解决了问题。

万一它没有帮助-我发现了以下代码片段:

private void SendKeysToWindow(string WindowName, string KeysToSend)
    { 
        IntPtr hWnd = FindWindow(null, WindowName);            
        ShowWindow(hWnd, SW_SHOWNORMAL);
        SetForegroundWindow(hWnd);
        Thread.Sleep(50);
        SendKeys.SendWait(KeysToSend);           
    }

来源:将击键从C#应用程序发送到Java应用程序-奇怪的行为吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM