c#自定义控件流速条

本文记录了使用C#创建自定义控件流速条的过程,包括新建Windows窗体控件库,继承自UserControl,并通过代码实现键盘事件来切换流速条的方向。
摘要由CSDN通过智能技术生成

今天学习了自定义控件,通过查资料实现了流速条自定义控件,现记录如下:

第一步:新建项目,创建windows窗体控件库;

第二步:书写程序;

本次创建的控件继承自UserControl类;

最终效果如图:

具体代码:

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;
using System.Drawing.Drawing2D;


namespace LiuSuTiao
{
    public partial class LiuSuTiao : UserControl
    {
        public LiuSuTiao()
        {
            InitializeComponent();
            //设置绘制对象的相应属性
            this.SetStyle(ControlStyles.UserPaint, true);//由控件而非操作系统绘制自身
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);//如果为true,则忽略窗口消息wm_erasebkgnd以减少闪烁
            //this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);//控件首先绘制到缓冲区而非直接绘制到屏幕
            this.SetStyle(ControlStyles.DoubleBuffer, true);//控件首先绘制到缓冲区,完成后将结果绘制到屏幕
            this.SetStyle(ControlStyles.ResizeRedraw, true);//控件在调整大小时进行重绘
            this.SetStyle(ControlStyles.Selectable, true);
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);

            Timer MyTimer = new Timer();//按用户定义的时间间隔触犯事件
            MyTimer.Enabled = true;
            MyTimer.Interval = 1000;//1秒
            MyTimer.Tick += MyTimer_Tick;
        }

        private void MyTimer_Tick(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            this.Invalidate();//使控件的整个图面无效,并重绘控件
        }

        #region 定义图面、宽度及高度
        private Graphics g;
        private int pic_width;
        private int pic_heigt;
        #endregion
        private bool active;
        [Description("控件是否激活,即流速条是否启动"), Category("自定义属性")]
        public bool Active
        {
            get
            {
                return active;
            }
            set
            {
                active = value;
            }
        }
        bool vertical = false;
        [Description("外部矩形方向"), Category("自定义属性")]
        public bool Vertical
        {
            get { return vertical; }
            set { vertical = value; }
        }


        int neibujuxing_length = 10;
        [Description("内部矩形长度"), Category("自定义属性")]
        public int Neibujuxing_Length
        {
            get { return neibujuxing_length; }
            set { neibujuxing_length = value; }
        }
        double neibujuxing_width = 0.75;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值