C#实现GDI+基本图的缩放、拖拽、移动
C#实现GDI+基本图的缩放、拖拽、移动示例代码如下:
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms; namespace ResizableControls{ public partial class MainForm : Form { /// <summary> /// 有关鼠标样式的相关枚举 /// </summary> private enum EnumMousePointPosition { MouseSizeNone = 0, //无 MouseSizeRight = 1, //拉伸右边框 MouseSizeLeft = 2, //拉伸左边框 MouseSizeBottom = 3, //拉伸下边框 MouseSizeTop = 4, //拉伸上边框 MouseSizeTopLeft = 5,//拉伸左上角 MouseSizeTopRight = 6,//拉伸右上角 MouseSizeBottomLeft = 7,//拉伸左下角 MouseSizeBottomRight = 8,//拉伸右下角 MouseDrag = 9 //鼠标拖动 } const int Band = 5;//范围半径 const int MinWidth = 10;//最低宽度 const int MinHeight = 10;//最低高度 private EnumMousePointPosition m_MousePointPosition; //鼠标样式枚举 private Point m_lastPoint; //光标上次移动的位置 private Point m_endPoint; //光标移动的当前位置 public MainForm() { InitializeComponent(); //窗体中控件的事件晚期绑定 for (int i = 0; i < this.Controls.Count; i++) { this.Controls[i].MouseDown += new MouseEventHandler(MyMouseDown); this.Controls[i].MouseLeave += new EventHandler(MyMouseLeave); this.Controls[i].MouseMove += new MouseEventHandler(MyMouseMove); } } //鼠标按下事件 private void MyMouseDown(object sender,MouseEventArgs e) { m_lastPoint.X = e.X; m_lastPoint.Y = e.Y; m_endPoint.X = e.X; m_endPoint.Y = e.Y; } //鼠标离开控件的事件 private void MyMouseLeave(object sender, System.EventArgs e) { m_MousePointPosition = EnumMousePointPosition.MouseSizeNone; this.Cursor = Cursors.Arrow; } //鼠标移过控件的事件 private void MyMouseMove(object sender, MouseEventArgs e) { Control lCtrl = (sender as Control);//获得事件源 //左键按下移动 if (e.Button == MouseButtons.Left) { switch (m_MousePointPosition) { case EnumMousePointPosition.MouseDrag: lCtrl.Left = lCtrl.Left + e.X - m_lastPoint.X; lCtrl.Top = lCtrl.Top + e.Y - m_lastPoint.Y; break; case EnumMousePointPosition.MouseSizeBottom: lCtrl.Height = lCtrl.Height + e.Y - m_endPoint.Y; m_endPoint.X = e.X; m_endPoint.Y = e.Y; //记录光标拖动的当前点 break; case EnumMousePointPosition.MouseSizeBottomRight: lCtrl.Width = lCtrl.Width + e.X - m_endPoint.X; lCtrl.Height = lCtrl.Height + e.Y - m_endPoint.Y; m_endPoint.X = e.X; m_endPoint.Y = e.Y; //记录光标拖动的当前点 break; case EnumMousePointPosition.MouseSizeRight: lCtrl.Width = lCtrl.Width + e.X - m_endPoint.X; //lCtrl.Height = lCtrl.Height + e.Y - m_endPoint.Y; m_endPoint.X = e.X; m_endPoint.Y = e.Y; //记录光标拖动的当前点 break; case EnumMousePointPosition.MouseSizeTop: lCtrl.Top = lCtrl.Top + (e.Y - m_lastPoint.Y); lCtrl.Height = lCtrl.Height - (e.Y - m_lastPoint.Y); break; case EnumMousePointPosition.MouseSizeLeft: lCtrl.Left = lCtrl.Left + e.X - m_lastPoint.X; lCtrl.Width = lCtrl.Width - (e.X - m_lastPoint.X); break; case EnumMousePointPosition.MouseSizeBottomLeft: lCtrl.Left = lCtrl.Left + e.X - m_lastPoint.X; lCtrl.Width = lCtrl.Width - (e.X - m_lastPoint.X); lCtrl.Height = lCtrl.Height + e.Y - m_endPoint.Y; m_endPoint.X = e.X; m_endPoint.Y = e.Y; //记录光标拖动的当前点 break; case EnumMousePointPosition.MouseSizeTopRight: lCtrl.Top = lCtrl.Top + (e.Y - m_lastPoint.Y); lCtrl.Width = lCtrl.Width + (e.X - m_endPoint.X); lCtrl.Height = lCtrl.Height - (e.Y - m_lastPoint.Y); m_endPoint.X = e.X; m_endPoint.Y = e.Y; //记录光标拖动的当前点 break; case EnumMousePointPosition.MouseSizeTopLeft: lCtrl.Left = lCtrl.Left + e.X - m_lastPoint.X; lCtrl.Top = lCtrl.Top + (e.Y - m_lastPoint.Y); lCtrl.Width = lCtrl.Width - (e.X - m_lastPoint.X); lCtrl.Height = lCtrl.Height - (e.Y - m_lastPoint.Y); break; default: break; } if (lCtrl.Width < MinWidth) lCtrl.Width = MinWidth; if (lCtrl.Height < MinHeight) lCtrl.Height = MinHeight; } else { //'判断光标的位置状态 m_MousePointPosition = MousePointPosition(lCtrl.Size, e); switch (m_MousePointPosition) //改变光标 { case EnumMousePointPosition.MouseSizeNone: this.Cursor = Cursors.Arrow;//箭头 break; case EnumMousePointPosition.MouseDrag: this.Cursor = Cursors.SizeAll;//四方向 break; case EnumMousePointPosition.MouseSizeBottom: this.Cursor = Cursors.SizeNS;//南北 break; case EnumMousePointPosition.MouseSizeTop: this.Cursor = Cursors.SizeNS;//南北 break; case EnumMousePointPosition.MouseSizeLeft: this.Cursor = Cursors.SizeWE;//东西 break; case EnumMousePointPosition.MouseSizeRight: this.Cursor = Cursors.SizeWE;//东西 break; case EnumMousePointPosition.MouseSizeBottomLeft: this.Cursor = Cursors.SizeNESW;//东北到南西 break; case EnumMousePointPosition.MouseSizeBottomRight: this.Cursor = Cursors.SizeNWSE;//东南到西北 break; case EnumMousePointPosition.MouseSizeTopLeft: this.Cursor = Cursors.SizeNWSE;//东南到西北 break; case EnumMousePointPosition.MouseSizeTopRight: this.Cursor = Cursors.SizeNESW;//东北到南西 break; default: break; } } } //坐标位置判定 private EnumMousePointPosition MousePointPosition(Size size, System.Windows.Forms.MouseEventArgs e) { if ((e.X >= -1 * Band) | (e.X <= size.Width) | (e.Y >= -1 * Band) | (e.Y <= size.Height)) { if (e.X < Band) { if (e.Y < Band) { return EnumMousePointPosition.MouseSizeTopLeft; } else { if (e.Y > -1 * Band + size.Height) { return EnumMousePointPosition.MouseSizeBottomLeft; } else { return EnumMousePointPosition.MouseSizeLeft; } } } else { if (e.X > -1 * Band + size.Width) { if (e.Y < Band) { return EnumMousePointPosition.MouseSizeTopRight; } else { if (e.Y > -1 * Band + size.Height) { return EnumMousePointPosition.MouseSizeBottomRight; } else { return EnumMousePointPosition.MouseSizeRight; } } } else { if (e.Y < Band) { return EnumMousePointPosition.MouseSizeTop; } else { if (e.Y > -1 * Band + size.Height) { return EnumMousePointPosition.MouseSizeBottom; } else { return EnumMousePointPosition.MouseDrag; } } } } } else { return EnumMousePointPosition.MouseSizeNone; } } }}
转载于:https://www.cnblogs.com/1175429393wljblog/p/5676348.html
C#实现GDI+基本图的缩放、拖拽、移动相关推荐
- web使用panzoom.js 缩放拖拽 工程图cad图
web使用panzoom.js 缩放拖拽 工程图cad图 前言 第一步下载图片转换工具 将cad的dwg转换为svg 第二步 普通的cad 转换后 大概4-5M 如果直接放入html 加载会很缓慢 需 ...
- 【项目技术点总结之一】vue集成d3.js利用svg加载图片实现缩放拖拽功能
[项目技术点总结之一]vue集成d3.js利用svg加载图片实现缩放拖拽功能 前言 概述 技术介绍 实现过程 插件安装 引用组件 初始化组件 实现效果 简单理解 使用d3创建一个svg 在svg中提添 ...
- iOS开发点击查看图片详情并可缩放拖拽图片长按图片下载
由图片缩略图进入查看图片原图页面,并可对图片进行缩放(最小不小于原图,最大不大于三倍原图),长按可保存到本地相册或者查看原图. 1)缩略图页面: 2)点击查看原图页面 代码部分: 1.初始化图片背景跟 ...
- vue拖动缩放组件(vue-drag-zoom) 增加禁止缩放拖拽功能
前段时间写了一个类似于百度ICOR,可拖拽缩放图片并在图片上框选文字的功能,这里的拖拽缩放功能就用到了vue-drag-zoom组件,组件是从npm下载的VUE2代码,放在VUE3+vite项目里面也 ...
- vue 百度地图点聚合MarkerClusterer,缩放拖拽后label消失
本地创建MarkerClusterer.js var BMapLib = window.BMapLib = BMapLib || {}; (function() {var getExtendedBou ...
- html5 canvas图片缩放,拖拽
想用html5 canvas来实现地图功能 主要是想把地图功能集合在系统中,而不是使用类似geoserver等发布,相当于两套系统了.地图是室内地图,所以高德百度什么的没用.我的理想情况是丢一副矢量图 ...
- 【Android】HorizontalScrollView内子控件横向拖拽
前言 网上ListView上下拖动的例子有,效果也很好,但是项目要横着拖的,只要硬着头皮自己写(主要是没找到合适的),参考文章1修改而来,分享一下. 声明 欢迎转载,但请保留文章原始出处:) 博客园 ...
- svg实现多个元素拖拽
下面使用到的index.css path{stroke:black;stroke-width:3;fill:none;stroke-linecap: round; } svg {} p{text-al ...
- iOS学习笔记-068.手势识别02——捏合、拖拽、旋转手势
手势识别02捏合拖拽旋转手势 一捏合手势 1 UIPinchGestureRecognizerh 2 代码示例 3 图示 二拖拽手势 1 UIPanGestureRecognizerh 2 代码示例 ...
最新文章
- 星际2的一些技术特性
- 第二篇:Mysql---约束条件、修改表的结构、键值
- mysql 5.1 互为主从,mysql数据库互为主从配置方法分享
- Lambda表达式有参数有返回值的练习(自定义接口)
- notepad php格式,notepad怎么格式xml
- mysql dump 10.13_mysqldump版本引起的问题
- linux基础练习,Linux基础指令练习
- TensorFlow tf.keras.losses.MeanSquaredError
- [LeetCode]Subsets II生成组合序列
- git遇到的错误集锦及解决方法
- 安装SQL server 提示重新启动计算机失败
- python 获取当前目录,上级目录,上上级目录
- 26个字母大小写的ASCII码值
- 基于onvif协议的嵌入式设备(摄像头)开发(客户端)
- 脸型测试软件在线测试,脸型测试软件最新版
- iis 支持apk json ipa下载
- 如何提交项目到git
- 8.利用红外遥控信号控制LED灯的亮灭
- 测18日2点墨西哥战法国,胜败如何?
- Android兼容性系统app开发的一些探究