控件 鼠标移动
来源:互联网 发布:c语言读取dat文件内容 编辑:程序博客网 时间:2024/05/21 08:42
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
//using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Point point = new Point();
point.X = this.panel.Left + this.groupBox1.Left;
point.Y = this.panel.Top + this.groupBox1.Top;
Resize rs = new Resize(label1, this, point);
Resize r1 = new Resize(label2, this, point);
Resize r2 = new Resize(button1, this, point);
}
}
public class Resize
{
bool IsMoving = false;
int ctrlLastWidth = 0;
int ctrlLastHeight = 0;
int ctrlWidth;
int ctrlHeight;
int ctrlLeft;
int ctrlTop;
int cursorL;
int cursorT;
int ctrlLastLeft;
int ctrlLastTop;
bool ctrlIsResizing = false;
System.Drawing.Rectangle ctrlRectangle = new System.Drawing.Rectangle();
private Control ctrl;
private Form frm;
//add
private Point point;
public Resize(Control c, Form frm, Point point)
{
ctrl = c;
this.frm = frm;
this.point = point;
ctrl.MouseDown += new MouseEventHandler(MouseDown);
ctrl.MouseMove += new MouseEventHandler(MouseMove);
ctrl.MouseUp += new MouseEventHandler(MouseUp);
}
private void MouseMove(object sender, MouseEventArgs e)
{
if (frm == null)
return;
if (e.Button == MouseButtons.Left)
{
if (this.IsMoving)
{
if (ctrlLastLeft == 0)
ctrlLastLeft = ctrlLeft;
if (ctrlLastTop == 0)
ctrlLastTop = ctrlTop;
int locationX = (Cursor.Position.X - this.cursorL + this.frm.DesktopLocation.X + this.ctrl.Location.X);
int locationY = (Cursor.Position.Y - this.cursorT + this.frm.DesktopLocation.Y + this.ctrl.Location.Y);
if (locationX < this.frm.DesktopLocation.X)
locationX = this.frm.DesktopLocation.X;
if (locationY < this.frm.DesktopLocation.Y)
locationY = this.frm.DesktopLocation.Y;
this.ctrlLeft = locationX;
this.ctrlTop = locationY;
ctrlRectangle.Location = new System.Drawing.Point(this.ctrlLastLeft, this.ctrlLastTop);
ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth, ctrlHeight);
ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
ctrlLastLeft = ctrlLeft;
ctrlLastTop = ctrlTop;
ctrlRectangle.Location = new System.Drawing.Point(ctrlLeft, ctrlTop);
ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth, ctrlHeight);
ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
return;
}
int sizeageX = (Cursor.Position.X - this.frm.DesktopLocation.X - this.ctrl.Location.X);
int sizeageY = (Cursor.Position.Y - this.frm.DesktopLocation.Y - this.ctrl.Location.Y);
if (sizeageX < 2)
sizeageX = 1;
if (sizeageY < 2)
sizeageY = 1;
ctrlWidth = sizeageX;
ctrlHeight = sizeageY;
if (ctrlLastWidth == 0)
ctrlLastWidth = ctrlWidth;
if (ctrlLastHeight == 0)
ctrlLastHeight = ctrlHeight;
if (ctrlIsResizing)
{
ctrlRectangle.Location = new System.Drawing.Point(this.frm.DesktopLocation.X + this.ctrl.Left, this.frm.DesktopLocation.Y + this.ctrl.Top);
ctrlRectangle.Size = new System.Drawing.Size(ctrlLastWidth, ctrlLastHeight);
}
ctrlIsResizing = true;
ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
ctrlLastWidth = ctrlWidth;
ctrlLastHeight = ctrlHeight;
ctrlRectangle.Location = new System.Drawing.Point(this.frm.DesktopLocation.X + this.ctrl.Left, this.frm.DesktopLocation.Y + this.ctrl.Top);
ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth, ctrlHeight);
ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
}
}
private void MouseDown(object sender, MouseEventArgs e)
{
if (frm == null)
return;
if (e.X < this.ctrl.Width - 10 || e.Y < this.ctrl.Height - 10)
{
this.IsMoving = true;
this.ctrlLeft = this.frm.DesktopLocation.X + this.ctrl.Left;
this.ctrlTop = this.frm.DesktopLocation.Y + this.ctrl.Top;
this.cursorL = Cursor.Position.X;
this.cursorT = Cursor.Position.Y;
this.ctrlWidth = this.ctrl.Width;
this.ctrlHeight = this.ctrl.Height;
}
ctrlRectangle.Location = new System.Drawing.Point(this.ctrlLeft + this.point.X, this.ctrlTop + this.point.Y);
ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth, ctrlHeight);
ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
}
private void MouseUp(object sender, MouseEventArgs e)
{
if (frm == null)
return;
ctrlIsResizing = false;
if (this.IsMoving)
{
ctrlRectangle.Location = new System.Drawing.Point(this.ctrlLeft, this.ctrlTop);
ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth, ctrlHeight);
ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
this.ctrl.Left = this.ctrlLeft - this.frm.DesktopLocation.X;
this.ctrl.Top = this.ctrlTop - this.frm.DesktopLocation.Y;
this.IsMoving = false;
return;
}
ctrlRectangle.Location = new System.Drawing.Point(this.frm.DesktopLocation.X + this.ctrl.Left, this.frm.DesktopLocation.Y + this.ctrl.Top);
ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth, ctrlHeight);
ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);
this.ctrl.Width = ctrlWidth;
this.ctrl.Height = ctrlHeight;
}
}
}
- 控件 鼠标移动
- 鼠标移动隐藏控件
- 鼠标拖动(移动、拖曳)控件
- button控件鼠标移动CSS效果
- flex控件鼠标移动前后样式
- JS实现控件跟随鼠标移动
- C# 鼠标拖拉移动窗体控件
- 鼠标移动窗体或点住控件移动窗体
- Winform鼠标拖动改变控件大小、移动控件位置
- 实现让一个控件跟着鼠标一起移动? vb.net
- 在运行时通过鼠标拖动移动控件位置(c#)
- 让Shockwave flash控件响应鼠标移动的事件
- 在运行时通过鼠标拖动移动控件位置(c#)
- 在运行时通过鼠标拖动移动控件位置(c#)
- 用鼠标左击事件实现移动WinForm中控件
- MFC:为STATIC控件添加鼠标移动事件(VS2010)
- 鼠标移动到控件上显示提示信息
- [MFC] 鼠标移动到控件上显示提示信息
- Zend Framework中的MVC架构 2
- Ubuntu NetWork Configure(linux网络配置)
- 去年这时候又辞退了一个老油条,不知道他现在是否在开公司了,可以对比一下混工资的水平
- gdb(1)
- 错误"ORA-04091: table is mutating, trigger/function may not see it"的原因以及解决办法
- 控件 鼠标移动
- 用MD5加密
- java反射简单理解
- String.Trim()真相大揭秘(转:http://www.cnblogs.com/breezewoo/archive/2010/01/13/string_trim.html)
- 使用lwip的DNS功能查询域名对应的IP地址
- 扩展/自定义 Extjs.Toolbar 组件
- telephony
- VB常用代码总结
- PL/SQL 常用函数