如何让用户自由改变控件大小和位置???

解决方案 »

  1.   

    Private Const GWL_STYLE = (-16)
    Private Const WS_THICKFRAME = &H40000
    Private Const SWP_NOSIZE = &H1
    Private Const SWP_NOZORDER = &H4
    Private Const SWP_NOMOVE = &H2
    Private Const SWP_DRAWFRAME = &H20Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
    Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As LongPrivate Sub ControlSize(ControlName As Control, SetTrue As Boolean)
        Dim dwStyle As Long
        dwStyle = GetWindowLong(ControlName.hwnd, GWL_STYLE)
        If SetTrue Then
          dwStyle = dwStyle Or WS_THICKFRAME
        Else
          dwStyle = dwStyle - WS_THICKFRAME
        End If
        dwStyle = SetWindowLong(ControlName.hwnd, GWL_STYLE, dwStyle)
        SetWindowPos ControlName.hwnd, ControlName.Parent.hwnd, 0, 0, 0, 0, SWP_NOZORDER Or SWP_NOSIZE Or SWP_NOMOVE Or SWP_DRAWFRAME
    End SubPrivate Sub Form_Load()
        ControlSize Picture1, True
        ControlSize Text1, True
    End Sub
      

  2.   

    思路:用一个变量记录鼠标上次在这个按钮的位置, 下次移动的位置和上次的位置做相对 计算, 然后再在form里头移动就好了具体不写了, 我实现过, 按下鼠标拖动控件
      

  3.   

    '窗体上放 TextBox、PictureBox、CheckBox 各一个'
    Option ExplicitPrivate Sub Form_DragOver(Source As Control, X As Single, Y As Single, State As Integer)
        If Source Is Picture1 Then
            With Text1
                If (X > .Left) And (Y > .Top) Then
                    Text1.Move .Left, .Top, X - .Left, Y - .Top
                    Picture1.Move .Left + .Width, .Top + .Height
                End If
            End With
        End If
    End SubPrivate Sub Form_Load()
        Picture1.DragMode = vbAutomatic
        Picture1.Visible = False
        Picture1.ZOrder
        Check1.Caption = "调整大小"
    End SubPrivate Sub Check1_Click()
        Picture1.Move Text1.Left + Text1.Width, Text1.Top + Text1.Height, 120, 120
        Picture1.Visible = Check1.Value
    End SubPrivate Sub Check1_DragOver(Source As Control, X As Single, Y As Single, State As Integer)
        Form_DragOver Source, Check1.Left + X, Check1.Top + Y, State
    End SubPrivate Sub Text1_DragOver(Source As Control, X As Single, Y As Single, State As Integer)
        Form_DragOver Source, Text1.Left + X, Text1.Top + Y, State
    End Sub