C#自定義無邊框MessageBox窗體


C#自定義無邊框MessageBox窗體

本例子中制作一個無邊框的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         }

效果展示:

 

工程源程序下載 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM