效果
功能
- 可以再父窗口中添加用户点击确定和取消的按钮事件
- 模态对话框的作用
- 调用函数就可以显示该模态对话框
- 显示内容随时变化
用法
实现代码
命名空间
using System;
using System.Windows.Forms;
using System.Drawing;
代码
public class MessageTip : Panel
{
private void button_init(Button button)
{
button.BackColor = Color.White;
button.FlatStyle = FlatStyle.Flat;
button.Font = new Font("微软雅黑", 10);
button.FlatAppearance.BorderColor = Color.Black;
button.FlatAppearance.MouseOverBackColor = Color.Black;
button.FlatAppearance.MouseDownBackColor = Color.Black;
button.MouseEnter += button_WinSignIn_SignIn_MouseEnter;
button.MouseLeave += button_Lev;
}
private void button_WinSignIn_SignIn_MouseEnter(object sender, EventArgs e)
{
Button button = (Button)sender;
button.ForeColor = Color.White;
}
private void button_Lev(object sender, EventArgs e)
{
Button button = (Button)sender;
button.ForeColor = Color.Black;
}
public event EventHandler sure;
private void sure_Click(object sender, EventArgs e)
{
this.Visible = false;
if (sure != null)
{
sure(sender, e);
}
}
public event EventHandler nosure;
private void nosure_Click(object sender, EventArgs e)
{
this.Visible = false;
if (nosure != null)
{
nosure(sender, e);
}
}
public void ShowMessage(string tips)
{
labels[0].Text = tips;
labels[0].Location = new Point(this.Width / 2 - labels[0].Width / 2, this.Height / 2 - labels[0].Height / 2 - 50);
buttons[0].Location = new Point(this.Width/2-buttons[0].Width-40, labels[0].Location.Y + labels[0].Height + 50);
buttons[1].Location = new Point(buttons[0].Location.X + buttons[0].Width + 80, buttons[0].Location.Y);
this.Visible = true;
this.BringToFront();
}
Label[] labels = new Label[1];
Button[] buttons = new Button[2];
public MessageTip(int messageW,int messageH,int messageX,int messageY)
{
this.Width = messageW;
this.Height = messageH;
this.BackColor = Color.White;
this.Visible = false;
this.Location = new Point(messageX, messageY);
labels[0] = new Label();
labels[0].AutoSize = true;
labels[0].Text = "NULL";
labels[0].Font = new Font("微软雅黑", 14);
labels[0].Location = new Point(this.Width / 2 - labels[0].Width / 2, this.Height/2 - labels[0].Height/2);
this.Controls.Add(labels[0]);
buttons[0] = new Button();
buttons[1] = new Button();
buttons[0].Text = "确定";
buttons[1].Text = "取消";
button_init(buttons[1]);
button_init(buttons[0]);
buttons[1].Font = new Font("微软雅黑", 10);
buttons[0].Font = new Font("微软雅黑", 10);
buttons[1].Width = 100;
buttons[0].Width = 100;
buttons[1].Height = 30;
buttons[0].Height = 30;
buttons[0].Location = new Point(labels[0].Location.X - 50, labels[0].Location.Y + labels[0].Height + 30);
buttons[1].Location = new Point(buttons[0].Location.X + buttons[0].Width + 80, buttons[0].Location.Y);
buttons[0].Click += sure_Click;
buttons[1].Click += nosure_Click;
this.Controls.Add(buttons[0]);
this.Controls.Add(buttons[1]);
}
}