[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.
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.
Re: [RESOLVED] Draggable Form
Quote:
Originally Posted by
Shaggy Hiker
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 :D
Quote:
Originally Posted by
passel
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.
Re: [RESOLVED] Draggable Form
It wouldn't be the click event, it would be the same event, i.e. MouseMove.
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
Re: [RESOLVED] Draggable Form
Quote:
Originally Posted by
passel
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.
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
Re: [RESOLVED] Draggable Form
Quote:
Originally Posted by
Pixy
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.
Re: [RESOLVED] Draggable Form
Quote:
Originally Posted by
.paul.
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!
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)
Re: [RESOLVED] Draggable Form
Quote:
Originally Posted by
Pixy
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...
Re: [RESOLVED] Draggable Form
Quote:
Originally Posted by
.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...
Ahh, now I understand.