C# gdi自定义控件:阀门

勇哥注:

阀门控件可用于组态页面,构建自动化逻辑的控制面板。

基本套路就是用GDI代码进行绘制。



下图是组态页面:

image.png


下图是阀门控件的绘制效果。

它有横向与竖向两种形态。

image.png


注意多边形的5个点如下:

image.png


其它没什么好讲的。


源码:

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

namespace 绘制阀门控件
{

    public enum DirectionEnum
    {
        横向=0,竖向
    }


    internal class FamenControl:Button
    {

        private Color myColor = Color.Yellow;
        [Description("阀门颜色"),Category("自定义")]
        public Color MyColor
        {
            get { return myColor; }
            set { myColor = value; }
        }

        private DirectionEnum myDirection;
        [Description("阀门方向"),Category("自定义")]
        public DirectionEnum MyDirection
        {
            get { return myDirection; }
            set { myDirection = value; }
        }

        Point p1, p2, p3, p4, p5,p7,p8;

        protected override void OnPaint(PaintEventArgs pevent)
        {
            this.Text = "";
            switch(MyDirection)
            {
                case DirectionEnum.横向:
                    p1 = new Point(this.Width / 4, this.Height / 3);
                    p2 = new Point(this.Width / 4, this.Height / 3 * 2);
                    p3 = new Point(this.Width / 2, this.Height / 2);
                    p4 = new Point(this.Width / 4 * 3, this.Height / 3);
                    p5 = new Point(this.Width / 4 * 3, this.Height / 3 * 2);
                    p7 = new Point(0, this.Height/ 2);
                    p8 = new Point(this.Width, this.Height / 2);
                    break;
                case DirectionEnum.竖向:
                    p1 = new Point(this.Width / 3, this.Height / 4);
                    p2 = new Point(this.Width / 3*2, this.Height / 4);
                    p3 = new Point(this.Width / 2, this.Height / 2);
                    p4 = new Point(this.Width / 3, this.Height /4*3);
                    p5 = new Point(this.Width / 3 * 2, this.Height / 4 * 3);
                    p7 = new Point(this.Width/2, this.Height);
                    p8 = new Point(this.Width/2, 0);
                    break;
            }

            var ptArr = new Point[] { p1, p2, p3, p4, p5};
            base.OnPaint(pevent);
            Graphics g = pevent.Graphics;
           
            g.DrawPolygon(new Pen(MyColor), ptArr);
            g.FillPolygon(new SolidBrush(MyColor), ptArr);
            g.DrawLine(new Pen(myColor,3), p7, p8);

        }
    }
}


本文出自勇哥的网站《少有人走的路》wwww.skcircle.com,转载请注明出处!讨论可扫码加群:
本帖最后由 勇哥,很想停止 于 2023-05-23 21:35:11 编辑
  • 评论列表:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

会员中心
搜索
«    2024年4月    »
1234567
891011121314
15161718192021
22232425262728
2930
网站分类
标签列表
最新留言
    热门文章 | 热评文章 | 随机文章
文章归档
友情链接
  • 订阅本站的 RSS 2.0 新闻聚合
  • 扫描加本站机器视觉QQ群,验证答案为:halcon勇哥的机器视觉
  • 点击查阅微信群二维码
  • 扫描加勇哥的非标自动化群,验证答案:C#/C++/VB勇哥的非标自动化群
  • 扫描加站长微信:站长微信:abc496103864
  • 扫描加站长QQ:
  • 扫描赞赏本站:
  • 留言板:

Powered By Z-BlogPHP 1.7.2

Copyright Your skcircle.com Rights Reserved.

鄂ICP备18008319号


站长QQ:496103864 微信:abc496103864