一個(gè)可攜帶附加消息的增強(qiáng)消息框MessageBoxEx
分享一個(gè)可攜帶附加消息的增強(qiáng)消息框MessageBoxEx
--------------201507160917更新---------------
無(wú)意中發(fā)現(xiàn)標(biāo)準(zhǔn)消息框在Windows7是有聲音的,只是在Windows server 2008(R2)無(wú)聲,而我用的剛好是后者,所以誤以為是MessageBeep API在所有NT6系統(tǒng)都不工作造成~汗,有人在stackoverflow也提過(guò)這問(wèn)題。但我仍然決定使用PlaySound API,不做修改
將聲音處理交給ProcessIcon方法負(fù)責(zé)??紤]松耦合,所以將MessageBoxIcon和聲音分開處理,但其實(shí)聲音就是根據(jù)前者而來(lái),兩者天然就是耦合的,分開處理多此一舉
--------------201507091034更新---------------
首先感謝猿友E204在回復(fù)中的反饋。
解決雙擊【詳細(xì)信息】按鈕造成的Checked狀態(tài)改變問(wèn)題,辦法是讓ToggleButton忽略WM_LBUTTONDBLCLK消息
修正收起詳細(xì)信息區(qū)邏輯,改為直接取用plAttachZone.Height。之前是取ExpandHeight,會(huì)造成視覺(jué)體驗(yàn)問(wèn)題
--------------201507082014原文(已更新)---------------
適用于:.net 2.0+的Winform項(xiàng)目
樣子:
有損錄制+制圖的原因不可能原樣展示出真實(shí)效果,可至文章結(jié)尾下載Demo體驗(yàn)。
功能和特點(diǎn):
- 相對(duì)父窗體居中
- 可附帶附加消息。附加消息可以是string和Exception類型,【詳細(xì)信息】按鈕會(huì)根據(jù)是否傳入附加信息顯示和隱藏。傳入Exception實(shí)例時(shí),呈現(xiàn)的是exception.ToString(),也就是可能攜帶StackTrace信息,所以如果你只是想呈現(xiàn)異常文本,還是老實(shí)傳入ex.Message
- 展開/收起附加信息時(shí)有動(dòng)畫效果。實(shí)用為王的你亦可設(shè)置EnableAnimate=false關(guān)閉動(dòng)畫效果
- 在Windows Server 2008 R2(未測(cè)試其它服務(wù)器系統(tǒng))也有聲音反饋。標(biāo)準(zhǔn)消息框在個(gè)人系統(tǒng)(XP/Win7等)是有聲音的,但在srv08卻沒(méi)有。同時(shí)亦提供了EnableSound屬性允許你關(guān)閉聲音反饋
- 移除了標(biāo)準(zhǔn)MessageBox提供的IWin32Window、MessageBoxOptions和Help相關(guān)參數(shù),原因是我用不到,懶得實(shí)現(xiàn)
- 可拖拉改變消息框尺寸,消息文本和附加文本會(huì)隨窗體大小重排。這是標(biāo)準(zhǔn)消息框未提供的能力。改變尺寸分兩種情況有不同的行為:①詳細(xì)信息未展開時(shí),改變的是主消息區(qū)大小;②詳細(xì)信息展開時(shí),改變的是詳細(xì)信息區(qū)的大小
總體來(lái)說(shuō),此消息框比較適合用在需要反饋大量消息文本的場(chǎng)合,用標(biāo)準(zhǔn)消息框的話,文本太多可能會(huì)使消息框超出屏幕大小,比如codeproject.com上這位老兄舉的例子,由于標(biāo)準(zhǔn)消息框不具備改變窗體大小的能力,將導(dǎo)致部分消息無(wú)法讓用戶看到。而就算沒(méi)有超出屏幕,一下子讓用戶面對(duì)那么多消息文字,體驗(yàn)也不地道。使用本消息框就可以解決此類問(wèn)題,比如可以將扼要信息顯示在主消息區(qū),將大量的明細(xì)消息(例如批量處理中的單項(xiàng)處理情況)、次要消息、異常信息等放置在詳細(xì)信息區(qū),由用戶或IT支持人員自己去展開獲取這些信息。同時(shí),在沒(méi)有附加消息的時(shí)候,你仍然可以像標(biāo)準(zhǔn)消息框一樣使用它,所以,如果你跟我一樣不會(huì)用到標(biāo)準(zhǔn)消息框的IWin32Window、MessageBoxOptions和Help相關(guān)參數(shù)的話,基本上可以在整個(gè)項(xiàng)目中全程用此消息框替換掉標(biāo)準(zhǔn)消息框,別忘了相比標(biāo)準(zhǔn)消息框,它還具備了可縮放、相對(duì)父窗體居中等額外能力??傃灾?,你值得擁有。至于如果你擔(dān)心性能問(wèn)題,這個(gè)~我想這么說(shuō),我對(duì)自己的代碼質(zhì)量還是有點(diǎn)信心的。也希望能得大俠指出槽點(diǎn),感激!
使用說(shuō)明:
先看公開成員:
//靜態(tài)屬性 MessageBoxEx.EnableAnimate MessageBoxEx.EnableSound //靜態(tài)方法 MessageBoxEx.Show(string, string, string) MessageBoxEx.Show(string, string, string, MessageBoxButtons) MessageBoxEx.Show(string, string, string, MessageBoxButtons, MessageBoxIcon) MessageBoxEx.Show(string, string, string, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton) MessageBoxEx.Show(string, string, Exception) MessageBoxEx.Show(string, string, Exception, MessageBoxButtons) MessageBoxEx.Show(string, string, Exception, MessageBoxButtons, MessageBoxIcon) MessageBoxEx.Show(string, string, Exception, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton)
屬性EnableAnimate和EnableSound上面提過(guò),分別是用來(lái)啟用/關(guān)閉動(dòng)畫、聲音效果的,默認(rèn)是都啟用。倆屬性影響范圍是全局的,比如設(shè)置EnableAnimate = false后,之后彈出的MessageBoxEx都沒(méi)有動(dòng)畫效果,直到重新設(shè)為true,EnableSound亦然。最佳實(shí)踐是將它倆與用戶偏好設(shè)置相關(guān)聯(lián),允許用戶自主控制
方法則只有一個(gè):Show(),從重載列表你大概都能知道如何使用。其中第3個(gè)參數(shù)就是附加消息,可接受string和Exception類的實(shí)例,其余參數(shù)的位置和意義與標(biāo)準(zhǔn)消息框一致。簡(jiǎn)要示例如下:
MessageBoxEx.Show("主消息", "標(biāo)題", "附加消息", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1); MessageBoxEx.Show("主消息", "標(biāo)題", ex, MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);
前3個(gè)參數(shù)可以放心為null,內(nèi)部有處理,后面的枚舉你也null不了,如果傳入無(wú)效枚舉值,會(huì)拋異常
只有3個(gè)string參數(shù)的那個(gè)方法,后面?zhèn)z參數(shù)是可選的。所以不講究消息體驗(yàn)的你仍然可以這樣使用:
MessageBoxEx.Show("阿斯頓發(fā)"); MessageBoxEx.Show("阿斯頓發(fā)", "士大夫");
方案源碼:
代碼不少,原因自然是有的,有興趣的童鞋請(qǐng)看后面的實(shí)現(xiàn)說(shuō)明。另外,千萬(wàn)不要認(rèn)為代碼量跟性能有直接關(guān)系,有時(shí)候更多的代碼恰恰是為了提升性能而存在,有時(shí)候則是為了健壯性。
using System; using System.ComponentModel; using System.Drawing; using System.IO; using System.Runtime.InteropServices; using System.Threading; using System.Windows.Forms; namespace AhDung.WinForm { /// <summary> /// 可以攜帶詳細(xì)信息的消息框 /// </summary> public static class MessageBoxEx { //異常消息文本 private const string InvalidButtonExString = "按鈕參數(shù)不是有效的枚舉項(xiàng)!"; private const string InvalidIconExString = "圖標(biāo)參數(shù)不是有效的枚舉項(xiàng)!"; private const string InvalidDfButtonExString = "默認(rèn)按鈕參數(shù)不是有效的枚舉項(xiàng)!"; /// <summary> /// 是否啟用動(dòng)畫效果 /// </summary> public static bool EnableAnimate { get; set; } /// <summary> /// 是否啟用聲音反饋 /// </summary> public static bool EnableSound { get; set; } //靜態(tài)構(gòu)造 static MessageBoxEx() { //默認(rèn)啟用動(dòng)畫+聲音 EnableAnimate = true; EnableSound = true; } #region 公開方法 /// <summary> /// 顯示消息框 /// </summary> /// <param name="message">消息文本</param> /// <param name="caption">消息框標(biāo)題</param> /// <param name="attachMessage">附加消息</param> public static DialogResult Show(string message, string caption = null, string attachMessage = null) { return ShowCore(message, caption, attachMessage, MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1); } /*下面這仨弄成重載而不是可選方法是為了避免不必要的參數(shù)檢查*/ /// <summary> /// 顯示消息框 /// </summary> /// <param name="message">消息文本</param> /// <param name="caption">消息框標(biāo)題</param> /// <param name="attachMessage">附加消息</param> /// <param name="buttons">按鈕組合</param> public static DialogResult Show(string message, string caption, string attachMessage, MessageBoxButtons buttons) { if (!Enum.IsDefined(typeof(MessageBoxButtons), buttons)) { throw new InvalidEnumArgumentException(InvalidButtonExString); } return ShowCore(message, caption, attachMessage, buttons, MessageBoxIcon.None, MessageBoxDefaultButton.Button1); } /// <summary> /// 顯示消息框 /// </summary> /// <param name="message">消息文本</param> /// <param name="caption">消息框標(biāo)題</param> /// <param name="attachMessage">附加消息</param> /// <param name="buttons">按鈕組合</param> /// <param name="icon">圖標(biāo)</param> public static DialogResult Show(string message, string caption, string attachMessage, MessageBoxButtons buttons, MessageBoxIcon icon) { if (!Enum.IsDefined(typeof(MessageBoxButtons), buttons)) { throw new InvalidEnumArgumentException(InvalidButtonExString); } if (!Enum.IsDefined(typeof(MessageBoxIcon), icon)) { throw new InvalidEnumArgumentException(InvalidIconExString); } return ShowCore(message, caption, attachMessage, buttons, icon, MessageBoxDefaultButton.Button1); } /// <summary> /// 顯示消息框 /// </summary> /// <param name="message">消息文本</param> /// <param name="caption">消息框標(biāo)題</param> /// <param name="attachMessage">附加消息</param> /// <param name="buttons">按鈕組合</param> /// <param name="icon">圖標(biāo)</param> /// <param name="defaultButton">默認(rèn)按鈕</param> public static DialogResult Show(string message, string caption, string attachMessage, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton) { if (!Enum.IsDefined(typeof(MessageBoxButtons), buttons)) { throw new InvalidEnumArgumentException(InvalidButtonExString); } if (!Enum.IsDefined(typeof(MessageBoxIcon), icon)) { throw new InvalidEnumArgumentException(InvalidIconExString); } if (!Enum.IsDefined(typeof(MessageBoxDefaultButton), defaultButton)) { throw new InvalidEnumArgumentException(InvalidDfButtonExString); } return ShowCore(message, caption, attachMessage, buttons, icon, defaultButton); } /********傳入異常的重載********/ /// <summary> /// 顯示消息框 /// </summary> /// <param name="message">消息文本</param> /// <param name="caption">消息框標(biāo)題</param> /// <param name="exception">異常實(shí)例</param> public static DialogResult Show(string message, string caption, Exception exception) { return Show(message, caption, exception == null ? string.Empty : exception.ToString()); } /// <summary> /// 顯示消息框 /// </summary> /// <param name="message">消息文本</param> /// <param name="caption">消息框標(biāo)題</param> /// <param name="exception">異常實(shí)例</param> /// <param name="buttons">按鈕組合</param> public static DialogResult Show(string message, string caption, Exception exception, MessageBoxButtons buttons) { return Show(message, caption, exception == null ? string.Empty : exception.ToString(), buttons); } /// <summary> /// 顯示消息框 /// </summary> /// <param name="message">消息文本</param> /// <param name="caption">消息框標(biāo)題</param> /// <param name="exception">異常實(shí)例</param> /// <param name="buttons">按鈕組合</param> /// <param name="icon">圖標(biāo)</param> public static DialogResult Show(string message, string caption, Exception exception, MessageBoxButtons buttons, MessageBoxIcon icon) { return Show(message, caption, exception == null ? string.Empty : exception.ToString(), buttons, icon); } /// <summary> /// 顯示消息框 /// </summary> /// <param name="message">消息文本</param> /// <param name="caption">消息框標(biāo)題</param> /// <param name="exception">異常實(shí)例</param> /// <param name="buttons">按鈕組合</param> /// <param name="icon">圖標(biāo)</param> /// <param name="defaultButton">默認(rèn)按鈕</param> public static DialogResult Show(string message, string caption, Exception exception, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton) { return Show(message, caption, exception == null ? string.Empty : exception.ToString(), buttons, icon, defaultButton); } #endregion //內(nèi)部方法,不檢查參數(shù)有效性 private static DialogResult ShowCore(string message, string caption, string attachMessage, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton) { using (MessageForm f = new MessageForm(message, caption, buttons, icon, defaultButton, attachMessage, EnableAnimate, EnableSound)) { return f.ShowDialog(); } } /*---------------- 下面是消息窗體相關(guān) ---------------*/ /// <summary> /// 消息窗體 /// </summary> /// <remarks>參數(shù)有效性由MessageBoxEx負(fù)責(zé)</remarks> private class MessageForm : Form { /* todo 存在問(wèn)題: * 當(dāng)消息區(qū)文本非常非常多時(shí),且反復(fù)進(jìn)行改變消息框窗口大小、位置、展開收起的操作,那么在某次展開時(shí) 詳細(xì)信息文本框可能會(huì)在原位置(即消息區(qū)內(nèi)某rect)瞬閃一下, 原因是文本框控件在顯示時(shí)總會(huì)在原位置WM_NCPAINT + WM_ERASEBKGND一下,暫無(wú)解決辦法。 實(shí)際應(yīng)用中碰到的幾率很小,就算碰到,影響也可以忽略。 */ #region 控件初始化 /// <summary> /// 必需的設(shè)計(jì)器變量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的資源。 /// </summary> /// <param name="disposing">如果應(yīng)釋放托管資源,為 true;否則為 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗體設(shè)計(jì)器生成的代碼 /// <summary> /// 設(shè)計(jì)器支持所需的方法 - 不要 /// 使用代碼編輯器修改此方法的內(nèi)容。 /// </summary> private void InitializeComponent() { this.button3 = new System.Windows.Forms.Button(); this.txbAttach = new TextBoxUnSelectAllable(); this.button2 = new System.Windows.Forms.Button(); this.button1 = new System.Windows.Forms.Button(); this.plButtonsZone = new AhDung.WinForm.MessageBoxEx.MessageForm.PanelBasic(); this.ckbToggle = new AhDung.WinForm.MessageBoxEx.MessageForm.ToggleButton(this.UseAnimate); this.plAttachZone = new AhDung.WinForm.MessageBoxEx.MessageForm.PanelBasic(); this.lbMsg = new AhDung.WinForm.MessageBoxEx.MessageForm.MessageViewer(); this.plButtonsZone.SuspendLayout(); this.plAttachZone.SuspendLayout(); this.SuspendLayout(); // // button3 // this.button3.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right; this.button3.Location = new System.Drawing.Point(320, 8); this.button3.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.button3.Name = "button3"; this.button3.Size = new System.Drawing.Size(85, 27); this.button3.TabIndex = 2; // // txbAttach // this.txbAttach.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right; this.txbAttach.Location = new System.Drawing.Point(10, 7); this.txbAttach.Margin = new System.Windows.Forms.Padding(3, 1, 3, 1); this.txbAttach.Name = "txbAttach"; this.txbAttach.ReadOnly = true; this.txbAttach.Multiline = true; this.txbAttach.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; this.txbAttach.Size = new System.Drawing.Size(395, 105); this.txbAttach.TabIndex = 0; // // button2 // this.button2.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right; this.button2.Location = new System.Drawing.Point(229, 8); this.button2.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(85, 27); this.button2.TabIndex = 1; // // button1 // this.button1.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right; this.button1.Location = new System.Drawing.Point(138, 8); this.button1.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(85, 27); this.button1.TabIndex = 0; // // plButtonsZone // this.plButtonsZone.Controls.Add(this.ckbToggle); this.plButtonsZone.Controls.Add(this.button1); this.plButtonsZone.Controls.Add(this.button2); this.plButtonsZone.Controls.Add(this.button3); this.plButtonsZone.Dock = System.Windows.Forms.DockStyle.Bottom; this.plButtonsZone.Location = new System.Drawing.Point(0, 96); this.plButtonsZone.Margin = new System.Windows.Forms.Padding(3, 1, 3, 1); this.plButtonsZone.Name = "plButtonsZone"; this.plButtonsZone.Size = new System.Drawing.Size(415, 36); this.plButtonsZone.TabIndex = 1; // // ckbToggle // this.ckbToggle.Location = new System.Drawing.Point(10, 8); this.ckbToggle.Name = "ckbToggle"; this.ckbToggle.Size = new System.Drawing.Size(93, 27); this.ckbToggle.TabIndex = 3; this.ckbToggle.Text = "詳細(xì)信息(&D)"; this.ckbToggle.CheckedChanged += this.ckbToggle_CheckedChanged; // // plAttachZone // this.plAttachZone.Controls.Add(this.txbAttach); this.plAttachZone.Dock = System.Windows.Forms.DockStyle.Fill; this.plAttachZone.Location = new System.Drawing.Point(0, 130); this.plAttachZone.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.plAttachZone.Name = "plAttachZone"; this.plAttachZone.Size = new System.Drawing.Size(415, 114); this.plAttachZone.TabIndex = 2; this.plAttachZone.Visible = false; // // lbMsg // this.lbMsg.Dock = System.Windows.Forms.DockStyle.Fill; this.lbMsg.Icon = null; this.lbMsg.Location = new System.Drawing.Point(0, 0); this.lbMsg.Name = "lbMsg"; this.lbMsg.Padding = new System.Windows.Forms.Padding(21, 18, 21, 18); //this.lbMsg.Size = new System.Drawing.Size(415, 96); this.lbMsg.TabIndex = 0; // // FmMsg // this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None; //this.ClientSize = new System.Drawing.Size(415, 261); this.Controls.Add(this.lbMsg); this.Controls.Add(this.plButtonsZone); this.Controls.Add(this.plAttachZone); this.DoubleBuffered = true; this.MaximizeBox = false; this.Name = "MessageForm"; this.Padding = new System.Windows.Forms.Padding(0, 0, 0, 17); this.ShowIcon = false; this.ShowInTaskbar = false; this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Show; this.plButtonsZone.ResumeLayout(false); this.plAttachZone.ResumeLayout(false); this.plAttachZone.PerformLayout(); this.ResumeLayout(false); } #endregion private ToggleButton ckbToggle; private TextBoxUnSelectAllable txbAttach; private MessageViewer lbMsg; private System.Windows.Forms.Button button2; private System.Windows.Forms.Button button1; private PanelBasic plButtonsZone; private PanelBasic plAttachZone; private System.Windows.Forms.Button button3; #endregion /// <summary> /// 最大默認(rèn)窗體客戶區(qū)寬度 /// </summary> const int MaxClientWidth = 700; string messageSound;//存儲(chǔ)供PlaySound API使用的系統(tǒng)消息音別名,在ProcessIcon中賦值,OnShown中取用 int expandHeight; /// <summary> /// 詳細(xì)信息區(qū)展開高度 /// </summary> private int ExpandHeight { get { return expandHeight < 150 ? 150 : expandHeight; } set { expandHeight = value; } } #region 屬性 /// <summary> /// 是否啟用動(dòng)畫效果 /// </summary> /// <remarks>此處還弄該屬性是為了保證窗體類的獨(dú)立性</remarks> private bool UseAnimate { get; set; } /// <summary> /// 是否啟用聲音反饋 /// </summary> /// <remarks>此處還弄該屬性是為了保證窗體類的獨(dú)立性</remarks> private bool UseSound { get; set; } /// <summary> /// 消息按鈕 /// </summary> private MessageBoxButtons MessageButtons { get; set; } /// <summary> /// 消息圖標(biāo) /// </summary> private MessageBoxIcon MessageIcon { get; set; } /// <summary> /// 默認(rèn)按鈕 /// </summary> private MessageBoxDefaultButton DefaultButton { get; set; } #endregion /// <summary> /// 創(chuàng)建消息窗體 /// </summary> private MessageForm(bool enableAnimate) { this.UseAnimate = enableAnimate;//須盡早設(shè)置,要供展開按鈕初始化用 InitializeComponent(); this.StartPosition = Form.ActiveForm == null ? FormStartPosition.CenterScreen : FormStartPosition.CenterParent; this.Font = SystemFonts.MessageBoxFont; //注冊(cè)事件 this.button1.Click += button_Click; this.button2.Click += button_Click; this.button3.Click += button_Click; this.plAttachZone.Resize += plAttachZone_Resize; } /// <summary> /// 創(chuàng)建消息窗體 /// </summary> public MessageForm(string message, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, string attachMessage, bool enableAnimate, bool enableSound) : this(enableAnimate) { this.lbMsg.Text = message; this.Text = caption; this.txbAttach.Text = attachMessage; this.MessageButtons = buttons; this.MessageIcon = icon; this.DefaultButton = defaultButton; this.UseSound = enableSound; } #region 重寫基類方法 protected override void OnLoad(EventArgs e) { //須在計(jì)算各種尺寸前搞掂 ProcessIcon(); ProcessButtons(); this.MinimumSize = SizeFromClientSize(new Size(GetPanelButtonMinWidth(), GetClientMinHeight())); //參數(shù)意義定為客戶區(qū)最大大小,所以需刨掉非客戶區(qū)高度后傳入 this.ClientSize = this.GetPreferredSize(new Size(MaxClientWidth, Screen.PrimaryScreen.WorkingArea.Height - (this.Height - this.ClientSize.Height))); base.OnLoad(e); } protected override void OnShown(EventArgs e) { //設(shè)置默認(rèn)按鈕焦點(diǎn)。須在OnShown中設(shè)置按鈕焦點(diǎn)才有用 Button dfBtn; if ((dfBtn = this.AcceptButton as Button) != null) { dfBtn.Focus(); } //播放消息提示音 if (this.UseSound) { PlaySystemSound(this.messageSound); } base.OnShown(e); } //重寫窗體參數(shù) protected override CreateParams CreateParams { get { CreateParams prms = base.CreateParams; if ((Convert.ToInt32(this.MessageButtons) & 1) == 0) //沒(méi)有Cancel按鈕時(shí)屏蔽關(guān)閉按鈕,剛好在偶數(shù)項(xiàng) { prms.ClassStyle |= 0x200; } return prms; } } /// <summary> /// 計(jì)算合適的窗口尺寸 /// </summary> /// <param name="proposedSize">該參數(shù)此處定義為客戶區(qū)可設(shè)置的最大尺寸</param> public override Size GetPreferredSize(Size proposedSize) { int reservedHeight = plButtonsZone.Height + Padding.Bottom; Size size = lbMsg.GetPreferredSize(new Size(proposedSize.Width, proposedSize.Height - reservedHeight)); size.Height += reservedHeight; return size; } #endregion #region 事件處理方法 //展開收起 private void ckbToggle_CheckedChanged(object sender, EventArgs e) { this.SuspendLayout(); if (ckbToggle.Checked) { plButtonsZone.SendToBack(); lbMsg.SendToBack(); lbMsg.Dock = DockStyle.Top; plButtonsZone.Dock = DockStyle.Top; ChangeFormHeight(ExpandHeight); plAttachZone.Visible = true; } else { ExpandHeight = plAttachZone.Height;//為再次展開記憶高度 plAttachZone.Visible = false; ChangeFormHeight(-plAttachZone.Height);//收起時(shí)直接取pl高度,不要取ExpandHeight plButtonsZone.SendToBack(); plButtonsZone.Dock = DockStyle.Bottom; lbMsg.Dock = DockStyle.Fill; } this.ResumeLayout(); } //按鈕事件 private void button_Click(object sender, EventArgs e) { this.DialogResult = (DialogResult)((sender as Button).Tag); } //用戶手工收完詳細(xì)區(qū)則觸發(fā)折疊 private void plAttachZone_Resize(object sender, EventArgs e) { if (ckbToggle.Checked && plAttachZone.Height == 0) { ckbToggle.Checked = false; } } #endregion #region 輔助+私有方法 /// <summary> /// 處理按鈕相關(guān) /// </summary> private void ProcessButtons() { this.ckbToggle.Visible = txbAttach.Text.Trim().Length != 0; //無(wú)詳細(xì)信息就不顯示展開按鈕 int btnCount = 3; //按鈕數(shù)量 switch (MessageButtons) //老實(shí)用case,可讀點(diǎn) { case MessageBoxButtons.AbortRetryIgnore: button1.Text = "中止(&A)"; button1.Tag = DialogResult.Abort; button2.Text = "重試(&R)"; button2.Tag = DialogResult.Retry; button3.Text = "忽略(&I)"; button3.Tag = DialogResult.Ignore; break; case MessageBoxButtons.OK: button1.Visible = false; button2.Visible = false; button3.Text = "確定"; button3.Tag = DialogResult.OK; btnCount = 1; break; case MessageBoxButtons.OKCancel: button1.Visible = false; button2.Text = "確定"; button2.Tag = DialogResult.OK; button3.Text = "取消"; button3.Tag = DialogResult.Cancel; btnCount = 2; break; case MessageBoxButtons.RetryCancel: button1.Visible = false; button2.Text = "重試(&R)"; button2.Tag = DialogResult.Retry; button3.Text = "取消"; button3.Tag = DialogResult.Cancel; btnCount = 2; break; case MessageBoxButtons.YesNo: button1.Visible = false; button2.Text = "是(&Y)"; button2.Tag = DialogResult.Yes; button3.Text = "否(&N)"; button3.Tag = DialogResult.No; btnCount = 2; break; case MessageBoxButtons.YesNoCancel: button1.Text = "是(&Y)"; button1.Tag = DialogResult.Yes; button2.Text = "否(&N)"; button2.Tag = DialogResult.No; button3.Text = "取消"; button3.Tag = DialogResult.Cancel; break; default: break; } //僅有OK和有取消按鈕時(shí)設(shè)CancelButton if ((int)MessageButtons == 0 || ((int)MessageButtons & 1) == 1) { this.CancelButton = button3; } //處理默認(rèn)按鈕 if (btnCount == 1) { this.AcceptButton = button3; } else if (btnCount == 2) { this.AcceptButton = DefaultButton == MessageBoxDefaultButton.Button2 ? button3 : button2; } else { Button[] btnArray = { button1, button2, button3 }; this.AcceptButton = btnArray[Convert.ToInt32(DefaultButton) / 0x100]; } } /// <summary> /// 處理圖標(biāo)(含聲音) /// </summary> private void ProcessIcon() { switch (MessageIcon) { //MessageBoxIcon.Information同樣 case MessageBoxIcon.Asterisk: lbMsg.Icon = SystemIcons.Information; messageSound = "SystemAsterisk"; break; //MessageBoxIcon.Hand、MessageBoxIcon.Stop同樣 case MessageBoxIcon.Error: lbMsg.Icon = SystemIcons.Error; messageSound = "SystemHand"; break; //MessageBoxIcon.Warning同樣 case MessageBoxIcon.Exclamation: lbMsg.Icon = SystemIcons.Warning; messageSound = "SystemExclamation"; break; case MessageBoxIcon.Question: lbMsg.Icon = SystemIcons.Question; messageSound = "SystemAsterisk";//Question原本是沒(méi)聲音的,此實(shí)現(xiàn)讓它蹭一下Information的 break; default: //MessageBoxIcon.None lbMsg.Icon = null; messageSound = "SystemDefault"; break; } } /// <summary> /// 計(jì)算窗體客戶區(qū)最小高度 /// </summary> private int GetClientMinHeight() { return lbMsg.MinimumHeight + plButtonsZone.Height + Padding.Bottom; } /// <summary> /// 計(jì)算按鈕區(qū)最小寬度 /// </summary> private int GetPanelButtonMinWidth() { int r = 20 /*左右Padding*/, visibleCount = -1 /*因?yàn)閮蓚€(gè)以上才會(huì)有間距*/; if (ckbToggle.Visible) { r += ckbToggle.Width; visibleCount++; } if (button1.Visible) { r += button1.Width * 3; visibleCount += 3; } else if (button2.Visible) { r += button2.Width * 2; visibleCount += 2; } else { r += button3.Width; visibleCount++; } if (visibleCount != -1) { r += visibleCount * 6; } //按鈕間距 return r; } /// <summary> /// 改變窗體高度。內(nèi)部有動(dòng)畫處理 /// </summary> /// <param name="increment">增量(負(fù)數(shù)即為減小高度)</param> private void ChangeFormHeight(int increment) { int finalHeight = this.Height + increment; //正確的目標(biāo)高度 if (!this.UseAnimate) //不使用動(dòng)畫 { this.Height = finalHeight; return; } const int step = 8; //幀數(shù) for (int i = 0; i < step; i++) { if (i == step - 1) //最后一步直達(dá)目標(biāo) { this.Height = finalHeight; return; } this.Height += increment / step; Application.DoEvents(); //必要 Thread.Sleep(10); } } /// <summary> /// 播放系統(tǒng)事件聲音 /// </summary> /// <remarks>之所以不用MessageBeep API是因?yàn)檫@貨在srv08上不出聲,所以用PlaySound代替</remarks> private static void PlaySystemSound(string soundAlias) { PlaySound(soundAlias, IntPtr.Zero, 0x10000 /*SND_ALIAS*/| 0x1 /*SND_ASYNC*/); } [DllImport("winmm.dll", CharSet = CharSet.Auto)] private static extern bool PlaySound([MarshalAs(UnmanagedType.LPWStr)] string soundName, IntPtr hmod, int soundFlags); #endregion #region 嵌套類 /// <summary> /// 基礎(chǔ)面板 /// </summary> private class PanelBasic : Control { public PanelBasic() { SetStyle(ControlStyles.AllPaintingInWmPaint, false);//關(guān)鍵,不然其上的ToolBar不正常 SetStyle(ControlStyles.OptimizedDoubleBuffer, true);//重要。不設(shè)置的話控件繪制不正常 SetStyle(ControlStyles.ContainerControl, true); SetStyle(ControlStyles.Selectable, false); } protected override void WndProc(ref Message m) { //屏蔽WM_ERASEBKGND。防止顯示時(shí)在原位置快閃 //不能通過(guò)ControlStyles.AllPaintingInWmPaint=true屏蔽 //會(huì)影響其上的ToolBar if (m.Msg == 0x14) { return; } base.WndProc(ref m); } protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified) { //防Dock時(shí)面板短暫滯留在原位置 base.SetBoundsCore(x, y, width, height, specified | BoundsSpecified.Y | BoundsSpecified.Width); } } /// <summary> /// 消息呈現(xiàn)控件 /// </summary> private class MessageViewer : Control { const TextFormatFlags textFlags = TextFormatFlags.EndEllipsis //未完省略號(hào) | TextFormatFlags.WordBreak //允許換行 | TextFormatFlags.NoPadding //無(wú)邊距 | TextFormatFlags.ExternalLeading //行間空白。NT5必須,不然文字?jǐn)D在一起 | TextFormatFlags.TextBoxControl; //避免半行 const int IconSpace = 5; //圖標(biāo)與文本間距 const float PreferredScale = 13;//最佳文本區(qū)塊比例(寬/高) /// <summary> /// 最小高度。不要重寫MinimumSize,那會(huì)在窗體移動(dòng)和縮放時(shí)都會(huì)執(zhí)行 /// </summary> public int MinimumHeight { get { return (this.Icon != null ? Math.Max(this.Icon.Height, this.FontHeight) : this.FontHeight) + Padding.Vertical; } } /// <summary> /// 獲取或設(shè)置圖標(biāo) /// </summary> public Icon Icon { get; set; } public MessageViewer() { this.SetStyle(ControlStyles.CacheText, true); this.SetStyle(ControlStyles.UserPaint, true); this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); this.SetStyle(ControlStyles.Selectable, false); this.SetStyle(ControlStyles.ResizeRedraw, true); //重要 this.DoubleBuffered = true; //雙緩沖 BackColor = Environment.OSVersion.Version.Major == 5 ? SystemColors.Control : Color.White; } //防Dock改變尺寸 protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified) { base.SetBoundsCore(x, y, width, height, specified | BoundsSpecified.Size); } /// <summary> /// 計(jì)算合適的消息區(qū)尺寸 /// </summary> /// <param name="proposedSize">該參數(shù)此處定義為此控件可設(shè)置的最大尺寸</param> /// <remarks>該方法對(duì)太長(zhǎng)的單行文本有做比例優(yōu)化處理,避免用戶擺頭幅度過(guò)大扭到脖子</remarks> public override Size GetPreferredSize(Size proposedSize) { if (proposedSize.Width < 10) { proposedSize.Width = int.MaxValue; } if (proposedSize.Height < 10) { proposedSize.Height = int.MaxValue; } int reservedWidth = Padding.Horizontal + (this.Icon == null ? 0 : (this.Icon.Width + IconSpace)); Size wellSize = Size.Empty; if (!string.IsNullOrEmpty(this.Text)) { //優(yōu)化文本塊寬高比例 Size size = TextRenderer.MeasureText(this.Text, this.Font, new Size(proposedSize.Width - reservedWidth, 0), textFlags);//用指定寬度測(cè)量文本面積 wellSize = Convert.ToSingle(size.Width) / size.Height > PreferredScale //過(guò)于寬扁的情況 ? Size.Ceiling(GetSameSizeWithNewScale(size, PreferredScale)) : size; //湊齊整行高,確保尾部顯示 int lineHeight = TextRenderer.MeasureText(" ", this.Font, new Size(int.MaxValue, 0), textFlags).Height;//單行高,F(xiàn)ont.Height不靠譜 int differ; wellSize.Height += (differ = wellSize.Height % lineHeight) == 0 ? 0 : (lineHeight - differ); } if (this.Icon != null) { wellSize.Width += this.Icon.Width + IconSpace; wellSize.Height = Math.Max(this.Icon.Height, wellSize.Height); } wellSize += Padding.Size; //不應(yīng)超過(guò)指定尺寸。寬度在上面已確保不會(huì)超過(guò) if (wellSize.Height > proposedSize.Height) { wellSize.Height = proposedSize.Height; } return wellSize; } /// <summary> /// 重繪 /// </summary> protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; Rectangle rect = GetPaddedRectangle(); //繪制圖標(biāo) if (this.Icon != null) { g.DrawIcon(this.Icon, Padding.Left, Padding.Top); //右移文本區(qū) rect.X += this.Icon.Width + IconSpace; rect.Width -= this.Icon.Width + IconSpace; //若文字太少,則與圖標(biāo)垂直居中 if (this.Text.Length < 100) { Size textSize = TextRenderer.MeasureText(g, this.Text, this.Font, rect.Size, textFlags); if (textSize.Height <= this.Icon.Height) { rect.Y += (this.Icon.Height - textSize.Height) / 2; } } } //g.FillRectangle(Brushes.Gainsboro, rect);//test //繪制文本 TextRenderer.DrawText(g, this.Text, this.Font, rect, Color.Black, textFlags); base.OnPaint(e); } /// <summary> /// 根據(jù)原尺寸,得到相同面積、且指定比例的新尺寸 /// </summary> /// <param name="src">原尺寸</param> /// <param name="scale">新尺寸比例。需是width/height</param> private static SizeF GetSameSizeWithNewScale(Size src, float scale) { int sqr = src.Width * src.Height;//原面積 double w = Math.Sqrt(sqr * scale);//新面積寬 return new SizeF(Convert.ToSingle(w), Convert.ToSingle(sqr / w)); } /// <summary> /// 獲取刨去Padding的內(nèi)容區(qū) /// </summary> private Rectangle GetPaddedRectangle() { Rectangle r = this.ClientRectangle; r.X += this.Padding.Left; r.Y += this.Padding.Top; r.Width -= this.Padding.Horizontal; r.Height -= this.Padding.Vertical; return r; } } /// <summary> /// 屏蔽全選消息的文本框 /// </summary> private class TextBoxUnSelectAllable : TextBox { protected override void WndProc(ref Message m) { //EM_SETSEL if (m.Msg == 0xB1) { return; } base.WndProc(ref m); } } /// <summary> /// 包裝ToolBarButton為單一控件 /// </summary> private class ToggleButton : Control { /// <summary> /// 展開/收起圖標(biāo)數(shù)據(jù) /// </summary> const string ImgDataBase64 = @"iVBORw0KGgoAAAANSUhEUgAAACAAAAAQCAYAAAB3AH1ZAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJ bWFnZVJlYWR5ccllPAAAA3NJREFUeNqklVlPFEEQx/8zPccue6gorMd6gBegeCAQD4w+oCx+AInx IB4EfTK8+g2MQUUTcBU8En0wmvigEkyMxgcTjRrUqHFVUBRQQaJGl2WPmbG6dzCLWUiESf7T0739 666urqqVDjVcxT9PAWkfqZKUY491ktpIzaRXGPv5L15J+dZIRx26dqAwf56c48+Cx+1CzDDR//13 /seevvx3HZ8OxmLxMzSvjhT5Z+Nx8UoKfHOu31e+qWwZPBkOMBkwTAvRuAE21QuvJwNz5s6U25++ rv365dtC+4SxifJsfeVWvsCJ2TOzqyo2FsHt1OBSFeiqTItIsOhHw7JgGBZM+s72TcOvX+GccHgw k7qttgHj5slOLNE0tXZNSQGYJEEhiDEJusLoW4ZMfZnGJVv0QmHhYuiaup+zE+W5Aftyc/xMURRh acJIKpowqDVhkhu5LCspiY6k0OIL5s9mdrCNyp9sDKL+6PExeW5AwOebigRNiiVMkoFIPIFwlLcG huIm4mRI3DRpAQg38oPMmD6Nuz4wGn+koRGH64/hxr1HuHjl2qg8D8JcZ4ZTRCtLSDjT1Ijz51rS 5lfVzj2o2rWXXCzDPcnNh3L5K5WntdHYdAqng6cwa/EK+AuK8SDUSx65gUAlxR1ZkcqLLDBpkJ+S R8yOvbXw+vx4GOoZsXlZyQqsK10pNlDpjlVZDPMs0FL55mATLl04C39+EWblFf3l2zs+w7jZii1b Kkfw3IDOcDiS5/G4yLjknQcCAbrPW3j8plvMWlu8XGwOsblMASYjFh3i3S4SS+W3Vddg++6apJ8t OwN4HHH/p+G5AW3f+gbyvB632DwGHigSyjdvpn4b9ElZWF9aJE6uMAanJsOlK3jdNcAXuE2y0vEQ rcXfyeCT0vPcES0funoNRTJpgixSRUQsLbapogIbVq8S47rKCORShQvbX7437NI6Km8Ol9sxeG7A i2g0Fnz2PAQ3TcjQGBw02UGWOqig8L7bweB1qCSFxHD3/nMMDkWDnJ0oP1yK6z529y1i8ovydaVL wXOaXxl3W7K4yKKykY/Rdq8dofe9d+x6jonyw6WYu+Pyj5/hzLedPcU61dDJLh1T3E4BRgYjCHV0 4/qdJ+bn/h+naW41KZpiwLh5Kc3fMS+vNXaRybVT7YMdcM2228d6/ov/I8AAPfkI7yO+mM8AAAAA SUVORK5CYII="; readonly bool isToggleMode; bool isChecked; bool useAnimate; readonly ImageList imgList; /// <summary> /// Checked改變后 /// </summary> public event EventHandler CheckedChanged; /// <summary> /// 使用動(dòng)畫按鈕效果 /// </summary> private bool UseAnimate { get { return useAnimate; } set { if (useAnimate == value) { return; } useAnimate = value; if (IsHandleCreated) { this.CreateHandle(); } } } /// <summary> /// 獲取或設(shè)置按鈕是否處于按下狀態(tài) /// </summary> [Description("獲取或設(shè)置按鈕是否處于按下狀態(tài)"), DefaultValue(false)] public bool Checked { get { if (IsHandleCreated) { //保證isChecked與實(shí)情吻合。TB_ISBUTTONCHECKED isChecked = Convert.ToBoolean(SendMessage(this.Handle, 0x40A, IntPtr.Zero, IntPtr.Zero).ToInt32()); } return isChecked; } set { if (isChecked == value || !isToggleMode) { return; } isChecked = value; if (IsHandleCreated) { //TB_CHECKBUTTON SendMessage(this.Handle, 0x402, IntPtr.Zero, new IntPtr(Convert.ToInt32(value))); } OnCheckedChanged(EventArgs.Empty); } } /// <summary> /// 創(chuàng)建ToolBarButtonControl /// </summary> public ToggleButton(bool useAnimate) { SetStyle(ControlStyles.UserPaint, false); SetStyle(ControlStyles.AllPaintingInWmPaint, true); SetStyle(ControlStyles.OptimizedDoubleBuffer, true); SetStyle(ControlStyles.ResizeRedraw, true); this.isToggleMode = true;//寫死好了,獨(dú)立版才提供設(shè)置 this.UseAnimate = useAnimate; //將圖標(biāo)加入imageList imgList = new ImageList { ImageSize = new System.Drawing.Size(16, 16), ColorDepth = ColorDepth.Depth32Bit }; using (MemoryStream ms = new MemoryStream(Convert.FromBase64String(ImgDataBase64))) { imgList.Images.AddStrip(Image.FromStream(ms)); } } /// <summary> /// 執(zhí)行左鍵單擊 /// </summary> public void PerformClick() { SendMessage(this.Handle, 0x201, new IntPtr(0x1), IntPtr.Zero);//WM_LBUTTONDOWN Application.DoEvents(); SendMessage(this.Handle, 0x202, IntPtr.Zero, IntPtr.Zero); //WM_LBUTTONUP } protected override void WndProc(ref Message m) { //忽略鼠標(biāo)雙擊消息,WM_LBUTTONDBLCLK if (m.Msg == 0x203) { return; } //有節(jié)操的響應(yīng)鼠標(biāo)動(dòng)作 if ((m.Msg == 0x201 || m.Msg == 0x202) && (!this.Enabled || !this.Visible)) { return; } base.WndProc(ref m); } //創(chuàng)建ToolBar protected override CreateParams CreateParams { get { CreateParams prms = base.CreateParams; prms.ClassName = "ToolbarWindow32"; prms.Style = 0x40000000 | 0x10000000 //| 0x2000000 //WS_CLIPCHILDREN //| 0x8000 | 0x1 | 0x4 | 0x8 | 0x40 | 0x1000 //TBSTYLE_LIST,圖標(biāo)文本橫排 ; if (UseAnimate) { prms.Style |= 0x800; }//TBSTYLE_FLAT。flat模式在NT6.x下,按鈕按下會(huì)有動(dòng)畫效果 prms.ExStyle = 0; return prms; } } protected override void OnHandleCreated(EventArgs e) { base.OnHandleCreated(e); //設(shè)置imgList SendMessage(this.Handle, 0x430, IntPtr.Zero, imgList.Handle);//TB_SETIMAGELIST //準(zhǔn)備添加按鈕 int btnStructSize = Marshal.SizeOf(typeof(TBBUTTON)); SendMessage(this.Handle, 0x41E, new IntPtr(btnStructSize), IntPtr.Zero);//TB_BUTTONSTRUCTSIZE,必須在添加按鈕前 //構(gòu)建按鈕信息 TBBUTTON btnStruct = new TBBUTTON { //iBitmap = 0, //idCommand = 0, fsState = 0x4, //TBSTATE_ENABLED iString = SendMessage(this.Handle, 0x44D, 0, this.Text + '\0')//TB_ADDSTRING }; if (this.isToggleMode) { btnStruct.fsStyle = 0x2; }//BTNS_CHECK。作為切換按鈕時(shí) IntPtr btnStructStart = IntPtr.Zero; try { btnStructStart = Marshal.AllocHGlobal(btnStructSize);//在非托管區(qū)創(chuàng)建一個(gè)指針 Marshal.StructureToPtr(btnStruct, btnStructStart, true);//把結(jié)構(gòu)體塞到上述指針 //添加按鈕 SendMessage(this.Handle, 0x444, new IntPtr(1)/*按鈕數(shù)量*/, btnStructStart);//TB_ADDBUTTONS。從指針取按鈕信息 //設(shè)置按鈕尺寸剛好為ToolBar尺寸 AdjustButtonSize(); } finally { if (btnStructStart != IntPtr.Zero) { Marshal.FreeHGlobal(btnStructStart); } } } protected override bool ProcessCmdKey(ref Message m, Keys keyData) { //將空格和回車作為鼠標(biāo)單擊處理 if (m.Msg == 0x100 && (keyData == Keys.Enter || keyData == Keys.Space)) { PerformClick(); return true; } return base.ProcessCmdKey(ref m, keyData); } /// <summary> /// 處理助記鍵 /// </summary> protected override bool ProcessMnemonic(char charCode) { if (IsMnemonic(charCode, this.Text)) { PerformClick(); return true; } return base.ProcessMnemonic(charCode); } protected override void OnClick(EventArgs e) { //忽略鼠標(biāo)右鍵 MouseEventArgs me = e as MouseEventArgs; if (me != null && me.Button != System.Windows.Forms.MouseButtons.Left) { return; } //若是切換模式,直接引發(fā)Checked事件(不要通過(guò)設(shè)置Checked屬性引發(fā),因?yàn)镺nClick發(fā)送之前就已經(jīng)Check了) //存在理論上的不可靠,但暫無(wú)更好辦法 if (isToggleMode) { this.OnCheckedChanged(EventArgs.Empty); } base.OnClick(e); } //重繪后重設(shè)按鈕尺寸 protected override void OnInvalidated(InvalidateEventArgs e) { base.OnInvalidated(e); AdjustButtonSize(); } /// <summary> /// 引發(fā)CheckedChanged事件 /// </summary> protected virtual void OnCheckedChanged(EventArgs e) { SetImageIndex(this.Checked ? 1 : 0); if (CheckedChanged != null) { CheckedChanged(this, e); } } /// <summary> /// 設(shè)置圖標(biāo)索引 /// </summary> private void SetImageIndex(int index) { //TB_CHANGEBITMAP SendMessage(this.Handle, 0x42B, IntPtr.Zero, new IntPtr(index)); } /// <summary> /// 調(diào)整按鈕尺寸剛好為ToolBar尺寸 /// </summary> private void AdjustButtonSize() { IntPtr lParam = new IntPtr((this.Width & 0xFFFF) | (this.Height << 0x10)); //MakeLParam手法 SendMessage(this.Handle, 0x41F, IntPtr.Zero, lParam); //TB_SETBUTTONSIZE } #region Win32 API [DllImport("user32.dll", CharSet = CharSet.Auto)] private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam); [DllImport("user32.dll", CharSet = CharSet.Auto)] private static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, string lParam); [StructLayout(LayoutKind.Sequential)] private struct TBBUTTON { public int iBitmap; public int idCommand; public byte fsState; public byte fsStyle; public byte bReserved0; public byte bReserved1; public IntPtr dwData; public IntPtr iString; } #endregion } #endregion } } }
實(shí)現(xiàn)說(shuō)明:
以下內(nèi)容獻(xiàn)給童鞋。這里先貼個(gè)概要類圖,詳細(xì)的后面有完整Demo下載,你可以down回去慢慢研究。
若干Show方法都是調(diào)用私有的ShowCore方法,這個(gè)是模仿標(biāo)準(zhǔn)MessageBox的命名。至于意義,是因?yàn)楣_方法要做參數(shù)檢查,檢查合格后的代碼則可以重用。另外,幾個(gè)存在參數(shù)檢查的方法都是調(diào)用內(nèi)部方法,而不是調(diào)參數(shù)最全的那個(gè)重載,也是因?yàn)橐M量避免無(wú)謂的參數(shù)檢查,因?yàn)閰?shù)最全的那個(gè)公開方法,參數(shù)檢查自然是做的最多的,那么少參方法本來(lái)已經(jīng)能確保傳入的是合法參數(shù),卻因?yàn)檎{(diào)它,就會(huì)造成無(wú)謂的檢查,而調(diào)內(nèi)部方法則可以避免,因?yàn)閮?nèi)部方法就應(yīng)該設(shè)計(jì)為不做或少做參數(shù)檢查的。啰嗦這個(gè)是想提醒初學(xué)者注意這些細(xì)節(jié)上的處理,性能要從細(xì)處抓起
靜態(tài)類MessageBoxEx內(nèi)部維護(hù)著一個(gè)MessageForm窗體類(下文簡(jiǎn)稱MsgFm),每次Show都會(huì)實(shí)例化一個(gè)MsgFm,show完即釋放。幾乎所有能力都是由后者提供,前者只是簡(jiǎn)單的對(duì)其封裝和暴露,所以下面主要說(shuō)MsgFm的事。另外,根據(jù)傳入的MessageBoxButtons有無(wú)Cancel項(xiàng),會(huì)啟用/屏蔽窗體右上角的關(guān)閉按鈕,因?yàn)閱螕絷P(guān)閉按鈕的對(duì)話框結(jié)果始終是DialogResult.Cancel,所以如果不屏蔽,在傳入YesNo這樣的參數(shù)時(shí)候,調(diào)用者可能因?yàn)橛脩羧c(diǎn)關(guān)閉按鈕而得到Y(jié)es、No以外的結(jié)果。標(biāo)準(zhǔn)消息框也是有這樣的屏蔽處理的
MsgFm由3個(gè)控件區(qū)構(gòu)成,分別是主消息區(qū)、按鈕區(qū)、詳細(xì)信息區(qū)。
主消息區(qū)是一個(gè)單一控件:MessageViewer,直接繼承自Control寫成。一開始是考慮用現(xiàn)成的Label控件,但發(fā)現(xiàn)后者的圖文混排效果差強(qiáng)人意(不要扯這個(gè)成語(yǔ)本來(lái)的意思),它是把文字直接蓋在圖標(biāo)上,呵呵,大概此控件的編寫者本意就是要把Image當(dāng)BackgroundImage用,所以不得已另寫一個(gè)MessageViewer。MV主要做了兩個(gè)事,繪制(圖標(biāo)和文本)+根據(jù)內(nèi)容確定自身尺寸,另外它還控制了最小高度,避免圖標(biāo)和文本整體被淹沒(méi)
按鈕區(qū)由一個(gè)容器類控件PanelBasic托起4個(gè)按鈕。PB同樣是繼承自Control,沒(méi)有直接選用Panel的原因,主要是Panel會(huì)在設(shè)置Dock時(shí)跳一下,根源在Control.SetBoundsCore的specified參數(shù)通知了無(wú)謂的信息,所以干脆直接繼承Control重寫該方法,順便處理一下消息,解決瞬閃的問(wèn)題,具體原因這里不細(xì)說(shuō),注釋里有簡(jiǎn)短說(shuō)明,總之相信我不是就行了。此外按鈕區(qū)會(huì)根據(jù)按鈕可見情況控制最小寬度,它與上面的MessageViewer的最小高度共同構(gòu)成了整個(gè)對(duì)話框的最小尺寸MinimumSize
PanelBasic上的4個(gè)按鈕分別是【詳細(xì)信息】按鈕和其它3個(gè)對(duì)話框命令按鈕。仨按鈕根據(jù)傳入的MessageBoxButtons參數(shù)動(dòng)態(tài)處理(按鈕文本、是否可見等),沒(méi)什么好說(shuō)的?!驹敿?xì)信息】按鈕(ToggleButton)則費(fèi)了番功夫,該按鈕從外觀上就可以看出不是標(biāo)準(zhǔn)的Button,事實(shí)上它是個(gè)工具欄按鈕:ToolBarButton,屬于ToolBar上的Item,本身不是的控件(直接繼承自Component)。這里扯一點(diǎn),由于.net 2.0起MS就建議用新式的ToolStrip代替ToolBar,類似的還有MenuStrip代替MainMenu、StatusStrip代替StatusBar、ContextMenuStrip代替ContextMenu,VS2010更是默認(rèn)就不在工具箱顯示這些“控件”(有些不算控件),所以估計(jì)知道的新童鞋不多。后者都是原生的win32組件,前者則是純.net實(shí)現(xiàn)的,有Office2003的控件風(fēng)格??傊畬?duì)于有win32 native控的我來(lái)說(shuō),對(duì)這些被建議替代的老式控件有特別的情結(jié)。所以這個(gè)ToggleButton實(shí)際上是由一個(gè)ToolBar和一個(gè)ToolBarButton組成的看起來(lái)像一個(gè)單一控件的東西,那為什么它還是繼承自Control而不是直接用ToolBar呢,我承認(rèn)這里面有練手的原因(遲些我可能會(huì)寫一篇【教你一步步封裝一個(gè)Win32原生控件】的文章),Hmmm~也就這個(gè)原因了,但它雖然增加了代碼量,但請(qǐng)務(wù)必相信性能不比直接用ToolBar差,理論上還要好過(guò),因?yàn)樽鳛橐粋€(gè)完備的ToolBar,MS要考慮的情況相當(dāng)多,顯然處理也少不了,而我這個(gè)ToggleButton由于只負(fù)責(zé)一個(gè)單一按鈕的功能,所以其實(shí)很Simple很Lite~聰明的你會(huì)理解的。最后為什么要費(fèi)事弄成ToolBarButton而不是直接用一個(gè)Button,是因?yàn)槲铱瓷狭薽stsc.exe的這個(gè)效果:
順便說(shuō)一點(diǎn),EnableAnimate屬性有作用到該按鈕,原理是當(dāng)ToolBar具有Flat樣式的時(shí)候,按鈕按下和彈起就有動(dòng)畫效果,否則沒(méi)有
最后是詳細(xì)信息區(qū),由一個(gè)PanelBasic托起一個(gè)簡(jiǎn)單改造過(guò)的TextBox構(gòu)成。干嘛不單純用一個(gè)TextBox,而要在它底下墊一層呢,是因?yàn)樵赬P上的效果不好(控件狗要考慮的情況很多了啦好不好),XP窗口邊框不如NT6粗,不加點(diǎn)襯料的話太單薄。話說(shuō)回來(lái),PanelBasic上面已說(shuō)過(guò),而所謂改造過(guò)的這個(gè)TextBox叫TextBoxUnSelectAllable,就干一件事,忽略全選消息(EM_SETSEL),避免焦點(diǎn)移進(jìn)去的時(shí)候藍(lán)瑩瑩一片嚇到觀眾。而為什么不用標(biāo)準(zhǔn)TextBox的Enter事件取消全選,一個(gè)字~太low
尚存在一個(gè)問(wèn)題,這個(gè)注釋里也有坦白,就是當(dāng)主消息文本非常非常多時(shí)~大概整屏那么長(zhǎng)(這其實(shí)是不正確的使用姿勢(shì),上面說(shuō)過(guò),大量信息應(yīng)該放詳細(xì)信息區(qū)),如果對(duì)對(duì)話框反復(fù)拖拉、展開/收起,那么在某次展開時(shí),TextBoxUnSelectAllable會(huì)瞬間在主消息區(qū)閃一下,這個(gè)問(wèn)題在PanelBasic得到了完美的解決,但TextBox實(shí)在無(wú)能為力,嘗試過(guò)直接用原生Edit控件也如此,所以暫時(shí)留著吧,哪有沒(méi)缺憾的人生呢
關(guān)于聲音,由于MessageBeep API在srv08系統(tǒng)無(wú)聲,所以用了PlaySound API代替。另外,讓原本沒(méi)聲音的MessageBoxIcon.Question蹭SystemIcons.Information的聲音,不能歧視人Question
最后,【詳細(xì)信息】按鈕上那倆圖標(biāo)(展開、收起各一個(gè))是我畫的,本來(lái)想揀mstsc.exe上的,但發(fā)現(xiàn)效果不如意,還不如自己畫
說(shuō)了這么多,自以為很理想的實(shí)現(xiàn),可能槽點(diǎn)也不少,再次懇請(qǐng)路過(guò)大俠指點(diǎn),謝謝。
最后,Demo在此,里面有個(gè)Tester供你體驗(yàn):
-文畢-
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#中comboBox實(shí)現(xiàn)三級(jí)聯(lián)動(dòng)
給大家分享了C#中comboBox實(shí)現(xiàn)三級(jí)聯(lián)動(dòng)的全部代碼,代碼經(jīng)過(guò)測(cè)試,有興趣的朋友跟著做一下。2018-03-03詳解C#中的定時(shí)器Timer類及其垃圾回收機(jī)制
這篇文章主要介紹了C#中的定時(shí)器Timer類及其垃圾回收機(jī)制,講解了Timer相關(guān)的單線程異步工作,需要的朋友可以參考下2016-04-04C#正則表達(dá)式分解和轉(zhuǎn)換IP地址實(shí)例(C#正則表達(dá)式大全 c#正則表達(dá)式語(yǔ)法)
這是我發(fā)了不少時(shí)間整理的C#的正則表達(dá)式,新手朋友注意一定要手冊(cè)一下哦,這樣可以節(jié)省很多寫代碼的時(shí)間。下面進(jìn)行了簡(jiǎn)單總結(jié)2013-12-12解析C#設(shè)計(jì)模式編程中適配器模式的實(shí)現(xiàn)
這篇文章主要介紹了C#設(shè)計(jì)模式編程中適配器模式的實(shí)現(xiàn),分別舉了類的對(duì)象適配器與對(duì)象的適配器模式的例子,需要的朋友可以參考下2016-02-02C#實(shí)現(xiàn)簡(jiǎn)單的雙色球抽取中獎(jiǎng)號(hào)碼代碼
這篇文章主要介紹了C#實(shí)現(xiàn)簡(jiǎn)單的雙色球抽取中獎(jiǎng)號(hào)碼代碼的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06C#實(shí)現(xiàn)的字符串轉(zhuǎn)MD5碼函數(shù)實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)的字符串轉(zhuǎn)MD5碼函數(shù),結(jié)合簡(jiǎn)單實(shí)例形式分析了C#字符串的轉(zhuǎn)換、遍歷、加密等操作技巧,需要的朋友可以參考下2016-07-07