C# 找不到 Java 应用程序按钮
C# Can't Find Java application button
我正在尝试将按钮点击发送到另一个应用程序,在本例中是 Java 应用程序。我正在使用 FindWindow()。我可以使用 SendKeys.SendWait() 将密钥发送到应用程序 window,但是当我尝试单击注册按钮时,查找 windowex() returns 0 按钮指针。我唯一的想法是,也许 FindWindowEx() 不喜欢父句柄和子句柄相同,但在这种情况下没有子 window 句柄。任何帮助将不胜感激。
[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);
}
}
如果 lpClassName
是 NULL
FindWindow
将仅按 lpWindowName
(window 的标题)搜索 window。如果特定 window 的 class 是可变的,
,这将很有用
我的问题是,您提供的 window 标题是否正确?
您可以使用进程资源管理器找到它 - 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?
我正在尝试将按钮点击发送到另一个应用程序,在本例中是 Java 应用程序。我正在使用 FindWindow()。我可以使用 SendKeys.SendWait() 将密钥发送到应用程序 window,但是当我尝试单击注册按钮时,查找 windowex() returns 0 按钮指针。我唯一的想法是,也许 FindWindowEx() 不喜欢父句柄和子句柄相同,但在这种情况下没有子 window 句柄。任何帮助将不胜感激。
[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);
}
}
如果 lpClassName
是 NULL
FindWindow
将仅按 lpWindowName
(window 的标题)搜索 window。如果特定 window 的 class 是可变的,
我的问题是,您提供的 window 标题是否正确?
您可以使用进程资源管理器找到它 - 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?