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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/271349.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

网络资产管理系统_固定资产管理系统的三种网络架构方式

随着互联网技术的发展和信息技术的广泛使用&#xff0c;固定资产管理系统在各行业的应用越来越普及&#xff0c;固定资产管理系统作为当今主流的企业固定资产信息化管理模式&#xff0c;能够对企业固定资产进行有效管理并提升企业的管理水平。对于固定资产管理系统的网络结构方…

计算机网络基础:广域网协议相关知识笔记

广域网常指覆盖范围广、数据传输速率较低&#xff0c;以数据通信为目的的数据通信网。广域网主要是通过专用的或交换式的连接把计算机连接起来。广域网传输协议主要包括&#xff1a;PPP&#xff08;点对点协议&#xff09;、DDN、ISDN&#xff08;综合业务数字网&#xff09;、…

mysql check table_修复MySQL的MyISAM表命令check table用法

MyISAM如果损坏了修复方法是比较简单了我们只要使用check table命令就可以了&#xff0c;下面我们来看一篇关于修复MySQL的MyISAM表命令check table用法&#xff0c;具体如下所示。MySQL日志文件里出现以下错误&#xff0c;MySQL表通常不会发生crash情况&#xff0c;一般是在更…

python字典append_python中字典重复赋值,append到list中引发的异常

今天遇到了一个关于python 字典的误用。先上代码&#xff1a; data [{id: 1, name: 管理员, role: admin, desc: 系统管理员, acl: None}, {id: 2, name: 研发, role: dev, desc: 研发人员, acl: None}, {id: 3, name: 测试, role: qa, desc: 测试人员, acl: None}, {id: 4, n…

计算机网络基础:TCP/IP协议相关知识笔记​

1、TCP/IP特性逻辑编址&#xff1a;每一块网卡会在出厂时由厂家分配了唯一的永久性物理地址。针对Internet&#xff0c;会为每台连入因特网的计算机分配一个逻辑地址也就是IP地址。路由选择&#xff1a;专门用于定义路由器如何选择网络路径的协议&#xff0c;即IP数据包的路由选…

终于做出来了

1 <!doctype html>2 <html lang"en">3 <head>4 <meta charset"UTF-8">5 <title>精英大赛2号 </title>6 <meta name"Keywords" content"关键字">7 <meta name"Desp…

ashx连接mysql_ASP.net与SQLite数据库通过js和ashx交互(连接和操作)

ASP.net与SQLite数据库通过js和ashx交互(连接和操作)&#xff1a;废话(也是思路)&#xff1a;用的是VS2010&#xff0c;打算做网站前后台。由于不喜欢前台语言里加些与html和css和js的其他内容&#xff0c;想实现前后台语言的分离&#xff0c;与前后台通过js的ajax实现交互&…

计算机网络:九大命令!解决网络故障新思路

一&#xff1a;ping命令ping是个使用频率极高的实用程序&#xff0c;主要用于确定网络的连通性。这对确定网络是否正确连接&#xff0c;以及网络连接的状况十分有用。简单的说&#xff0c;ping就是一个测试程序&#xff0c;如果ping运行正确&#xff0c;大体上就可以排除网络访…

webpack打包后引用cdn的js_JS逆向:Webpack打包后的代码怎么搞?猿人学爬虫比赛第十六题详细题解...

实战地址http://match.yuanrenxue.com/match/16抓包分析地址栏输入 地址&#xff0c;按下F12并回车&#xff0c;发现数据在这里:查看cookie&#xff0c;无加密相关的字段。请求的接口倒是有个m的加密参数&#xff0c;看来这题的主要目的就是 看看m参数怎么进行加密的吧。切换 I…

计算机网络基础:IP基础知识笔记

1、 IP地址概念IP是用来唯一标识主机地址。IP地址 网络地址 主机地址(又称&#xff1a;主机号和网络号组成)例如IP&#xff1a;192.168.100.168 子网掩码 255.255.255.0 对应的网络地址和主机地址如下&#xff1a;192.168.100.168&#xff08;IP地址&#xff09; 192.168.1.…

bs架构的系统能连接mysql吗_HTTP、BS架构

Django 底层原理快捷键方向键方向键本键如果活动选项是选项按钮或文件则为移动焦点&#xff1b;方向键 Win键(简称Win键)使窗口全屏、最小化、靠左半边、靠右半边(部分版本不支持)&#xff1b;方向键Shift键将连续的文字或文件选中方向键(左右)Ctrl键 在英文单词或中文词语间跳…

离散卷积的计算

本文转自&#xff1a; 离散卷积与自相关----------信号处理系列 http://www.cnblogs.com/einyboy/archive/2012/12/30/2839633.html 一、 定义 离散信号f(n),g(n)的定义如下&#xff1a; N-----为信号f(n)的长度 s(n)----为卷积结果序列,长度为len(f(n))len(g(n))-1 以3个元…

计算机网络基础:Internet常用服务介绍​

1、域名服务Internet中的域名地址和IP地址是等价的&#xff0c;它们之间是通过域名服务完成映射的。实际上DNS是一种分布式地址信息数据库系统&#xff0c;服务器中包含整个数据库的某部分信息&#xff0c;并供客户查询。域名系统采用客户端/服务器模式&#xff0c;整个系统由解…

lamba

lamba /*** lamba*/Testpublic void test5() {Runnable r () -> System.out.println("hello");r.run();}Testpublic void test6() {int num 0;Runnable r new Runnable() {Overridepublic void run() {System.out.println("java");}};r.run();}

第五章 Response(JavaTM Servlet 规范3.1 )

The Response 响应 响应对象包装了从服务器端返回到客户端的所有信息。在HTTP协议上&#xff0c;这些信息既可以通过HTTP headers 又可以通过响应体从服务器端传输到客户端。 5.1 缓冲 为了效率&#xff0c;servlet 容器允许但非必须缓冲到客户端的输出。典型地&#xff0c;服…

c语言深度剖析第三版pdf_入门到入坟,蕴含全网最强知识点3283页笔记、pdf教程,活到老,学到老...

又到了“金九银十”面试求职高峰期&#xff0c;在金三银四时也参与过不少面试&#xff0c;2020都说工作不好找&#xff0c;也是对开发人员的要求变高。前段时间自己有整理了一些Java后端开发面试常问的高频考点问题做成一份PDF文档&#xff08;1000道高频题&#xff09;&#x…

mysql 5.7.23要钱吗_最新mysql 5.7.23安装配置图文教程

2018年最新mysql5.7详细安装与配置&#xff0c;总共分为四步&#xff0c;其中环境变量配置不是必须的。1、安装包下载2、安装过程3、环境变量配置4、连接测试一、官网下载mysql安装包1.前往官网下载&#xff0c;下载链接为&#xff1a;2.选择合适你电脑系统的版本进行安装。如果…

计算机基础:信息安全相关知识笔记

1、信息安全要素机密性&#xff1a;保证信息不暴露给未授权的用户。完整性&#xff1a;得到允许的用户可以修改数据&#xff0c;并且可以判断数据是否被篡改。可用性&#xff1a;拥有授权的用户可以在需要时访问数据。可控性&#xff1a;可控制授权的范围内的信息流向以及行为方…