Hy guys!
请帮帮我;
我正在尝试从带有user32.dll API的javascript警告框中获取文本,但我无法弄清楚lpszClass名称。
如果这是一个愚蠢的问题,请有人帮助我,对不起。
<DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function FindWindowEx(hwndParent As IntPtr, hwndChildAfter As IntPtr, lpszClass As String, lpszWindow As String) As IntPtr
End Function
<DllImport("user32.dll", EntryPoint:="FindWindow", SetLastError:=True)> _
Private Shared Function FindWindow(lpClassName As String, lpWindowName As String) As IntPtr
End Function
<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Private Shared Function SendMessage(hWnd As IntPtr, Msg As UInt32, wParam As IntPtr, lParam As IntPtr) As IntPtr
End Function
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function GetWindowText(ByVal hwnd As IntPtr, ByVal lpString As System.Text.StringBuilder, ByVal cch As Integer) As Integer
End Function
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function GetWindowTextLength(ByVal hwnd As IntPtr) As Integer
End Function
Private Function Form1_Deactivate(sender As Object, e As EventArgs) Handles Me.Deactivate As String
Dim hwnd As IntPtr = FindWindow("#32770", "Mensagem da página da web")
'hwnd = FindWindowEx(hwnd, IntPtr.Zero, "<NEED TO KNOW WHAT TO PUT HERE", Nothing)
Dim length As Integer = GetWindowTextLength(hwnd)
Dim sb As New System.Text.StringBuilder("", length + 1)
GetWindowText(hwnd, sb, sb.Capacity)
return sb.ToString()
End Function
答案 0 :(得分:0)
这里是Windows API的新手,但是这是我做类似事情的方式:
VisualStudio附带了一个名为Spy ++的工具(位于“工具”菜单下),该工具列出了系统上的每个“窗口”以及相关信息。您正在寻找的信息显示在窗口标题的后面:
在我的情况下,我需要找到多个具有相同类的子级,因此我最终使用了EnumChildWindows函数而不是FindWindowEx。这是我的Java代码(使用JNA):
User32 u32 = User32.INSTANCE;
WinDef.HWND window = u32.FindWindow("#32770", "Control Center Login");
List<WinDef.HWND> windows = new ArrayList<>();
u32.EnumChildWindows(window, (child, data) -> {
char[] name = new char[256];
u32.GetClassName(child, name, 256);
String nameStr = new String(name).trim();
if(nameStr.equals("Edit")) {
windows.add(child);
}
return true;
}, null);
我希望您在过去两年中一直没有等待这个答案,但是如果其他人发现了这个问题,这是一种解决方法:)