源文件:http://ded.nuaa.edu.cn/download/Windows%20Extended%20Controls.rar 示例代码:http://ded.nuaa.edu.cn/download/WindowsApplication6.rar 最近做一个图象的采集,需要一个图形的选择控件,但是在.net下没有类似vb中的shape控件,所以考虑了自己写一个控件。 下面我将从头创建控件,这个控件主要是用来选择图形的Rectangle,有一下几个属性Color BorderColor:边框颜色,Color BackColor:背景颜色,bool ReSizeble:是否可移动, Rectangle SelectRectangle:选择区域。 打开vs2003(我用的这个版本),新建一个c#控件库,ok,拷贝如下代码到你的代码里。 using System; using System.Collections; using System.ComponentModel; using System.Drawing; using System.Data; using System.Windows.Forms; namespace WindowsExtendedControls { /// <summary> /// 控件 /// </summary> public class ShapeEx : System.Windows.Forms.Control { /// <summary> /// 必需的设计器变量。 /// </summary> /// private Color _BorderColor=new Color(); private Color _BackColor=new Color(); private bool _ReSizeble; private Point _SelfLocation=new Point(); private Point _MouseLocation=new Point(); private int _SelfWidth; private int _SelfHeight; private int _SelectSelctedIndex;//0-8,0:SizeAll private Rectangle _rectLeftSelector=new Rectangle(); private Rectangle _rectTopSelector=new Rectangle(); private Rectangle _rectRightSelector=new Rectangle(); private Rectangle _rectBottomSelector=new Rectangle(); private Rectangle _rectLeftTopSelector=new Rectangle(); private Rectangle _rectRightTopSelector=new Rectangle(); private Rectangle _rectRightBottomSelector=new Rectangle(); private Rectangle _rectLeftBottomSelector=new Rectangle(); private System.ComponentModel.Container components = null; public ShapeEx() { // 该调用是 Windows.Forms 窗体设计器所必需的。 InitializeComponent(); // TODO: 在 InitComponent 调用后添加任何初始化 } [DefaultValue("Black"),Description("边框颜色"),Category("Appearance")] public Color BorderColor { get { // Insert code here. return _BorderColor; } set { _BorderColor=value; this.Invalidate(); } } [DefaultValue("Control"),Description("背景颜色"),Category("Appearance")] public override Color BackColor { get { // Insert code here. return _BackColor; } set { _BackColor=value; this.Invalidate(); } } [DefaultValue(false),Description("运行中控件大小是否可拖拽编辑"),Category("Behavior")] public bool ReSizeble { get { // Insert code here. return _ReSizeble; } set { _ReSizeble=value; this.Invalidate(); } } [Description("控件选择区域"),Category("Behavior")] public Rectangle SelectRectangle { get { Rectangle selectRectangler=new Rectangle(); selectRectangler.X = this.Location.X+7; selectRectangler.Y = this.Location.Y+7; selectRectangler.Height = this.Height-15; selectRectangler.Width = this.Width-15; return selectRectangler; } } protected override void OnPaint(PaintEventArgs pe) { // Calling the base class OnPaint base.OnPaint(pe); ReDrawControl(pe.Graphics); } private void DrawSelector(Graphics graphics) { SolidBrush SelectorPen=new SolidBrush(Color.White); Pen borderPen=new Pen(this._BorderColor,1); try { //实心 PointF[] LeftPoints=getPointF(0,this.Height/2-3,6,6); graphics.FillClosedCurve(SelectorPen, LeftPoints); PointF[] TopPoints=getPointF(this.Width/2-3,0,6,6); graphics.FillClosedCurve(SelectorPen, TopPoints); PointF[] RightPoints=getPointF(this.Width-7,this.Height/2-3,6,6); graphics.FillClosedCurve(SelectorPen, RightPoints); PointF[] BottomPoints=getPointF(this.Width/2-3,this.Height-7,6,6); graphics.FillClosedCurve(SelectorPen, BottomPoints); PointF[] LeftTopPoints=getPointF(0,0,6,6); graphics.FillClosedCurve(SelectorPen, LeftTopPoints); PointF[] RightTopPoints=getPointF(this.Width-7,0,6,6); graphics.FillClosedCurve(SelectorPen, RightTopPoints); PointF[] RightBottomPoints=getPointF(this.Width-7,this.Height-7,6,6); graphics.FillClosedCurve(SelectorPen, RightBottomPoints); PointF[] LeftBottomPoints=getPointF(0,this.Height-7,6,6); graphics.FillClosedCurve(SelectorPen, LeftBottomPoints); //边框 _rectLeftSelector.X = 0; _rectLeftSelector.Y = this.Height/2-3; _rectLeftSelector.Height = 6; _rectLeftSelector.Width = 6; graphics.DrawRectangle(borderPen, _rectLeftSelector); _rectTopSelector.X = this.Width/2-3; _rectTopSelector.Y = 0; _rectTopSelector.Height = 6; _rectTopSelector.Width = 6; graphics.DrawRectangle(borderPen, _rectTopSelector); _rectRightSelector.X = this.Width-7; _rectRightSelector.Y = this.Height/2-3; _rectRightSelector.Height = 6; _rectRightSelector.Width = 6; graphics.DrawRectangle(borderPen, _rectRightSelector); _rectBottomSelector.X = this.Width/2-3; _rectBottomSelector.Y = this.Height-7; _rectBottomSelector.Height = 6; _rectBottomSelector.Width = 6; graphics.DrawRectangle(borderPen, _rectBottomSelector); _rectLeftTopSelector.X=0; _rectLeftTopSelector.Y=0; _rectLeftTopSelector.Width=6; _rectLeftTopSelector.Height=6; graphics.DrawRectangle(borderPen, _rectLeftTopSelector); _rectRightTopSelector.X=this.Width-7; _rectRightTopSelector.Y=0; _rectRightTopSelector.Width=6; _rectRightTopSelector.Height=6; graphics.DrawRectangle(borderPen, _rectRightTopSelector); _rectRightBottomSelector.X=this.Width-7; _rectRightBottomSelector.Y=this.Height-7; _rectRightBottomSelector.Width=6; _rectRightBottomSelector.Height=6; graphics.DrawRectangle(borderPen, _rectRightBottomSelector); _rectLeftBottomSelector.X=0; _rectLeftBottomSelector.Y=this.Height-7; _rectLeftBottomSelector.Width=6; _rectLeftBottomSelector.Height=6; graphics.DrawRectangle(borderPen, _rectLeftBottomSelector); } catch(Exception E) { throw E; } finally { SelectorPen.Dispose(); borderPen.Dispose(); } } private void ReDrawControl(Graphics graphics) { try { //绘制背景 /* graphics.Clear(this._BackColor); SolidBrush backPen=new SolidBrush(this._BackColor); PointF point1 = new PointF(1,1); PointF point2 = new PointF(this.Width-2,1); PointF point3 = new PointF(this.Width-2,this.Height-2); PointF point4 = new PointF(1,this.Height-2); PointF[] points = {point1, point2, point3, point4}; graphics.FillClosedCurve(backPen, points); */ //绘制边框 Rectangle rectBorder=new Rectangle(); Pen borderPen=new Pen(this._BorderColor,1); rectBorder.X = 7; rectBorder.Y = 7; rectBorder.Height = this.Height-15; rectBorder.Width = this.Width-15; graphics.DrawRectangle(borderPen, rectBorder); //绘制编辑框 if (_ReSizeble) { DrawSelector(graphics); } } catch(Exception E) { throw E; } finally { graphics.Dispose(); } } /// <summary> /// 清理所有正在使用的资源。 /// </summary> private PointF[] getPointF(int x,int y,int Width,int Height){ PointF point1 = new PointF(x,y); PointF point2 = new PointF(x+Width,y); PointF point3 = new PointF(x+Width,y+Height); PointF point4 = new PointF(x,y+Height); PointF[] points = {point1, point2, point3, point4}; return points; } protected override void Dispose( bool disposing ) { if( disposing ) { if( components != null ) components.Dispose(); } base.Dispose( disposing ); } #region 组件设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器 /// 修改此方法的内容。 /// </summary> private void InitializeComponent() { components = new System.ComponentModel.Container(); this.Resize+=new EventHandler(ShapeEx_Resize); this.MouseDown+=new MouseEventHandler(ShapeEx_MouseDown); this.MouseMove+=new MouseEventHandler(ShapeEx_MouseMove); this.MouseLeave+=new EventHandler(ShapeEx_MouseLeave); this.MouseUp+=new MouseEventHandler(ShapeEx_MouseUp); this._BorderColor=Color.Black; this._BackColor=Color.FromName("Control"); this._ReSizeble=false; this._SelectSelctedIndex=-1; SetStyle(ControlStyles.SupportsTransparentBackColor, true); } #endregion private void ShapeEx_Resize(object sender, EventArgs e) { if (this.Width<16 || this.Height<16) { this.Width=16; this.Height=16; } this.Invalidate(); } private void ShapeEx_MouseDown(object sender, MouseEventArgs e) { if (_ReSizeble) { if (_rectLeftSelector.Contains(e.X,e.Y) || _rectRightSelector.Contains(e.X,e.Y) || _rectTopSelector.Contains(e.X,e.Y) || _rectBottomSelector.Contains(e.X,e.Y) ||_rectLeftTopSelector.Contains(e.X,e.Y) || _rectRightTopSelector.Contains(e.X,e.Y) || _rectRightBottomSelector.Contains(e.X,e.Y) || _rectLeftBottomSelector.Contains(e.X,e.Y)) { if (_rectLeftTopSelector.Contains(e.X,e.Y) ) { this.Cursor=Cursors.SizeNWSE; this._SelectSelctedIndex=1; } if (_rectTopSelector.Contains(e.X,e.Y) ) { this.Cursor=Cursors.SizeNS; this._SelectSelctedIndex=2; } if (_rectRightTopSelector.Contains(e.X,e.Y) ) { this.Cursor=Cursors.SizeNESW; this._SelectSelctedIndex=3; } if (_rectRightSelector.Contains(e.X,e.Y) ) { this.Cursor=Cursors.SizeWE; this._SelectSelctedIndex=4; } if (_rectRightBottomSelector.Contains(e.X,e.Y) ) { this.Cursor=Cursors.SizeNWSE; this._SelectSelctedIndex=5; } if (_rectBottomSelector.Contains(e.X,e.Y)) { this.Cursor=Cursors.SizeNS; this._SelectSelctedIndex=6; } if (_rectLeftBottomSelector.Contains(e.X,e.Y) ) { this.Cursor=Cursors.SizeNESW; this._SelectSelctedIndex=7; } if (_rectLeftSelector.Contains(e.X,e.Y)) { this.Cursor=Cursors.SizeWE; this._SelectSelctedIndex=8; } } else { this.Cursor=Cursors.SizeAll; this._SelectSelctedIndex=0; } this._SelfLocation.X=this.Location.X; this._SelfLocation.Y=this.Location.Y; this._MouseLocation.X=Cursor.Position.X; this._MouseLocation.Y=Cursor.Position.Y; this._SelfWidth=this.Width; this._SelfHeight=this.Height; } } private void ShapeEx_MouseMove(object sender, MouseEventArgs e) { //move and resize switch (this._SelectSelctedIndex) { case 0: this.Location=new Point(Cursor.Position.X-(_MouseLocation.X-_SelfLocation.X),Cursor.Position.Y-(_MouseLocation.Y-_SelfLocation.Y)); break; case 1: this.Height=this._SelfHeight-(Cursor.Position.Y-_MouseLocation.Y); this.Width=this._SelfWidth-(Cursor.Position.X-_MouseLocation.X); this.Location=new Point(Cursor.Position.X-_MouseLocation.X+_SelfLocation.X,Cursor.Position.Y-_MouseLocation.Y+_SelfLocation.Y); break; case 2: this.Height=this._SelfHeight-(Cursor.Position.Y-_MouseLocation.Y); this.Location=new Point(_SelfLocation.X,Cursor.Position.Y-_MouseLocation.Y+_SelfLocation.Y); break; case 3: this.Height=this._SelfHeight-(Cursor.Position.Y-_MouseLocation.Y); this.Width=this._SelfWidth+(Cursor.Position.X-_MouseLocation.X); this.Location=new Point(_SelfLocation.X,Cursor.Position.Y-(_MouseLocation.Y-_SelfLocation.Y)); break; case 4: this.Width=this._SelfWidth+(Cursor.Position.X-_MouseLocation.X); break; case 5: this.Height=this._SelfHeight+(Cursor.Position.Y-_MouseLocation.Y); this.Width=this._SelfWidth+(Cursor.Position.X-_MouseLocation.X); break; case 6: this.Height=this._SelfHeight+(Cursor.Position.Y-_MouseLocation.Y); break; case 7: this.Height=this._SelfHeight+(Cursor.Position.Y-_MouseLocation.Y); this.Width=this._SelfWidth-(Cursor.Position.X-_MouseLocation.X); this.Location=new Point(Cursor.Position.X-_MouseLocation.X+_SelfLocation.X,_SelfLocation.Y); break; case 8: this.Width=this._SelfWidth-(Cursor.Position.X-_MouseLocation.X); this.Location=new Point(Cursor.Position.X-_MouseLocation.X+_SelfLocation.X,_SelfLocation.Y); break; } } private void ShapeEx_MouseLeave(object sender, EventArgs e) { this.Cursor=Cursors.Default; this._SelectSelctedIndex=-1; } private void ShapeEx_MouseUp(object sender, MouseEventArgs e) { this.Cursor=Cursors.Default; this._SelectSelctedIndex=-1; } } }
相关推荐
C#自定义控件VS用户控件
C#自定义控件VS用户控件1、C#中自定义控件VS用户控件大比拼2、为自定义控件(或类)的方法属性添加注解2.1、Description:在属性窗口中添加属性及属性说明2.2、Browsable2.3、EditorBrowsable2.4、Category2.5、ToolboxBitmap 1、C#中自定义控件VS用户控件大比拼 自定义控件与用户控件区别 WinForm中,用户控件(User Control):继承自 UserControl,主要用于开发 Container 控件,Container控件可以
C#控件开发-自定义控件
控件开发
WinFrom C#自定义控件的实现与添加流程
一、基本流程介绍 保姆级流程:主要针对会C#但不熟悉VS控件制作流程的同学学习。我会复制一个简单的Demo然后对他的代码进行简单的分析,希望对大家有帮助。 制作一个自定义的控件基本需要以下三步 1.创建一个Winform 的类库项目 2.设计自定义控件的一些方法和属性 编译生成.dll文件 3.在一个winform的工具箱加入该dll,接下来只需要将自己做的控件托出即可。 二、创建工程 打开VS 选择创建一个新项目 进入如下界面输入 C# windows 然后选择Windows窗体控件库 如下图所示 给
C#自定义控件
① 添加一个自定义控件,右键解决方案,点击添加,然后点击用户控件。⑤ 查看工具箱就能够看见我们的自定义控件已经在工具箱生成了。2、 Visual Studio社区版 2019(C#)④ 从左侧工具箱拖曳控件并布局,如下图所示。② 选择用户控件(Windows窗体)二、 创建自定义控件窗体。③ 得到如下图所示控件。一、 开发环境和工具。1、 WIN10系统。三、 添加相应函数。② 更新测试时间。③ 更新测试状态。四、 控件应用。
C#的自定义控件
C#的UserControl让用户自定义控件容易了很多,创建WindowsFormsControlLibrary工程,自定义UserControl,生成的.dll加入到任何新工程,就能在新工程中使用自定义的UserControl。 这里主要以一个例子大致记录如何使用C#里的自定义控件。
C#中新建用户控件示例
C#工具箱中已经自带了一些基础控件,对于要求不高的项目来说已经可以满足需求,如果觉得自带控件不好用的可以使用第三方的控件库,这个收费和免费的都有很多,如果这些还满足不了自己的个性化需求的就只能自己写了,基本上自己写的控件库根据自定义的程度不同可以分为三档: 第一档:复合控件 这类控件只是为了解决某一些重复出现的控件组合的需求而编写的,比如TrackBar和label组合显示的复合控件,这类控件从U...
Visual Studio C# WinForm开发入门(3):各种控件介绍
除了可以使用Windows自带的标准对话框外,用户还可以把自己设计的窗体定义成对话框。使用自定义对话框有以下几个要点。(1)将窗体的FormBorderStyle属性值设置为FixedDialog。(2)根据需要向窗体上添加控件。(3)使用窗体的ShowDialog方法显示窗体,即显示出对话框。
C#自定义控件开发案例教程
在IT行业中,C#(发音为 "C Sharp")是微软推出的一种优雅的面向对象编程语言,它与.NET Framework(.NET框架)紧密集成,用于构建各种类型的应用程序。.NET框架是一个软件框架,开发人员在编写代码时可以调用它提供的各种功能和服务。本章节将介绍C#的基础知识及其与.NET Framework的关系。托管控件是在.NET环境中构建的控件,它们在CLR(公共语言运行时)的控制下运行。
C# 自定义控件
自定义控件
联想拯救者r7000p在ubuntu20.04未找到wifi适配器,安装rtl8852ae网卡驱动问题解决方案
新买的拯救者r7000p 2021款,装完ubuntu20.04之后显示未找到wifi适配器, 更换N次源之后还是只有一个显卡驱动,在网上找了很多方法,再自己折腾了一段时间后,总算是解决了这个问题。 1.设置bios 打开主板系统,将security boot设置为disenable,因为设计到修改内核配置,必须得关掉安全启动。 2.查询网卡状态 使用命令查询网卡(hardware of Internet)状态 lshw -C network 因为是双系统,在Windows下控制面板-设备管理器中可以
基于STM32的智能衣柜系统开发实战(附完整源码与硬件设计)
本文详细介绍了基于STM32的智能衣柜系统开发实战,包括硬件设计、软件架构和云端功能实现。系统具备实时温湿度监控、自动加热除湿、紫外线杀菌等核心功能,通过STM32F103C8T6主控芯片和ESP8266无线模块实现智能控制。附完整源码与硬件设计,适合科技爱好者和开发者参考。
c#自定义控件开发实例(1)(2)
c#自定义控件开发实例(1)源文件:http://ded.nuaa.edu.cn/download/Windows%20Extended%20Controls.rar 示例代码:http://ded.nuaa.edu.cn/download/WindowsApplication6.rar 最近做一个图象的采集,需要一个图形的选择控件,但是在.net下没有类似vb中的shape控件,所以考虑
DeepSeek+知网研学:三步搞定文献矩阵与对比分析(实战指南)
本文提供了一套高效实用的文献管理分析实战指南,详细介绍了如何结合DeepSeek大模型与知网研学平台,通过三个核心步骤快速完成文献梳理。该方法能自动提炼文献核心要素、生成结构化对比矩阵,并辅助发现研究空白,显著提升学术研究者的文献分析效率与深度。
C#自定义控件编程轻松入门(1)
C#控件编写的第一课,最容易的入门方式,从一个空白控件开始,逐步掌握自定义控件的编写过程。
通达信浮筹多空波段抓牛擒龙指标公式源码
VAR33:=VA3>=REF(VA4,1) AND C<REF(C,1) AND VAR19=60 AND VAR18=0 AND 活跃线>=REF(活跃线,1) AND VA12<REF(VA13,1);DRAWTEXT(VAR28 AND COUNT(VAR28,30)=1,65,' ★超涨注意风险'),COLOR0000FF;DRAWTEXT(VAR26 AND COUNT(VAR26,30)=1,90,' ←继续等'),COLOR00FFFF;浮筹线:VA3,COLORFF00FF;
YOLOv8改进 | 主干网络 | 在backbone添加Swin-Transformer层【论文必备】
YOLOv8改进,Swin-Transform,YOLOv8添加Swin-Transform
C# 自定义控件制作和使用实例(winform)
C# 自定义控件制作和使用实例(winform) 2018年04月26日 15:01:11乌班图ysm阅读数 5788 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.youkuaiyun.com/u012278016/article/details/79929765 C#自定义用户控件 此处为转载文章,用于记录自我学习过程,原文链接地址http://blog...
1、JMP脚本编程全面指南:从基础到高级应用
本博客全面介绍了JMP脚本编程的各个方面,从JMP基础和JSL入门,到数据类型、数据结构、编程方法、数据表操作、图形与三维场景脚本,再到应用程序创建与扩展功能。内容涵盖脚本工具使用、常见任务实现、兼容性说明及未来发展趋势,旨在帮助用户掌握JMP脚本语言的强大功能,提升数据分析的自动化与定制化能力。适合从初学者到高级用户的深入学习与实践参考。
C# 自定义控件制作和使用实例
C# 自定义用户控件xiongxuanwen 上篇:控件制作 本例是制作一个简单的自定义控件,然后用一个简单的测试程序,对于初学者来说,本例子比较简单,只能起到抛石引玉的效果。我也是在学习当中,今后会将自己所学的逐步写出来和大家交流共享。 第一步:新建一个控件库项目:myControl 第二步:从工具箱里面拖动1个PictureBox、1个Button、6
康耐视visionpro-CogCreateSegmentAvgSegsTool操作工具详细说明
康耐视visionpro-CogCreateSegmentAvgSegsTool操作工具详细说明
知识图谱-KGE-语义匹配-双线性模型-2018:SimplE
【paper】 SimplE Embedding for Link Prediction in Knowledge Graphs 【简介】 本文是加拿大英属哥伦比亚大学的两位学者发表在 NIPS 2018 上的工作,文章提出了 SimplE(Simple Embedding)。这篇和前面一篇差不多,也是对 1927 年的 CP 进行改进(简化),传统CP为实体分配的两个向量是独立训练的,SimplE利用关系的逆在三元组打分函数中加上了一个对称项,使得每个实体的两个向量依赖学习,并且 SimplE 的复杂度随
c#自定义控件库的开发
当我们做c#开发时候,我们可以将常用到的功能直接开发成组件,当下次需要相同的功能时候,我们直接调用自己开发好的控件库即可,方便省事。入门资料可以参考这位博主的 https://www.cnblogs.com/yangfengwu/p/5956841.html。 这里我介绍我简单的几个自定义控件: 1、时间控件: 主要代码实现如下: public void LetThin...
Outlook Express 6 (OE6) for Win7/Win8/Win10
英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。
5045



被折叠的 条评论
为什么被折叠?



