热门标签 | 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窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 本文介绍了C#中数据集DataSet对象的使用及相关方法详解,包括DataSet对象的概述、与数据关系对象的互联、Rows集合和Columns集合的组成,以及DataSet对象常用的方法之一——Merge方法的使用。通过本文的阅读,读者可以了解到DataSet对象在C#中的重要性和使用方法。 ... [详细]
  • 拥抱Android Design Support Library新变化(导航视图、悬浮ActionBar)
    转载请注明明桑AndroidAndroid5.0Loollipop作为Android最重要的版本之一,为我们带来了全新的界面风格和设计语言。看起来很受欢迎࿰ ... [详细]
  • Go GUIlxn/walk 学习3.菜单栏和工具栏的具体实现
    本文介绍了使用Go语言的GUI库lxn/walk实现菜单栏和工具栏的具体方法,包括消息窗口的产生、文件放置动作响应和提示框的应用。部分代码来自上一篇博客和lxn/walk官方示例。文章提供了学习GUI开发的实际案例和代码示例。 ... [详细]
  • 导出功能protectedvoidbtnExport(objectsender,EventArgse){用来打开下载窗口stringfileName中 ... [详细]
  • 本文讨论了在Windows 8上安装gvim中插件时出现的错误加载问题。作者将EasyMotion插件放在了正确的位置,但加载时却出现了错误。作者提供了下载链接和之前放置插件的位置,并列出了出现的错误信息。 ... [详细]
  • 本文介绍了C#中生成随机数的三种方法,并分析了其中存在的问题。首先介绍了使用Random类生成随机数的默认方法,但在高并发情况下可能会出现重复的情况。接着通过循环生成了一系列随机数,进一步突显了这个问题。文章指出,随机数生成在任何编程语言中都是必备的功能,但Random类生成的随机数并不可靠。最后,提出了需要寻找其他可靠的随机数生成方法的建议。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • 本文详细介绍了Linux中进程控制块PCBtask_struct结构体的结构和作用,包括进程状态、进程号、待处理信号、进程地址空间、调度标志、锁深度、基本时间片、调度策略以及内存管理信息等方面的内容。阅读本文可以更加深入地了解Linux进程管理的原理和机制。 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • Go语言实现堆排序的详细教程
    本文主要介绍了Go语言实现堆排序的详细教程,包括大根堆的定义和完全二叉树的概念。通过图解和算法描述,详细介绍了堆排序的实现过程。堆排序是一种效率很高的排序算法,时间复杂度为O(nlgn)。阅读本文大约需要15分钟。 ... [详细]
  • JDK源码学习之HashTable(附带面试题)的学习笔记
    本文介绍了JDK源码学习之HashTable(附带面试题)的学习笔记,包括HashTable的定义、数据类型、与HashMap的关系和区别。文章提供了干货,并附带了其他相关主题的学习笔记。 ... [详细]
  • 海马s5近光灯能否直接更换为H7?
    本文主要介绍了海马s5车型的近光灯是否可以直接更换为H7灯泡,并提供了完整的教程下载地址。此外,还详细讲解了DSP功能函数中的数据拷贝、数据填充和浮点数转换为定点数的相关内容。 ... [详细]
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社区 版权所有