-
Moving a Borderless form, simple & short.
First of all credits go to CoderJoe though he isn't active.
Here is the code:
vb Code:
Public Class Form1
'Declare the variables
Dim drag As Boolean
Dim mousex As Integer
Dim mousey As Integer
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
drag = True 'Sets the variable drag to true.
mousex = Windows.Forms.Cursor.Position.X - Me.Left 'Sets variable mousex
mousey = Windows.Forms.Cursor.Position.Y - Me.Top 'Sets variable mousey
End Sub
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
'If drag is set to true then move the form accordingly.
If drag Then
Me.Top = Windows.Forms.Cursor.Position.Y - mousey
Me.Left = Windows.Forms.Cursor.Position.X - mousex
End If
End Sub
Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
drag = False 'Sets drag to false, so the form does not move according to the code in MouseMove
End Sub
End Class
You can place the code on the events of any control. Say if you would like to use an image as a title bar, then just place the code on the appropiate events of the image.
-
Re: Moving a Borderless form, simple & short.
In VB6 you could do this:
vb Code:
Option Explicit
Private Declare Function ReleaseCapture Lib "user32" () As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const HTCAPTION = 2
Private Const WM_NCLBUTTONDOWN = &HA1
Private Const WM_SYSCOMMAND = &H112
Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
ReleaseCapture
SendMessage hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0&
End Sub
I assume if you fix the API declaration, it would work as well?
-
Re: Moving a Borderless form, simple & short.
Well im not good with APIs and i only know .NET though:
The way i supplied is very simple, short & easy (like my title).
Also the code i supplied works completly fine, no flaus. Unless you can find any.
-
Re: Moving a Borderless form, simple & short.
I just thought I'd mention another method. Have all the methods put together in one thread :)
-
Re: Moving a Borderless form, simple & short.
Well is the API method better?
I also added your code to my first post.
-
Re: Moving a Borderless form, simple & short.
Actually the code is VB6, so the declarations at least will need a small conversion, as well as the function declaration of the Picture1_MouseDown :)
Can't really say if one is better than the other. Both work, so after that it's just a matter of what you prefer I suppose...
-
Re: Moving a Borderless form, simple & short.
Using plain .net code (as Noahssite's example) can give flickers and lags especially if the form contains images and/or a lot of controls while using API will produce a much smoother move of the form.
-
Re: Moving a Borderless form, simple & short.
Im really new to this, im trying to find codes so that i can implement the features i want in the forms, nothing makes sense to me, i copy the codes people post, like this one, all turns out with errors, this one i get Event MouseDown cannot be found, and this one #
If drag Then
#
Me.Top = Windows.Forms.Cursor.Position.Y - mousey
#
Me.Left = Windows.Forms.Cursor.Position.X - mousex
Expression is a value and therefore cannot be the target of an assignement.
I dont know what it is about this coding but it seriously pisses me off, it should be easy, i dont understand why i cant complete a simple form without going into some stupid coding to tell it what to do, allready most of the parts about the window is there in a graphical UI, so why isnt the rest, if this language was so smart, how come it cant be made simpler. you basically type one thing wrong and you screw up the whole project lol
-
Re: Moving a Borderless form, simple & short.
Actually, overriding WndProc is the best way...
vb Code:
Protected Overrides Sub WndProc(ByRef e As Message)
MyBase.WndProc(e)
If e.Msg = &H84 AndAlso e.Result.ToInt32() = 1 Then e.Result = New IntPtr(2)
End Sub
EDIT: Sorry, it's ByRef.
-
Re: Moving a Borderless form, simple & short.
what do i do if i have a Private Sub under the form and want the Private Sub to be moveable?
-
Re: Moving a Borderless form, simple & short.
What do you mean you want the Private Sub to be moveable?
-
Re: Moving a Borderless form, simple & short.
Quote:
Originally Posted by
noahssite
What do you mean you want the Private Sub to be moveable?
I figured it out, i had problem where to put the code with a control panel on top of form1.
-
Re: Moving a Borderless form, simple & short.
If I put the code on PictureBoxX_MouseHover, then it will only move to the right and down, it won't go up and left. any way to fix that cuz like this it only works on the main form area. Thanks anyway, proves useful.
-
Re: Moving a Borderless form, simple & short.
Quote:
Originally Posted by
Legjendat
If I put the code on PictureBoxX_MouseHover, then it will only move to the right and down, it won't go up and left. any way to fix that cuz like this it only works on the main form area. Thanks anyway, proves useful.
Post your code.
-
Re: Moving a Borderless form, simple & short.
The same code as in the thread opening post, that is.
Quote:
Dim drag As Boolean
Dim mousex As Integer
Dim mousey As Integer
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
drag = True
mousex = Windows.Forms.Cursor.Position.X - Me.Left
mousey = Windows.Forms.Cursor.Position.Y - Me.Top
End Sub
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
If drag Then
Me.Top = Windows.Forms.Cursor.Position.Y - mousey
Me.Left = Windows.Forms.Cursor.Position.X - mousex
End If
End Sub
Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
drag = False 'Sets drag to false, so the form does not move according to the code in MouseMove
End Sub
-
Re: Moving a Borderless form, simple & short.
No the code that failed to work...
You said
Quote:
Originally Posted by Legjendat
If I put the code on PictureBoxX_MouseHover,
How did you use Picturebox_MouseHover? Did you use it instead of Form_MouseMove? Post THAT code.
-
Re: Moving a Borderless form, simple & short.
Quote:
Private Sub PictureBox5_MouseHover(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox5.MouseHover
drag = True
mousex = Windows.Forms.Cursor.Position.X - Me.Left
mousey = Windows.Forms.Cursor.Position.Y - Me.Top
If drag Then
Me.Top = Windows.Forms.Cursor.Position.Y - mousey
Me.Left = Windows.Forms.Cursor.Position.X - mousex
End If
End Sub
Actually, I was mistaken. it won't move at all, i guess the reason it moved, is because i hadn't deleted ur code. so till the form1_mousemove was there it would move as I explained, now that I deleted the code in form1_mousemove it won't move at all. same thing happens in picturebox_click
EDIT:
your last comment finally made me understand where my mistake stood, i wasn't putting the code under the correct properties. I finally made it thanks for your comment again, it made me understand.