我在自定义控件中封装了一个BUTTON按钮,我想给他提供一个运行时可以拖放并改变大小这一功能,我的思路是这样的:
1。在鼠标按下时获取其矩形区域(已实现)
2。在大小改变前获取鼠标在矩形区域的位置,用来判断是向左延伸/右延伸/上延伸/下延伸/等等(已实现)
3。获取鼠标左键按下并移动时的需要改变坐标的X,Y与原X,Y的差值(已实现)
4。让控件MOVE到新的RECT区域(问题:在设计时状态如何来封装这个移动的方法?)

解决方案 »

  1.   

    晕,.什么MOVE啊,
    直接将你将
    UserControl.width = UserControl.width+X(差值)
    UserControl.height= UserControl.height+X(差值)如果不会弄.就代码给我,我帮你弄,
      

  2.   

    回复3楼:
    鼠标拖动左边框时向左延伸,右侧保持不动,你直接加是达不到那个效果的!
    同理,右拖,上拖,下拖,左上拖,右下拖,左下拖,右上拖,他们的处理方式都不一样,必须使用SetWindowPos才能达到效果
      

  3.   


    Public Function fd()
        Dim obj As Object
    For Each obj In UserControl.Parent.Controls
        If obj.hWnd = UserControl.hWnd Then
           '现在找到的控件就是当前控件
           '设置LEFT与TOP属性
           'obj.LEFT=XX
           'obj.TOP=XX
        End If
    Next
    End FunctionPublic Property Get hWnd() As Long
        hWnd = UserControl.hWnd
    End Property
      

  4.   

    'UserControl: ResizableControl
    'By Tiger_Zhao, 2008
    Option ExplicitPrivate Const HWND_DESKTOP As Long = 0Private Type POINTAPI
        X               As Long
        Y               As Long
    End TypePrivate Type RECT
        Left            As Long
        Top             As Long
        Right           As Long
        Bottom          As Long
    End TypePrivate Declare Function GetParent Lib "user32.dll" (ByVal hwnd As Long) As Long
    Private Declare Function MapWindowPoints Lib "user32.dll" (ByVal hwndFrom As Long, ByVal hwndTo As Long, ByRef lppt As Any, ByVal cPoints As Long) As Long
    Private Declare Function GetWindowRect Lib "user32.dll" (ByVal hwnd As Long, ByRef lpRect As RECT) As Long
    Private Declare Function ReleaseCapture Lib "user32.dll" () As Long
    Private Declare Function SetCapture Lib "user32.dll" (ByVal hwnd As Long) As Long
    Private Declare Function GetCursorPos Lib "user32.dll" (ByRef lpPoint As POINTAPI) As LongPrivate Enum BorderEnum
        None = 0
        Left = 1
        Right = 2
        Top = 4
        TopLeft = 5
        TopRight = 6
        Bottom = 8
        BottomLeft = 9
        BottomRight = 10
    End EnumPrivate m_DragBorder As BorderEnum
    Private m_ptStart As POINTAPIPrivate Function HitTest(ByVal X As Long, ByVal Y As Long) As BorderEnum
        Dim eReturn As BorderEnum
        
        If X <= 30 Then
            eReturn = BorderEnum.Left
        ElseIf (X >= Command1.Width - 30) Then
            eReturn = BorderEnum.Right
        End If
        If Y <= 30 Then
            eReturn = eReturn Or BorderEnum.Top
        ElseIf (Y >= Command1.Height - 30) Then
            eReturn = eReturn Or BorderEnum.Bottom
        End If
        Debug.Print X, Y, Command1.Width, Command1.Height, eReturn
        HitTest = eReturn
    End FunctionPrivate Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
        m_DragBorder = HitTest(X, Y)
        If m_DragBorder <> BorderEnum.None Then
            GetCursorPos m_ptStart
            
            ReleaseCapture
            SetCapture UserControl.hwnd
        End If
    End SubPrivate Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
        If m_DragBorder = BorderEnum.None Then
            Select Case HitTest(X, Y)
                Case BorderEnum.None
                    MousePointer = MousePointerConstants.vbDefault
                Case BorderEnum.Left, BorderEnum.Right
                    MousePointer = MousePointerConstants.vbSizeWE
                Case BorderEnum.Top, BorderEnum.Bottom
                    MousePointer = MousePointerConstants.vbSizeNS
                Case BorderEnum.TopLeft, BorderEnum.BottomRight
                    MousePointer = MousePointerConstants.vbSizeNWSE
                Case BorderEnum.TopRight, BorderEnum.BottomLeft
                    MousePointer = MousePointerConstants.vbSizeNESW
            End Select
        End If
    End SubPrivate Sub UserControl_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
        Dim ptEnd As POINTAPI, ptDelta As POINTAPI
        Dim rc As RECT
        Dim parentScale As ScaleModeConstants
        
        If m_DragBorder <> BorderEnum.None Then
            GetCursorPos ptEnd
            ptDelta.X = ptEnd.X - m_ptStart.X
            ptDelta.Y = ptEnd.Y - m_ptStart.Y
            
            GetWindowRect UserControl.hwnd, rc
            MapWindowPoints HWND_DESKTOP, GetParent(UserControl.hwnd), rc, 2
            
            If (m_DragBorder And BorderEnum.Left) <> 0 Then
                rc.Left = rc.Left + ptDelta.X
            ElseIf (m_DragBorder And BorderEnum.Right) <> 0 Then
                rc.Right = rc.Right + ptDelta.X
            End If
            If (m_DragBorder And BorderEnum.Top) <> 0 Then
                rc.Top = rc.Top + ptDelta.Y
            ElseIf (m_DragBorder And BorderEnum.Bottom) <> 0 Then
                rc.Bottom = rc.Bottom + ptDelta.Y
            End If        parentScale = UserControl.Extender.Container.ScaleMode
            UserControl.Extender.Move ScaleX(rc.Left, vbPixels, parentScale), _
                                      ScaleY(rc.Top, vbPixels, parentScale), _
                                      ScaleX(rc.Right - rc.Left, vbPixels, parentScale), _
                                      ScaleY(rc.Bottom - rc.Top, vbPixels, parentScale)
            m_DragBorder = BorderEnum.None
        End If
    End SubPrivate Sub UserControl_Resize()
        Command1.Move 0, 0, ScaleWidth, ScaleHeight
    End Sub