Results 1 to 4 of 4

Thread: [VB6]. Move a Window by dragging from any part of it

  1. #1

    Thread Starter
    Lively Member Tabi's Avatar
    Join Date
    Jan 2024
    Location
    Argentina, Santa Fe
    Posts
    67

    [VB6]. Move a Window by dragging from any part of it

    Good morning. I want to share this simple code. It allows the user to move the window, by clicking and dragging, from any point on the Form.
    The scrolling achieved is smooth and pleasant, just as if the click had been on the title bar.

    With almost no code, it provides elegant functionality.
    Also useful when the Window Title Bar is higher up the screen. In this case, the Form can be downloaded by fishing it from any point on it.

    You can also make the call from the MouseMove Event of a Control Frame. In this case, clicking on it and dragging moves the Form.
    You could even put it in the MouseMove Event of any particular control, achieving the same effect, moving the form to any part of the screen. [/B]

    Please, if you implemented it, comment if you liked it or not. This is a translation from Spanish, sorry if the reading is not pleasant. Thank you
    .



    In each Form to move:

    Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    FormMove Me, Button, X, Y
    End Sub



    In a module:

    Static Sub FormMove(Formulario As Form, Button As Integer, X As Single, Y As Single)
    Dim MemX As Single, MemY As Single, YaMovio As Boolean, AchFrm As Single
    AchFrm = Formulario.Width
    If Button = 1 Then
    If Not YaMovio Then YaMovio = True: MemX = X: MemY = Y
    Formulario.Left = Formulario.Left + X - MemX
    Formulario.Top = Formulario.Top + Y - MemY
    Else
    YaMovio = False
    End If
    End Sub
    Last edited by Tabi; Feb 9th, 2024 at 09:20 PM.

  2. #2
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,454

    Re: [VB6]. Move a Window by dragging from any part of it

    A shorter and more generic variant would be (also supporting Controls):
    Code:
    Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
      MoveFrmOrCtl Me, Button, X, Y
    End Sub
    Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
      MoveFrmOrCtl Picture1, Button, X, Y
    End Sub
    
    Sub MoveFrmOrCtl(FrmOrCtl As Object, Button As Integer, X As Single, Y As Single)
      Static X0 As Single, Y0 As Single
      If Button <> 1 Then X0 = X: Y0 = Y Else _
         FrmOrCtl.Move FrmOrCtl.Left + X - X0, FrmOrCtl.Top + Y - Y0
    End Sub
    Olaf

  3. #3
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,909

    Re: [VB6]. Move a Window by dragging from any part of it

    Here's the way I do it.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  4. #4

    Thread Starter
    Lively Member Tabi's Avatar
    Join Date
    Jan 2024
    Location
    Argentina, Santa Fe
    Posts
    67

    Re: [VB6]. Move a Window by dragging from any part of it

    Thank you very much for improving the code!
    I hope, later, I can upload something more useful.

Tags for this Thread

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