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.pswp.cn/news/271349.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/271349.shtml
英文地址,請注明出處:http://en.pswp.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;可控制授權的范圍內的信息流向以及行為方…