带有图标的MessageBox文本如何获取? [英] How to get the text of a MessageBox when it has an icon?

查看:228
本文介绍了带有图标的MessageBox文本如何获取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试关闭特定的MessageBox(如果它根据标题和文本显示).当MessageBox没有图标时,我可以使用它.

I am working on trying to close a specific MessageBox if it shows up based on the caption and text. I have it working when the MessageBox doesn't have an icon.

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);
}

显示没有图标时,上面的代码可以正常工作,如下所示.

The above code works just fine when the MessageBox is shown without an icon like the following.

MessageBox.Show("Original message", "Caption");

但是,如果它包含如下所示的图标(来自MessageBoxIcon),则它不起作用; GetWindowTextLength返回0,什么也没有发生.

However, if it includes an icon (from MessageBoxIcon) like the following, it doesn't work; GetWindowTextLength returns 0 and nothing happens.

MessageBox.Show("Original message", "Caption", MessageBoxButtons.OK, MessageBoxIcon.Information);

我最好的猜测是FindWindowEx的第三和/或第四参数需要更改,但是我不确定该通过什么.还是可能需要更改第二个参数才能跳过图标?我不太确定.

My best guess is that the 3rd and/or 4th paramters of FindWindowEx need to change but I'm not sure what to pass instead. Or maybe the 2nd parameter needs to change to skip the icon? I'm not really sure.

推荐答案

看来,当MessageBox具有图标时,FindWindowEx返回第一个子代的文本(在这种情况下为图标),因此返回零.长度.现在,在此答案的帮助下,我有了一个想法,可以遍历孩子,直到找到一个有文字的孩子.这应该起作用:

It appears that when the MessageBox has an icon, FindWindowEx returns the text of the first child (which is the icon in this case) hence, the zero length. Now, with the help of this answer, I got the idea to iterate the children until finding one with a text. This should work:

IntPtr handle = FindWindowByCaption(IntPtr.Zero, "Caption");

if (handle == IntPtr.Zero)
    return;

//Get the Text window handle
IntPtr txtHandle = IntPtr.Zero;
int len;
do
{
    txtHandle = FindWindowEx(handle, txtHandle, "Static", null);
    len = GetWindowTextLength(txtHandle);
} while (len == 0 && txtHandle != IntPtr.Zero);

//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);
}

很明显,您可以对其进行调整以适合您的特定情况(例如,不断进行迭代,直到找到您要查找的实际文本为止),尽管我认为带有文本的孩子可能永远是第二个孩子:

Obviously, you could adjust it to fit your particular situation (e.g., keep iterating until you find the actual text you're looking for) although I think the child with the text will probably always be the second one:

这篇关于带有图标的MessageBox文本如何获取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆