Results 1 to 6 of 6

Thread: Moving a form without a titlebar

  1. #1

    Thread Starter
    Fanatic Member Emcrank's Avatar
    Join Date
    Jan 2009
    Posts
    566

    Moving a form without a titlebar

    To make a form movable when it has no titlebar
    Use
    VB.NET Code:
    1. #Region " MoveFormWithoutTitleBar "
    2. Const WS_MAXIMIZEBOX As Integer = &H10000
    3.  
    4.     Const GWL_STYLE As Integer = -16
    5.  
    6.     Const WM_NCHITTEST As Integer = &H84
    7.  
    8.     Const HTCLIENT As Integer = &H1
    9.  
    10.     Const HTCAPTION As Integer = &H2
    11.     <System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint:="SetWindowLong")> _
    12.  
    13.     Public Shared Function SetWindowLong(<System.Runtime.InteropServices.InAttribute()> ByVal hWnd As System.IntPtr, ByVal nIndex As Integer, ByVal dwNewLong As UInt64) As Integer
    14.  
    15.     End Function
    16.  
    17. <System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint:="GetWindowLong")> _
    18.  
    19.     Public Shared Function GetWindowLong(<System.Runtime.InteropServices.InAttribute()> ByVal hWnd As System.IntPtr, ByVal nIndex As Integer) As UInt64
    20.  
    21.     End Function
    22.  
    23.     'Example in a button click event - form load event may be more appropriate
    24.  
    25.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    26.  
    27.         Dim Result As UInt64 = GetWindowLong(Me.Handle, GWL_STYLE)
    28.  
    29.         Result = (Result And Not CULng(WS_MAXIMIZEBOX))
    30.  
    31.         SetWindowLong(Me.Handle, GWL_STYLE, Result)
    32.  
    33.  
    34.     End Sub
    35.  
    36.  
    37.     Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    38.  
    39.         Select Case m.Msg
    40.  
    41.             Case WM_NCHITTEST
    42.  
    43.                 MyBase.WndProc(m)
    44.  
    45.                 If CInt(m.Result) = HTCLIENT Then
    46.  
    47.                     m.Result = New IntPtr(HTCAPTION)
    48.  
    49.                 End If
    50.  
    51.             Case Else
    52.  
    53.                 MyBase.WndProc(m)
    54.  
    55.         End Select
    56.  
    57.     End Sub
    58. #End Region

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Moving a form without a titlebar

    If you are using this method then you might also want to take a look at this thread to avoid the form being maximized whenever the user double clicks anywhere on the form: http://www.vbforums.com/showthread.php?t=616342
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  3. #3

    Thread Starter
    Fanatic Member Emcrank's Avatar
    Join Date
    Jan 2009
    Posts
    566

    Re: Moving a form without a titlebar

    i just put a timer on 1millisecond interval and on tick i put If windowstate maximised then windowstate normal. Works just as well But yea you could do that way.

  4. #4
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Moving a form without a titlebar

    Quote Originally Posted by Emcrank View Post
    i just put a timer on 1millisecond interval and on tick i put If windowstate maximised then windowstate normal. Works just as well But yea you could do that way.
    That's a pretty dumb reason to use a Timer. You can handle the Resize event, check for WindowState.Maximized and set it to normal instead of having a Timer constantly running in your program.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  5. #5
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Moving a form without a titlebar

    Quote Originally Posted by JuggaloBrotha View Post
    That's a pretty dumb reason to use a Timer. You can handle the Resize event, check for WindowState.Maximized and set it to normal instead of having a Timer constantly running in your program.
    I agree that your suggestion is far better than using a timer to check every millisecond but with your method the form is still going to maximize and then minimize again. If you just use either of the API's that myself or Paul suggested in that thread then nothing at all happens when the user double clicks the form, so surely that is the best solution?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  6. #6

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