自己在写程序时,需要一个英文版的弹窗,即确认按钮为"OK"。在网上搜集了一些资料,整理出了适合自己需求的方法,其他有类似需求的可以参考:
public partial class CustomMessageBoxForm : Form
{
private Label messageLabel;
private Button okButton;
public CustomMessageBoxForm(string message)
{
InitializeComponent();
InitializeControls(message);
}
private void InitializeControls(string message)
{
// 设置窗体属性
this.FormBorderStyle = FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.ClientSize = new Size(600, 150);
this.StartPosition = FormStartPosition.CenterScreen;
this.Text = "Message";
// 设置消息文本label
messageLabel = new Label();
messageLabel.Text = message;
messageLabel.Font = new Font("Segoe UI", 10, FontStyle.Regular);
//messageLabel.AutoSize = false;
messageLabel.Size = new Size(this.ClientSize.Width - 20, this.ClientSize.Height - 50);
//messageLabel.TextAlign = ContentAlignment.MiddleCenter; // 设置文本居中对齐
messageLabel.TextAlign = ContentAlignment.MiddleLeft; // 设置文本左边对齐
//messageLabel.Dock = DockStyle.Fill; // 填充整个窗体
messageLabel.Location = new Point(20, 10);
this.Controls.Add(messageLabel); //添加控件到窗体
// 设置确认按钮
okButton = new Button();
okButton.Text = "OK";
okButton.Font = new Font("Segoe UI", 10, FontStyle.Regular);
okButton.Size = new Size(80, 25);
okButton.Location = new Point(this.ClientSize.Width - okButton.Width - 30, this.ClientSize.Height - okButton.Height - 10);
//okButton.Location = new Point(this.Width / 2 - okButton.Width / 2, 70);
okButton.Click += OkButton_Click;
this.Controls.Add(okButton); //添加控件到窗体
}
private void OkButton_Click(object sender, EventArgs e)
{
this.Close(); //关闭窗口
//Application.Exit(); //退出整个进程
}
public static void Show(string message)
{
using (CustomMessageBoxForm customMessageBox = new CustomMessageBoxForm(message))
{
customMessageBox.ShowDialog();
}
}
}
主程序调用方法:CustomMessageBoxForm.Show();
显示效果(文本内容已打码):