当前位置: 首页 > news >正文

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

1、自定义控件

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、应用

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、运行效果
在这里插入图片描述
在这里插入图片描述

相关文章:

  • maven项目下使用Jacoco测试覆盖率
  • openSSL 如何降版本
  • 中国篆刻——孙溟㠭浅析碑帖《停雲馆法帖》
  • CocosCreator面试真题详解
  • 【论文阅读】CiteTracker: Correlating Image and Text for Visual Tracking
  • 长芯微国产LS0104电平转换器/电平移位器P2P替代TXS0104
  • How to handle the response OpenAI Text-To-Speech API in Node.js?
  • 数据安全的重要性:守护数字时代的基石
  • Elasticsearch
  • 【CSS】如何写渐变色文字并且有打光效果
  • 华为人工智能重要服务总结
  • HALCON根据需要创建自定义函数
  • 【无人机设计与控制】 四轴飞行器的位移控制
  • 真实案例分享:零售企业如何避免销售数据的无效分析?
  • Android 优雅封装Glide
  • SprinBoot+Vue校园数字化图书馆系统的设计与实现
  • Oracle高级压缩和透明数据加密组合实验
  • 读软件设计的要素05概念的特性
  • 基于STM32设计的智能安防系统(微信小程序)(218)
  • linux学习之线程
  • 美国政府将暂时恢复部分受影响留学生的合法身份,并将制订新标准
  • 当智驾成标配,车企暗战升级|2025上海车展
  • 价格周报|猪价继续回暖:二次育肥热度仍存,对猪价仍有一定支撑
  • 印巴在克什米尔实控线附近小规模交火,巴防长发出“全面战争”警告
  • 儒说︱问世间孝为何物
  • 李良生已任应急管理部党委委员、政治部主任