zoukankan      html  css  js  c++  java
  • C#之用户自定义控件

    一、新建用户自定义控件

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

    LED亮:

    LED灭:

    首先新建一个用户控件类,名字为LedControl.cs,如下图所示步骤:

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

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

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

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

    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;this.Invalidate();}                 // 用Invalidate()使区域无效就可触发该控件的重画
                       
                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

  • 相关阅读:
    并发编程--锁--锁的理解及分类
    String -- 从源码剖析String类
    Spring 抽象的缓存包 spring-cache
    小白学做菜笔记
    使用Lists.partition切分性能优化
    String--常见面试题
    常用的Linux命令
    去除字符串中的空格
    微信小程序切换选中状态
    微信小程序面试题总结
  • 原文地址:https://www.cnblogs.com/yangxuli/p/8328327.html
Copyright © 2011-2022 走看看