C#自定义无边框MessageBox窗体

时间:2022-04-16 21:26:11

C#自定义无边框MessageBox窗体

本例子中制作一个无边框的MessageBox窗体

展示效果:

C#自定义无边框MessageBox窗体

窗体设计

1.添加一个窗体继承原生Form

public partial class MessageBoxEX : Form

2.属性添加

 1         private string _titleText = "提示";
 2 
 3         public string TitleText
 4         {
 5             get { return _titleText; }
 6             set { _titleText = value; }
 7         }
 8 
 9 
10         private string _contentText = "暂无信息!";
11 
12         public string ContentText
13         {
14             get { return _contentText; }
15             set { _contentText = value; }
16         }

3.事件添加

 1  /// <summary>
 2         /// 窗体load的时候讲文本赋值给消息框
 3         /// </summary>
 4         /// <param name="sender"></param>
 5         /// <param name="e"></param>
 6         private void MessageBoxEX_Load(object sender, EventArgs e)
 7         {
 8             if (this._contentText.Trim() != "")
 9             {
10                 this.lblTitalContent.Text = this._titleText;
11                 this.lblMessage.Text = this._contentText;
12             }
13         }
14         /// <summary>
15         /// 鼠标按下标题栏移动窗体
16         /// </summary>
17         /// <param name="sender"></param>
18         /// <param name="e"></param>
19         private void lblTitleBar_MouseDown(object sender, MouseEventArgs e)
20         {
21             //为当前的应用程序释放鼠标捕获
22             ReleaseCapture();
23             //发送消息﹐让系統误以为在标题栏上按下鼠标
24             SendMessage((int)this.Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
25         }
26 
27         /// <summary>
28         /// 关闭按钮
29         /// </summary>
30         /// <param name="sender"></param>
31         /// <param name="e"></param>
32         private void btnClose_Click(object sender, EventArgs e)
33         {
34             this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
35         }
36 
37         /// <summary>
38         /// 确定按钮事件
39         /// </summary>
40         /// <param name="sender"></param>
41         /// <param name="e"></param>
42         private void btnOK_Click(object sender, EventArgs e)
43         {
44             this.DialogResult = System.Windows.Forms.DialogResult.OK;
45         }
46 
47         /// <summary>
48         /// 取消按钮
49         /// </summary>
50         /// <param name="sender"></param>
51         /// <param name="e"></param>
52         private void btnCancel_Click(object sender, EventArgs e)
53         {
54             this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
55         }

4.对外公共方法show设计 这里只填写俩个(有需要可以自行修改)

 1 public static DialogResult Show(string text)
 2         {
 3             MessageBoxEX msgbox = new MessageBoxEX(text);
 4             return msgbox.ShowDialog();
 5         }
 6 
 7         public static DialogResult Show(string title,string text)
 8         {
 9             MessageBoxEX msgbox = new MessageBoxEX(title,text);
10             return msgbox.ShowDialog();
11         }

5.添加单击窗体标题栏移动窗体

1        [DllImport("user32.dll", EntryPoint = "SendMessageA")]
2         private static extern int SendMessage(int hwnd, int wMsg, int wParam, int lParam);
3 
4         [DllImport("user32.dll")]
5         private static extern int ReleaseCapture();
6 
7         private const int WM_NCLBUTTONDOWN = 0XA1;   //.定义鼠標左鍵按下
8         private const int HTCAPTION = 2;
1         private void lblTitleBar_MouseDown(object sender, MouseEventArgs e)
2         {
3             //为当前的应用程序释放鼠标捕获
4             ReleaseCapture();
5             //发送消息﹐让系統误以为在标题栏上按下鼠标
6             SendMessage((int)this.Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
7         }

效果展示:

 C#自定义无边框MessageBox窗体

工程源程序下载