要自定義MessageBox.Show的樣式,您可以使用Windows Forms或WPF,具體取決于您正在使用的應用程序類型。以下是兩種方法的詳細說明:
方法1:使用Windows Forms
using System;
using System.Windows.Forms;
namespace CustomMessageBoxExample
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("這是一個自定義樣式的MessageBox", "自定義MessageBox", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
例如:
public class CustomMessageBox : Form
{
public CustomMessageBox()
{
this.FormBorderStyle = FormBorderStyle.None;
this.TopMost = true;
this.ShowIcon = false;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.StartPosition = FormStartPosition.CenterScreen;
}
}
private void button1_Click(object sender, EventArgs e)
{
CustomMessageBox customMessageBox = new CustomMessageBox();
customMessageBox.Text = "這是一個自定義樣式的MessageBox";
customMessageBox.Label.Text = "自定義MessageBox";
customMessageBox.ShowDialog();
}
方法2:使用WPF
例如:
public class CustomMessageBox : Window
{
public CustomMessageBox()
{
this.WindowStyle = WindowStyle.None;
this.Topmost = true;
this.ShowIcon = false;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.StartPosition = WindowStartupLocation.CenterScreen;
}
}
private void button1_Click(object sender, RoutedEventArgs e)
{
CustomMessageBox customMessageBox = new CustomMessageBox();
customMessageBox.Title = "自定義MessageBox";
customMessageBox.Content = "這是一個自定義樣式的MessageBox";
customMessageBox.ShowDialog();
}