Results 1 to 22 of 22

Thread: [RESOLVED] Draggable Form

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2018
    Posts
    39

    Resolved [RESOLVED] Draggable Form

    I'm trying to make a borderless form draggable. I'm using the following code:

    Code:
    Imports System.Runtime.InteropServices
    
    Public Class FormDrag
        Public Const WM_NCLBUTTONDOWN As Integer = &HA1
        Public Const HT_CAPTION As Integer = &H2
        <DllImport("user32.dll")>
        Public Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    
        End Function
        <DllImport("user32.dll")>
        Public Shared Function ReleaseCapture() As Boolean
    
        End Function
    End Class
    Code:
        ' Make Form1 draggable
        Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
            FormDrag.ReleaseCapture()
            FormDrag.SendMessage(Me.Handle, FormDrag.WM_NCLBUTTONDOWN, FormDrag.HT_CAPTION, 0)
        End Sub
    Not working, help please.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Draggable Form

    Here's how...

    Code:
    Public Class Form1
    
        Const WM_NCLBUTTONDOWN As Integer = &HA1
        Const HT_CAPTION As Integer = &H2
    
        Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
            MyBase.OnMouseDown(e)
    
            Me.Capture = False
            Dim msg As Message = _
                Message.Create(Me.Handle, WM_NCLBUTTONDOWN, _
                        New IntPtr(HT_CAPTION), IntPtr.Zero)
            Me.DefWndProc(msg)
    
        End Sub
    
    End Class

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2018
    Posts
    39

    Re: Draggable Form

    Is this virus? Name:  dragv.jpg
Views: 781
Size:  30.4 KB

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Draggable Form

    To start, No it's not a virus if you're referring to the code i gave you. Please post text for code snippets and text for error messages. Your image is barely readable...

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Draggable Form

    Don't take my word for it... Ask some experienced members. I'll ask a moderator to check it.

  6. #6

    Thread Starter
    Member
    Join Date
    Oct 2018
    Posts
    39

    Re: Draggable Form

    Quote Originally Posted by .paul. View Post
    To start, No it's not a virus if you're referring to the code i gave you. Please post text for code snippets and text for error messages. Your image is barely readable...
    Avira thinks it's a virus. Is there any more legit way to do this?

    Name:  aSsHVgp.jpg
Views: 736
Size:  30.3 KB
    Name:  ad79KoI.jpg
Views: 742
Size:  8.3 KB
    Name:  LrCECXp.png
Views: 658
Size:  314.3 KB

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Draggable Form

    That is a legit way. If you want a more legit answer, get rid of Avira and get a legit antivirus program.
    It really shouldn't cause a virus scare. Are you sure your other code is ok?

  8. #8

    Thread Starter
    Member
    Join Date
    Oct 2018
    Posts
    39

    Re: Draggable Form

    Quote Originally Posted by .paul. View Post
    That is a legit way. If you want a more legit answer, get rid of Avira and get a legit antivirus program.
    It really shouldn't cause a virus scare. Are you sure your other code is ok?
    Bad avira
    Yes, rest of the code is button click event.

  9. #9

    Thread Starter
    Member
    Join Date
    Oct 2018
    Posts
    39

    Re: Draggable Form

    Other than the antivirus issue it works. Resolved.
    Last edited by Pixy; Mar 31st, 2019 at 02:35 AM.

  10. #10
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Draggable Form

    As an alternative to using an API, I've used code similar to this since VB1 days.
    Code:
      Private Sub Form1_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        Static lpos As Point
        If e.Button = Windows.Forms.MouseButtons.Left Then
          Me.Location += New Size(e.X - lpos.X, e.Y - lpos.Y)
        Else
          lpos = e.Location
        End If
      End Sub

  11. #11

    Thread Starter
    Member
    Join Date
    Oct 2018
    Posts
    39

    Re: Draggable Form

    Quote Originally Posted by passel View Post
    As an alternative to using an API, I've used code similar to this since VB1 days.
    Code:
      Private Sub Form1_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        Static lpos As Point
        If e.Button = Windows.Forms.MouseButtons.Left Then
          Me.Location += New Size(e.X - lpos.X, e.Y - lpos.Y)
        Else
          lpos = e.Location
        End If
      End Sub
    Nice, will check it out.

  12. #12
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: [RESOLVED] Draggable Form

    Those screen shots are barely legible. It sure looks like the screen shot was the code that .Paul. posted, though, and there's no way that's a virus. I'm amazed that the anti-virus even saw it as anything. However, it did suggest that it was a heuristic scan, in which case it was looking for patters that it saw as being like a virus. I suppose, that if you were being overly broad in your definition, then creating and sending a message to a message queue is something that a virus might do. Of course, at that point, you are also ruling out LOADS of legitimate programs, as well.

    I recently ran into a case where a program I wrote was flagged by an overly aggressive heuristics engine simply because it started up...then immediately spun off a thread that called out to a URL. Of course, that was what it was supposed to do, since it had to get information from the API at the URL to allow the user to decide what to do next, but it's certainly something a trojan would also do, as well.

    Anti-virus is a constantly moving field. I don't know what companies are better and which are worse, and anything I said today would be wrong tomorrow, anyways. The result is that some are a bit overly/incorrectly aggressive. That's all this seems to be.
    My usual boring signature: Nothing

  13. #13

    Thread Starter
    Member
    Join Date
    Oct 2018
    Posts
    39

    Re: [RESOLVED] Draggable Form

    Quote Originally Posted by Shaggy Hiker View Post
    Those screen shots are barely legible. It sure looks like the screen shot was the code that .Paul. posted, though, and there's no way that's a virus. I'm amazed that the anti-virus even saw it as anything. However, it did suggest that it was a heuristic scan, in which case it was looking for patters that it saw as being like a virus. I suppose, that if you were being overly broad in your definition, then creating and sending a message to a message queue is something that a virus might do. Of course, at that point, you are also ruling out LOADS of legitimate programs, as well.

    I recently ran into a case where a program I wrote was flagged by an overly aggressive heuristics engine simply because it started up...then immediately spun off a thread that called out to a URL. Of course, that was what it was supposed to do, since it had to get information from the API at the URL to allow the user to decide what to do next, but it's certainly something a trojan would also do, as well.

    Anti-virus is a constantly moving field. I don't know what companies are better and which are worse, and anything I said today would be wrong tomorrow, anyways. The result is that some are a bit overly/incorrectly aggressive. That's all this seems to be.
    Very true. Especially Avira is really touchy and thinks that everything is a virus. I had the same issue with similar code. It's probably because the mouse behavior in relation to the windows form was modified. For example there's an old virus that changes right click with left click

    Quote Originally Posted by passel View Post
    As an alternative to using an API, I've used code similar to this since VB1 days.
    Code:
      Private Sub Form1_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        Static lpos As Point
        If e.Button = Windows.Forms.MouseButtons.Left Then
          Me.Location += New Size(e.X - lpos.X, e.Y - lpos.Y)
        Else
          lpos = e.Location
        End If
      End Sub
    passel, how can I do this for a picturebox? I used:
    Code:
    Private Sub PictureBox4_Click(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox4.Click
            Static lpos As Point
            If e.Button = Windows.Forms.MouseButtons.Left Then
                Me.Location += New Size(e.X - lpos.X, e.Y - lpos.Y)
            Else
                lpos = e.Location
            End If
        End Sub
    but the window moves after 10+ seconds and many tries while lagging.
    Last edited by Pixy; Apr 2nd, 2019 at 10:16 AM.

  14. #14
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: [RESOLVED] Draggable Form

    It wouldn't be the click event, it would be the same event, i.e. MouseMove.

  15. #15
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: [RESOLVED] Draggable Form

    Also, "Me.Location", Me refers to the form, so you need to change Me to the object you want to move, like Picturebox1.
    But, the "Sender" parameter refers to the object that the event is associated with, so you could cast Sender to a Picturebox type or Form type.
    If you want to have a single Sub that can handle most any control, then cast Sender to Control.
    Here's a quick example that handles the form and picturebox1 on the form.
    Code:
      Private Sub Control_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) _
                  Handles PictureBox1.MouseMove, Me.MouseMove
    
        Dim pb As Control = DirectCast(sender, Control)
        Static lpos As Point
        If e.Button = Windows.Forms.MouseButtons.Left Then
          pb.Location += New Size(e.X - lpos.X, e.Y - lpos.Y)
        Else
          lpos = e.Location
        End If
      End Sub
    Last edited by passel; Apr 2nd, 2019 at 10:46 AM.

  16. #16

    Thread Starter
    Member
    Join Date
    Oct 2018
    Posts
    39

    Re: [RESOLVED] Draggable Form

    Quote Originally Posted by passel View Post
    Also, "Me.Location", Me refers to the form, so you need to change Me to the object you want to move, like Picturebox1.
    But, the "Sender" parameter refers to the object that the event is associated with, so you could cast Sender to a Picturebox type or Form type.
    If you want to have a single Sub that can handle most any control, then cast Sender to Control.
    Here's a quick example that handles the form and picturebox1 on the form.
    Code:
      Private Sub Control_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) _
                  Handles PictureBox1.MouseMove, Me.MouseMove
    
        Dim pb As Control = DirectCast(sender, Control)
        Static lpos As Point
        If e.Button = Windows.Forms.MouseButtons.Left Then
          pb.Location += New Size(e.X - lpos.X, e.Y - lpos.Y)
        Else
          lpos = e.Location
        End If
      End Sub
    Interesting. Using this the form moves if you drag your mouse inside the form and when you drag the mouse inside the picturebox the picturebox alone moves. What I want to is to imitate the dragging effect of the top bar where the title is in normal forms. My form being borderless does not have the top bar so I use a picturebox to imitate one. So if the mouse is dragged inside the picturebox the form as a whole should move. If the mouse is dragged outside of the picturebox nothing should happen. Right now I'm trying to figure this out, if you have something in mind I would appreciate it.

  17. #17
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: [RESOLVED] Draggable Form

    Code:
    Private Sub Control_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) _
                  Handles PictureBox1.MouseMove
    
        Static lpos As Point
        If e.Button = Windows.Forms.MouseButtons.Left Then
          me.Location += New Size(e.X - lpos.X, e.Y - lpos.Y)
        Else
          lpos = e.Location
        End If
    End Sub

  18. #18
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: [RESOLVED] Draggable Form

    Quote Originally Posted by Pixy View Post
    Interesting. Using this the form moves if you drag your mouse inside the form and when you drag the mouse inside the picturebox the picturebox alone moves. What I want to is to imitate the dragging effect of the top bar where the title is in normal forms. ...
    Well, if you had mentioned that then I might not have posted post #15. The answer in post #14 would have done what you wanted, which is .paul. shows explicitly.

    Hopefully, you will want to understand what the code is doing and why, rather than just pasting posted code blindly without understanding what it is doing. Many of the responses to questions here can be an example that may have to be tweaked to fit your specific requirement.
    In this case, that wasn't the case, you just needed to understand that the code is keeping track of where the mouse is in the mouseMove event. If the mouse button is down and the mouse moved, the amount that it moved is added to the location of the form, which essentially puts the point where the mouse was click back under the mouse's current position.

  19. #19

    Thread Starter
    Member
    Join Date
    Oct 2018
    Posts
    39

    Re: [RESOLVED] Draggable Form

    Quote Originally Posted by .paul. View Post
    Code:
    Private Sub Control_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) _
                  Handles PictureBox1.MouseMove
    
        Static lpos As Point
        If e.Button = Windows.Forms.MouseButtons.Left Then
          me.Location += New Size(e.X - lpos.X, e.Y - lpos.Y)
        Else
          lpos = e.Location
        End If
    End Sub
    Ah Me.MouseMove was the problem. I'm not familiar with Me. at all. Thanks a bunch paul!

  20. #20
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: [RESOLVED] Draggable Form

    You don't even need the picturebox. You can just check if the mousemove (drag) started in a specific part of the form (i.e. In your fake titlebar area)

  21. #21
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: [RESOLVED] Draggable Form

    Quote Originally Posted by Pixy View Post
    Ah Me.MouseMove was the problem. I'm not familiar with Me. at all. Thanks a bunch paul!


    In the context it is used there, Me refers to the Form

    Edit: The way passel showed you, that moved either the whole form when dragging in the form, or moved the picturebox within the form when dragging in the picturebox, was because passel was handling the mousemove event for both the form and the picturebox, and setting the location for the sender object, which was either the form or the picturebox...
    Last edited by .paul.; Apr 2nd, 2019 at 01:18 PM.

  22. #22

    Thread Starter
    Member
    Join Date
    Oct 2018
    Posts
    39

    Re: [RESOLVED] Draggable Form

    Quote Originally Posted by .paul. View Post
    In the context it is used there, Me refers to the Form

    Edit: The way passel showed you, that moved either the whole form when dragging in the form, or moved the picturebox within the form when dragging in the picturebox, was because passel was handling the mousemove event for both the form and the picturebox, and setting the location for the sender object, which was either the form or the picturebox...
    Ahh, now I understand.

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