我正在努力尝试关闭特定的内容MessageBox
(如果它根据标题和文本显示)。MessageBox
当没有图标时我可以使用它。
IntPtr handle = FindWindowByCaption(IntPtr.Zero, "Caption");
if (handle == IntPtr.Zero)
return;
//Get the Text window handle
IntPtr txtHandle = FindWindowEx(handle, IntPtr.Zero, "Static", null);
int len = GetWindowTextLength(txtHandle);
//Get the text
StringBuilder sb = new StringBuilder(len + 1);
GetWindowText(txtHandle, sb, len + 1);
//close the messagebox
if (sb.ToString() == "Original message")
{
SendMessage(new HandleRef(null, handle), WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
}
Run Code Online (Sandbox Code Playgroud)
MessageBox
当显示没有像下面这样的图标时,上面的代码工作得很好。
MessageBox.Show("Original message", "Caption");
Run Code Online (Sandbox Code Playgroud)
但是,如果它包含如下所示的图标(来自MessageBoxIcon
),则它不起作用;GetWindowTextLength
返回 0 并且什么也没有发生。
MessageBox.Show("Original message", "Caption", MessageBoxButtons.OK, …
Run Code Online (Sandbox Code Playgroud) 假设我为其中一个进程设置了这个Window层次结构:
Main Window (class name: XYZ_Widget_1)
`- Child Window (class name: XYZ_Widget_0)
`- Child-Child Window (class name: XYZ_Renderer)
Run Code Online (Sandbox Code Playgroud)
我如何找到HWND Child-Child Window
?
我尝试FindWindow
在XYZ_Renderer
类上使用Win32 API函数,但该FindWindow
函数找不到子窗口.
然后我试图使用FindWindow
发现Main Window
,它成功了,但使用后FindWindowEx
只能找到Child Window
作为Child-Child Window
是不是一个孩子Main Window
.
我想我可以更深入一层并在Child Window
找到它之后调用FindWindowEx .
但在我这样做之前,我想可能有一个简单的方法可以找到Child-Child Window
?
我正在尝试获取子对话框窗口的句柄。我试过使用FindWindowEx,但没有用。相反,FindWindow确实起作用。
我使用以下代码对Visual Studio的“选项”窗口进行了实验:
IntPtr vsHandle = Process.GetProcessById(vsProcessId).MainWindowHandle; // consistent with spy++'s parent handle of options window
IntPtr optionsHandle = FindWindowEx(vsHandle, IntPtr.Zero, "#32770", "Options"); // returns 0
IntPtr optionsHandle2 = FindWindow("#32770", "Options"); // returns correct handle
Run Code Online (Sandbox Code Playgroud)
据我了解,FindWindowEx应该已经工作了,它是一个子窗口。
我正在运行Windows XP,并且还尝试使用FindWindowEx(vsHandle,IntPtr.Zero,“#32770”,null)。没用 似乎唯一的获得方法是使用FindWindow,因为打开具有相同对话框的两个父实例并不足够。
这是声明:
[DllImport("user32.dll")]
Private static extern IntPtr FindWindow(string className, string windowTitle);
[DllImport("user32.dll")]
Private static extern IntPtr FindWindowEx(IntPtr parentHWnd, IntPtr childAfterHWnd, string className, string windowTitle);
Run Code Online (Sandbox Code Playgroud)
提前致谢。
所以,我正在尝试捕获一个包含2个文本框的窗口,并向这两个文本框发送一些文本.但是两个文本框都没有标题和相同的类名"编辑".到目前为止,我所能做的就是捕获第一个文本框,就是这样.
贴在下面是我的代码.
Imports System.Runtime.InteropServices
Imports System.Text
Public Class Form1
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, _
ByVal lParam As String) As Integer
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
(ByVal hWnd1 As Integer, ByVal hWnd2 As Integer, ByVal lpsz1 As String, _
ByVal lpsz2 …
Run Code Online (Sandbox Code Playgroud)