Results 1 to 10 of 10

Thread: Borderless Form Question

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2008
    Posts
    113

    Borderless Form Question

    I am using the following code to move a border less form and it works just fine, however when I double click anywhere on the form, the form is maximized, how do I stop this happening?

    vb Code:
    1. Const WM_NCHITTEST As Integer = &H84
    2.     Const HTCLIENT As Integer = &H1
    3.     Const HTCAPTION As Integer = &H2
    4.     Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    5.         Select Case m.Msg
    6.             Case WM_NCHITTEST
    7.                 MyBase.WndProc(m)
    8.                 If m.Result = HTCLIENT Then m.Result = HTCAPTION
    9.                
    10.             Case Else
    11.                 'Make sure you pass unhandled messages back to the default message handler.
    12.                 MyBase.WndProc(m)
    13.         End Select
    14.     End Sub

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

    Re: Borderless Form Question

    Set the MaximizeBox property of the form to False ?
    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
    Lively Member
    Join Date
    May 2008
    Posts
    113

    Re: Borderless Form Question

    Quote Originally Posted by chris128 View Post
    Set the MaximizeBox property of the form to False ?
    Doesn't that just set whether the maximise box is displayed or not?
    I need to stop the form maximising.

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

    Re: Borderless Form Question

    No it also stops other methods of maximising the form (its a bit of a badly named property really)
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

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


  5. #5

    Thread Starter
    Lively Member
    Join Date
    May 2008
    Posts
    113

    Re: Borderless Form Question

    Even after setting MaximizeBox property to false, I can still maximize form by mouse double click anywhere on form.
    I really want to keep the borderless form but need to disable maximize.

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

    Re: Borderless Form Question

    Oh yeah so it does... that is weird - if you set the form border style to anything other than None then even with that code to make any part of the window draggable it still doesnt maximise when you double click anywhere (even the title bar). As soon as I set it to None though it mazimises when you double click anywhere just like you said.
    I guess I understand why it does it - because that code you are using basically tricks the OS into thinking that every time you click anywhere on the form you are actually clicking on the title bar, and double clicking the title bar normally maximises the window. I suppose when you set the border style to None then .NET doesnt do whatever it would normally do when you set the MaximizeBox property to False because it thinks there is no need to, as there will be no Maximize Box.
    I'm sure you could use a Windows API to do whatever it is the .NET framework does that stops it from being maximised though... give me a minute!
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

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


  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Borderless Form Question

    the problem with your code is that when you modify the message:

    vb Code:
    1. If m.Result = HTCLIENT Then m.Result = HTCAPTION

    vb interprets mouseclicks in the client area as mouseclicks in the caption area, + doubleclicking a titlebar toggles the windowstate. try this instead:

    vb Code:
    1. Imports System.Runtime.InteropServices
    2.  
    3. Public Class Form1
    4.  
    5. #Region " Functions and Constants "
    6.  
    7.     <DllImport("user32.dll")> _
    8.     Public Shared Function ReleaseCapture() As Boolean
    9.     End Function
    10.  
    11.     <DllImport("user32.dll")> _
    12.     Public Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    13.     End Function
    14.  
    15.     Public Declare Function GetCapture Lib "user32" Alias "GetCapture" () As Integer
    16.  
    17.     Private Const WM_NCLBUTTONDOWN As Integer = &HA1
    18.     Private Const HTCAPTION As Integer = 2
    19.    
    20. #End Region
    21.  
    22.     Private mouseOnMove As Boolean = False
    23.  
    24.     Private Sub move_mousedown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
    25.         mouseOnMove = True
    26.     End Sub
    27.  
    28.     Private Sub move_mousemove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
    29.         If mouseOnMove Then
    30.             ReleaseCapture()
    31.             SendMessage(Me.Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0)
    32.             If GetCapture = 0 Then
    33.                 mouseOnMove = False
    34.             End If
    35.         End If
    36.     End Sub
    37.  
    38. End Class

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

    Re: Borderless Form Question

    Or if you want to stick with your current method I've got the API working that disables the maximise function.

    Here is a full example, including your original code (which I had to edit slightly because you have not got Option Strict turned on - I would recommend you turn it on) :

    vb Code:
    1. Public Class Form1
    2.  
    3.     Const WS_MAXIMIZEBOX As Integer = &H10000
    4.     Const GWL_STYLE As Integer = -16
    5.     Const WM_NCHITTEST As Integer = &H84
    6.     Const HTCLIENT As Integer = &H1
    7.     Const HTCAPTION As Integer = &H2
    8.  
    9.     <System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint:="SetWindowLong")> _
    10.     Public Shared Function SetWindowLong(<System.Runtime.InteropServices.InAttribute()> ByVal hWnd As System.IntPtr, ByVal nIndex As Integer, ByVal dwNewLong As UInt64) As Integer
    11.     End Function
    12.  
    13.     <System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint:="GetWindowLong")> _
    14.     Public Shared Function GetWindowLong(<System.Runtime.InteropServices.InAttribute()> ByVal hWnd As System.IntPtr, ByVal nIndex As Integer) As UInt64
    15.     End Function
    16.  
    17.     'Example in a button click event - form load event may be more appropriate
    18.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    19.         'Get the current window settings
    20.         Dim Result As UInt64 = GetWindowLong(Me.Handle, GWL_STYLE)
    21.         'Specify that we do not want the maximize box
    22.         Result = (Result And Not CULng(WS_MAXIMIZEBOX))
    23.         'Apply the new settings to the window
    24.         SetWindowLong(Me.Handle, GWL_STYLE, Result)
    25.  
    26.     End Sub
    27.  
    28.     Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    29.         Select Case m.Msg
    30.             Case WM_NCHITTEST
    31.                 MyBase.WndProc(m)
    32.                 If CInt(m.Result) = HTCLIENT Then
    33.                     m.Result = New IntPtr(HTCAPTION)
    34.                 End If
    35.             Case Else
    36.                 'Make sure you pass unhandled messages back to the default message handler.
    37.                 MyBase.WndProc(m)
    38.         End Select
    39.     End Sub
    40.  
    41. End Class
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

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


  9. #9

    Thread Starter
    Lively Member
    Join Date
    May 2008
    Posts
    113

    Re: Borderless Form Question

    Many thanks chris128 and .paul.

    Both work great.

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

    Re: Borderless Form Question

    If you need resizing too, check out the link in my signature. It uses the same method that paul described above, and some additional code to allow resizing + change the mouse cursor to an appropriate resizing cursor.

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