C# 鼠标拉伸、移动控件 改变鼠标形状

摘要:
refCursorcursor){boolret=false;Pointcontrolpoint=control.PointToClient(Cursor.Position);control.Size.Width-m_border&control.Size。Height-m_border){type=ResizeType.RightBottom;

这是在昨天的移动控件的基础上增加了拉伸功能

代码看起来长了很多,主要是因为拉伸有8种情况

拉伸时要判断鼠标是否处于边缘,根据位置改变鼠标形状(感觉完全可以由系统直接支持啊)

用法仍然是对控件调用EnableDrag即可

关于Tag,控件都有个Tag,用来放一些用户数据,使用时需要做类型转换,我用Tag存储了控件的位置和大小,方便另外的目的(如何修改代码以便运行,代码中有说明)

    class DragControl
    {
        private enum ResizeType { Left, Right, Top, Bottom, LeftTop, RightTop, LeftBottom, RightBottom };//拉伸的8种情况
        static private int m_border = 5;//边缘宽度
        static private ResizeType m_ResizeType;//拉伸操作的边框
        static public bool m_OnResize = false;//处于拉伸模式
        static public bool m_OnDrag = false;//处于移动模式
        static private Point m_LastPoint = new Point();

//入口点,对控件调用这个
static public void EnableDrag(Control control) { //添加事件处理程序 control.MouseDown += new MouseEventHandler(MouseDown); control.MouseMove += new MouseEventHandler(MouseMove); control.MouseUp += new MouseEventHandler(MouseUp); }
//判断鼠标是否处于边缘,获得边缘类型(四条边四个角)以及对应的光标
static private bool isBorder(Control control, Point newpoint,ref ResizeType type,ref Cursor cursor) { bool ret = false; Point controlpoint = control.PointToClient(Cursor.Position); if (controlpoint.X < m_border && controlpoint.Y < m_border) { type = ResizeType.LeftTop; cursor = Cursors.SizeNWSE; ret = true; } else if (controlpoint.X > control.Size.Width - m_border && controlpoint.Y < m_border) { type = ResizeType.RightTop; cursor = Cursors.SizeNESW; ret = true; } else if (controlpoint.X < m_border && controlpoint.Y > control.Size.Height - m_border) { type = ResizeType.LeftBottom; cursor = Cursors.SizeNESW; ret = true; } else if (controlpoint.X > control.Size.Width - m_border && controlpoint.Y > control.Size.Height - m_border) { type = ResizeType.RightBottom; cursor = Cursors.SizeNWSE; ret = true; } else if (controlpoint.X < m_border) { type = ResizeType.Left; cursor = Cursors.SizeWE; ret = true; } else if (controlpoint.X > control.Size.Width - m_border) { type = ResizeType.Right; cursor = Cursors.SizeWE; ret = true; } else if (controlpoint.Y > control.Size.Height - m_border) { type = ResizeType.Bottom; cursor = Cursors.SizeNS; ret = true; } else if (controlpoint.Y < m_border) { type = ResizeType.Top; cursor = Cursors.SizeNS; ret = true; } return ret; } static private void MouseDown(object sender, MouseEventArgs e) { m_OnResize = false; m_OnDrag = false; Control control = (Control)sender; Point newpoint = control.Parent.PointToClient(Cursor.Position); Cursor cursor = new Cursor(Cursor.Current.Handle);//Cursor的构造函数是不需要,但是下面调用的时候要求初始化过,或许这个参数应该用out m_OnResize = isBorder(control, newpoint, ref m_ResizeType, ref cursor); if (m_OnResize) { control.Cursor = cursor; } else {//不是边缘就是移动模式 control.Cursor = Cursors.SizeAll; m_OnDrag = true; } m_LastPoint = newpoint; } static private void MouseMove(object sender, MouseEventArgs e) { Control control = (Control)sender; Point newpoint = control.Parent.PointToClient(Cursor.Position);//当前鼠标位置 if (!m_OnDrag && !m_OnResize) {//还没按下鼠标,只显示光标 Cursor cursor = new Cursor(Cursor.Current.Handle); if (isBorder(control, newpoint, ref m_ResizeType, ref cursor)) { control.Cursor = cursor; } else { control.Cursor = Cursors.Default; } return; } Point moved = new Point(newpoint.X - m_LastPoint.X, newpoint.Y - m_LastPoint.Y);//鼠标位置变化量
//tvi里面放了几个变量,替换了即可
//tvi.m_AutoLocation bool 表明这个对象位置大小是否自动计算(是否经过了定制),就一句代码用到,删掉
//tvi.m_location Point 对象的位置,用临时变量替换即可,初值=control.Location
//tvi.m_size Size 对象的大小,用临时变量替换即可,初值=control.Size
//tvi.m_view 就是对象本身,用control替换即可
TableRelation.TableViewTag tvi
= (TableRelation.TableViewTag)control.Tag; tvi.m_AutoLocation = false; if (m_OnDrag) { tvi.m_location.X += moved.X; tvi.m_location.Y += moved.Y; } if (m_OnResize) { switch (m_ResizeType) { case ResizeType.Left: tvi.m_size = new Size(control.Size.Width - moved.X, control.Size.Height); tvi.m_location = new Point(control.Location.X + moved.X, control.Location.Y); break; case ResizeType.Right: tvi.m_size = new Size(control.Size.Width + moved.X, control.Size.Height); break; case ResizeType.Top: tvi.m_size = new Size(control.Size.Width, control.Size.Height - moved.Y); tvi.m_location = new Point(control.Location.X, control.Location.Y + moved.Y); break; case ResizeType.Bottom: tvi.m_size = new Size(control.Size.Width, control.Size.Height + moved.Y); break; case ResizeType.LeftTop: tvi.m_size = new Size(control.Size.Width - moved.X, control.Size.Height - moved.Y); tvi.m_location = new Point(control.Location.X + moved.X, control.Location.Y + moved.Y); break; case ResizeType.LeftBottom: tvi.m_size = new Size(control.Size.Width - moved.X, control.Size.Height + moved.Y); tvi.m_location = new Point(control.Location.X + moved.X, control.Location.Y); break; case ResizeType.RightTop: tvi.m_size = new Size(control.Size.Width + moved.X, control.Size.Height - moved.Y); tvi.m_location = new Point(control.Location.X, control.Location.Y + moved.Y); break; case ResizeType.RightBottom: tvi.m_size = new Size(control.Size.Width + moved.X, control.Size.Height + moved.Y); break; default: MessageBox.Show(m_ResizeType.ToString()); break; } } tvi.m_view.Location = tvi.m_location; tvi.m_view.Size = tvi.m_size; m_LastPoint = newpoint; } static private void MouseUp(object sender, MouseEventArgs e) { Control control = (Control)sender; control.Cursor = Cursors.Default; m_OnResize = false; m_OnDrag = false; } }

免责声明:文章转载自《C# 鼠标拉伸、移动控件 改变鼠标形状》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇SQL 语句where条件中分情况用node.js解决编程题的输入问题下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Style.BasedOn 属性

Style.BasedOn 属性可通过多种方式在 WPF 的样式可以扩展或继承的。 样式可以基于其他样式通过此属性。 当您使用此属性,样式将继承该样式没有显式重新定义原始样式的值。 在下面的示例中, Style2 继承 Yellow的 Control.Background 值,并添加 Blue的 Control.Foreground 值。 <Styl...

C# WinForm遍历窗体控件的3种方法

C# WinForm遍历窗体控件的3种方法 转 https://www.cnblogs.com/zhaoshujie/p/14638460.html 1.循环遍历 private void GetControls(Control fatherControl) { Control.ControlCollection sonControls = fa...