|
-
Aug 29th, 2008, 03:58 PM
#1
Thread Starter
Frenzied Member
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.
Last edited by noahssite; Sep 13th, 2008 at 07:52 PM.
-
Sep 13th, 2008, 03:23 AM
#2
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?
Has someone helped you? Then you can Rate their helpful post. 
-
Sep 13th, 2008, 07:18 AM
#3
Thread Starter
Frenzied Member
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.
-
Sep 13th, 2008, 02:55 PM
#4
-
Sep 13th, 2008, 07:44 PM
#5
Thread Starter
Frenzied Member
Re: Moving a Borderless form, simple & short.
Well is the API method better?
I also added your code to my first post.
-
Sep 13th, 2008, 07:47 PM
#6
-
Sep 15th, 2008, 09:31 AM
#7
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.
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Jan 9th, 2010, 07:52 PM
#8
Junior Member
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
-
Jan 9th, 2010, 09:34 PM
#9
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.
Last edited by minitech; Mar 18th, 2011 at 11:23 AM.
-
Jan 9th, 2010, 09:40 PM
#10
Junior Member
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?
Last edited by dekz; Jan 9th, 2010 at 10:12 PM.
-
Jan 16th, 2010, 11:21 AM
#11
Thread Starter
Frenzied Member
Re: Moving a Borderless form, simple & short.
What do you mean you want the Private Sub to be moveable?
-
Jan 16th, 2010, 11:42 PM
#12
Junior Member
Re: Moving a Borderless form, simple & short.
 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.
Last edited by dekz; Jan 16th, 2010 at 11:54 PM.
-
Apr 27th, 2010, 03:14 PM
#13
Addicted Member
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.
Last edited by Legjendat; Apr 28th, 2010 at 06:32 AM.
-
Apr 29th, 2010, 06:01 PM
#14
Thread Starter
Frenzied Member
Re: Moving a Borderless form, simple & short.
 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.
-
Apr 30th, 2010, 06:56 AM
#15
Addicted Member
Re: Moving a Borderless form, simple & short.
The same code as in the thread opening post, that is.
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
-
Apr 30th, 2010, 03:25 PM
#16
Thread Starter
Frenzied Member
Re: Moving a Borderless form, simple & short.
No the code that failed to work...
You said
 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.
-
Apr 30th, 2010, 03:37 PM
#17
Addicted Member
Re: Moving a Borderless form, simple & short.
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.
Last edited by Legjendat; Apr 30th, 2010 at 03:59 PM.
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
|