C#自定义控件的放置与拖动

1、自定义控件

csharp 复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PartApp.Uc
{
    public class And:Control
    {
        private bool dragging = false;
        private Point dragCursorPoint;
        private Point dragFormPoint;
        public And()
        {
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            this.MouseUp += And_MouseUp;
            this.MouseMove += And_MouseMove;
            this.MouseDown += And_MouseDown;
        }

        private void And_MouseDown(object sender, MouseEventArgs e)
        {
            dragging = true;
            dragCursorPoint = Cursor.Position;
            dragFormPoint = this.Location;
        }

        private void And_MouseMove(object sender, MouseEventArgs e)
        {
            if (dragging)
            {
                Point dif = Point.Subtract(Cursor.Position, new Size(dragCursorPoint));
                this.Location = Point.Add(dragFormPoint, new Size(dif));
            }
        }

        private void And_MouseUp(object sender, MouseEventArgs e)
        {
            dragging = false;
        }

        private double length = 40;
        [Browsable(true)]
        [Category("Pin")]
        [Description("引脚长度")]
        public double Length
        {
            get { return length; }
            set { length = value; }
        }

        private Size sensitive = new Size(10, 10);
        [Browsable(true)]
        [Category("Pin")]
        [Description("感应区")]
        public Size Sensitive
        {
            get { return sensitive; }
            set { sensitive = value; }
        }
        //边框线宽
        //框内字体类型
        //框内字体大小

        //引脚字体类型
        //引脚字体大小
        //文字缩进

        protected override void OnPaint(PaintEventArgs e)
        {
            Width = 80; Height = 50;
            int indentation = 2;

            Graphics g = e.Graphics;
            g.SmoothingMode = SmoothingMode.HighQuality;
            int frameTop = Height / 10; //边框上边距
            int frameLeft = Sensitive.Width;
            g.DrawRectangle(new Pen(Color.Black, 5), frameLeft, frameTop, Width-2* frameLeft, Height-2* frameTop);
            g.FillRectangle(new SolidBrush(Color.AliceBlue), frameLeft, frameTop, Width - 2 * frameLeft, Height-2* frameTop);
            Font font = new Font("Arial", 10, FontStyle.Bold);
            SizeF size = g.MeasureString("&",font);
            g.DrawString("&", font, new SolidBrush(Color.Blue), (Width-size.Width)/2, (Height-size.Height)/2);

            int interval = Height / 5;
            int currentInterval = interval;
            font = new Font("Arial", 9, FontStyle.Regular);
            g.FillRectangle(new SolidBrush(Color.Blue), 0, currentInterval, Sensitive.Width, Sensitive.Height);
            g.DrawString("I1", font, new SolidBrush(Color.Blue), Sensitive.Width + indentation, currentInterval);

            currentInterval = 3*interval;
            g.FillRectangle(new SolidBrush(Color.Blue), 0, currentInterval, Sensitive.Width, Sensitive.Height);
            g.DrawString("I1", font, new SolidBrush(Color.Blue), Sensitive.Width+ indentation, currentInterval);

            currentInterval = 2 * interval;
            g.FillRectangle(new SolidBrush(Color.Blue), Width- frameLeft, currentInterval, Sensitive.Width, Sensitive.Height);
            size = g.MeasureString("O1", font);
            g.DrawString("O1", font, new SolidBrush(Color.Blue), Width - Sensitive.Width- indentation-size.Width, currentInterval);
        }
    }
}

2、应用

csharp 复制代码
using PartApp.Uc;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PartApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            

        }
        string currentPart = "";
        private void button1_Click(object sender, EventArgs e)
        {
            currentPart = "Button";
        }
        
        private void panel1_MouseClick(object sender, MouseEventArgs e)
        {
            if(e.Button == MouseButtons.Left)
            {
                if (currentPart != "")
                {
                    And and = new And();
                    and.Location = e.Location;
                    and.Sensitive = new System.Drawing.Size(10, 10);
                    and.Size = new System.Drawing.Size(80, 50);
                    this.panel1.Controls.Add(and);
                    currentPart = "";
                }
            }
        }
    }
}

3、运行效果

在这里插入图片描述

相关推荐
爱上语文几秒前
Springboot的三层架构
java·开发语言·spring boot·后端·spring
编程零零七2 小时前
Python数据分析工具(三):pymssql的用法
开发语言·前端·数据库·python·oracle·数据分析·pymssql
2401_858286113 小时前
52.【C语言】 字符函数和字符串函数(strcat函数)
c语言·开发语言
铁松溜达py3 小时前
编译器/工具链环境:GCC vs LLVM/Clang,MSVCRT vs UCRT
开发语言·网络
everyStudy3 小时前
JavaScript如何判断输入的是空格
开发语言·javascript·ecmascript
C-SDN花园GGbond5 小时前
【探索数据结构与算法】插入排序:原理、实现与分析(图文详解)
c语言·开发语言·数据结构·排序算法
迷迭所归处6 小时前
C++ —— 关于vector
开发语言·c++·算法
架构文摘JGWZ6 小时前
Java 23 的12 个新特性!!
java·开发语言·学习
leon6256 小时前
优化算法(一)—遗传算法(Genetic Algorithm)附MATLAB程序
开发语言·算法·matlab
锦亦之22337 小时前
QT+OSG+OSG-earth如何在窗口显示一个地球
开发语言·qt