[英]C# Can't Find Java application button
I'm trying to send a button click to another application, in this case its a Java application. 我正在尝试将按钮单击发送到另一个应用程序,在本例中为另一个Java应用程序。 I'm using FindWindow().
我正在使用FindWindow()。 I can use SendKeys.SendWait() to send keys to the application window, however when I attempt to click the Register button, Findwindowex() returns 0 for the button pointer.
我可以使用SendKeys.SendWait()将密钥发送到应用程序窗口,但是当我尝试单击“注册”按钮时,Findwindowex()为按钮指针返回0。 My only thought is that perhaps the FindWindowEx() doesn't like the parent and child handles being the same, but in this case there is no child window handle.
我唯一的想法是,也许FindWindowEx()不喜欢父子句柄相同,但是在这种情况下,没有子窗口句柄。 Any help would be greatly appreciated.
任何帮助将不胜感激。
[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);
}
}
If lpClassName
is NULL
FindWindow
will search for the window by the lpWindowName
(window's title) only. 如果
lpClassName
为NULL
FindWindow
将仅按lpWindowName
(窗口的标题)搜索窗口。 This is useful if the class of a particular window is variable, 如果特定窗口的类是可变的,这很有用,
My question, are you giving the correct window's title ? 我的问题是,您给正确的窗口标题吗?
You can find it by using process explorer - https://technet.microsoft.com/en-us/sysinternals/bb896653.aspx 您可以使用流程浏览器找到它-https: //technet.microsoft.com/zh-cn/sysinternals/bb896653.aspx
Let me know if that solved the issue for you. 让我知道这是否为您解决了问题。
In case it wasn't helpful - I've found the below code snippet: 万一它没有帮助-我发现了以下代码片段:
private void SendKeysToWindow(string WindowName, string KeysToSend)
{
IntPtr hWnd = FindWindow(null, WindowName);
ShowWindow(hWnd, SW_SHOWNORMAL);
SetForegroundWindow(hWnd);
Thread.Sleep(50);
SendKeys.SendWait(KeysToSend);
}
Source: Sending keystrokes from a C# application to a Java application - strange behaviour? 来源:将击键从C#应用程序发送到Java应用程序-奇怪的行为吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.