在开发中最重要的就是美化form窗口,在开发中,大多都是用会用自主美化的窗口开发程序。
本文只是点多,分为上中下节。分段讲解。
本文主要讲解窗口美化关键步骤。
首先美化窗体,就需要自己绘制最大化 最小化 关闭按钮。
其次就是界面样式,标题区域等
这一步很重要,首先要将窗体属性设置为None。
其次,可以在属性中将背景颜色调整,本教程是酷黑色,用的值为 37,37,38。可以根据自己需求使用自主值。
public WenForm() { InitializeComponent(); RefreshPadding(); SystemButtonAdd(); ba( Con | Con | Con | Con | Con | Con, true); ba(); //获取显示器的工作区。工作区是显示器的桌面区域,不包括任务栏、停靠窗口和停靠工具栏。 = Screen.PrimaryScreen.WorkingArea; = Color.FromArgb(37, 37, 38); = Color.White; }
关键构造函数中相关内容,重绘用的最多的就是如上代码,基本上在重绘中使用很平凡。
private void SystemButtonAdd() { buttonPointX = 0; CloseButtonAdd(); MaxButtonAdd(); MinButtonAdd(); ConfigButtonAdd(); SkinButtonAdd(); }
接下来绘制系统按钮 ,包含最大化 最小化 关闭 按钮 ,可以根据自己需求增加按钮,例如设置按钮, 皮肤按钮等,本文教程主要绘制 5个按钮 ,代码相近。
#region 添加关闭按钮 private void CloseButtonAdd() { if ["SystemButtonClose"] is WenControl close) { (close); } buttonPointX = buttonPointX + 46; int x = buttonPointX; WenControl wenControl = new WenControl() { BackColor = Color.Transparent, Width = 46, Height = 30, Location = new Poin - x, 0), Name = "SystemButtonClose" }; wenCon += (s, e) => { wenCon = Color.FromArgb(63, 63, 65); }; wenCon += (s, e) => { wenCon = Color.Transparent; }; wenCon += (s, e) => { Graphics g = e.Graphics; g.SetGDIHigh(); using Pen p = new Pen, 1); g.DrawLine(p, 18, 11, 18 + 8, 11 + 8); g.DrawLine(p, 18, 11 + 8, 18 + 8, 11); }; wenCon += (s, e) => { (); }; += (s, e) => { wenCon = new Poin - x, 0); }; (wenControl); } #endregion
关闭按钮
#region 添加最大化按钮 private void MaxButtonAdd() { if ["SystemButtonMax"] is WenControl max) { (max); } if (!MaximizeBox) return; buttonPointX = buttonPointX + 46; int x = buttonPointX; WenControl wenControl = new WenControl() { BackColor = Color.Transparent, Width = 46, Height = 30, Location = new Poin - x, 0), Name = "SystemButtonMax" }; wenCon += (s, e) => { wenCon = Color.FromArgb(63, 63, 65); }; wenCon += (s, e) => { wenCon = Color.Transparent; }; wenCon += (s, e) => { Graphics g = e.Graphics; g.SetGDIHigh(); using Pen p = new Pen, 1); if == FormWindowS) { g.DrawRectangle(p, 18, 11 + 2, 6, 6); g.DrawRectangle(p, 18 + 2, 11, 6, 6); } else { g.DrawRectangle(p, 18, 11, 8, 8); } }; wenCon += (s, e) => { if (WindowState == FormWindowS) { = FormWindowS; } else { = FormWindowS; } }; += (s, e) => { wenCon = new Poin - x, 0); }; (wenControl); } #endregion
添加最大化按钮
#region 最小化按钮 private void MinButtonAdd() { if ["SystemButtonMin"] is WenControl min) { (min); } if (!MinimizeBox) return; buttonPointX = buttonPointX + 46; int x = buttonPointX; WenControl wenControl = new WenControl() { BackColor = Color.Transparent, Width = 46, Height = 30, Location = new Poin - x, 0), Name = "SystemButtonMin" }; wenCon += (s, e) => { wenCon = Color.FromArgb(63, 63, 65); }; wenCon += (s, e) => { wenCon = Color.Transparent; }; wenCon += (s, e) => { Graphics g = e.Graphics; g.SetGDIHigh(); using Pen p = new Pen, 1); g.DrawLine(p, 18, 15, 18 + 8, 15); }; wenCon += (s, e) => { = FormWindowS; }; += (s, e) => { wenCon = new Poin - x, 0); }; (wenControl); } #endregion
最小化按钮
#region 设置按钮 private void ConfigButtonAdd() { if ["SystemButtonConfig"] is WenControl c) { (c); } if (!ConfigButtonBox) return; buttonPointX = buttonPointX + 46; int x = buttonPointX; WenControl wenControl = new WenControl() { BackColor = Color.Transparent, Width = 46, Height = 30, Location = new Poin - x, 0), Name = "SystemButtonConfig" }; wenCon += (s, e) => { wenCon = Color.FromArgb(63, 63, 65); }; wenCon += (s, e) => { wenCon = Color.Transparent; }; wenCon += (s, e) => { Graphics g = e.Graphics; g.SetGDIHigh(); g.DrawImage, new Rectangle(13, 5, 20, 20)); }; wenCon += (s, e) => { ConfigButtonClick?.Invoke(this, e); }; += (s, e) => { wenCon = new Poin - x, 0); }; (wenControl); } #endregion
设置按钮
#region 皮肤按钮 private void SkinButtonAdd() { if ["SystemButtonSkin"] is WenControl c) { (c); } if (!SkinButtonBox) return; buttonPointX = buttonPointX + 46; int x = buttonPointX; WenControl wenControl = new WenControl() { BackColor = Color.Transparent, Width = 46, Height = 30, Location = new Poin - x, 0), Name = "SystemButtonSkin" }; wenCon += (s, e) => { wenCon = Color.FromArgb(63, 63, 65); }; wenCon += (s, e) => { wenCon = Color.Transparent; }; wenCon += (s, e) => { Graphics g = e.Graphics; g.SetGDIHigh(); g.DrawImage, new Rectangle(13, 5, 20, 20)); }; wenCon += (s, e) => { SkinButtonClick?.Invoke(this, e); }; += (s, e) => { wenCon = new Poin - x, 0); }; (wenControl); } #endregion
皮肤按钮
本文中 关闭 ,最大化 ,最小化按钮用GDI+画。也可以用图档代替。
设置按钮,和皮肤按钮,在阿里图标库中下载,可以自主下载编辑。
关注文林软控,带你一起用C# 美化.NET控件。