欢迎光临

木子屋

标题: C#控件的拖动和缩放技术全解

作者: dnawo

日期: 2009-04-07 04:01 PM

分类: Win编程

内容: 先在代码页面中加入如下代码：
复制内容到剪贴板 程序代码const int BAND = 5;
const int MINWIDTH = 10;
const int MINHEIGHT = 10;
private EnumMousePointPosition m_MousePointPosition;
private Point p1, p2;
/// <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 // '鼠标拖动
}
/// <summary>
/// 确定光标在控件不同位置的样式
/// </summary>
/// <param name="size"></param>
/// <param name="e"></param>
/// <returns></returns>
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; }
}
/// <summary>
/// MouseDown事件处理程序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MyMouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
p1.X = e.X;
p1.Y = e.Y;
p2.X = e.X;
p2.Y = e.Y;
}
/// <summary>
/// MouseLeave事件处理程序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MyMouseLeave(object sender, System.EventArgs e)
{
m_MousePointPosition = EnumMousePointPosition.MouseSizeNone;
this.Cursor = Cursors.Arrow;
}
/// <summary>
/// MouseMove事件处理程序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MyMouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
Control lCtrl = (sender as Control);
if (e.Button == MouseButtons.Left)
{
switch (m_MousePointPosition)
{
case EnumMousePointPosition.MouseDrag:
lCtrl.Left = lCtrl.Left + e.X - p1.X;
lCtrl.Top = lCtrl.Top + e.Y - p1.Y;
break;
case EnumMousePointPosition.MouseSizeBottom:
lCtrl.Height = lCtrl.Height + e.Y - p2.Y;
p2.X = e.X;
p2.Y = e.Y; //'记录光标拖动的当前点
break;
case EnumMousePointPosition.MouseSizeBottomRight:
lCtrl.Width = lCtrl.Width + e.X - p2.X;
lCtrl.Height = lCtrl.Height + e.Y - p2.Y;
p2.X = e.X;
p2.Y = e.Y; //'记录光标拖动的当前点
break;
case EnumMousePointPosition.MouseSizeRight:
lCtrl.Width = lCtrl.Width + e.X - p2.X;
// lCtrl.Height = lCtrl.Height + e.Y - p1.Y;
p2.X = e.X;
p2.Y = e.Y; //'记录光标拖动的当前点
break;
case EnumMousePointPosition.MouseSizeTop:
lCtrl.Top = lCtrl.Top + (e.Y - p1.Y);
lCtrl.Height = lCtrl.Height - (e.Y - p1.Y);
break;
case EnumMousePointPosition.MouseSizeLeft:
lCtrl.Left = lCtrl.Left + e.X - p1.X;
lCtrl.Width = lCtrl.Width - (e.X - p1.X);
break;
case EnumMousePointPosition.MouseSizeBottomLeft:
lCtrl.Left = lCtrl.Left + e.X - p1.X;
lCtrl.Width = lCtrl.Width - (e.X - p1.X);
lCtrl.Height = lCtrl.Height + e.Y - p2.Y;
p2.X = e.X;
p2.Y = e.Y; //'记录光标拖动的当前点
break;
case EnumMousePointPosition.MouseSizeTopRight:
lCtrl.Top = lCtrl.Top + (e.Y - p1.Y);
lCtrl.Width = lCtrl.Width + (e.X - p2.X);
lCtrl.Height = lCtrl.Height - (e.Y - p1.Y);
p2.X = e.X;
p2.Y = e.Y; //'记录光标拖动的当前点
break;
case EnumMousePointPosition.MouseSizeTopLeft:
lCtrl.Left = lCtrl.Left + e.X - p1.X;
lCtrl.Top = lCtrl.Top + (e.Y - p1.Y);
lCtrl.Width = lCtrl.Width - (e.X - p1.X);
lCtrl.Height = lCtrl.Height - (e.Y - p1.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;
}
}
}
将需要进行拖动和缩放的控件的MouseDown、MouseLeave、MouseMove事件处理程序分别设置为MyMouseDown、MyMouseLeave、MyMouseMove即可，例如：
复制内容到剪贴板 程序代码pictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(MyMouseDown);
pictureBox1.MouseLeave += new System.EventHandler(MyMouseLeave);
pictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(MyMouseMove);

+ 查看当前日志评论 (0)

登录

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 2.219 ms

返回

标题: C#控件的拖动和缩放技术全解

你没有权限发表评论

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 2.219 ms

返回

暂无评论

返回

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 2.266 ms

返回