C#自定义用户控件并加载调用

1、创建一个C#工程生成DLL

a、打开Visual Studio,新建一个C# winform项目。点击“添加新项”,选择“用户控件”,输入文件名“CoverButton.cs”,点击添加完成新建。运行调试后退出,可以在窗体设计界面的工具箱中看到我们刚刚创建的CoverButton控件。这种方式不能为其他项目所使用,无法作为dll文件给其他工程调用。
b、新建C# windows控件库,命名为CoverButton,新建用户控件,文件名为CoverButton.cs,在设计完控件后点击生成,在项目中找到CoverButton.dll文件库。
以自定义按钮为例:

dll库文件分享链接:https://pan.baidu.com/s/12xr0ltWSc0LwuGb0ShqkOw
提取码:wrpi

自定义控件

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;


[DefaultEvent("Click")]
public partial class CoverButton : UserControl
{
    public CoverButton()
    {
        InitializeComponent();

        //设置控件样式
        this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
        this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
        this.SetStyle(ControlStyles.ResizeRedraw, true);

        this.BackColor = Color.Transparent;
    }

    /// 
    /// 定义按钮属性,相当于button的text属性
    /// 
    private string _MyText = "";

    public string MyText
    {
        get { return _MyText; }
        set { _MyText = value; this.Invalidate(); }
    }


    private SolidBrush MouseEnterBrush = new SolidBrush(Color.FromArgb(50, Color.White));

    /// 
    /// 定义鼠标移到按钮上面时按钮的背景颜色
    /// 
    public Color MouseEnterColor
    {
        get { return MouseEnterBrush.Color; }
        set { MouseEnterBrush.Color = value; }
    }

    private bool IsMouseEnter = false;

    protected override void OnMouseEnter(EventArgs e)
    {
        base.OnMouseEnter(e);

        IsMouseEnter = true;
        this.Invalidate();
    }

    protected override void OnMouseLeave(EventArgs e)
    {
        base.OnMouseLeave(e);

        IsMouseEnter = false;
        this.Invalidate();
    }

    public override System.Drawing.Color ForeColor
    {
        get
        {
            return base.ForeColor;
        }
        set
        {
            base.ForeColor = value;
            ForeBrush.Color = value;
            this.Invalidate();
        }
    }

    /// 
    /// 定义背景颜色
    /// 
    public Color BgColor
    {
        get
        {
            return BackBrush.Color;
        }
        set
        {
            BackBrush.Color = value;
            this.Invalidate();
        }
    }

    public Color BorderColor
    {
        get
        {
            return BorderPen.Color;
        }
        set
        {
            BorderPen.Color = value;
            this.Invalidate();
        }
    }


    private Pen BorderPen = new Pen(Color.Silver);
    private SolidBrush BackBrush = new SolidBrush(Color.Transparent);
    private SolidBrush ForeBrush = new SolidBrush(Color.Black);

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
        if (this.Font.Size > 30)
        {
            e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
        }

        if (_UseRoundedRect)
        {
            if (_IsRightFlat)
            {
                using (GraphicsPath path = GetRoundedRectangleNoR(new Rectangle(0, 0, this.Width - 1, this.Height - 1), _RoundedRectRadius))
                {
                    e.Graphics.FillPath(BackBrush, path);

                    if (IsMouseEnter)
                    {
                        e.Graphics.FillPath(MouseEnterBrush, path);
                    }

                    if (_ShowBorder)
                    {
                        e.Graphics.DrawPath(BorderPen, path);
                    }
                }
            }
            else
            {
                using (GraphicsPath path = GetRoundedRectangle(new Rectangle(0, 0, this.Width - 1, this.Height - 1), _RoundedRectRadius))
                {
                    e.Graphics.FillPath(BackBrush, path);

                    if (IsMouseEnter)
                    {
                        e.Graphics.FillPath(MouseEnterBrush, path);
                    }

                    if (_ShowBorder)
                    {
                        e.Graphics.DrawPath(BorderPen, path);
                    }
                }
            }
        }
        else
        {
            e.Graphics.FillRectangle(BackBrush, 0, 0, this.Width - 1, this.Height - 1);

            if (IsMouseEnter)
            {
                e.Graphics.FillRectangle(MouseEnterBrush, 0, 0, this.Width - 1, this.Height - 1);
            }

            if (_ShowBorder)
            {
                e.Graphics.DrawRectangle(BorderPen, 0, 0, this.Width - 1, this.Height - 1);
            }
        }

        if (_IsFoucs)
        {
            float dy = this.Height * 0.4f;
            float dx = dy * 0.4f;

            float x = this.Width - dx;
            float yt = this.Height / 2 - dy / 2;
            float yb = this.Height / 2 + dy / 2;

            PointF[] pts = new PointF[] { new PointF(x, this.Height / 2), new PointF(this.Width, yt), new PointF(this.Width, yb) };

            e.Graphics.FillPolygon(FoucsBrush, pts);
        }


        if (!string.IsNullOrEmpty(_MyText))
        {
            if (_TextAligment == ETextAligment.Center)
            {
                SizeF sf = e.Graphics.MeasureString(_MyText, this.Font);
                e.Graphics.DrawString(_MyText, this.Font, ForeBrush, this.Width / 2 - sf.Width / 2, this.Height / 2 - sf.Height / 2 + 1);
            }
            else if (_TextAligment == ETextAligment.Left)
            {
                SizeF sf = e.Graphics.MeasureString(_MyText, this.Font);
                e.Graphics.DrawString(_MyText, this.Font, ForeBrush, _PadLeft, this.Height / 2 - sf.Height / 2 + 1);
            }
        }
    }

    private int _PadLeft = 3;

    public int PadLeft
    {
        get { return _PadLeft; }
        set { _PadLeft = value; }
    }

    private SolidBrush FoucsBrush = new SolidBrush(Color.White);

    public Color FoucsColor
    {
        get { return FoucsBrush.Color; }
        set { FoucsBrush.Color = value; }
    }

    private bool _UseRoundedRect = false;

    public bool UseRoundedRect
    {
        get { return _UseRoundedRect; }
        set { _UseRoundedRect = value; }
    }

    private int _RoundedRectRadius = 3;

    public int RoundedRectRadius
    {
        get { return _RoundedRectRadius; }
        set { _RoundedRectRadius = value; }
    }

    private bool _ShowBorder = false;

    public bool ShowBorder
    {
        get { return _ShowBorder; }
        set { _ShowBorder = value; }
    }

    private bool _IsFoucs = false;

    public bool IsFoucs
    {
        get { return _IsFoucs; }
        set { _IsFoucs = value; this.Invalidate(); }
    }

    private bool _IsRightFlat = false;

    public bool IsRightFlat
    {
        get { return _IsRightFlat; }
        set { _IsRightFlat = value; }
    }

    private ETextAligment _TextAligment = ETextAligment.Center;

    public ETextAligment TextAligment
    {
        get { return _TextAligment; }
        set { _TextAligment = value; }
    }

    private GraphicsPath GetRoundedRectangle(Rectangle rect, int cornerRadius)
    {
        GraphicsPath roundedRect = new GraphicsPath();

        roundedRect.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);
        roundedRect.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y);
        roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);
        roundedRect.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 2);
        roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);
        roundedRect.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.X + cornerRadius * 2, rect.Bottom);
        roundedRect.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);
        roundedRect.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2);
        roundedRect.CloseFigure();

        return roundedRect;
    }

    private GraphicsPath GetRoundedRectangleNoR(Rectangle rect, int cornerRadius)
    {
        GraphicsPath roundedRect = new GraphicsPath();

        roundedRect.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);
        roundedRect.AddLine(rect.X + cornerRadius, rect.Y, rect.Right, rect.Y);
        roundedRect.AddLine(rect.Right, rect.Y, rect.Right, rect.Y + rect.Height);
        roundedRect.AddLine(rect.Right, rect.Bottom, rect.X + cornerRadius * 2, rect.Bottom);
        roundedRect.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);
        roundedRect.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2);
        roundedRect.CloseFigure();

        return roundedRect;
    }
}

public enum ETextAligment
{
    Center,
    Left
}


然后点击生成解决方案,在项目debug目录下就有生成的dll文件。

2.使用生成的dll

新建->项目->Visual C#->控制台应用程序->Test

在项目中找到References进行导入外部CoverButton.dll库文件,然后在工具箱中再次进行导入CoverButton.dll库文件,之后在工具箱中可以找到CoverButton这个用户自定义的空间。

在需要使用该控件的地方进行使用,调整好控件属性值即可。

你可能感兴趣的:(C#自定义用户控件并加载调用)