Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bold text in MessageBox

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

like image 786
Karthick Avatar asked Feb 13 '10 20:02

Karthick


People also ask

How do I print in bold text in Python?

\033[1m is the escape code for bold in the terminal. \033[0m is the escape code for end the edited text and back default text format. If you do not use \033[0m then all upcoming text of the terminal will become bold.

What is the 2nd parameter in Messagebox show ()?

The first parameter msg is the string displayed in the dialog box as the message. The second and third parameters are optional and respectively designate the type of buttons and the title displayed in the dialog box. MsgBox Function returns a value indicating which button the user has chosen.


2 Answers

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.

like image 131
Hans Passant Avatar answered Sep 23 '22 06:09

Hans Passant


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.
like image 21
Amirshk Avatar answered Sep 21 '22 06:09

Amirshk