运行时的活动控件,控件可拖拽和改变大小,鼠标在控件上游走。
当鼠标到达控件边角时,改变光标外观。
如何实现??

解决方案 »

  1.   

    部分代码如下:
    private EnumMousePointPosition MousePointPosition(Size size,MouseEventArgs e)
            {
                const int band = 10;
                //鼠标在size区域内时
                if (e.X > -1 * band & e.X <= size.Width & e.Y >= -1 * band && e.Y <= size.Height)
                {
                    #region//鼠标在左边时
                    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;
                        }
                    }
                    #endregion
                    #region//鼠标在右边时
                    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;
                        }
                    }
                    #endregion
                    #region//鼠标在上方时
                    if (e.Y < band)
                    {
                        return EnumMousePointPosition.MouseSizeTop;
                    }
                    #endregion
                    #region//鼠标在下方时
                    else if (e.Y > -1*band + size.Height)
                    {
                        return EnumMousePointPosition.MouseSizeBottom;
                    }
                    #endregion
                    #region//鼠标不在上述区域而在Size区域内时
                    else
                    {
                        return EnumMousePointPosition.MouseDrag;
                    }
                    #endregion
                }
                //鼠标不在Size区域中
                else
                {
                    return EnumMousePointPosition.MouseSizeNone;
                }
            }
      

  2.   

    我记得有几个API是可以直接拖拉改变控件大小的
      

  3.   

    这样吧,如果你要处理的都是Window的控件,比如Button,TextBox等,可以从这些控件继承,然后按下的样子来改后再使用控件就可以 了:调整大小位置和鼠标样式都是标准的class ButtonEx : Button//这里可以是任何一个从Coutrol继承的类,(Label除外,因国Label有自己的处理)
    {
        private int m_BorderWidth = 4;    internal static int WM_NCHITTEST = 0x84; //移动鼠标,按住或释放鼠标时发生的系统消息
        internal static int WM_NCACTIVATE = 0x86;//窗体的激活状态发生改变的消息    internal static IntPtr HTCLIENT = (IntPtr)0x1;//工作区
        internal static IntPtr HTSYSMENU = (IntPtr)3;//系统菜单
        internal static IntPtr HTCAPTION = (IntPtr)0x2; //标题栏    internal static IntPtr HTLEFT = (IntPtr)10;//向左
        internal static IntPtr HTRIGHT = (IntPtr)11;//向右
        internal static IntPtr HTTOP = (IntPtr)12;//向上
        internal static IntPtr HTTOPLEFT = (IntPtr)13;//向左上
        internal static IntPtr HTTOPRIGHT = (IntPtr)14;//向右上
        internal static IntPtr HTBOTTOM = (IntPtr)15;//向下
        internal static IntPtr HTBOTTOMLEFT = (IntPtr)16;//向左下
        internal static IntPtr HTBOTTOMRIGHT = (IntPtr)17;//向右下    protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_NCHITTEST)
            {
                base.WndProc(ref m);
                if (DesignMode)
                {
                    return;
                }
                if (m.Result == HTCLIENT)
                {
                    m.HWnd = this.Handle;                System.Drawing.Rectangle rect = this.RectangleToScreen(this.ClientRectangle);
                    Point C_Pos = Cursor.Position;
                    if ((Cursor.Position.X <= rect.Left + m_BorderWidth) && (C_Pos.Y <= rect.Top + m_BorderWidth))
                        m.Result = HTTOPLEFT;//左上
                    else if ((C_Pos.X >= rect.Left + rect.Width - m_BorderWidth) && (C_Pos.Y <= rect.Top + m_BorderWidth))
                        m.Result = HTTOPRIGHT;//右上
                    else if ((C_Pos.X <= rect.Left + m_BorderWidth) && (C_Pos.Y >= rect.Top + rect.Height - m_BorderWidth))
                        m.Result = HTBOTTOMLEFT;//左下
                    else if ((C_Pos.X >= rect.Left + rect.Width - m_BorderWidth) && (C_Pos.Y >= rect.Top + rect.Height - m_BorderWidth))
                        m.Result = HTBOTTOMRIGHT;//右下
                    else if ((C_Pos.X <= rect.Left + m_BorderWidth - 1))
                        m.Result = HTLEFT;//左
                    else if ((C_Pos.X >= rect.Left + rect.Width - m_BorderWidth))
                        m.Result = HTRIGHT;//右
                    else if ((C_Pos.Y <= rect.Top + m_BorderWidth - 1))
                        m.Result = HTTOP;//上
                    else if ((C_Pos.Y >= rect.Top + rect.Height - m_BorderWidth))
                        m.Result = HTBOTTOM;//下
                    else if (C_Pos.Y <= rect.Top + m_BorderWidth)
                    {
                        m.Result = HTCAPTION;//模拟标题栏,移动或双击可以最大或最小化窗体
                    }
                }
                return;
            }
            base.WndProc(ref m);
        }
    }
      

  4.   

    给你个类文件吧,使用如下的类,可以声明它的一个实例并把要操作的控件传入就行了:
    使用上可以这样://操作button1控件
     MoveableWindow mw = new MoveableWindow(this.Button1);
    类的代码:class MoveableWindow : NativeWindow
    {
        private int m_BorderWidth = 4;    internal static int WM_NCHITTEST = 0x84; //移动鼠标,按住或释放鼠标时发生的系统消息
        internal static int WM_NCACTIVATE = 0x86;//窗体的激活状态发生改变的消息    internal static IntPtr HTCLIENT = (IntPtr)0x1;//工作区
        internal static IntPtr HTSYSMENU = (IntPtr)3;//系统菜单
        internal static IntPtr HTCAPTION = (IntPtr)0x2; //标题栏    internal static IntPtr HTLEFT = (IntPtr)10;//向左
        internal static IntPtr HTRIGHT = (IntPtr)11;//向右
        internal static IntPtr HTTOP = (IntPtr)12;//向上
        internal static IntPtr HTTOPLEFT = (IntPtr)13;//向左上
        internal static IntPtr HTTOPRIGHT = (IntPtr)14;//向右上
        internal static IntPtr HTBOTTOM = (IntPtr)15;//向下
        internal static IntPtr HTBOTTOMLEFT = (IntPtr)16;//向左下
        internal static IntPtr HTBOTTOMRIGHT = (IntPtr)17;//向右下    private Control m_control;
        public MoveableWindow(Control control)
        {
            m_control = control;
            this.AssignHandle(control.Handle);
        }
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_NCHITTEST)
            {
                base.WndProc(ref m);
                if (m.Result == HTCLIENT)
                {
                    m.HWnd = this.Handle;                System.Drawing.Rectangle rect = m_control.RectangleToScreen(m_control.ClientRectangle);
                    Point C_Pos = Cursor.Position;
                    if ((Cursor.Position.X <= rect.Left + m_BorderWidth) && (C_Pos.Y <= rect.Top + m_BorderWidth))
                        m.Result = HTTOPLEFT;//左上
                    else if ((C_Pos.X >= rect.Left + rect.Width - m_BorderWidth) && (C_Pos.Y <= rect.Top + m_BorderWidth))
                        m.Result = HTTOPRIGHT;//右上
                    else if ((C_Pos.X <= rect.Left + m_BorderWidth) && (C_Pos.Y >= rect.Top + rect.Height - m_BorderWidth))
                        m.Result = HTBOTTOMLEFT;//左下
                    else if ((C_Pos.X >= rect.Left + rect.Width - m_BorderWidth) && (C_Pos.Y >= rect.Top + rect.Height - m_BorderWidth))
                        m.Result = HTBOTTOMRIGHT;//右下
                    else if ((C_Pos.X <= rect.Left + m_BorderWidth - 1))
                        m.Result = HTLEFT;//左
                    else if ((C_Pos.X >= rect.Left + rect.Width - m_BorderWidth))
                        m.Result = HTRIGHT;//右
                    else if ((C_Pos.Y <= rect.Top + m_BorderWidth - 1))
                        m.Result = HTTOP;//上
                    else if ((C_Pos.Y >= rect.Top + rect.Height - m_BorderWidth))
                        m.Result = HTBOTTOM;//下
                    else
                    {
                        m.Result = HTCAPTION;//模拟标题栏,移动或双击可以最大或最小化窗体
                    }
                }
                return;
            }
            base.WndProc(ref m);
        }
    }
      

  5.   

    你这样来写吧:希望你能看明白,比较乱
    internal enum MouseAction
    {
        None,
        CrossLeftTop,
        CrossLeftBottom,
        CrossRightTop,
        CrossRightBottom,
        SizeAll,
        SizeLeft,
        SizeTop,
        SizeRight,
        SizeBottom,
        SizeLeftTop,
        SizeRightTop,
        SizeRightBottom,
        SizeLeftBottom,
    }private Rectangle lastBounds;
    private Point lastMsPoint;
    private Size lastSize;
    private void label1_MouseDown(object sender, MouseEventArgs e)
    {
        this.lastBounds = this.label1.Bounds;
        this.lastSize = this.label1.Size;
        this.lastMsPoint = Control.MousePosition;
    }MouseAction ma = MouseAction.None;
    private void label1_MouseMove(object sender, MouseEventArgs e)
    {
        Point mp = Control.MousePosition;
        if (!(sender as Label).Capture)
        {
            Rectangle bound = (sender as Label).Bounds;
            ma = this.getAction(bound, this.PointToClient(mp));
            this.SetCursor(ma);
            return;
        }    if (e.Button == MouseButtons.Left)
        {
            switch (ma)
            {
                case MouseAction.SizeAll:
                    this.label1.Location = new Point(this.lastBounds.X + mp.X - this.lastMsPoint.X, this.lastBounds.Y + mp.Y - this.lastMsPoint.Y);
                    break;
                case MouseAction.SizeLeft:
                    this.label1.Bounds = Rectangle.FromLTRB(this.lastBounds.X + mp.X - this.lastMsPoint.X, this.lastBounds.Y, lastBounds.Right, lastBounds.Bottom);
                    break;
                case MouseAction.SizeTop:
                    this.label1.Bounds = Rectangle.FromLTRB(this.lastBounds.X, this.lastBounds.Y + mp.Y - this.lastMsPoint.Y, lastBounds.Right, lastBounds.Bottom);
                    break;
                case MouseAction.SizeRight:
                    this.label1.Bounds = Rectangle.FromLTRB(this.lastBounds.X, this.lastBounds.Y + mp.Y - this.lastMsPoint.Y, lastBounds.Right + mp.X - this.lastMsPoint.X, lastBounds.Bottom);
                    break;
                case MouseAction.SizeBottom:
                    this.label1.Bounds = Rectangle.FromLTRB(this.lastBounds.X, this.lastBounds.Y, lastBounds.Right, lastBounds.Bottom + mp.Y - this.lastMsPoint.Y);
                    break;
                case MouseAction.SizeLeftTop:
                    this.label1.Bounds = Rectangle.FromLTRB(this.lastBounds.X + mp.X - this.lastMsPoint.X, this.lastBounds.Y + mp.Y - this.lastMsPoint.Y, lastBounds.Right, lastBounds.Bottom);
                    break;
                case MouseAction.SizeLeftBottom:
                    this.label1.Bounds = Rectangle.FromLTRB(this.lastBounds.X + mp.X - this.lastMsPoint.X, this.lastBounds.Y, lastBounds.Right, lastBounds.Bottom + mp.Y - this.lastMsPoint.Y);
                    break;
                case MouseAction.SizeRightBottom:
                    this.label1.Bounds = Rectangle.FromLTRB(this.lastBounds.X, this.lastBounds.Y, lastBounds.Right + mp.X - this.lastMsPoint.X, lastBounds.Bottom + mp.Y - this.lastMsPoint.Y);
                    break;
                case MouseAction.SizeRightTop:
                    this.label1.Bounds = Rectangle.FromLTRB(this.lastBounds.X, this.lastBounds.Y + mp.Y - this.lastMsPoint.Y, lastBounds.Right + mp.X - this.lastMsPoint.X, lastBounds.Bottom);
                    break;
            }
        }
    }private MouseAction getAction(Rectangle bound, Point e)
    {
        int borderWidth = 6;
        Rectangle outerRect = bound;
        Rectangle innerRect = outerRect;
        innerRect.Inflate(-borderWidth, -borderWidth);
        if (innerRect.Contains(e))
        {
            return MouseAction.SizeAll;
        }
        else if (e.X > outerRect.X && e.X < innerRect.X && e.Y > outerRect.Y && e.Y < innerRect.Y)//左上
        {
            return MouseAction.SizeLeftTop;
        }
        else if (e.X < outerRect.Right && e.X > innerRect.Right && e.Y > outerRect.Y && e.Y < innerRect.Y)//右上
        {
            return MouseAction.SizeRightTop;
        }
        else if (e.X > outerRect.X && e.X < innerRect.X && e.Y < outerRect.Bottom && e.Y > innerRect.Bottom)//左下
        {
            return MouseAction.SizeLeftBottom;
        }
        else if (e.X < outerRect.Right && e.X > innerRect.Right && e.Y < outerRect.Bottom && e.Y > innerRect.Bottom)//右下
        {
            return MouseAction.SizeRightBottom;
        }
        else if (e.X > outerRect.X && e.X < innerRect.X)
        {
            return MouseAction.SizeLeft;
        }
        else if (e.X < outerRect.Right && e.X > innerRect.Right)
        {
            return MouseAction.SizeRight;
        }
        else if (e.Y > outerRect.X && e.Y < innerRect.Y)
        {
            return MouseAction.SizeTop;
        }
        else if (e.Y < outerRect.Bottom && e.Y > innerRect.Bottom)
        {
            return MouseAction.SizeBottom;
        }
        return MouseAction.None;
    }
    private void SetCursor(MouseAction action)
    {
        switch (action)
        {
            case MouseAction.None:
                this.Cursor = Cursors.Default;
                break;
            case MouseAction.CrossLeftBottom:
                this.Cursor = Cursors.Cross;
                break;
            case MouseAction.CrossLeftTop:
                this.Cursor = Cursors.Cross;
                break;
            case MouseAction.CrossRightBottom:
                this.Cursor = Cursors.Cross;
                break;
            case MouseAction.CrossRightTop:
                this.Cursor = Cursors.Cross;
                break;
            case MouseAction.SizeAll:
                this.Cursor = Cursors.SizeAll;
                break;
            case MouseAction.SizeBottom:
                this.Cursor = Cursors.SizeNS;
                break;
            case MouseAction.SizeLeft:
                this.Cursor = Cursors.SizeWE;
                break;
            case MouseAction.SizeLeftBottom:
                this.Cursor = Cursors.SizeNESW;
                break;
            case MouseAction.SizeLeftTop:
                this.Cursor = Cursors.SizeNWSE;
                break;
            case MouseAction.SizeRight:
                this.Cursor = Cursors.SizeWE;
                break;
            case MouseAction.SizeRightBottom:
                this.Cursor = Cursors.SizeNWSE;
                break;
            case MouseAction.SizeRightTop:
                this.Cursor = Cursors.SizeNESW;
                break;
            case MouseAction.SizeTop:
                this.Cursor = Cursors.SizeNS;
                break;
            default:
                this.Cursor = Cursors.Default;
                break;
        }
    }
      

  6.   

    因为你那是Label,这个控件在内部代码做了处理,我在上面的代码里做了注了。你再试下新的代码看看吧。
      

  7.   

    这样吧,我改成了一个类了,这个类从Label继承下来的,你在你的项目里把所有类都换成这个新的Lable类,就可以了。
    要做的只是把你的原来的Label类型改成新的Label类型,类代码如下:
    internal class SizeableLabel : Label
    {
        internal enum MouseAction
        {
            None,
            CrossLeftTop,
            CrossLeftBottom,
            CrossRightTop,
            CrossRightBottom,
            SizeAll,
            SizeLeft,
            SizeTop,
            SizeRight,
            SizeBottom,
            SizeLeftTop,
            SizeRightTop,
            SizeRightBottom,
            SizeLeftBottom,
        }
        private Rectangle lastBounds;
        private Point lastMsPoint;
        private MouseAction ma = MouseAction.None;    protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            this.lastBounds = this.Bounds;
            this.lastMsPoint = this.Parent.PointToClient(Control.MousePosition);
        }
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            Point mp = this.Parent.PointToClient(Control.MousePosition);
            if (!this.Capture)
            {
                Rectangle bound = this.Bounds;
                ma = this.getAction(bound, mp);
                this.setCursor(ma);
                return;
            }        if (e.Button == MouseButtons.Left)
            {
                switch (ma)
                {
                    case MouseAction.SizeAll:
                        this.Location = new Point(this.lastBounds.X + mp.X - this.lastMsPoint.X, this.lastBounds.Y + mp.Y - this.lastMsPoint.Y);
                        break;
                    case MouseAction.SizeLeft:
                        this.Bounds = Rectangle.FromLTRB(this.lastBounds.X + mp.X - this.lastMsPoint.X, this.lastBounds.Y, lastBounds.Right, lastBounds.Bottom);
                        break;
                    case MouseAction.SizeTop:
                        this.Bounds = Rectangle.FromLTRB(this.lastBounds.X, this.lastBounds.Y + mp.Y - this.lastMsPoint.Y, lastBounds.Right, lastBounds.Bottom);
                        break;
                    case MouseAction.SizeRight:
                        this.Bounds = Rectangle.FromLTRB(this.lastBounds.X, this.lastBounds.Y + mp.Y - this.lastMsPoint.Y, lastBounds.Right + mp.X - this.lastMsPoint.X, lastBounds.Bottom);
                        break;
                    case MouseAction.SizeBottom:
                        this.Bounds = Rectangle.FromLTRB(this.lastBounds.X, this.lastBounds.Y, lastBounds.Right, lastBounds.Bottom + mp.Y - this.lastMsPoint.Y);
                        break;
                    case MouseAction.SizeLeftTop:
                        this.Bounds = Rectangle.FromLTRB(this.lastBounds.X + mp.X - this.lastMsPoint.X, this.lastBounds.Y + mp.Y - this.lastMsPoint.Y, lastBounds.Right, lastBounds.Bottom);
                        break;
                    case MouseAction.SizeLeftBottom:
                        this.Bounds = Rectangle.FromLTRB(this.lastBounds.X + mp.X - this.lastMsPoint.X, this.lastBounds.Y, lastBounds.Right, lastBounds.Bottom + mp.Y - this.lastMsPoint.Y);
                        break;
                    case MouseAction.SizeRightBottom:
                        this.Bounds = Rectangle.FromLTRB(this.lastBounds.X, this.lastBounds.Y, lastBounds.Right + mp.X - this.lastMsPoint.X, lastBounds.Bottom + mp.Y - this.lastMsPoint.Y);
                        break;
                    case MouseAction.SizeRightTop:
                        this.Bounds = Rectangle.FromLTRB(this.lastBounds.X, this.lastBounds.Y + mp.Y - this.lastMsPoint.Y, lastBounds.Right + mp.X - this.lastMsPoint.X, lastBounds.Bottom);
                        break;
                }
            }
        }
        private MouseAction getAction(Rectangle bound, Point e)
        {
            int borderWidth = 6;
            Rectangle outerRect = bound;
            Rectangle innerRect = outerRect;
            innerRect.Inflate(-borderWidth, -borderWidth);
            if (innerRect.Contains(e))
            {
                return MouseAction.SizeAll;
            }
            else if (e.X > outerRect.X && e.X < innerRect.X && e.Y > outerRect.Y && e.Y < innerRect.Y)//左上
            {
                return MouseAction.SizeLeftTop;
            }
            else if (e.X < outerRect.Right && e.X > innerRect.Right && e.Y > outerRect.Y && e.Y < innerRect.Y)//右上
            {
                return MouseAction.SizeRightTop;
            }
            else if (e.X > outerRect.X && e.X < innerRect.X && e.Y < outerRect.Bottom && e.Y > innerRect.Bottom)//左下
            {
                return MouseAction.SizeLeftBottom;
            }
            else if (e.X < outerRect.Right && e.X > innerRect.Right && e.Y < outerRect.Bottom && e.Y > innerRect.Bottom)//右下
            {
                return MouseAction.SizeRightBottom;
            }
            else if (e.X > outerRect.X && e.X < innerRect.X)
            {
                return MouseAction.SizeLeft;
            }
            else if (e.X < outerRect.Right && e.X > innerRect.Right)
            {
                return MouseAction.SizeRight;
            }
            else if (e.Y > outerRect.X && e.Y < innerRect.Y)
            {
                return MouseAction.SizeTop;
            }
            else if (e.Y < outerRect.Bottom && e.Y > innerRect.Bottom)
            {
                return MouseAction.SizeBottom;
            }
            return MouseAction.None;
        }    private void setCursor(MouseAction action)
        {
            switch (action)
            {
                case MouseAction.None:
                    this.Cursor = Cursors.Default;
                    break;
                case MouseAction.CrossLeftBottom:
                    this.Cursor = Cursors.Cross;
                    break;
                case MouseAction.CrossLeftTop:
                    this.Cursor = Cursors.Cross;
                    break;
                case MouseAction.CrossRightBottom:
                    this.Cursor = Cursors.Cross;
                    break;
                case MouseAction.CrossRightTop:
                    this.Cursor = Cursors.Cross;
                    break;
                case MouseAction.SizeAll:
                    this.Cursor = Cursors.SizeAll;
                    break;
                case MouseAction.SizeBottom:
                    this.Cursor = Cursors.SizeNS;
                    break;
                case MouseAction.SizeLeft:
                    this.Cursor = Cursors.SizeWE;
                    break;
                case MouseAction.SizeLeftBottom:
                    this.Cursor = Cursors.SizeNESW;
                    break;
                case MouseAction.SizeLeftTop:
                    this.Cursor = Cursors.SizeNWSE;
                    break;
                case MouseAction.SizeRight:
                    this.Cursor = Cursors.SizeWE;
                    break;
                case MouseAction.SizeRightBottom:
                    this.Cursor = Cursors.SizeNWSE;
                    break;
                case MouseAction.SizeRightTop:
                    this.Cursor = Cursors.SizeNESW;
                    break;
                case MouseAction.SizeTop:
                    this.Cursor = Cursors.SizeNS;
                    break;
                default:
                    this.Cursor = Cursors.Default;
                    break;
            }
        }
    }