热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

C#控件自由拖动、边角拖拉缩放

效果就是在一幅图上画一个白框,可移动可缩放usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentMode

效果就是在一幅图上画一个白框,可移动可缩放 

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;
using Emgu;
using Emgu.CV;
using Emgu.Util;
using Emgu.CV.Structure;
namespace huitugongju
{
public partial class Form1 : Form
{
Point start; //画框的起始点
Point end;//画框的结束点
bool blnDraw;//判断是否绘制
Rectangle rect;
List roi_location = new List();
List roi_size = new List();
MouseDirection direction;//指针形状
bool size_change;//画框大小改变
bool position_change;//画框位置改变
Point rectangle_start;//画框内的起始点
Point rectangle_end;//画框内结束点
public enum MouseDirection
{
East,
West,
South,
North,
Southeast,
Southwest,
Northeast,
Northwest,
None
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Mat src = CvInvoke.Imread("dog.jpg");
this.Width = src.Width;
this.Height = src.Height;
this.BackgroundImage = src.Bitmap;
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
try
{
var p1 = this.Controls.Find("panel1", true);
if (p1[0].Size.Width !=0 || p1[0].Size.Height !=0)
{
roi_location.Add(p1[0].Location);
roi_size.Add(p1[0].Size);
}
this.Controls.Clear();
}
catch (Exception)
{
}
finally
{
Panel p = new Panel();
p.Visible = false;
p.Name = "panel1";
p.BackColor = Color.White;
this.Controls.Add(p);
start = e.Location;
blnDraw = true;
}
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (blnDraw)
{
if (e.Button != MouseButtons.Left)//判断是否按下左键
return;
Point tempEndPoint = e.Location; //记录框的位置和大小
//var p = this.Controls.Find("panel1", true);
rect.Location = new Point(
Math.Min(start.X, tempEndPoint.X),
Math.Min(start.Y, tempEndPoint.Y));
rect.Size = new Size(Math.Abs(start.X - tempEndPoint.X),Math.Abs(start.Y - tempEndPoint.Y));
this.Invalidate();
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
if (blnDraw)
{
this.Invalidate();
var p = this.Controls.Find("panel1", true);
p[0].BackColor = Color.White;
p[0].Location = start;
p[0].Size = rect.Size;
p[0].Visible = true;
p[0].MouseDown += new MouseEventHandler(this.panel_MouseDown);
p[0].MouseMove += new MouseEventHandler(this.panel_MouseMove);
}
blnDraw = false; //结束绘制
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
if (blnDraw)
{
if (this.BackgroundImage != null)
{
if (rect != null && rect.Width > 0 && rect.Height > 0)
{
e.Graphics.DrawRectangle(new Pen(Color.Red, 1), rect);//重新绘制颜色为红色
}
}
}
}
private void panel_MouseDown(object sender, MouseEventArgs e)
{
rectangle_start = e.Location;
}
private void panel_MouseMove(object sender, MouseEventArgs e)
{
var p = this.Controls.Find("panel1", true);
Point tempEndPoint = e.Location;
if (e.Button == MouseButtons.Left)
{
if (direction == MouseDirection.None)
{
int x = tempEndPoint.X - rectangle_start.X;
int y = tempEndPoint.Y - rectangle_start.Y;
p[0].Location = new Point(p[0].Location.X + x, p[0].Location.Y + y);
}
else if (direction == MouseDirection.East)
{
p[0].Width = tempEndPoint.X;
}
else if (direction == MouseDirection.South)
{
p[0].Height = tempEndPoint.Y;
}
else if (direction == MouseDirection.West)
{
int x = tempEndPoint.X - rectangle_start.X;
p[0].Width = p[0].Width - x;
p[0].Location = new Point(p[0].Location.X + x, p[0].Location.Y);
}
else if (direction == MouseDirection.North)
{
int y = tempEndPoint.Y - rectangle_start.Y;
p[0].Height = p[0].Height - y;
p[0].Location = new Point(p[0].Location.X, p[0].Location.Y + y);
}
else if (direction == MouseDirection.Southeast)
{
p[0].Width = tempEndPoint.X;
p[0].Height = tempEndPoint.Y;
}
else if (direction == MouseDirection.Northeast)
{
p[0].Width = tempEndPoint.X;
int y = tempEndPoint.Y - rectangle_start.Y;
p[0].Height = p[0].Height - y;
p[0].Location = new Point(p[0].Location.X, p[0].Location.Y + y);
}
else if (direction == MouseDirection.Southwest)
{
p[0].Height = tempEndPoint.Y;
int x = tempEndPoint.X - rectangle_start.X;
p[0].Width = p[0].Width - x;
p[0].Location = new Point(p[0].Location.X + x, p[0].Location.Y);
}
else if (direction == MouseDirection.Northwest)
{
int x = tempEndPoint.X - rectangle_start.X;
int y = tempEndPoint.Y - rectangle_start.Y;
p[0].Height = p[0].Height - y;
p[0].Width = p[0].Width - x;
p[0].Location = new Point(p[0].Location.X + x, p[0].Location.Y + y);
}
}
else
{
if (e.Location.X <10 && e.Location.Y <10)
{
p[0].Cursor = Cursors.SizeNWSE;
direction = MouseDirection.Northwest;
}
else if (e.Location.X > p[0].Width - 10 && e.Location.Y <10)
{
p[0].Cursor = Cursors.SizeNESW;
direction = MouseDirection.Northeast;
}
else if (e.Location.X > p[0].Width - 10 && e.Location.Y > p[0].Height - 10)
{
p[0].Cursor = Cursors.SizeNWSE;
direction = MouseDirection.Southeast;
}
else if (e.Location.X <10 && e.Location.Y > p[0].Height - 10)
{
p[0].Cursor = Cursors.SizeNESW;
direction = MouseDirection.Southwest;
}
else if (e.Location.X <10 && e.Location.Y 10)
{
p[0].Cursor = Cursors.SizeWE;
direction = MouseDirection.West;
}
else if (e.Location.X > 10 && e.Location.X {
p[0].Cursor = Cursors.SizeNS;
direction = MouseDirection.North;
}
else if (e.Location.X > p[0].Width - 10 && e.Location.Y 10)
{
p[0].Cursor = Cursors.SizeWE;
direction = MouseDirection.East;
}
else if (e.Location.X > 10 && e.Location.X p[0].Height - 10)
{
p[0].Cursor = Cursors.SizeNS;
direction = MouseDirection.South;
}
else
{
p[0].Cursor = Cursors.SizeAll;
direction = MouseDirection.None;
}
}
}
}
}

大概效果这样:



推荐阅读
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 本文介绍了C#中数据集DataSet对象的使用及相关方法详解,包括DataSet对象的概述、与数据关系对象的互联、Rows集合和Columns集合的组成,以及DataSet对象常用的方法之一——Merge方法的使用。通过本文的阅读,读者可以了解到DataSet对象在C#中的重要性和使用方法。 ... [详细]
  • 本文详细介绍了Linux中进程控制块PCBtask_struct结构体的结构和作用,包括进程状态、进程号、待处理信号、进程地址空间、调度标志、锁深度、基本时间片、调度策略以及内存管理信息等方面的内容。阅读本文可以更加深入地了解Linux进程管理的原理和机制。 ... [详细]
  • JDK源码学习之HashTable(附带面试题)的学习笔记
    本文介绍了JDK源码学习之HashTable(附带面试题)的学习笔记,包括HashTable的定义、数据类型、与HashMap的关系和区别。文章提供了干货,并附带了其他相关主题的学习笔记。 ... [详细]
  • 本文介绍了使用Python解析C语言结构体的方法,包括定义基本类型和结构体类型的字典,并提供了一个示例代码,展示了如何解析C语言结构体。 ... [详细]
  • 本文介绍了pack布局管理器在Perl/Tk中的使用方法及注意事项。通过调用pack()方法,可以控制部件在显示窗口中的位置和大小。同时,本文还提到了在使用pack布局管理器时,应注意将部件分组以便在水平和垂直方向上进行堆放。此外,还介绍了使用Frame部件或Toplevel部件来组织部件在窗口内的方法。最后,本文强调了在使用pack布局管理器时,应避免在中间切换到grid布局管理器,以免造成混乱。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • 《数据结构》学习笔记3——串匹配算法性能评估
    本文主要讨论串匹配算法的性能评估,包括模式匹配、字符种类数量、算法复杂度等内容。通过借助C++中的头文件和库,可以实现对串的匹配操作。其中蛮力算法的复杂度为O(m*n),通过随机取出长度为m的子串作为模式P,在文本T中进行匹配,统计平均复杂度。对于成功和失败的匹配分别进行测试,分析其平均复杂度。详情请参考相关学习资源。 ... [详细]
  • 拥抱Android Design Support Library新变化(导航视图、悬浮ActionBar)
    转载请注明明桑AndroidAndroid5.0Loollipop作为Android最重要的版本之一,为我们带来了全新的界面风格和设计语言。看起来很受欢迎࿰ ... [详细]
  • 本文介绍了MVP架构模式及其在国庆技术博客中的应用。MVP架构模式是一种演变自MVC架构的新模式,其中View和Model之间的通信通过Presenter进行。相比MVC架构,MVP架构将交互逻辑放在Presenter内部,而View直接从Model中读取数据而不是通过Controller。本文还探讨了MVP架构在国庆技术博客中的具体应用。 ... [详细]
  • 本文整理了Java面试中常见的问题及相关概念的解析,包括HashMap中为什么重写equals还要重写hashcode、map的分类和常见情况、final关键字的用法、Synchronized和lock的区别、volatile的介绍、Syncronized锁的作用、构造函数和构造函数重载的概念、方法覆盖和方法重载的区别、反射获取和设置对象私有字段的值的方法、通过反射创建对象的方式以及内部类的详解。 ... [详细]
  • 深入解析Linux下的I/O多路转接epoll技术
    本文深入解析了Linux下的I/O多路转接epoll技术,介绍了select和poll函数的问题,以及epoll函数的设计和优点。同时讲解了epoll函数的使用方法,包括epoll_create和epoll_ctl两个系统调用。 ... [详细]
author-avatar
淘气111006
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有