Results 1 to 10 of 10

Thread: Move borderless form.

  1. #1

    Thread Starter
    Hyperactive Member DerekM's Avatar
    Join Date
    Jun 2009
    Location
    Colorado
    Posts
    296

    Move borderless form.

    I already got the move part, that's easy, what I'm having trouble with is when I click on the form and try to move it, it shifts a little, that is really annoying, I just want it to be smooth. I want it to be as if I'm moving the form from the title bar. Here's my code.
    vb Code:
    1. Public Class Form1
    2.     Dim Down As Boolean = False
    3.     Dim Local1 As Point
    4.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    5.  
    6.     End Sub
    7.  
    8.     Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
    9.         Down = True
    10.         Local1 = e.Location
    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 Down = True Then
    15.             Me.Location = MousePosition - Local1
    16.         End If
    17.     End Sub
    18.  
    19.     Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
    20.         Down = False
    21.     End Sub
    22. End Class

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

    Re: Move borderless form.

    Add this and take out all of your moving code:
    vb.net Code:
    1. Protected Overrides Sub WndProc(ByRef e As Message)
    2.          MyBase.WndProc(e)
    3.          If e.Message = &H84 AndAlso e.Result = &H1 Then e.Result = &H2
    4. End Sub

  3. #3

    Thread Starter
    Hyperactive Member DerekM's Avatar
    Join Date
    Jun 2009
    Location
    Colorado
    Posts
    296

    Re: Move borderless form.

    Quote Originally Posted by minitech View Post
    Add this and take out all of your moving code:
    vb.net Code:
    1. Protected Overrides Sub WndProc(ByRef e As Message)
    2.          MyBase.WndProc(e)
    3.          If e.Message = &H84 AndAlso e.Result = &H1 Then e.Result = &H2
    4. End Sub
    That doesn't really want to work for me. e.message is underlined.

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

    Re: Move borderless form.

    Quote Originally Posted by DerekM View Post
    That doesn't really want to work for me. e.message is underlined.
    Change it to e.Msg. Sorry!

  5. #5
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Move borderless form.

    Quote Originally Posted by minitech View Post
    Add this and take out all of your moving code:
    vb.net Code:
    1. Protected Overrides Sub WndProc(ByRef e As Message)
    2.          MyBase.WndProc(e)
    3.          If e.Message = &H84 AndAlso e.Result = &H1 Then e.Result = &H2
    4. End Sub
    Not so sure about those magic numbers. What are &H84, &H1 and &H2?

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

    Re: Move borderless form.

    &H84: Some sort of mousedown
    &H1 result of &H84: The client area of a window.
    &H2 result of &H84: The title bar of a window.

    You're telling the windows message system that if the mouse is down on the client area, it's actually on the nonexistant title bar.

    Normally people use named constants, but I think it's unneccessary.

  7. #7
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Move borderless form.

    Quote Originally Posted by minitech View Post
    Normally people use named constants, but I think it's unneccessary.
    Thats fine so long as no one else will ever need to work with your code. You should always use named constants if you are writing code that someone else will need to look at (like if you're using your code to illustrate a mechanism on a forum!)

  8. #8

    Thread Starter
    Hyperactive Member DerekM's Avatar
    Join Date
    Jun 2009
    Location
    Colorado
    Posts
    296

    Re: Move borderless form.

    Quote Originally Posted by minitech View Post
    Change it to e.Msg. Sorry!
    Works great! But how could I apply this to any control?

  9. #9
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: Move borderless form.

    Quote Originally Posted by minitech View Post
    Normally people use named constants, but I think it's unneccessary.
    I used to think so too, but then I had to edit 2 year old code that I wrote... it changed my way of thinking PDQ!
    Last edited by kebo; Sep 21st, 2009 at 08:06 PM.
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  10. #10
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Move borderless form.

    Take a look at my signature, there's a codebank submission on doing what you want to do. Instead of only moving, there is also resizing, including a cursor that changes to the correct resizing cursor.

    It uses more or less the same code as minitech's code; it makes the window think the user is moving it via the title bar, instead of you manually setting the Location each time. This way it is much smoother, and there is no danger of 'letting the form slip' (if you use the manual way and move too fast, the form will not be able to update quickly enough or something and you will lose it).

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