少有人走的路

勇哥的工业自动化技术网站

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);

        }
    }
}


  • 评论列表

发表评论:

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

«    2025年12月    »
1234567
891011121314
15161718192021
22232425262728
293031
控制面板
您好,欢迎到访网站!
  查看权限
网站分类
搜索
最新留言
文章归档
网站收藏
友情链接

Powered By Z-BlogPHP 1.7.3

Copyright www.skcircle.com Rights Reserved.

鄂ICP备18008319号


站长QQ:496103864 微信:abc496103864