当前位置: 首页 > 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、运行效果
在这里插入图片描述
在这里插入图片描述

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 使用Unity的准备
  • CSP-S 2022 提高级 第一轮 阅读程序(3)
  • webCppCluster
  • Matlab实现MPC算法
  • Elasticsearch:使用 inference API 进行语义搜索
  • 智能对决:提示词攻防中的AI安全博弈
  • [数据集][目标检测]玉米病害检测数据集VOC+YOLO格式6000张4类别
  • 搭建线上虚拟展厅,需要哪些技术?
  • 如何使用useMemo来优化React组件的性能?
  • SpringBoot整合第三方技术
  • PowerBi 柱形图,数据标签无法显示在端外
  • 基于STM32设计的防盗书包(华为云IOT)(216)
  • 大数据Flink(一百一十三):Flink Python写DataStreamAPI作业快速入门
  • PySpark
  • 面向Data+AI时代的数据湖创新与优化(附Iceberg案例)
  • 4. 路由到控制器 - Laravel从零开始教程
  • CSS实用技巧
  • ES6 ...操作符
  • Java 多线程编程之:notify 和 wait 用法
  • JavaScript创建对象的四种方式
  • leetcode386. Lexicographical Numbers
  • LeetCode541. Reverse String II -- 按步长反转字符串
  • node-sass 安装卡在 node scripts/install.js 解决办法
  • node学习系列之简单文件上传
  • Redis的resp协议
  • Spark VS Hadoop:两大大数据分析系统深度解读
  • TiDB 源码阅读系列文章(十)Chunk 和执行框架简介
  • vue学习系列(二)vue-cli
  • 问题之ssh中Host key verification failed的解决
  • 学习笔记TF060:图像语音结合,看图说话
  • 京东物流联手山西图灵打造智能供应链,让阅读更有趣 ...
  • ​ 轻量应用服务器:亚马逊云科技打造全球领先的云计算解决方案
  • ​【原创】基于SSM的酒店预约管理系统(酒店管理系统毕业设计)
  • ​补​充​经​纬​恒​润​一​面​
  • $emit传递多个参数_PPC和MIPS指令集下二进制代码中函数参数个数的识别方法
  • (2022版)一套教程搞定k8s安装到实战 | RBAC
  • (4)(4.6) Triducer
  • (Mac上)使用Python进行matplotlib 画图时,中文显示不出来
  • (poj1.3.2)1791(构造法模拟)
  • (pojstep1.3.1)1017(构造法模拟)
  • (待修改)PyG安装步骤
  • (第61天)多租户架构(CDB/PDB)
  • (附源码)ssm跨平台教学系统 毕业设计 280843
  • (规划)24届春招和25届暑假实习路线准备规划
  • (强烈推荐)移动端音视频从零到上手(下)
  • (十一)JAVA springboot ssm b2b2c多用户商城系统源码:服务网关Zuul高级篇
  • (图文详解)小程序AppID申请以及在Hbuilderx中运行
  • (一)appium-desktop定位元素原理
  • (一)基于IDEA的JAVA基础1
  • (转)JVM内存分配 -Xms128m -Xmx512m -XX:PermSize=128m -XX:MaxPermSize=512m
  • (转载)OpenStack Hacker养成指南
  • .NET DevOps 接入指南 | 1. GitLab 安装
  • .net framework 4.0中如何 输出 form 的name属性。
  • .NET Framework与.NET Framework SDK有什么不同?
  • .net 受管制代码