MessageBox 中的粗体文本

发布于 2024-08-21 20:12:18 字数 61 浏览 3 评论 0原文

如何使用 C# 在 MessageBox.Show 显示的对话框中以粗体显示文本?

How can I show the text in bold in the dialog displayed by MessageBox.Show, using C#?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

中性美 2024-08-28 20:12:18

有可能,消息框是一个常规窗口,可以像其他窗口一样被弄乱。然而,这样做的代码有点粗糙。将一个新类添加到您的项目中并粘贴此代码:

using System;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

class BoldMessageBox : IDisposable {
  private int mTries = 0;
  private Form mOwner;
  private Font mFont;

  public BoldMessageBox(Form owner) {
    mOwner = owner;
    owner.BeginInvoke(new MethodInvoker(findDialog));
  }

  private void findDialog() {
    // Enumerate windows to find the message box
    if (mTries < 0) return;
    EnumThreadWndProc callback = new EnumThreadWndProc(checkWindow);
    if (EnumThreadWindows(GetCurrentThreadId(), callback, IntPtr.Zero)) {
      if (++mTries < 10) mOwner.BeginInvoke(new MethodInvoker(findDialog));
    }
  }
  private bool checkWindow(IntPtr hWnd, IntPtr lp) {
    // Checks if <hWnd> is a dialog
    StringBuilder sb = new StringBuilder(260);
    GetClassName(hWnd, sb, sb.Capacity);
    if (sb.ToString() != "#32770") return true;
    // Got it, get the STATIC control that displays the text
    IntPtr hText = GetDlgItem(hWnd, 0xffff);
    if (hText != IntPtr.Zero) {
      // Get the current font
      IntPtr hFont = SendMessage(hText, WM_GETFONT, IntPtr.Zero, IntPtr.Zero);
      Font font = Font.FromHfont(hFont);
      // And make it bold (note the size change to keep enough space!!)
      mFont = new Font(font.FontFamily, font.SizeInPoints - 1f, FontStyle.Bold);
      SendMessage(hText, WM_SETFONT, mFont.ToHfont(), (IntPtr)1);
    }
    // Done
    return false;
  }
  public void Dispose() {
    mTries = -1;
    mOwner = null;
    if (mFont != null) mFont.Dispose();
  }

  // P/Invoke declarations
  private const int WM_SETFONT = 0x30;
  private const int WM_GETFONT = 0x31;
  private delegate bool EnumThreadWndProc(IntPtr hWnd, IntPtr lp);
  [DllImport("user32.dll")]
  private static extern bool EnumThreadWindows(int tid, EnumThreadWndProc callback, IntPtr lp);
  [DllImport("kernel32.dll")]
  private static extern int GetCurrentThreadId();
  [DllImport("user32.dll")]
  private static extern int GetClassName(IntPtr hWnd, StringBuilder buffer, int buflen);
  [DllImport("user32.dll")]
  private static extern IntPtr GetDlgItem(IntPtr hWnd, int item);
  [DllImport("user32.dll")]
  private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
}

并像这样使用它:

private void button1_Click(object sender, EventArgs e) {
  using (new BoldMessageBox(this)) {
    MessageBox.Show("Nobugz waz here");
  }
}

这种方法有一个缺陷。将字体设置为粗体后,文本必须仍然适合消息框为文本保留的静态控件。这需要我把字体变小。您可能需要调整该值。

It is possible, a message box is a regular window that can be messed with like any other. The code to do so is however a bit gritty. Add a new class to your project and paste this code:

using System;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

class BoldMessageBox : IDisposable {
  private int mTries = 0;
  private Form mOwner;
  private Font mFont;

  public BoldMessageBox(Form owner) {
    mOwner = owner;
    owner.BeginInvoke(new MethodInvoker(findDialog));
  }

  private void findDialog() {
    // Enumerate windows to find the message box
    if (mTries < 0) return;
    EnumThreadWndProc callback = new EnumThreadWndProc(checkWindow);
    if (EnumThreadWindows(GetCurrentThreadId(), callback, IntPtr.Zero)) {
      if (++mTries < 10) mOwner.BeginInvoke(new MethodInvoker(findDialog));
    }
  }
  private bool checkWindow(IntPtr hWnd, IntPtr lp) {
    // Checks if <hWnd> is a dialog
    StringBuilder sb = new StringBuilder(260);
    GetClassName(hWnd, sb, sb.Capacity);
    if (sb.ToString() != "#32770") return true;
    // Got it, get the STATIC control that displays the text
    IntPtr hText = GetDlgItem(hWnd, 0xffff);
    if (hText != IntPtr.Zero) {
      // Get the current font
      IntPtr hFont = SendMessage(hText, WM_GETFONT, IntPtr.Zero, IntPtr.Zero);
      Font font = Font.FromHfont(hFont);
      // And make it bold (note the size change to keep enough space!!)
      mFont = new Font(font.FontFamily, font.SizeInPoints - 1f, FontStyle.Bold);
      SendMessage(hText, WM_SETFONT, mFont.ToHfont(), (IntPtr)1);
    }
    // Done
    return false;
  }
  public void Dispose() {
    mTries = -1;
    mOwner = null;
    if (mFont != null) mFont.Dispose();
  }

  // P/Invoke declarations
  private const int WM_SETFONT = 0x30;
  private const int WM_GETFONT = 0x31;
  private delegate bool EnumThreadWndProc(IntPtr hWnd, IntPtr lp);
  [DllImport("user32.dll")]
  private static extern bool EnumThreadWindows(int tid, EnumThreadWndProc callback, IntPtr lp);
  [DllImport("kernel32.dll")]
  private static extern int GetCurrentThreadId();
  [DllImport("user32.dll")]
  private static extern int GetClassName(IntPtr hWnd, StringBuilder buffer, int buflen);
  [DllImport("user32.dll")]
  private static extern IntPtr GetDlgItem(IntPtr hWnd, int item);
  [DllImport("user32.dll")]
  private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
}

And use it like this:

private void button1_Click(object sender, EventArgs e) {
  using (new BoldMessageBox(this)) {
    MessageBox.Show("Nobugz waz here");
  }
}

There is one flaw in this approach. After making the font bold, the text must still fit in the static control that the message box reserved for the text. That required me to make the font smaller. You may have to tweak this value.

浸婚纱 2024-08-28 20:12:18

你不能。这是 API MessageBoxEx 的包装器。

创建您自己的自定义消息框来执行此操作。


您可以按照教程作为如何实现的示例。

创建此类表单的基本步骤:

  1. 创建一个新表单
  2. 添加标签和两个按钮
  3. 将标签字体设置为粗体
  4. 将处理程序添加到两个按钮,关闭表单并设置按下按钮的一些属性。

You can't. This is a wrapper for the API MessageBoxEx.

Create your own custom messagebox to do it.


You can follow this tutorial, as an example of how to implement one.

The basics steps of creating such a form:

  1. Create a new form
  2. Add a label and two buttons
  3. Set the label font to Bold
  4. add handler to both buttons, closing the form and setting some property for which button was pressed.
空气里的味道 2024-08-28 20:12:18

没有可以做的。你必须建造自己的盒子。我假设这是WinForms,如果是ASP.NET我没有资格回答。

No can do. You'll have to build your own box. I'm assuming that this is WinForms, if it's ASP.NET I'm not qualified to answer.

甚是思念 2024-08-28 20:12:18

扩展 MessageBox .NET 程序集
XMSG .NET 网页:更多信息,下载

动态调整各种消息框视觉设置。

可调整的功能包括消息字体和颜色、按钮标题、字体和工具提示、对话框背景、对话框位置、对话框图标、超时等。根据所选的消息字体,对话框窗口会自动调整自身大小以适应消息。

可以选择显示的其他控件:复选框、文本输入、Web 链接、最多 3 个额外按钮。

在 .NET 代码中,您仍然调用常规 MessageBox.Show。扩展MessageBox不是一个定制的对话框。这仍然是常规的 MessageBox,添加了扩展功能。

支持的操作系统:XP、2000、2003、2008 Vista、Win7 - 32 或 64 位。

下载包括全功能试用版和带有完整 C# 源代码的普通版。

Extended MessageBox .NET Assembly
XMSG .NET web page: more info, download

Adjusts on the fly wide variety of MessageBox visual settings.

Adjustable features include message font and color, button captions, fonts and tooltips, dialog background, dialog position, dialog icon, timeout and more. Depending on message font selected, the dialog window automatically resizes itself to accommodate the message.

Additional controls that can be optionally displayed: check box, text input, web link, up to 3 extra buttons.

In your .NET code you still call regular MessageBox.Show. Extended MessageBox is not a custom-made dialog. This is still regular MessageBox with extended features added.

OS supported: XP, 2000, 2003, 2008 Vista, Win7 -- 32 or 64-bit.

Downloads include fully-functional trial version, and regular version with complete C# source code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文