C#之用户自定义控件

一、新建用户自定义控件

如下图所示,想通过LED的点击来实现亮和灭使用去控制下位机。

LED亮:

1100486-20180122093525444-1517763664.png

LED灭:

1100486-20180122093543444-1099852845.png

首先新建一个用户控件类,如下图所示步骤:

1100486-20180122101637694-366428392.png

1100486-20180122103004662-233652914.png

在资源中,添加现有文件中加入图片

1100486-20180122103148381-52105554.png

加入的图片可以在Resources中看到列表

1100486-20180122130817725-1420206624.png

编译成功后,在工具箱中看到新建出来的用户控件:

1100486-20180122103538365-1544035094.png

二、新建用户控件的源码及注释

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TB562
{
    public partial class LedControl : UserControl
    {
        public LedControl()
        {
            InitializeComponent();

            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);        //禁止擦除背景

            this.SetStyle(ControlStyles.DoubleBuffer, true);                //启用双缓冲,双缓冲为了解决控件刷新闪烁的问题

            this.SetStyle(ControlStyles.ResizeRedraw, true);                //调整大小时重绘

            this.SetStyle(ControlStyles.Selectable, true);                  //控件可以接收焦点

            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);//透明效果

            this.SetStyle(ControlStyles.UserPaint, true);                   //自绘

            this.BackColor = Color.Transparent;                             //获取系统定义的颜色

            this.Cursor = Cursors.Hand;                                     //表达鼠标移动到控件上面会变成手势光标

            this.Size = new Size(87, 27);                                   //自定义控件的大小尺寸
        }
        //定义一个公共属性
        bool isCheck = false;
        /// <summary>
        /// 是否选中
        /// </summary>
        public bool Checked
        {
            set { isCheck = value; }                    
            get { return isCheck; }
        }
        public enum CheckStyle
        {
            style1,
            style2,
            style3,
            style4,
        };
        CheckStyle checkStyle = CheckStyle.style1;//定义多种控件样式选择
        /// <summary>
        /// 样式
        /// </summary>
        public CheckStyle CheckStyleX
        {
            set { checkStyle = value; } //
            get { return checkStyle; }
        }
        public event EventHandler CheckChanged;
        //鼠标点击的触发事件
        private void LedControl_Click(object sender, EventArgs e)
        {
            isCheck = !isCheck;
            this.Invalidate();                              //Invalidate将控件标记为重绘,触发重写OnPaint
        }
        protected override void OnPaint(PaintEventArgs e)   //重写控件的事件
        {
            Bitmap bitMapOn = null;
            Bitmap bitMapOff = null;
            if (checkStyle == CheckStyle.style1)
            {
                bitMapOn = global::TB562.Properties.Resources.ledopen;              //资源文件中图片的文件名ledopen定义
                bitMapOff = global::TB562.Properties.Resources.ledclose;            //资源文件中图片的文件名ledopen定义
            }
            Graphics g = e.Graphics;  //创建画布
            Rectangle rec = new Rectangle(0, 0, this.Size.Width, this.Size.Height);//矩形的位置和大小
            if (isCheck)
            {
                g.DrawImage(bitMapOn, rec);   //画LED灯开的图像
            }
            else
            {
                g.DrawImage(bitMapOff, rec);  //画LED灯关的图像
            }
        }
    }
}

另外要使用自定义控件前还需要先使用,如下:

                    ledControl1.Enabled = true;
                    ledControl2.Enabled = true;
                    ledControl3.Enabled = true;
                    ledControl4.Enabled = true;
                    ledControl5.Enabled = true;
                    ledControl6.Enabled = true;
                    ledControl7.Enabled = true;
                    ledControl8.Enabled = true;
                    ledControl9.Enabled = true;

三、参考文档

https://www.cnblogs.com/dyllove98/archive/2013/07/05/3174536.html
https://www.cnblogs.com/yelanggu/p/6224587.html
http://blog.sina.com.cn/s/blog_752ca76a0100qjub.html

by 羊羊得亿
2018-01-22 ShenZhen

转载于:https://www.cnblogs.com/yangxuli/p/8328327.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值