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

时间:2015-07-25 14:17:51

标签: java c# button sendmessage

我正在尝试将按钮单击发送到另一个应用程序,在本例中是一个Java应用程序。我正在使用FindWindow()。我可以使用SendKeys.SendWait()将密钥发送到应用程序窗口,但是当我尝试单击Register按钮时,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);
        }
    }

1 个答案:

答案 0 :(得分:0)

如果lpClassNameNULL FindWindow,则会仅通过lpWindowName(窗口标题)搜索该窗口。如果特定窗口的类是可变的,

,这很有用

我的问题是,你给出了正确的窗口标题吗?

您可以使用流程资源管理器找到它 - https://technet.microsoft.com/en-us/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);           
    }

来源:Sending keystrokes from a C# application to a Java application - strange behaviour?