Winform美化MessageBox
现在在做的项目美工要求比较高,所以根据网上搜索的资料,自定义了一整套的弹出框,供大家参考,之网上其他大神有调用系统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;
}
}
}
Winform美化MessageBox的更多相关文章
- C# WinForm 中 MessageBox的使用详解
1.C# WinForm 中 MessageBox的使用详解:http://www.cnblogs.com/bq-blog/archive/2012/07/27/2611810.html
- Winform 美化
首先,我们先来实现主界面的扁平化 此处分为两个步骤,第一步是更改winform自带的MainForm窗体属性,第二步是添加窗体事件. 将主窗体FormBorderStyle更改为None,这样就得到了 ...
- winform中messageBox七个参数的使用(转载)
private void button1_Click(object sender, EventArgs e) { MessageBox.Show(" 1 个参数 ”); } private ...
- C# winform OpenFileDialog MessageBox
1.弹出窗体选择本地文件-OpenFileDialog OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Tit ...
- C# winform 中MessageBox用法大全(附效果图) (转载+说明)
声明:这篇文章是转载的转载的,由于原作者的博客被关闭 我就不再列出了,提前先说明下,if语句中的判断有些太长,建议提前声明一个变量, DialogResult MsgBoxResult; ...
- winform 关于Messagebox自动定时关闭
添加一个类库MessageBoxTimeOut public class MessageBoxTimeOut { private string _caption; public void Show(s ...
- c# winform 自动关闭messagebox 模拟回车
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- C# winform 中MessageBox用法大全(附效果图)
我们在程序中经常会用到MessageBox. MessageBox.Show()共有21中重载方法.现将其常见用法总结如下: 1.MessageBox.Show(“Hello~~~~”); 最简单的, ...
- winform 中 MessageBox 用法大全
(转自:http://blog.csdn.net/xuenzhen123/article/details/4808005) MessageBox.Show()共有21中重载方法.现将其常见用法总结如下 ...
随机推荐
- 辩证看待 iostat
前言 经常做系统分析会接触到很多有用的工具,比如 iostat,它是用来分析磁盘性能.系统 I/O 的利器. 本文将重点介绍 iostat 命令的使用,并分析容易引起误解的几个指标. iostat i ...
- Intellij-创建Maven项目速度慢
原因: IDEA根据maven archetype的本质,其实是执行mvn archetype:generate命令,该命令执行时,需要指定一个archetype-catalog.xml文件. 该命令 ...
- CentOS6.x机器安装Azure CLI2.0【1】
安装Azure CLI 2.0的前提是:机器中必须有 Python 2.7.x 或 Python 3.x.如果机器中没有其中任何一个Python版本,请及时安装 1.准备一台CentOS 6.9的机器 ...
- 企业级分布式存储应用与实战FastDFS实现
FASTDFS是什么 FastDFS是由国人余庆所开发,其项目地址:https://github.com/happyfish100 FastDFS是一个轻量级的开源分布式文件系统,主要解决了大容量的文 ...
- 07_jquery入门第一天
视频来源:麦子学院 讲师:魏畅然 补充:JSON.stringify()函数 [https://www.cnblogs.com/damonlan/archive/2012/03/13/2394787. ...
- 【转】DEM DTM DLG DRG DOM DSM
pasting DTM DLG DRG DOM DSM" title="[转载]DEM DTM DLG DRG DOM DSM" height="477&quo ...
- LIUNX-Centos 7 编译GDAL
一.准备工作 安装编译环境 sudo yum install gcc gcc-c++ gcc-g77 flex bison autoconf automake bzip2-devel zlib-dev ...
- Docker之容器
容器(Container) 容器介绍: docker是通过容器来运行业务的,就像运行一个kvm虚拟机是一样的.容器其实就是从镜像创建的一个实例. 我们可以对容器进行增删改查,容器之间也是相互隔离的.和 ...
- Codeforce D. Make a Permutation!
D. Make a Permutation! time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- 深入浅出Hadoop之HDFS
hadoop生态系统一直是大数据灵域的热点,其中包括今天要聊的HDFS,和计划以后想聊的yarn, mapreduce, spark, hive, hbase, 已经聊过的zookeeper,等等. ...