現在在做的項目美工要求比較高,所以根據網上搜索的資料,自定義了一整套的彈出框,供大家參考,之網上其他大神有調用系統ICO的,容易導致異常,我在此使用本地資源ICO,效率高不異常。using System; using System.Drawing; using System.Drawing.Draw... ...
現在在做的項目美工要求比較高,所以根據網上搜索的資料,自定義了一整套的彈出框,供大家參考,之網上其他大神有調用系統ICO的,容易導致異常,我在此使用本地資源ICO,效率高不異常。
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; using System.Runtime.InteropServices; using YLYJ_Cashier.Common; namespace YLYJ_Cashier { public partial class MyMsgBox : SkinMain { [DllImport("user32.dll", CharSet = CharSet.Auto)] private static extern bool MessageBeep(uint type); [DllImport("User32.dll", EntryPoint = "SetWindowLong", SetLastError = true)] public static extern bool SetWindowLong(IntPtr hWnd, int nIndex, int nFlags); [DllImport("User32.dll", EntryPoint = "GetWindowLong", SetLastError = true)] public static extern int GetWindowLong(IntPtr hWnd, int nFlags); [DllImport("User32.dll", EntryPoint = "SetWindowPos", SetLastError = true)] public static extern int SetWindowPos(IntPtr hWnd, IntPtr hWndWinInsertAfter, int x, int y, int cx, int cy, int nFlags); const int WS_EX_TOOLWINDOW = 0x80; const int GWL_EXSTYLE = -20; const int HWND_TOPMOST = -1; const int SWP_NOSIZE = 0x0001; const int SWP_NOMOVE = 0x0002; static private MyMsgBox newMessageBox; static private Label frmTitle; static private Label frmMessage; static private PictureBox pIcon; static private FlowLayoutPanel flpButtons; static private Icon frmIcon; static private Button btnOK; static private Button btnAbort; static private Button btnRetry; static private Button btnIgnore; static private Button btnCancel; static private Button btnYes; static private Button btnNo; static private DialogResult CYReturnButton; public enum MyIcon { Information, Question, Warning } public enum MyButtons { AbortRetryIgnore, OK, OKCancel, RetryCancel, YesNo, YesNoCancel } static private void BuildMessageBox(string title) { try { newMessageBox = new MyMsgBox(); newMessageBox.Text = title; newMessageBox.Size = new System.Drawing.Size(350, 150); newMessageBox.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; newMessageBox.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; newMessageBox.Paint += new PaintEventHandler(newMessageBox_Paint); newMessageBox.BackColor = System.Drawing.Color.White; TableLayoutPanel tlp = new TableLayoutPanel(); tlp.RowCount = 3; tlp.ColumnCount = 0; tlp.Dock = System.Windows.Forms.DockStyle.Fill; tlp.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 22)); tlp.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F)); tlp.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 50)); tlp.BackColor = System.Drawing.Color.Transparent; tlp.Padding = new Padding(2, 5, 2, 2); frmTitle = new Label(); frmTitle.Dock = System.Windows.Forms.DockStyle.Fill; frmTitle.BackColor = System.Drawing.Color.Transparent; frmTitle.ForeColor = System.Drawing.Color.White; frmTitle.Font = new Font("微軟雅黑", 10, FontStyle.Bold); frmMessage = new Label(); frmMessage.Dock = System.Windows.Forms.DockStyle.Fill; frmMessage.BackColor = System.Drawing.Color.White; frmMessage.ForeColor=ColorTranslator.FromHtml("#333333"); frmMessage.Font = new Font("微軟雅黑", 12, FontStyle.Regular); frmMessage.Padding = new Padding(0, 10, 0, 0); frmMessage.Text = "hiii"; pIcon = new PictureBox(); flpButtons = new FlowLayoutPanel(); flpButtons.FlowDirection = System.Windows.Forms.FlowDirection.RightToLeft; flpButtons.Padding = new Padding(0, 5, 5, 0); flpButtons.Dock = System.Windows.Forms.DockStyle.Fill; flpButtons.BackColor = System.Drawing.Color.White; TableLayoutPanel tlpMessagePanel = new TableLayoutPanel(); tlpMessagePanel.BackColor = System.Drawing.Color.White; tlpMessagePanel.ForeColor = ColorTranslator.FromHtml("#333333"); tlpMessagePanel.Dock = System.Windows.Forms.DockStyle.Fill; tlpMessagePanel.ColumnCount = 2; tlpMessagePanel.RowCount = 0; tlpMessagePanel.Padding = new Padding(4, 5, 4, 4); tlpMessagePanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 50)); tlpMessagePanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F)); tlpMessagePanel.Controls.Add(pIcon); tlpMessagePanel.Controls.Add(frmMessage); tlp.Controls.Add(frmTitle); tlp.Controls.Add(tlpMessagePanel); tlp.Controls.Add(flpButtons); newMessageBox.Controls.Add(tlp); } catch (Exception ee) { Log.WriteLog("消息框彈出時發生異常:" + ee.Message.ToString()); } } /// <summary> /// 預設消息提示,圖標為感嘆號 /// </summary> static public DialogResult Show(string Message) { BuildMessageBox("提示"); frmTitle.Text = "提示"; frmMessage.Text = Message; ShowOKButton(); IconStatements(MyIcon.Information); Image imageIcon = new Bitmap(frmIcon.ToBitmap(), 38, 38); pIcon.Image = imageIcon; newMessageBox.ShowDialog(); return CYReturnButton; } /// <summary> /// Message: Text to display in the message box. /// </summary> static public DialogResult Show(string Message, MyIcon MIcon) { BuildMessageBox("提示"); frmTitle.Text = "提示"; frmMessage.Text = Message; ShowOKButton(); IconStatements(MIcon); Image imageIcon = new Bitmap(frmIcon.ToBitmap(), 38, 38); pIcon.Image = imageIcon; newMessageBox.ShowDialog(); return CYReturnButton; } /// <summary> /// Title: Text to display in the title bar of the messagebox. /// </summary> static public DialogResult Show(string Message, string Title) { BuildMessageBox(Title); frmTitle.Text = Title; frmMessage.Text = Message; ShowOKButton(); newMessageBox.ShowDialog(); return CYReturnButton; } /// <summary> /// MButtons: Display MyButtons on the message box. /// </summary> static public DialogResult Show(string Message, string Title, MyButtons MButtons) { BuildMessageBox(Title); // BuildMessageBox method, responsible for creating the MessageBox frmTitle.Text = Title; // Set the title of the MessageBox frmMessage.Text = Message; //Set the text of the MessageBox ButtonStatements(MButtons); // ButtonStatements method is responsible for showing the appropreiate buttons newMessageBox.ShowDialog(); // Show the MessageBox as a Dialog. return CYReturnButton; // Return the button click as an Enumerator } /// <summary> /// MIcon: Display MyIcon on the message box. /// </summary> static public DialogResult Show(string Message, string Title, MyButtons MButtons, MyIcon MIcon) { BuildMessageBox(Title); frmTitle.Text = Title; frmMessage.Text = Message; ButtonStatements(MButtons); IconStatements(MIcon); Image imageIcon = new Bitmap(frmIcon.ToBitmap(), 38, 38); pIcon.Image = imageIcon; newMessageBox.ShowDialog(); return CYReturnButton; } static void btn_MouseEnter(object sender, EventArgs e) { var btn = (Button)sender; btn.BackgroundImage = Properties.Resources.buttonHover; } static void btn_MouseLeave(object sender, EventArgs e) { var btn = (Button)sender; btn.BackgroundImage = Properties.Resources.buttonBack; } static void btnOK_Click(object sender, EventArgs e) { CYReturnButton = DialogResult.OK; newMessageBox.Dispose(); } static void btnAbort_Click(object sender, EventArgs e) { CYReturnButton = DialogResult.Abort; newMessageBox.Dispose(); } static void btnRetry_Click(object sender, EventArgs e) { CYReturnButton = DialogResult.Retry; newMessageBox.Dispose(); } static void btnIgnore_Click(object sender, EventArgs e) { CYReturnButton = DialogResult.Ignore; newMessageBox.Dispose(); } static void btnCancel_Click(object sender, EventArgs e) { CYReturnButton = DialogResult.Cancel; newMessageBox.Dispose(); } static void btnYes_Click(object sender, EventArgs e) { CYReturnButton = DialogResult.Yes; newMessageBox.Dispose(); } static void btnNo_Click(object sender, EventArgs e) { CYReturnButton = DialogResult.No; newMessageBox.Dispose(); } static private void ShowOKButton() { btnOK = new Button(); btnOK.Text = "確認"; btnOK.Size = new System.Drawing.Size(80, 32); btnOK.BackColor = System.Drawing.Color.Transparent; ; btnOK.BackgroundImage = Properties.Resources.buttonBack; btnOK.Font = new Font("微軟雅黑", 9, FontStyle.Regular); btnOK.BackgroundImageLayout = ImageLayout.Stretch; btnOK.ForeColor = System.Drawing.Color.White; btnOK.TextAlign = ContentAlignment.TopCenter; btnOK.FlatAppearance.BorderSize = 0; btnOK.FlatStyle = FlatStyle.Flat; btnOK.Click += new EventHandler(btnOK_Click); btnOK.MouseEnter += new EventHandler(btn_MouseEnter); btnOK.MouseLeave += new EventHandler(btn_MouseLeave); btnOK.Cursor=Cursors.Hand; flpButtons.Controls.Add(btnOK); } static private void ShowAbortButton() { btnAbort = new Button(); btnAbort.Text = "停止"; btnAbort.Size = new System.Drawing.Size(80, 32); btnAbort.BackColor = System.Drawing.Color.Transparent; btnAbort.BackgroundImage = Properties.Resources.buttonBack; btnAbort.Font = new Font("微軟雅黑", 9, FontStyle.Regular); btnAbort.BackgroundImageLayout = ImageLayout.Stretch; btnAbort.ForeColor = System.Drawing.Color.White; btnAbort.TextAlign = ContentAlignment.TopCenter; btnAbort.FlatAppearance.BorderSize = 0; btnAbort.FlatStyle = FlatStyle.Flat; btnAbort.Click += new EventHandler(btnAbort_Click); btnAbort.MouseEnter += new EventHandler(btn_MouseEnter); btnAbort.MouseLeave += new EventHandler(btn_MouseLeave); btnAbort.Cursor = Cursors.Hand; flpButtons.Controls.Add(btnAbort); } static private void ShowRetryButton() { btnRetry = new Button(); btnRetry.Text = "重試"; btnRetry.Size = new System.Drawing.Size(80, 32); btnRetry.BackColor = System.Drawing.Color.Transparent; btnRetry.BackgroundImage = Properties.Resources.buttonBack; btnRetry.Font = new Font("微軟雅黑", 9, FontStyle.Regular); btnRetry.BackgroundImageLayout = ImageLayout.Stretch; btnRetry.ForeColor = System.Drawing.Color.White; btnRetry.TextAlign = ContentAlignment.TopCenter; btnRetry.FlatAppearance.BorderSize = 0; btnRetry.FlatStyle = FlatStyle.Flat; btnRetry.Click += new EventHandler(btnRetry_Click); btnRetry.MouseEnter += new EventHandler(btn_MouseEnter); btnRetry.MouseLeave += new EventHandler(btn_MouseLeave); btnRetry.Cursor = Cursors.Hand; flpButtons.Controls.Add(btnRetry); } static private void ShowIgnoreButton() { btnIgnore = new Button(); btnIgnore.Text = "忽略"; btnIgnore.Size = new System.Drawing.Size(80, 32); btnIgnore.BackColor = System.Drawing.Color.Transparent; btnIgnore.BackgroundImage = Properties.Resources.buttonBack; btnIgnore.Font = new Font("微軟雅黑", 9, FontStyle.Regular); btnIgnore.BackgroundImageLayout = ImageLayout.Stretch; btnIgnore.ForeColor = System.Drawing.Color.White; btnIgnore.TextAlign = ContentAlignment.TopCenter; btnIgnore.FlatAppearance.BorderSize = 0; btnIgnore.FlatStyle = FlatStyle.Flat; btnIgnore.Click += new EventHandler(btnIgnore_Click); btnIgnore.MouseEnter += new EventHandler(btn_MouseEnter); btnIgnore.MouseLeave += new EventHandler(btn_MouseLeave); btnIgnore.Cursor = Cursors.Hand; flpButtons.Controls.Add(btnIgnore); } static private void ShowCancelButton() { btnCancel = new Button(); btnCancel.Text = "取消"; btnCancel.Size = new System.Drawing.Size(80, 32); btnCancel.BackColor = System.Drawing.Color.Transparent; btnCancel.BackgroundImage = Properties.Resources.buttonBack; btnCancel.Font = new Font("微軟雅黑",9, FontStyle.Regular); btnCancel.BackgroundImageLayout = ImageLayout.Stretch; btnCancel.ForeColor = System.Drawing.Color.White; btnCancel.TextAlign = ContentAlignment.TopCenter; btnCancel.FlatAppearance.BorderSize = 0; btnCancel.FlatStyle = FlatStyle.Flat; btnCancel.Click += new EventHandler(btnCancel_Click); btnCancel.MouseEnter += new EventHandler(btn_MouseEnter); btnCancel.MouseLeave += new EventHandler(btn_MouseLeave); btnCancel.Cursor = Cursors.Hand; flpButtons.Controls.Add(btnCancel); } static private void ShowYesButton() { btnYes = new Button(); btnYes.Text = "是"; btnYes.Size = new System.Drawing.Size(80, 32); btnYes.BackColor = System.Drawing.Color.Transparent; btnYes.BackgroundImage = Properties.Resources.buttonBack; btnYes.Font = new Font("微軟雅黑", 9, FontStyle.Regular); btnYes.BackgroundImageLayout = ImageLayout.Stretch; btnYes.ForeColor = System.Drawing.Color.White; btnYes.TextAlign = ContentAlignment.TopCenter; btnYes.FlatAppearance.BorderSize = 0; btnYes.FlatStyle = FlatStyle.Flat; btnYes.Click += new EventHandler(btnYes_Click); btnYes.MouseEnter += new EventHandler(btn_MouseEnter); btnYes.MouseLeave += new EventHandler(btn_MouseLeave); btnYes.Cursor = Cursors.Hand; btnYes.TabIndex = 1; flpButtons.Controls.Add(btnYes); } static private void ShowNoButton() { btnNo = new Button(); btnNo.Text = "否"; btnNo.Size = new System.Drawing.Size(80, 32); btnNo.BackColor = System.Drawing.Color.Transparent; btnNo.BackgroundImage = Properties.Resources.buttonBack; btnNo.Font = new Font("微軟雅黑", 9, FontStyle.Regular); btnNo.BackgroundImageLayout = ImageLayout.Stretch; btnNo.ForeColor = System.Drawing.Color.White; btnNo.TextAlign = ContentAlignment.TopCenter; btnNo.FlatAppearance.BorderSize = 0; btnNo.FlatStyle = FlatStyle.Flat; btnNo.Click += new EventHandler(btnNo_Click); btnNo.MouseEnter += new EventHandler(btn_MouseEnter); btnNo.MouseLeave += new EventHandler(btn_MouseLeave); btnNo.Cursor = Cursors.Hand; flpButtons.Controls.Add(btnNo); } static private void ButtonStatements(MyButtons MButtons) { try { if (MButtons == MyButtons.AbortRetryIgnore) { ShowIgnoreButton(); ShowRetryButton(); ShowAbortButton(); } if (MButtons == MyButtons.OK) { ShowOKButton(); } if (MButtons == MyButtons.OKCancel) { ShowCancelButton(); ShowOKButton(); } if (MButtons == MyButtons.RetryCancel) { ShowCancelButton(); ShowRetryButton(); } if (MButtons == MyButtons.YesNo) { ShowNoButton(); ShowYesButton(); } if (MButtons == MyButtons.YesNoCancel) { ShowCancelButton(); ShowNoButton(); ShowYesButton(); } } catch (Exception ee) { Log.WriteLog("消息框彈出時發生異常:" + ee.Message.ToString()); } } static private void IconStatements(MyIcon MIcon) { try { if (MIcon == MyIcon.Information) { MessageBeep(0); frmIcon = Properties.Resources.information; } if (MIcon == MyIcon.Question) { MessageBeep(0); frmIcon = Properties.Resources.question; } if (MIcon == MyIcon.Warning) { MessageBeep(30); frmIcon = Properties.Resources.warning; } } catch (Exception ee) { Log.WriteLog("消息框彈出時發生異常:" + ee.Message.ToString()); } } static void newMessageBox_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; Rectangle frmTitleL = new Rectangle(0, 0, (newMessageBox.Width / 2), 30); Rectangle frmTitleR = new Rectangle((newMessageBox.Width / 2), 0, (newMessageBox.Width / 2), 30); Rectangle frmMessageBox = new Rectangle(0, 0, (newMessageBox.Width - 1), (newMessageBox.Height - 1)); LinearGradientBrush frmLGBL = new LinearGradientBrush(frmTitleL, Color.FromArgb(85, 100, 217), Color.FromArgb(209, 230, 243), LinearGradientMode.Horizontal); LinearGradientBrush frmLGBR = new LinearGradientBrush(frmTitleR, Color.FromArgb(209, 230, 243), Color.FromArgb(85, 100, 217), LinearGradientMode.Horizontal); Pen frmPen = new Pen(Color.FromArgb(63, 119, 143), 1); g.FillRectangle(frmLGBL, frmTitleL); g.FillRectangle(frmLGBR, frmTitleR); g.DrawRectangle(frmPen, frmMessageBox); } private void InitializeComponent() { System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MyMsgBox)); this.SuspendLayout(); // // MyMsgBox // this.BackColor = System.Drawing.Color.White; this.ClientSize = new System.Drawing.Size(284, 262); this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.KeyPreview = true; this.Name = "MyMsgBox"; this.ShowInTaskbar = false; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent; this.ShowInTaskbar = false; this.TopMost = true; this.Load += new System.EventHandler(this.MyMsgBox_Load); this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.MyMsgBox_KeyDown); this.ResumeLayout(false); } private void MyMsgBox_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Escape) //ESC鍵按下 { this.Close(); } } /// <summary> /// 控制彈出框永遠在最上方 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void MyMsgBox_Load(object sender, EventArgs e) { SetWindowLong(this.Handle, GWL_EXSTYLE, GetWindowLong(Handle, GWL_EXSTYLE) | WS_EX_TOOLWINDOW); SetWindowPos(this.Handle, (IntPtr)HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE); this.TopMost = true; } } }