官网

http://www.hzhcontrols.com

前提

入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。

GitHub:https://github.com/kwwwvagaa/NetWinformControl

码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git

如果觉得写的还行,请点个 star 支持一下吧

欢迎前来交流探讨: 企鹅群568015492 

目录

https://www.cnblogs.com/bfyx/p/11364884.html

准备工作

自定义的分为控件和窗体2种类型,分别都有一个基类,基类实现公共的大部分工作

开始

首先从基类控件开始吧,

主要实现功能:

  1. 圆角
  2. 边框
  3. 填充颜色

添加一个用户控件,命名为UCControlBase,写入相关属性,包含圆角角度,边框颜色,边框宽度,填充颜色,背景色等

  private bool _isRadius = false;

         private int _cornerRadius = ;

         private bool _isShowRect = false;

         private Color _rectColor = Color.FromArgb(, , );

         private int _rectWidth = ;

         private Color _fillColor = Color.Transparent;
/// <summary>
/// 是否圆角
/// </summary>
[Description("是否圆角"), Category("自定义")]
public bool IsRadius
{
get
{
return this._isRadius;
}
set
{
this._isRadius = value;
}
}
//圆角角度
[Description("圆角角度"), Category("自定义")]
public int ConerRadius
{
get
{
return this._cornerRadius;
}
set
{
this._cornerRadius = value;
}
} /// <summary>
/// 是否显示边框
/// </summary>
[Description("是否显示边框"), Category("自定义")]
public bool IsShowRect
{
get
{
return this._isShowRect;
}
set
{
this._isShowRect = value;
}
}
/// <summary>
/// 边框颜色
/// </summary>
[Description("边框颜色"), Category("自定义")]
public Color RectColor
{
get
{
return this._rectColor;
}
set
{
this._rectColor = value;
this.Refresh();
}
}
/// <summary>
/// 边框宽度
/// </summary>
[Description("边框宽度"), Category("自定义")]
public int RectWidth
{
get
{
return this._rectWidth;
}
set
{
this._rectWidth = value;
}
}
/// <summary>
/// 当使用边框时填充颜色,当值为背景色或透明色或空值则不填充
/// </summary>
[Description("当使用边框时填充颜色,当值为背景色或透明色或空值则不填充"), Category("自定义")]
public Color FillColor
{
get
{
return this._fillColor;
}
set
{
this._fillColor = value;
}
}

需要做的就是重写OnPaint,来画边框以及填充颜色

 protected override void OnPaint(PaintEventArgs e)
{
if (this.Visible)
{
if (this._isRadius)
{
this.SetWindowRegion();
}
if (this._isShowRect)
{
Color rectColor = this._rectColor;
Pen pen = new Pen(rectColor, (float)this._rectWidth);
Rectangle clientRectangle = base.ClientRectangle;
GraphicsPath graphicsPath = new GraphicsPath();
graphicsPath.AddArc(, , _cornerRadius, _cornerRadius, 180f, 90f);
graphicsPath.AddArc(clientRectangle.Width - _cornerRadius - , , _cornerRadius, _cornerRadius, 270f, 90f);
graphicsPath.AddArc(clientRectangle.Width - _cornerRadius - , clientRectangle.Height - _cornerRadius - , _cornerRadius, _cornerRadius, 0f, 90f);
graphicsPath.AddArc(, clientRectangle.Height - _cornerRadius - , _cornerRadius, _cornerRadius, 90f, 90f);
graphicsPath.CloseFigure();
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
if (_fillColor != Color.Empty && _fillColor != Color.Transparent && _fillColor != this.BackColor)
e.Graphics.FillPath(new SolidBrush(this._fillColor), graphicsPath);
e.Graphics.DrawPath(pen, graphicsPath);
}
}
base.OnPaint(e);
} private void SetWindowRegion()
{
GraphicsPath path = new GraphicsPath();
Rectangle rect = new Rectangle(-, -, base.Width + , base.Height);
path = this.GetRoundedRectPath(rect, this._cornerRadius);
base.Region = new Region(path);
} private GraphicsPath GetRoundedRectPath(Rectangle rect, int radius)
{
Rectangle rect2 = new Rectangle(rect.Location, new Size(radius, radius));
GraphicsPath graphicsPath = new GraphicsPath();
graphicsPath.AddArc(rect2, 180f, 90f);//左上角
rect2.X = rect.Right - radius;
graphicsPath.AddArc(rect2, 270f, 90f);//右上角
rect2.Y = rect.Bottom - radius;
rect2.Width += ;
rect2.Height += ;
graphicsPath.AddArc(rect2, 360f, 90f);//右下角
rect2.X = rect.Left;
graphicsPath.AddArc(rect2, 90f, 90f);//左下角
graphicsPath.CloseFigure();
return graphicsPath;
}

至此基类控件就完成了,下面是完成代码

 // 版权所有  黄正辉  交流群:568015492   QQ:623128629
// 文件名称:UCControlBase.cs
// 创建日期:2019-08-15 16:04:12
// 功能描述:ControlBase
// 项目地址:https://gitee.com/kwwwvagaa/net_winform_custom_control
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D; namespace HZH_Controls.Controls
{
[Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(System.ComponentModel.Design.IDesigner))]
public partial class UCControlBase : UserControl, IContainerControl
{
private bool _isRadius = false; private int _cornerRadius = ; private bool _isShowRect = false; private Color _rectColor = Color.FromArgb(, , ); private int _rectWidth = ; private Color _fillColor = Color.Transparent;
/// <summary>
/// 是否圆角
/// </summary>
[Description("是否圆角"), Category("自定义")]
public bool IsRadius
{
get
{
return this._isRadius;
}
set
{
this._isRadius = value;
}
}
//圆角角度
[Description("圆角角度"), Category("自定义")]
public int ConerRadius
{
get
{
return this._cornerRadius;
}
set
{
this._cornerRadius = value;
}
} /// <summary>
/// 是否显示边框
/// </summary>
[Description("是否显示边框"), Category("自定义")]
public bool IsShowRect
{
get
{
return this._isShowRect;
}
set
{
this._isShowRect = value;
}
}
/// <summary>
/// 边框颜色
/// </summary>
[Description("边框颜色"), Category("自定义")]
public Color RectColor
{
get
{
return this._rectColor;
}
set
{
this._rectColor = value;
this.Refresh();
}
}
/// <summary>
/// 边框宽度
/// </summary>
[Description("边框宽度"), Category("自定义")]
public int RectWidth
{
get
{
return this._rectWidth;
}
set
{
this._rectWidth = value;
}
}
/// <summary>
/// 当使用边框时填充颜色,当值为背景色或透明色或空值则不填充
/// </summary>
[Description("当使用边框时填充颜色,当值为背景色或透明色或空值则不填充"), Category("自定义")]
public Color FillColor
{
get
{
return this._fillColor;
}
set
{
this._fillColor = value;
}
} public UCControlBase()
{
this.InitializeComponent();
base.SetStyle(ControlStyles.UserPaint, true);
base.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
base.SetStyle(ControlStyles.DoubleBuffer, true);
} protected override void OnPaint(PaintEventArgs e)
{
if (this.Visible)
{
if (this._isRadius)
{
this.SetWindowRegion();
}
if (this._isShowRect)
{
Color rectColor = this._rectColor;
Pen pen = new Pen(rectColor, (float)this._rectWidth);
Rectangle clientRectangle = base.ClientRectangle;
GraphicsPath graphicsPath = new GraphicsPath();
graphicsPath.AddArc(, , _cornerRadius, _cornerRadius, 180f, 90f);
graphicsPath.AddArc(clientRectangle.Width - _cornerRadius - , , _cornerRadius, _cornerRadius, 270f, 90f);
graphicsPath.AddArc(clientRectangle.Width - _cornerRadius - , clientRectangle.Height - _cornerRadius - , _cornerRadius, _cornerRadius, 0f, 90f);
graphicsPath.AddArc(, clientRectangle.Height - _cornerRadius - , _cornerRadius, _cornerRadius, 90f, 90f);
graphicsPath.CloseFigure();
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
if (_fillColor != Color.Empty && _fillColor != Color.Transparent && _fillColor != this.BackColor)
e.Graphics.FillPath(new SolidBrush(this._fillColor), graphicsPath);
e.Graphics.DrawPath(pen, graphicsPath);
}
}
base.OnPaint(e);
} private void SetWindowRegion()
{
GraphicsPath path = new GraphicsPath();
Rectangle rect = new Rectangle(-, -, base.Width + , base.Height);
path = this.GetRoundedRectPath(rect, this._cornerRadius);
base.Region = new Region(path);
} private GraphicsPath GetRoundedRectPath(Rectangle rect, int radius)
{
Rectangle rect2 = new Rectangle(rect.Location, new Size(radius, radius));
GraphicsPath graphicsPath = new GraphicsPath();
graphicsPath.AddArc(rect2, 180f, 90f);//左上角
rect2.X = rect.Right - radius;
graphicsPath.AddArc(rect2, 270f, 90f);//右上角
rect2.Y = rect.Bottom - radius;
rect2.Width += ;
rect2.Height += ;
graphicsPath.AddArc(rect2, 360f, 90f);//右下角
rect2.X = rect.Left;
graphicsPath.AddArc(rect2, 90f, 90f);//左下角
graphicsPath.CloseFigure();
return graphicsPath;
} protected override void WndProc(ref Message m)
{
if (m.Msg != )
{
base.WndProc(ref m);
}
} }
}
  partial class UCControlBase
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region 组件设计器生成的代码 /// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
base.SuspendLayout();
base.AutoScaleDimensions = new SizeF(9f, 20f);
base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
this.DoubleBuffered = true;
this.Font = new Font("微软雅黑", 15f, FontStyle.Regular, GraphicsUnit.Pixel);
base.Margin = new Padding(, , , );
base.Name = "UCBase";
base.Size = new Size(, );
base.ResumeLayout(false);
} #endregion
}

用处及效果

用处:你可以把它当作一个panel来用,比如需要包裹一些控件并显示一个圆角边框的时候,你应该想到用这个控件

效果图:其实就是一个圆角边框的面板

最后的话

如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星星吧

(一)c#Winform自定义控件-基类控件的更多相关文章

  1. winform 自定义控件:半透明Loading控件

    winform  自定义控件:半透明Loading控件 by wgscd date:2015-05-05 效果: using System;using System.Drawing;using Sys ...

  2. (十七)c#Winform自定义控件-基类窗体

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  3. C# 自定义控件VS用户控件

    1 自定义控件与用户控件区别 WinForm中, 用户控件(User Control):继承自 UserControl,主要用于开发 Container 控件,Container控件可以添加其他Con ...

  4. 自定义控件VS用户控件

    自定义控件VS用户控件 2015-06-16 1 自定义控件与用户控件区别 WinForm中, 用户控件(User Control):继承自 UserControl,主要用于开发 Container ...

  5. kettle系列-[KettleUtil]kettle插件,类似kettle的自定义java类控件

    该kettle插件功能类似kettle现有的定义java类插件,自定java类插件主要是支持在kettle中直接编写java代码实现自定特殊功能,而本控件主要是将自定义代码转移到jar包,就是说自定义 ...

  6. 富客户端 wpf, Winform 多线程更新UI控件

    前言 在富客户端的app中,如果在主线程中运行一些长时间的任务,那么应用程序的UI就不能正常相应.因为主线程要负责消息循环,相应鼠标等事件还有展现UI. 因此我们可以开启一个线程来格外处理需要长时间的 ...

  7. C# WinForm 跨线程访问控件

    问题出现: 在WinForm 处理多线程访问主线程的控件时候,就会出现如图所示的错误对话框:    解决方案:  方案一:去掉线程访问主线程UI控件的安全检查,使用: Control.CheckFor ...

  8. Winform跨窗体操作控件(使用委托)

    Winform跨窗体操作控件是winform开发中很常见的形式,最常见且简单有效的方式便是使用委托的方式来进行操作,下面我将通过一个小实例来说明如何使用委托跨窗体实现控件操作. 实例介绍:两个窗体,F ...

  9. Android开发技巧——自定义控件之组合控件

    Android开发技巧--自定义控件之组合控件 我准备在接下来一段时间,写一系列有关Android自定义控件的博客,包括如何进行各种自定义,并分享一下我所知道的其中的技巧,注意点等. 还是那句老话,尽 ...

随机推荐

  1. 阿里云体验:安装jdk

    在阿里云的linux服务器上默认是没有安装java环境的,需要自己安装.查了许多资料,发现这篇文章简洁易用.http://www.cnblogs.com/cloudwind/archive/2012/ ...

  2. ASP.NET Core系列(一): .NET Core简介及安装开发环境

    大家都知道Java是跨平台的,.NET因为不具有跨平台的特性,被越来越多的开发者诟病,之前有各种间接的跨平台的方案,比如mono.但是由于各种兼容问题,最终 .NET Core出现了,它可以让程序在W ...

  3. 赛前集训的第二个小总结(OI生涯盛极必衰orNOIP前最后试炼?)+关于学OI目的的思考

    这次只有成绩统计是全的…… 看上去好像还是挺跌宕起伏的啊…… 话说亲爱的教练员又一次食言了,说好的10次就换呢?! 名次表只有前10次的了…… 不得不说前10次还是挺好的,10次考试,7次前十,5次前 ...

  4. ESXI好好研究

    之前几周在公司要搭建一个平台,因为服务器不够用,所以需要要一台服务器上装虚拟机.有人说用ESXI装虚拟机,并且不用装操作系统,我当时还纳闷儿了,不装操作系统,直接装虚机?这里我有点孤陋寡闻了,其实ES ...

  5. [leetcode] 111.Mininum Depth of Binary Tree (Easy)

    原题 寻找二叉树最短深度 这里用了dfs,beat 100%,4ms class Solution { public: int minDepth(TreeNode *root, int minNum ...

  6. UPC Contest RankList – 2019年第二阶段我要变强个人训练赛第十六场

    E: 飞碟解除器 •题目描述 wjyyy在玩跑跑卡丁车的时候,获得了一个飞碟解除器,这样他就可以免受飞碟的减速干扰了.飞碟解除器每秒末都会攻击一次飞碟,但每次只有p/q的概率成功攻击飞碟.当飞碟被成功 ...

  7. 0 推荐系统——CB和CF

    一.基于内容的推荐(CB,Content-based Recommendations): 基于内容的推荐CB应该算是最早被使用的推荐方法,它根据用户过去喜欢的产品(本文统称为 item),为用户推荐和 ...

  8. Angular JS 中的内置方法之filter

    通过过滤器可以实现很多数据格式化的功能 常用方法形如{{ data | uppercase}} 或者是{{ 123.456 | number:2 }} 也可以通过在控制器中注入$filter来实现功能 ...

  9. 通过ping命令了解三层转发流程

    ping命令:因特网包探索器.本文主要通过路由器两端不同网段PC互ping来讲解三层转发流程. 例子:PC-A是如何 ping 通 PC-C 的,有几种情况? 说明:1.在条件1阶段PC-C不会刷新a ...

  10. 【iOS】iOS main() 简介

    C 语言编写的程序,其执行入口都是 main(). 用 Objective-C 语言编写的程序也是这样. main.m 中的代码如下: int main(int argc, char * argv[] ...