Moving a form without a titlebar
To make a form movable when it has no titlebar
Use
VB.NET Code:
#Region " MoveFormWithoutTitleBar "
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
Dim Result As UInt64 = GetWindowLong(Me.Handle, GWL_STYLE)
Result = (Result And Not CULng(WS_MAXIMIZEBOX))
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
MyBase.WndProc(m)
End Select
End Sub
#End Region
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
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.
Re: Moving a form without a titlebar
Quote:
Originally Posted by
Emcrank
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.
Re: Moving a form without a titlebar
Quote:
Originally Posted by
JuggaloBrotha
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?
Re: Moving a form without a titlebar