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

news2024/9/20 1:07:41

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2108852.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【mysql】mysql修改sql_mode之后无法启动

现象:修改后mysql无法启动,不报错 原因:MySQL在8以后sql_mode已经取消了NO_AUTO_CREATE_USER这个关键字。去掉这个关键字后,启动就可以了 修改前: sql_modeSTRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR…

jenkins 工具使用

使用方式 替代手动,自动化拉取、集成、构建、测试;是CI/CD持续集成、持续部署主流开发模式中重要的环节;必须组件 jenkins-gitlab,代码公共仓库服务器(至少6G内存);jenkins-server,…

查看当前主机的硬盘是固态硬盘还是机械硬盘

windows主机下查看硬盘类型方法: 打开dos界面,输入 powershell进入powershell界面 在PowerShell窗口中,输入 Get-PhysicalDisk 命令并按回车。 发现MediaType下面的值为HDD,即为机械硬盘,如果是固态硬盘,则为SSD

什么是CDN及其如何影响SEO?

有没有想过,为什么你的网站在谷歌搜索结果的后几页徘徊,即使你已经优化了每一个网页? 有时候, 慢速的网站性能可能是罪魁祸首。 如果这个问题引起了你的共鸣,那么你可能想要探索一下内容分发网络(Content…

Android平台通过RTSP服务实现摄像头麦克风共享

技术背景 前些年,我们在完成Android平台RTMP直播推送模块后,遇到这样的技术需求,好多开发者希望在Android平台,实现摄像头和麦克风音视频数据采集编码打包后,对外提供RTSP(Real Time Streaming Protocol&a…

【DEV工具-IDEA】新建springboot项目,无法选择java8?

问题:新建springboot项目,无法选择java8。 #解决: 替换为 :https://start.aliyun.com

基于蜣螂优化最小二乘支持向量机的数据分类预测Matlab程序DBO-LSSVM 多特征输入多类别输出 含基础程序

基于蜣螂优化最小二乘支持向量机的数据分类预测Matlab程序DBO-LSSVM 多特征输入多类别输出 含基础程序 文章目录 一、基本原理DBO(Dung Beetle Optimization)算法原理LSSVM(Least Squares Support Vector Machine)模型原理DBO-LSS…

【C#跨平台开发详解】C#跨平台开发技术之.NET Core基础学习及快速入门

1. C#与.NET的发展历程 C#是由Microsoft开发的现代编程语言,最初伴随着.NET Framework发布。随着技术的进步,特别是针对跨平台开发的需求,Microsoft推出了.NET Core,这是一个开源且跨平台的框架,支持Windows、macOS和…

微信小程序手写签名

微信小程序手写签名组件 该组件基于signature_pad封装,signature_pad本身是web端的插件,此处将插件代码修改为小程序端可用。 signature_pad.js /*!* Signature Pad v5.0.3 | https://github.com/szimek/signature_pad* (c) 2024 Szymon Nowak | Releas…

Windows10 安全加固之禁止光驱、U盘等自动播放

在使用Windows10系统的电脑中插入插入光盘或者U盘时,默认是自动播放的,这样会引入一些可能不安全的因素。因此,为了系统安全,有必要禁止光驱、U盘等自动播放。具体方法如下: 方法一:通过设置页面关闭 第1步:单击win10系统的“开始”菜单->“设置”,打开“windows设…

算法:区间dp

文章目录 一、适用场景二、基本思路步骤时间复杂度: 三、例题 区间动态规划(Interval DP)是一种用于解决某些需要处理区间或子段问题的动态规划方法,特别适合于问题的解可以通过子区间的解进行组合的情况。该方法的核心思想是在子…

pico手柄和人物模型手部旋转同步,实现手柄控制手臂手部位置移动、手部旋转和手指的操作了

这里的主要内容就是下述代码; // 获取左手控制器的旋转(四元数表示)Quaternion aRotationQuaternion leftHandController.rotation;// 计算旋转差值(四元数表示)Quaternion rotationDifference Quaternion.Euler(0, …

Qt创建项目及相关问题

文章目录 1. Qt项目创建2. 认识项目代码main.cppwidget.hwidget.cppwidget.ui.pro构建过程生成的中间文件 3. Hello World程序图形化方式创建代码方式创建内存泄漏问题编辑框方式创建按钮方式创建 4. 对象树引入对象树原因对象树自动释放对象实验演示 5. 乱码问题 1. Qt项目创建…

Databend 产品月报(2024年8月)

很高兴为您带来 Databend 2024 年 8 月的最新更新、新功能和改进!我们希望这些增强功能对您有所帮助,并期待您的反馈。 Kafka Connect Sink Connector 插件 我们推出了一种将 Kafka 连接到 Databend 的新方式:databend-kafka-connect&#…

2023Idea版本无法下载通义灵码插件以及无法登录问题

进入下载插件处 在插件主页安装时,通常会根据当前自己访问的IDEA版本推荐合适的插件版本,所以最终会安装成功 idea中通义灵码插件无法登录问题

书客、松下、飞利浦护眼台灯怎么样?测评寻找护眼台灯天花板!

大家好,我是专注在护眼领域的一名评测师,长期以来,我致力于探索并体验各类能保护视力健康的护眼产品。今天,我来和大家分享我对护眼台灯的深入评测。护眼台灯作为日常学习生活的一部分,视觉体验的好坏往往取决于所选用…

Pygame中获取鼠标按键状态的方法

在《Pygame中获取鼠标位置的方法》中提到,可以通过鼠标事件和mouse模块中的函数获取鼠标位置,这两种方法同样适用于获取鼠标按键状态。 1 通过鼠标点击事件获取鼠标按键状态 通过鼠标点击事件获取鼠标按键状态的代码如图1所示。 图1 鼠标点击事件获取鼠…

HRGraph: 利用大型语言模型(LLMs)构建基于信息传播的HR数据知识图谱与职位推荐

知识图谱(KGs)作为语义网络,在管理不同领域复杂互联数据方面表现出极高的有效性,通过提供统一、上下文化和结构化的表示,具有灵活性,能够轻松适应不断发展的知识。在处理复杂的人力资源(HR&…

RP2040 C SDK RTC功能使用

RP2040 C SDK RTC功能使用 📍《RP2040 C SDK串口功能使用》🥕RP2040 RTC API官方文档说明:https://www.raspberrypi.com/documentation/pico-sdk/hardware.html#group_hardware_rtc🥕官方例程参考:https://github.com/…

su root 提示 Permission denied

今天在某机器上新建了一个test账号,然后使用su root时,居然提示我没有权限,具体如下所示: [testlocalhost ~]$ su root Password: su: Permission denied [testlocalhost ~]$我确定密码是对的,试了好几次,…