【问题标题】:How do I get MessageBox button caption?如何获取 MessageBox 按钮标题?
【发布时间】:2009-05-31 16:35:01
【问题描述】:

我正在制作一个允许您复制文本的自定义消息框,但我希望它看起来与标准消息框完全一样,因此我想将按钮文本设置为任何系统语言,如 MessageBox 类做。 有谁知道如何获取该文本(“是”、“否”、“取消”等)?

【问题讨论】:

  • 如果您没有找到解决方案,也许您可​​以将消息文本放在剪贴板上,并通知用户消息在剪贴板上。
  • 如果在显示标准 MessageBox 时按 Ctrl+C,默认情况下会将文本复制到剪贴板。
  • 感谢您的提示,但无论如何我需要更多的自定义设置,例如滚动条来获取更大的消息。

标签: c# .net winforms messagebox


【解决方案1】:

感谢您对 Snarfblam 链接的回答,我可以弄清楚其余的。

class Program {

[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern int LoadString(IntPtr hInstance, uint uID, StringBuilder lpBuffer, int nBufferMax);
[DllImport("kernel32")]
static extern IntPtr LoadLibrary(string lpFileName);

private const uint OK_CAPTION = 800;
private const uint CANCEL_CAPTION = 801;
private const uint ABORT_CAPTION = 802;
private const uint RETRY_CAPTION = 803;
private const uint IGNORE_CAPTION = 804;
private const uint YES_CAPTION = 805;
private const uint NO_CAPTION = 806;
private const uint CLOSE_CAPTION = 807;
private const uint HELP_CAPTION = 808;
private const uint TRYAGAIN_CAPTION = 809;
private const uint CONTINUE_CAPTION = 810;

static void Main(string[] args) {
    StringBuilder sb = new StringBuilder(256);

    IntPtr user32 = LoadLibrary(Environment.SystemDirectory + "\\User32.dll");

    LoadString(user32, OK_CAPTION, sb, sb.Capacity);
    string ok = sb.ToString();

    LoadString(user32, CANCEL_CAPTION, sb, sb.Capacity);
    string cancel = sb.ToString();

    LoadString(user32, ABORT_CAPTION, sb, sb.Capacity);
    string abort = sb.ToString();

    LoadString(user32, RETRY_CAPTION, sb, sb.Capacity);
    string retry = sb.ToString();

    LoadString(user32, IGNORE_CAPTION, sb, sb.Capacity);
    string ignore = sb.ToString();

    LoadString(user32, YES_CAPTION, sb, sb.Capacity);
    string yes = sb.ToString();

    LoadString(user32, NO_CAPTION, sb, sb.Capacity);
    string no = sb.ToString();

    LoadString(user32, CLOSE_CAPTION, sb, sb.Capacity);
    string close = sb.ToString();

    LoadString(user32, HELP_CAPTION, sb, sb.Capacity);
    string help = sb.ToString();

    LoadString(user32, TRYAGAIN_CAPTION, sb, sb.Capacity);
    string tryAgain = sb.ToString();

    LoadString(user32, CONTINUE_CAPTION, sb, sb.Capacity);
    string cont = sb.ToString();

}

【讨论】:

    【解决方案2】:

    这些字符串似乎存储在 User32.dll 库中。在 Pure BASIC 论坛的this discussion 中有详细信息,靠近底部。

    【讨论】:

    • 非常感谢我能找到解决方案,我会在几分钟后发布代码。
    【解决方案3】:

    您可以在Reflector 中打开 System.Windows.Forms.dll 并查看它是如何设置按钮文本的。

    【讨论】:

    • 我这样做了,但是消息框调用是user32.dll中的一个函数,所以文本是在低级别获取的,我确定有一个api可以获取它。
    【解决方案4】:

    在您输入该消息时,您可以输入“Yes”、“No”、“Ok”、“Cancel”...

    【讨论】:

    • 我很确定 OP 想要从系统中获取文本,以便使用正确的语言(系统区域设置)
    • 正是亚当,这就是我需要的,所以我不必用尽所有语言
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-07
    • 1970-01-01
    • 1970-01-01
    • 2019-07-27
    • 1970-01-01
    相关资源
    最近更新 更多