Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MessageBox中的粗体文本_C#_Winforms_Messagebox - Fatal编程技术网

C# MessageBox中的粗体文本

C# MessageBox中的粗体文本,c#,winforms,messagebox,C#,Winforms,Messagebox,如何在消息框显示的对话框中以粗体显示文本。使用C#显示?不行。你必须建造自己的盒子。我假设这是WinForms,如果是ASP.NET,我没有资格回答。你不能回答。这是API的包装器 创建您自己的自定义messagebox来执行此操作 您可以遵循本教程,作为如何实现一个示例 创建此类表单的基本步骤: 创建一个新表单 添加一个标签和两个按钮 将标签字体设置为粗体 向两个按钮添加处理程序,关闭表单并设置按下按钮的某些属性 消息框可能是一个常规窗口,可以像其他窗口一样被弄乱。然而,这样做的代码有点粗糙

如何在
消息框显示的对话框中以粗体显示文本。使用C#显示

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

你不能回答。这是API的包装器

创建您自己的自定义messagebox来执行此操作


您可以遵循本教程,作为如何实现一个示例

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

  • 创建一个新表单
  • 添加一个标签和两个按钮
  • 将标签字体设置为粗体
  • 向两个按钮添加处理程序,关闭表单并设置按下按钮的某些属性

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

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

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

    扩展MessageBox.NET程序集

    动态调整各种MessageBox视觉设置

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

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

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

    支持的操作系统:XP、2000、2003、2008 Vista、Win32或64位


    下载内容包括功能齐全的试用版和带有完整C#源代码的常规版。

    谢谢,你能建议一些步骤吗?是的,甚至添加了一些基本步骤作为指导。MessageBox是包装MessageBoxEx的表单。如果仔细观察,它是一个基于表单的模态或非模态对话框。你可以自己创建自己的类来模仿同样的行为。我知道这已经有一段时间了,但是对于当前的问题和后代来说,请选择一个答案——也是为了纪念那些花时间回应的人。
    private void button1_Click(object sender, EventArgs e) {
      using (new BoldMessageBox(this)) {
        MessageBox.Show("Nobugz waz here");
      }
    }