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

http://www.lryc.cn/news/431071.html

相关文章:

  • python circular import python循环导入问题
  • kafka集群安装
  • SQL通用语法、SQL分类以及DDL
  • 静态链接和动态链接
  • 构建智能门禁安防系统:树莓派 4B、OpenCV、SQLite 和 MQTT 的应用(代码示例)
  • 基于 Konva 实现Web PPT 编辑器(二)
  • 【开源免费】基于SpringBoot+Vue.JS在线竞拍系统(JAVA毕业设计)
  • Qt TabWidget添加多个窗口,实现分页窗体布局
  • HarmonyOS开发实战( Beta5版)合理使用动画丢帧规范实践
  • 基于BiLSTM-CRF的医学命名实体识别研究(下)模型构建
  • 5.sklearn-朴素贝叶斯算法、决策树、随机森林
  • VMWARE VCENTER6.7 VCSA通过Web5480进行版本升级
  • GIT使用常见问题
  • 内核链表
  • 行空板上YOLO和Mediapipe视频物体检测的测试
  • 【Spring Boot 3】【Web】ProblemDetail
  • 市占率最高的显示器件,TFT_LCD的驱动系统设计--Part 1
  • Linux基础 -- 获取CPU负载信息
  • Django 中的用户界面 - 创建速度计算器
  • spring security 如何解决跨域的
  • 日志系统前置知识
  • 【Spring Boot 3】【Web】全局异常处理
  • Dcoker 运行es
  • 7系列FPGA HR/HP I/O区别
  • sqli-labs靶场通关攻略(五十一到六十关)
  • c语言中的动态内存管理
  • 生信机器学习入门4 - scikit-learn训练逻辑回归(LR)模型和支持向量机(SVM)模型
  • COD论文笔记 Adaptive Guidance Learning for Camouflaged Object Detection
  • 9.5LeetCode
  • 数据仓库系列13:增量更新和全量更新有什么区别,如何选择?