-
Mar 30th, 2019, 10:18 PM
#1
Thread Starter
Member
[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.
-
Mar 30th, 2019, 11:28 PM
#2
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 30th, 2019, 11:42 PM
#3
Thread Starter
Member
Re: Draggable Form
Is this virus?
-
Mar 30th, 2019, 11:51 PM
#4
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...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 30th, 2019, 11:55 PM
#5
Re: Draggable Form
Don't take my word for it... Ask some experienced members. I'll ask a moderator to check it.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 31st, 2019, 12:21 AM
#6
Thread Starter
Member
-
Mar 31st, 2019, 12:43 AM
#7
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?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 31st, 2019, 12:48 AM
#8
Thread Starter
Member
Re: Draggable Form
Originally Posted by .paul.
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.
-
Mar 31st, 2019, 12:49 AM
#9
Thread Starter
Member
Re: Draggable Form
Other than the antivirus issue it works. Resolved.
Last edited by Pixy; Mar 31st, 2019 at 02:35 AM.
-
Mar 31st, 2019, 03:45 AM
#10
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
-
Mar 31st, 2019, 04:07 AM
#11
Thread Starter
Member
Re: Draggable Form
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
Nice, will check it out.
-
Mar 31st, 2019, 08:55 AM
#12
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
-
Apr 2nd, 2019, 10:07 AM
#13
Thread Starter
Member
Re: [RESOLVED] Draggable Form
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
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.
Last edited by Pixy; Apr 2nd, 2019 at 10:16 AM.
-
Apr 2nd, 2019, 10:15 AM
#14
Re: [RESOLVED] Draggable Form
It wouldn't be the click event, it would be the same event, i.e. MouseMove.
-
Apr 2nd, 2019, 10:43 AM
#15
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.
-
Apr 2nd, 2019, 12:03 PM
#16
Thread Starter
Member
Re: [RESOLVED] Draggable Form
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.
-
Apr 2nd, 2019, 12:29 PM
#17
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 2nd, 2019, 12:52 PM
#18
Re: [RESOLVED] Draggable Form
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.
-
Apr 2nd, 2019, 12:56 PM
#19
Thread Starter
Member
Re: [RESOLVED] Draggable Form
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!
-
Apr 2nd, 2019, 12:59 PM
#20
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)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 2nd, 2019, 01:02 PM
#21
Re: [RESOLVED] Draggable Form
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...
Last edited by .paul.; Apr 2nd, 2019 at 01:18 PM.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 2nd, 2019, 07:12 PM
#22
Thread Starter
Member
Re: [RESOLVED] Draggable Form
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.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|