Results 1 to 17 of 17

Thread: Moving a Borderless form, simple & short.

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Moving a Borderless form, simple & short.

    First of all credits go to CoderJoe though he isn't active.

    Here is the code:

    vb Code:
    1. Public Class Form1
    2.     'Declare the variables
    3.     Dim drag As Boolean
    4.     Dim mousex As Integer
    5.     Dim mousey As Integer
    6.  
    7.     Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
    8.         drag = True 'Sets the variable drag to true.
    9.         mousex = Windows.Forms.Cursor.Position.X - Me.Left 'Sets variable mousex
    10.         mousey = Windows.Forms.Cursor.Position.Y - Me.Top 'Sets variable mousey
    11.     End Sub
    12.  
    13.     Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
    14.         'If drag is set to true then move the form accordingly.
    15.         If drag Then
    16.             Me.Top = Windows.Forms.Cursor.Position.Y - mousey
    17.             Me.Left = Windows.Forms.Cursor.Position.X - mousex
    18.         End If
    19.     End Sub
    20.  
    21.     Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
    22.         drag = False 'Sets drag to false, so the form does not move according to the code in MouseMove
    23.     End Sub
    24. 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.

  2. #2
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Moving a Borderless form, simple & short.

    In VB6 you could do this:

    vb Code:
    1. Option Explicit
    2.  
    3. Private Declare Function ReleaseCapture Lib "user32" () As Long
    4. 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
    5.  
    6. Private Const HTCAPTION = 2
    7. Private Const WM_NCLBUTTONDOWN = &HA1
    8.  
    9. Private Const WM_SYSCOMMAND = &H112
    10. Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    11.     ReleaseCapture
    12.     SendMessage hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0&
    13. 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.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    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.

  4. #4
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Moving a Borderless form, simple & short.

    I just thought I'd mention another method. Have all the methods put together in one thread


    Has someone helped you? Then you can Rate their helpful post.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: Moving a Borderless form, simple & short.

    Well is the API method better?
    I also added your code to my first post.

  6. #6
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    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...


    Has someone helped you? Then you can Rate their helpful post.

  7. #7
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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 -

  8. #8
    Junior Member
    Join Date
    Jan 2010
    Posts
    16

    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

  9. #9
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Moving a Borderless form, simple & short.

    Actually, overriding WndProc is the best way...
    vb Code:
    1. Protected Overrides Sub WndProc(ByRef e As Message)
    2.       MyBase.WndProc(e)
    3.       If e.Msg = &H84 AndAlso e.Result.ToInt32() = 1 Then e.Result = New IntPtr(2)
    4. End Sub

    EDIT: Sorry, it's ByRef.
    Last edited by minitech; Mar 18th, 2011 at 11:23 AM.

  10. #10
    Junior Member
    Join Date
    Jan 2010
    Posts
    16

    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.

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: Moving a Borderless form, simple & short.

    What do you mean you want the Private Sub to be moveable?

  12. #12
    Junior Member
    Join Date
    Jan 2010
    Posts
    16

    Re: Moving a Borderless form, simple & short.

    Quote Originally Posted by noahssite View Post
    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.

  13. #13
    Addicted Member
    Join Date
    Apr 2010
    Posts
    131

    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.

  14. #14

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: Moving a Borderless form, simple & short.

    Quote Originally Posted by Legjendat View Post
    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.

  15. #15
    Addicted Member
    Join Date
    Apr 2010
    Posts
    131

    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

  16. #16

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    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.

  17. #17
    Addicted Member
    Join Date
    Apr 2010
    Posts
    131

    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
  •  



Click Here to Expand Forum to Full Width