[英]Intercepting VB Messageboxes with icons (vbCritical, vbInformation) in C#
我已经使用没有通知图标的 Microsoft Word 模板中的 VB 消息框成功测试了以下代码。 我能够获取文本并关闭消息框。
但是,当消息框有图标时, sb.ToString()
返回一个空字符串。
此外, GetWindowTextLength(txtHandle)
返回0
。 文本句柄被正确识别,因为 Messageboxes 在SendMessage
命令处关闭。
我使用 Spy++ 来分析消息框。 这是 output 的屏幕截图。
从它的外观来看,有两个 windows,class 名称为“Statis”,代码似乎采用第一个。 我需要获取“ O eroare la salvarea documentului in baza de date. ”文本。
为了得到这个文本,我应该改变什么?
[DllImport("user32.dll")] static extern int FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")] static extern int SendMessage(int hWnd, int wMsg, int wParam, int lParam);
[DllImport("user32.dll", SetLastError = true)] static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] static extern int GetWindowTextLength(IntPtr hWnd);
public static void KillMbox()
{
for (int h = 0; h == 0;)
{
Thread.Sleep(1000);
h = FindWindow(null, "Microsoft Word");
if (h != 0)
{
IntPtr hAsIntPtr = new IntPtr(h);
IntPtr txtHandle = FindWindowEx(hAsIntPtr, IntPtr.Zero, "Static", null);
int len = GetWindowTextLength(txtHandle);
StringBuilder sb = new StringBuilder();
GetWindowText(txtHandle, sb, len + 1);
SendMessage(h, 16, 0, 0);
MessageBox.Show(sb.ToString());
}
}
}
我能够通过使用索引引用第二个 window 和 class 名称Static
来获取 MessageBox 的内容。
以下答案对我有帮助-> https://stackoverflow.com/a/5685715/10468231
public void KillMbox()
{
IntPtr h = FindWindow("#32770", "Microsoft Word");
if (h != IntPtr.Zero)
{
IntPtr TextMesaj = FindWindowByIndex(h, 2);
int len = GetWindowTextLength(TextMesaj);
StringBuilder sb = new StringBuilder(len + 1);
GetWindowText(TextMesaj, sb, len + 1);
SendMessage(new HandleRef(null, h), 16, IntPtr.Zero, IntPtr.Zero);
MessageBox.Show(sb.ToString());
}
}
static IntPtr FindWindowByIndex(IntPtr hWndParent, int index)
{
if (index == 0)
return hWndParent;
else
{
int ct = 0;
IntPtr result = IntPtr.Zero;
do
{
result = FindWindowEx(hWndParent, result, "Static", null);
if (result != IntPtr.Zero)
++ct;
}
while (ct < index && result != IntPtr.Zero);
return result;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.