Results 1 to 11 of 11

Thread: Making a form moveable

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2001
    Location
    Baltimore,MD
    Posts
    536

    Making a form moveable

    I know you can make a form moveable by setting moveable to true. However if you make the control box property false and delete the caption then at runtime you cant make the form moveable is there a way I can make a form moveable through code?
    Walter Richardson
    Striver2000 Christian Productions
    Iam seventeen but since I started VB in June of 01 GOD has been helping me excell by finding this great forum with a bunch of GREAT PEOPLE!

  2. #2
    PowerPoster
    Join Date
    Aug 2002
    Location
    NY, NY
    Posts
    2,139
    Here's what's involved:
    VB Code:
    1. Option Explicit
    2.  
    3. Public Const WM_NCLBUTTONDOWN = &HA1
    4. Private Const HTCAPTION = 2
    5.  
    6. Public Declare Function ReleaseCapture Lib "user32" () As Long
    7. Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    8.  
    9. Public Sub DragForm(frm As Form)
    10. '=================================
    11. On Local Error Resume Next
    12.  
    13.     Call ReleaseCapture
    14.     Call SendMessage(frm.hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0)
    15.  
    16. End Sub
    17.  
    18. Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
    19. '================================================
    20.     If Button = vbLeftButton Then
    21.         Call DragForm(Me)
    22.     End If
    23.  
    24. End Sub
    Roy

  3. #3
    Fanatic Member Ruku's Avatar
    Join Date
    Jul 2002
    Location
    Canada
    Posts
    655

    Re: Making a form moveable

    I'm not sure, but I think you can simply use the

    With (Form)
    .Top
    .Left
    End With

    To modify the position...


    Originally posted by Walter100
    I know you can make a form moveable by setting moveable to true. However if you make the control box property false and delete the caption then at runtime you cant make the form moveable is there a way I can make a form moveable through code?

  4. #4
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    A simple way is:

    public mX, mY

    in the mousemove event:

    if button = 1 then move left+x-mx, top+y-my

    in the mousedown event

    mx = x
    my = y

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2001
    Location
    Baltimore,MD
    Posts
    536
    I inserted the code into a new project and received errors
    Walter Richardson
    Striver2000 Christian Productions
    Iam seventeen but since I started VB in June of 01 GOD has been helping me excell by finding this great forum with a bunch of GREAT PEOPLE!

  6. #6
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    Try my code.

  7. #7
    PowerPoster
    Join Date
    Aug 2002
    Location
    NY, NY
    Posts
    2,139
    Change all Public declarations to Private and run it.
    Roy

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2001
    Location
    Baltimore,MD
    Posts
    536
    never mind I have gotten it to work thanks everyone
    Walter Richardson
    Striver2000 Christian Productions
    Iam seventeen but since I started VB in June of 01 GOD has been helping me excell by finding this great forum with a bunch of GREAT PEOPLE!

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2001
    Location
    Baltimore,MD
    Posts
    536

    Thumbs up

    Yours works digital error thanks
    Walter Richardson
    Striver2000 Christian Productions
    Iam seventeen but since I started VB in June of 01 GOD has been helping me excell by finding this great forum with a bunch of GREAT PEOPLE!

  10. #10
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    You're welcome.

  11. #11
    Software Eng. Megatron's Avatar
    Join Date
    Mar 1999
    Location
    Canada
    Posts
    11,286
    Try
    Code:
    Dim OldX As Single
    Dim OldY As Single
    
    Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
        If Button = 1 Then
            OldX = X
            OldY = Y
        End If
    End Sub
    
    Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
        If Button = vbLeftButton Then Move Left + (X - OldX), Top + (Y - OldY)
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width