Winform自定义弹窗 二
一:创建出一个form新窗体,绘制出大概的样式。
1.属性:
StartPosition:CenterScreen 初始位置设为居中
FormBorderStyle:None 不要边框
我这里拖入了一个Panel控件 用于装提示框内容 背景颜色设置为:WhiteSmoke 类似于灰白色一样的。
然后在Panel控件里拖入一个PictrueBox组件用于显示消息框图标
在加入一个Button搞定。 界面就简单的做出来了
二.窗体操作
1.拖动:
由于窗体失去了边框,无法拖动 需要自己手动加入点击窗体时拖动的事件
1 //移动窗体 2 const int WM_NCLBUTTONDOWN = 0xA1; 3 const int HT_CAPTION = 0x2; 4 [System.Runtime.InteropServices.DllImport("user32.dll")] 5 static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam); 6 //给窗体添加MouseDown事件 7 private void Form_Alert_MouseDown(object sender, MouseEventArgs e) 8 { 9 if (e.Button == MouseButtons.Left & this.WindowState == FormWindowState.Normal) 10 { 11 //移动窗体 12 this.Capture = false; 13 SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0); 14 }
1 private void timer1_Tick(object sender, EventArgs e) 2 { 3 switch (this.action) 4 { 5 case enmAction.Start: 6 timer1.Interval = 1; 7 if (this.Opacity < 1.0) 8 { 9 this.Opacity += 0.1; 10 } 11 break; 12 case enmAction.Close: 13 timer1.Interval = 1; 14 this.Opacity -= 0.1; 15 if (this.Opacity == 0.0) 16 { 17 base.Close(); 18 } 19 break; 20 } 21 }
15 }
2.圆角:
1 /// <summary> 2 /// 设置窗体的Region 3 /// </summary> 4 public void SetWindowRegion() 5 { 6 GraphicsPath FormPath; 7 Rectangle rect = new Rectangle(0, 0, this.Width, this.Height); 8 FormPath = GetRoundedRectPath(rect, 25); 9 this.Region = new Region(FormPath); 10 } 11 /// <summary> 12 /// 绘制圆角路径 13 /// </summary> 14 /// <param name="rect"></param> 15 /// <param name="radius"></param> 16 /// <returns></returns> 17 private GraphicsPath GetRoundedRectPath(Rectangle rect, int radius) 18 { 19 int diameter = radius; 20 Rectangle arcRect = new Rectangle(rect.Location, new Size(diameter, diameter)); 21 GraphicsPath path = new GraphicsPath(); 22 // 左上角 23 path.AddArc(arcRect, 180, 90); 24 // 右上角 25 arcRect.X = rect.Right - diameter; 26 path.AddArc(arcRect, 270, 90); 27 // 右下角 28 arcRect.Y = rect.Bottom - diameter; 29 path.AddArc(arcRect, 0, 90); 30 // 左下角 31 arcRect.X = rect.Left; 32 path.AddArc(arcRect, 90, 90); 33 path.CloseFigure();//闭合曲线 34 return path; 35 } 36 //给窗体添加该变大小事件 37 private void Form_Alert_Resize(object sender, EventArgs e) 38 { 39 //重设圆角 40 SetWindowRegion(); 41 }
3.内容居中
//设置控件居中 private void SetCenterControl() { //标题居中 this.labTitle.Left = (this.Width - labTitle.Width) / 2; //内容居中 this.labText.Left = (this.Width + AlertIcon.Width - labText.Width) / 2; if (labText.Width < 200) { //如果内容长度过短 就只以窗体宽度居中 this.labText.Left = (this.Width - labText.Width) / 2; } this.labText.MaximumSize = new Size(300,0); this.labText.Top = (this.Height - labText.Height) / 2 - (btnOk.Height + 5); } private void Form_Alert_Resize(object sender, EventArgs e) { SetWindowRegion(); //在窗体大小改变事件中调用 SetCenterControl(); }
三:枚举动作
1 //窗口类型 2 public enum enmType 3 { 4 Success, 5 Error, 6 Info, 7 Warn 8 } 9 //动作 10 public enum enmAction 11 { 12 Start, 13 Close 14 } 15 //记录当前动作 16 private Form_Alert.enmAction action;
四:显示
1.拖入一个定时器用来处理窗体的显示过度和行动事件
监听每1毫秒当前弹框的动作, 设置Opacity透明度,用来过度显示和关闭, 不然导致很僵硬。
1 private void timer1_Tick(object sender, EventArgs e) 2 { 3 switch (this.action) 4 { 5 case enmAction.Start: 6 timer1.Interval = 1; 7 if (this.Opacity < 1.0) 8 { 9 this.Opacity += 0.1; 10 } 11 break; 12 case enmAction.Close: 13 timer1.Interval = 1; 14 this.Opacity -= 0.1; 15 if (this.Opacity == 0.0) 16 { 17 base.Close(); 18 } 19 break; 20 } 21 }
五:定义一个显示方法
1 // 内容 弹框类型 标题 2 public void Alert(string Content, enmType type, string Title) 3 { 4 this.Opacity = 0.0; 5 switch (type) 6 { 7 //根据类型设置弹框的颜色和图标 8 case enmType.Success: 9 this.BackColor = Color.SeaGreen; 10 this.btnOk.BackColor= Color.SeaGreen; 11 this.AlertIcon.Image = Resources.Success_gray; 12 break; 13 case enmType.Error: 14 this.BackColor = Color.DarkRed; 15 this.btnOk.BackColor = Color.DarkRed; 16 this.AlertIcon.Image = Resources.Error_gray; 17 break; 18 case enmType.Info: 19 this.BackColor = Color.RoyalBlue; 20 this.btnOk.BackColor = Color.RoyalBlue; 21 this.AlertIcon.Image = Resources.Info_gray; 22 break; 23 case enmType.Warn: 24 this.BackColor = Color.DarkOrange; 25 this.btnOk.BackColor = Color.DarkOrange; 26 this.AlertIcon.Image = Resources.Warn_gray; 27 break; 28 } 29 this.Show();//显示 30 this.labTitle.Text = Title; 31 this.labText.Text = Content; 32 this.action = enmAction.Start; 33 this.timer1.Interval = 1; 34 this.timer1.Start(); 35 RightVoiceAndHint();//这是定义的一个播放提示音的方法 36 }
六:最后调用 显示
在新建第二个新窗体主程序,弹出提示框,看看效果
1 //对话框显示方法 2 public void MoudelAlert(string Content, Form_Alert.enmType type, string Title = "提示") 3 { 4 Form_Alert Moudel = new Form_Alert(); 5 Moudel.Alert(Content, type, Title); 6 } 7 //弹出成功对话框 8 private void btn_AlSuccess_Click(object sender, EventArgs e) 9 { 10 this.MoudelAlert("Alert Success...", Form_Alert.enmType.Success,"温馨提示"); 11 } 12 //警告 13 private void btn_AlWarn_Click(object sender, EventArgs e) 14 { 15 this.MoudelAlert("Alert Warning...", Form_Alert.enmType.Warn); 16 } 17 //错误 18 private void btn_AlError_Click(object sender, EventArgs e) 19 { 20 this.MoudelAlert("Alert Error...", Form_Alert.enmType.Error); 21 } 22 //提示 23 private void btn_AlInfo_Click(object sender, EventArgs e) 24 { 25 this.MoudelAlert("Alert Info...", Form_Alert.enmType.Info); 26 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义