在MessageBox中粗体文字 [英] Bold text in MessageBox

查看:751
本文介绍了在MessageBox中粗体文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何可以显示通过 MessageBox.Show 显示在对话框中以粗体文字,使用C#?

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

推荐答案

这是可能的,一个消息框,是可以与像任何其他会混乱常规窗口。然而,code这样做是有点坚韧不拔。添加一个新类到您的项目并粘贴此code:

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

和使用这样的:

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.

这篇关于在MessageBox中粗体文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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