簡體   English   中英

MessageBox中的粗體文本

[英]Bold text in MessageBox

如何使用C#在MessageBox.Show顯示的對話框中以粗體顯示文本?

有可能,消息框是一個常規窗口,可以像任何其他窗口一樣混亂。 但是,這樣做的代碼有點粗糙。 在項目中添加一個新類並粘貼此代碼:

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

這種方法存在一個缺陷。 在使字體變為粗體之后,文本仍必須適合為文本保留的消息框的靜態控件。 這要求我讓字體變小。 您可能需要調整此值。

你不能。 這是API MessageBoxEx的包裝器。

創建自己的自定義消息框來執行此操作。


您可以按照教程,作為如何實現一個示例。

創建此類表單的基本步驟:

  1. 創建一個新表單
  2. 添加標簽和兩個按鈕
  3. 將標簽字體設置為粗體
  4. 向兩個按鈕添加處理程序,關閉表單並設置按下按鈕的某個屬性。

沒有辦法。 你必須建立自己的盒子。 我假設這是WinForms,如果它是ASP.NET我沒有資格回答。

擴展的MessageBox .NET程序集 XMSG .NET網頁:更多信息,下載

快速調整各種MessageBox視覺設置。

可調節功能包括消息字體和顏色,按鈕標題,字體和工具提示,對話框背景,對話框位置,對話框圖標,超時等。 根據所選的消息字體,對話框窗口會自動調整其大小以適應消息。

可選擇顯示的其他控件:復選框,文本輸入,Web鏈接,最多3個額外按鈕。

在您的.NET代碼中,您仍然調用常規的MessageBox.Show。 擴展MessageBox不是自定義對話框。 這仍然是常規MessageBox,添加了擴展功能。

操作系統支持:XP,2000,2003,2008 Vista,Win7 - 32或64位。

下載包括全功能試用版和帶有完整C#源代碼的常規版。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM