|
-
May 27th, 2010, 03:17 PM
#1
Thread Starter
Lively Member
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:
Const WM_NCHITTEST As Integer = &H84
Const HTCLIENT As Integer = &H1
Const HTCAPTION As Integer = &H2
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
Select Case m.Msg
Case WM_NCHITTEST
MyBase.WndProc(m)
If m.Result = HTCLIENT Then m.Result = HTCAPTION
Case Else
'Make sure you pass unhandled messages back to the default message handler.
MyBase.WndProc(m)
End Select
End Sub
-
May 27th, 2010, 03:22 PM
#2
Re: Borderless Form Question
Set the MaximizeBox property of the form to False ?
-
May 27th, 2010, 03:30 PM
#3
Thread Starter
Lively Member
Re: Borderless Form Question
 Originally Posted by chris128
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.
-
May 27th, 2010, 03:33 PM
#4
Re: Borderless Form Question
No it also stops other methods of maximising the form (its a bit of a badly named property really)
-
May 27th, 2010, 03:43 PM
#5
Thread Starter
Lively Member
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.
-
May 27th, 2010, 04:02 PM
#6
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!
-
May 27th, 2010, 04:17 PM
#7
Re: Borderless Form Question
the problem with your code is that when you modify the message:
vb Code:
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:
Imports System.Runtime.InteropServices
Public Class Form1
#Region " Functions and Constants "
<DllImport("user32.dll")> _
Public Shared Function ReleaseCapture() As Boolean
End Function
<DllImport("user32.dll")> _
Public Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
End Function
Public Declare Function GetCapture Lib "user32" Alias "GetCapture" () As Integer
Private Const WM_NCLBUTTONDOWN As Integer = &HA1
Private Const HTCAPTION As Integer = 2
#End Region
Private mouseOnMove As Boolean = False
Private Sub move_mousedown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
mouseOnMove = True
End Sub
Private Sub move_mousemove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
If mouseOnMove Then
ReleaseCapture()
SendMessage(Me.Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0)
If GetCapture = 0 Then
mouseOnMove = False
End If
End If
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 27th, 2010, 04:20 PM
#8
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:
Public Class Form1
Const WS_MAXIMIZEBOX As Integer = &H10000
Const GWL_STYLE As Integer = -16
Const WM_NCHITTEST As Integer = &H84
Const HTCLIENT As Integer = &H1
Const HTCAPTION As Integer = &H2
<System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint:="SetWindowLong")> _
Public Shared Function SetWindowLong(<System.Runtime.InteropServices.InAttribute()> ByVal hWnd As System.IntPtr, ByVal nIndex As Integer, ByVal dwNewLong As UInt64) As Integer
End Function
<System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint:="GetWindowLong")> _
Public Shared Function GetWindowLong(<System.Runtime.InteropServices.InAttribute()> ByVal hWnd As System.IntPtr, ByVal nIndex As Integer) As UInt64
End Function
'Example in a button click event - form load event may be more appropriate
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Get the current window settings
Dim Result As UInt64 = GetWindowLong(Me.Handle, GWL_STYLE)
'Specify that we do not want the maximize box
Result = (Result And Not CULng(WS_MAXIMIZEBOX))
'Apply the new settings to the window
SetWindowLong(Me.Handle, GWL_STYLE, Result)
End Sub
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
Select Case m.Msg
Case WM_NCHITTEST
MyBase.WndProc(m)
If CInt(m.Result) = HTCLIENT Then
m.Result = New IntPtr(HTCAPTION)
End If
Case Else
'Make sure you pass unhandled messages back to the default message handler.
MyBase.WndProc(m)
End Select
End Sub
End Class
-
May 28th, 2010, 02:33 AM
#9
Thread Starter
Lively Member
Re: Borderless Form Question
Many thanks chris128 and .paul.
Both work great.
-
May 28th, 2010, 02:55 AM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|