c#

c#中messagebox如何设置字体大小

小亿
488
2024-03-19 18:21:36
栏目: 编程语言

在C#中,可以使用以下方法来设置MessageBox的字体大小:

using System;
using System.Windows.Forms;
using System.Drawing;

class CustomMessageBox : Form
{
    public static DialogResult Show(string text, string caption, int fontSize)
    {
        Form form = new Form();
        Label label = new Label();
        Button button = new Button();

        form.Text = caption;
        label.Text = text;
        label.AutoSize = true;
        label.Font = new Font("Arial", fontSize);
        button.Text = "OK";
        button.Click += (sender, e) => form.Close();

        form.Controls.Add(label);
        form.Controls.Add(button);
        form.AutoSize = true;

        DialogResult result = form.ShowDialog();
        form.Dispose();
        
        return result;
    }
}

class Program
{
    static void Main()
    {
        CustomMessageBox.Show("Hello, World!", "Custom MessageBox", 16);
    }
}

在上面的例子中,我们创建了一个自定义的MessageBox类CustomMessageBox,其中包含一个静态方法Show来显示一个自定义的MessageBox。在Show方法中,我们创建了一个新的Form,并向其中添加一个Label和一个Button,然后设置Label的字体大小为指定的大小。最后,调用form.ShowDialog()来显示自定义的MessageBox。

0
看了该问题的人还看了