自己网上Ctrl + C 、Ctrl + V 收集,并自己开发的,自用 简单窗体控件库,如有喜欢的朋友可以下载去探索下。
目前有 10个重写控件,分别是:Button、CheckBox、Form、GlassButton、ImageButton、ListView、MessageBox、RadioButtom
SplashScreen、TextBox。不会写的很复杂,适合新手,一看就明白。源码注释明确。
控件演示窗口:

消息窗口展示:

文件目录展示:

展示部分窗体代码:
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace EM.Controls
{
/// <summary>
/// 自定义扩展窗体Form
/// </summary>
public class Form : System.Windows.Forms.Form
{
#region 私有变量
//系统按钮管理器
private SystemButtonManager _systemButtonManager;
#endregion
#region 构造函数
public Form()
{
InitializeComponent();
StartPosition = FormStartPosition.CenterScreen;
_systemButtonManager = new SystemButtonManager(this);
FormIni();
}
#endregion
#region 属性
[Description("确定窗体标题栏的右上角是否有关闭框。")]
public bool CloseBox { get; set; } = true;
[Description("窗体圆角的半径。")]
public int Radius { get; set; } = 5;
[Description("是否允许窗体改变大小。")]
public bool CanResize { get; set; } = true;
[Description("窗体背景图片。")]
public override Image BackgroundImage { get; set; }
[Description("绘制窗体标题的字体。")]
public Font TextFont { get; set; } = new Font("宋体", 9.0f, FontStyle.Bold);
[Description("绘制窗体标题的颜色。")]
public Color TextForeColor { get; set; } = Color.FromArgb(250, Color.White);
[Description("绘制带有阴影的窗体标题。")]
public bool TextWithShadow { get; set; } = true;
[Description("使窗体绘制阴影。")]
public Color TextShadowColor { get; set; } = Color.FromArgb(2, Color.Black);
[Description("窗体阴影的宽度。默认值:3")]
public int TextShadowWidth { get; set; } = 3;
[Description("最小窗体宽大小。")]
public int MinFormSizeWidth { get; set; } = 50;
[Description("最小窗体高大小。")]
public int MinFormSizeHeight { get; set; } = 30;
[Browsable(false)]
[Description("返回窗体关闭系统按钮所在的坐标矩形")]
public Rectangle CloseBoxRect
{
get { return SystemButtonManager.SystemButtonArray[0].LocationRect; }
}
[Browsable(false)]
[Description("返回窗体最大化或者还原系统按钮所在的坐标矩形")]
public Rectangle MaximiziBoxRect
{
get { return SystemButtonManager.SystemButtonArray[1].LocationRect; }
}
[Browsable(false)]
[Description("返回窗体最小化系统按钮所在的坐标矩形")]
public Rectangle MinimiziBoxRect
{
get { return SystemButtonManager.SystemButtonArray[2].LocationRect; }
}
/// <summary>
/// 窗体 Icon 矩形
/// </summary>
internal Rectangle IconRect
{
get
{
if (base.ShowIcon && base.Icon != null)
{
return new Rectangle(8, 8, SystemInformation.SmallIconSize.Width, SystemInformation.SmallIconSize.Width);
}
return Rectangle.Empty;
}
}
/// <summary>
/// 窗体标题矩形
/// </summary>
internal Rectangle TextRect
{
get
{
if (base.Text.Length != 0)
{
return new Rectangle(IconRect.Right + 5, 8, Width - (8 + IconRect.Width + 2), TextFont.Height);
}
return Rectangle.Empty;
}
}
internal SystemButtonManager SystemButtonManager
{
get
{
if (_systemButtonManager == null)
{
_systemButtonManager = new SystemButtonManager(this);
}
return _systemButtonManager;
}
}
#endregion
#region 继承
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
if (!DesignMode)
{
if (MaximizeBox) { cp.Style |= (int)WindowStyle.WS_MAXIMIZEBOX; }
if (MinimizeBox) { cp.Style |= (int)WindowStyle.WS_MINIMIZEBOX; }
cp.ExStyle |= (int)WindowStyle.WS_CLIPCHILDREN; //防止因窗体控件太多出现闪烁
cp.ClassStyle |= (int)ClassStyle.CS_DropSHADOW; //实现窗体边框阴影效果
}
return cp;
}
}
protected override void OnCreateControl()
{
base.OnCreateControl();
RenderHelper.SetFormRoundRectRgn(this, Radius);
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
UpdateSystemButtonRect();
}
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
if (this.Width <= MinFormSizeWidth) { this.Width = MinFormSizeWidth; }
if (this.Height <= MinFormSizeHeight) { this.Height = MinFormSizeHeight; }
RenderHelper.SetFormRoundRectRgn(this, Radius);
//更新 最大化 按钮 图标
UpdateMaxButton();
// 更新 按钮的位置
UpdateSystemButtonRect();
}
/// <summary>
/// 处理Win 系统消息
/// </summary>
/// <param name="m"></param>
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case Win32.WM_ERASEBKGND:
m.Result = IntPtr.Zero;
break;
case Win32.WM_NCHITTEST://这个消息是当鼠标移动或者有鼠标键按下时候发出的
WmNcHitTest(ref m);
break;
default:
base.WndProc(ref m);
break;
}
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
SystemButtonManager.ProcessMouseOperate(e.Location, MouseOperate.Move);
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
if (e.Button == MouseButtons.Left)
{
SystemButtonManager.ProcessMouseOperate(e.Location, MouseOperate.Down);
}
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
if (e.Button == MouseButtons.Left)
{
SystemButtonManager.ProcessMouseOperate(e.Location, MouseOperate.Up);
}
}
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
SystemButtonManager.ProcessMouseOperate(Point.Empty, MouseOperate.Leave);
}
/// <summary>
/// 重写控件的 OnPaint 事件
/// </summary>
/// <param name="e"></param>
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.CompositingQuality = CompositingQuality.Default;
e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
//绘制 背景图片
if (BackgroundImage != null)
{
switch (BackgroundImageLayout)
{
case ImageLayout.Stretch:
case ImageLayout.Zoom:
e.Graphics.DrawImage(BackgroundImage, ClientRectangle, new Rectangle(0, 0, BackgroundImage.Width, BackgroundImage.Height), GraphicsUnit.Pixel);
break;
case ImageLayout.Center:
case ImageLayout.None:
case ImageLayout.Tile:
e.Graphics.DrawImage(BackgroundImage, ClientRectangle, ClientRectangle, GraphicsUnit.Pixel);
break;
}
}
//绘制窗体主体部分白色透明层
RenderHelper.DrawFromAlphaMainPart(this, e.Graphics);
//绘制 边缘
RenderHelper.DrawFormFringe(this, e.Graphics, RenderHelper.GetImageFormResourceStream("Control.Form.Res.fringe_bkg.png"), Radius);
//绘制 系统按钮
SystemButtonManager.DrawSystemButtons(e.Graphics);
//绘制 icon
if (Icon != null && ShowIcon) { e.Graphics.DrawIcon(Icon, IconRect); }
//绘制文本
if (Text.Length != 0)
{
if (TextWithShadow)
{
using (Image textImg = RenderHelper.GetStringImgWithShadowEffect(Text, TextFont, TextForeColor, TextShadowColor, TextShadowWidth))
{
e.Graphics.DrawImage(textImg, TextRect.Location);
}
}
else
{
TextRenderer.DrawText(e.Graphics, Text, TextFont, TextRect, TextForeColor, TextFormatFlags.SingleLine | TextFormatFlags.EndEllipsis);
}
}
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (disposing)
{
_systemButtonManager?.Dispose();
TextFont?.Dispose();
BackgroundImage?.Dispose();
}
}
#endregion
#region 私有方法
private void InitializeComponent()
{
this.SuspendLayout();
//
// Form
//
this.ClientSize = new System.Drawing.Size(528, 385);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "Form";
this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
this.ResumeLayout(false);
}
private void FormIni()
{
this.MaximumSize = Screen.PrimaryScreen.WorkingArea.Size;
this.DoubleBuffered = true; //设置本窗体双缓冲
SetStyles();
}
private void SetStyles()
{
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
SetStyle(ControlStyles.ResizeRedraw, true);
SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
UpdateStyles();
}
/// <summary>
/// 通过获取系统消息,调整窗体大小。
/// </summary>
/// <param name="m"></param>
private void WmNcHitTest(ref Message m)
{
if (CanResize == true && WindowState != FormWindowState.Maximized)
{
Point mouseLocation = PointToClient(new Point(Cursor.Position.X, Cursor.Position.Y)); //获取鼠标在屏幕的坐标点
//左上斜角
if (mouseLocation.X < 5 && mouseLocation.Y < 5)
{
m.Result = new IntPtr(Win32.HTTOPLEFT);
return;
}
//右上斜角
if (mouseLocation.X > Width - 5 && mouseLocation.Y < 5)
{
m.Result = new IntPtr(Win32.HTTOPRIGHT);
return;
}
//左下斜角
if (mouseLocation.X < 5 && mouseLocation.Y > Height - 5)
{
m.Result = new IntPtr(Win32.HTBOTTOMLEFT);
return;
}
//右下斜角
if (mouseLocation.X > Width - 5 && mouseLocation.Y > Height - 5)
{
m.Result = new IntPtr(Win32.HTBOTTOMRIGHT);
return;
}
//上
if (mouseLocation.Y < 5)
{
m.Result = new IntPtr(Win32.HTTOP);
return;
}
//下
if (mouseLocation.Y > Height - 5)
{
m.Result = new IntPtr(Win32.HTBOTTOM);
return;
}
//左
if (mouseLocation.X < 5)
{
m.Result = new IntPtr(Win32.HTLEFT);
return;
}
//右
if (mouseLocation.X > Width - 5)
{
m.Result = new IntPtr(Win32.HTRIGHT);
return;
}
}
m.Result = new IntPtr(Win32.HTCLIENT);
}
private void UpdateMaxButton()
{
//获取窗体 最大化 状态。
bool isMax = WindowState == FormWindowState.Maximized;
if (isMax) //最大化状态,变换按钮图标、提示
{
SystemButtonManager.SystemButtonArray[1].NormalImg = RenderHelper.GetImageFormResourceStream("Control.Form.Res.SystemButtons.restore_normal.png");
SystemButtonManager.SystemButtonArray[1].HighLightImg = RenderHelper.GetImageFormResourceStream("Control.Form.Res.SystemButtons.restore_highlight.png");
SystemButtonManager.SystemButtonArray[1].DownImg = RenderHelper.GetImageFormResourceStream("Control.Form.Res.SystemButtons.restore_down.png");
SystemButtonManager.SystemButtonArray[1].ToolTip = "还原";
}
else //非最大化状态,变换按钮图标、提示
{
SystemButtonManager.SystemButtonArray[1].NormalImg = RenderHelper.GetImageFormResourceStream("Control.Form.Res.SystemButtons.max_normal.png");
SystemButtonManager.SystemButtonArray[1].HighLightImg = RenderHelper.GetImageFormResourceStream("Control.Form.Res.SystemButtons.max_highlight.png");
SystemButtonManager.SystemButtonArray[1].DownImg = RenderHelper.GetImageFormResourceStream("Control.Form.Res.SystemButtons.max_down.png");
SystemButtonManager.SystemButtonArray[1].ToolTip = "最大化";
}
}
protected void UpdateSystemButtonRect()
{
//更新关闭按钮位置矩形。
Rectangle closeRect = new Rectangle(Width - SystemButtonManager.SystemButtonArray[0].NormalImg.Width,
-1,
SystemButtonManager.SystemButtonArray[0].NormalImg.Width,
SystemButtonManager.SystemButtonArray[0].NormalImg.Height);
SystemButtonManager.SystemButtonArray[0].LocationRect = CloseBox ? closeRect : Rectangle.Empty;
//Max
Rectangle MaxButtonRect = new Rectangle(closeRect.X - SystemButtonManager.SystemButtonArray[1].NormalImg.Width,
-1,
SystemButtonManager.SystemButtonArray[1].NormalImg.Width,
SystemButtonManager.SystemButtonArray[1].NormalImg.Height);
SystemButtonManager.SystemButtonArray[1].LocationRect = MaximizeBox ? MaxButtonRect : Rectangle.Empty;
//Min
//如果没有最小化按钮,则画空白矩形,
if (!MinimizeBox)
{
SystemButtonManager.SystemButtonArray[2].LocationRect = Rectangle.Empty;
return;
}
//如果没有最大化按钮,最小化按钮,就画在关闭按钮旁,
int MinLocation = MaximizeBox ?
MaxButtonRect.X - SystemButtonManager.SystemButtonArray[2].NormalImg.Width :
closeRect.X - SystemButtonManager.SystemButtonArray[2].NormalImg.Width;
Rectangle MinButtonRect = new Rectangle(MinLocation,
-1,
SystemButtonManager.SystemButtonArray[2].NormalImg.Width,
SystemButtonManager.SystemButtonArray[2].NormalImg.Height);
SystemButtonManager.SystemButtonArray[2].LocationRect = MinButtonRect;
}
#endregion
}
}
代码,目前还有比较多的功能BUG。有能力,有兴趣的朋友,可以一起完善,可以加我们的QQ群:7261922