views:

272

answers:

2

Hi,

I want to add an option to my windows form application (written in vb.net) that will give the user the option to hide the menu bar and the title bar. I can do the menu but i'm not sure what the best way is to hide the title.

I could just change the FormBorderStyle to none, but is that the best way of doing it?

Cheers Luke

+1  A: 

From this page, to do it at writing time:

form1.borderstyle = 0 (None), 1 (Fixed Single), 2 (Sizeable), 3 (Fixed Dialog), 4 (Fixed Toolwindow), 5 (Sizeable Toolwindow)

However, to turn on/off at runtime is much harder, see the reasoning and the example of how to do so Here

DVK
looks like what i am looking for, thanks a lot
beakersoft
+1  A: 

To ensure the routine works on both 32 and 64 bit systems, you need to do a little extra checking. In cases like these, I use reflector to have a look at how the framework implements the pinvokes. In particular, have a look at System.Windows.Forms.SafeNativeMethods and System.Windows.Forms.UnSafeNativeMethods.

Below is the code I use which takes advantage of extension methods.

'See: System.Windows.Forms.SafeNativeMethods.SetWindowPos
<DllImport("user32.dll", CharSet:=CharSet.Auto, ExactSpelling:=True)> _
Private Function SetWindowPos(ByVal hWnd As HandleRef, ByVal hWndInsertAfter As HandleRef, ByVal x As Integer, ByVal y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal flags As Integer) As Boolean
End Function

'See: System.Windows.Forms.UnSafeNativeMethods.GetWindowLong*
<DllImport("user32.dll", EntryPoint:="GetWindowLong", CharSet:=CharSet.Auto)> _
Private Function GetWindowLong32(ByVal hWnd As HandleRef, ByVal nIndex As Integer) As IntPtr
End Function

<DllImport("user32.dll", EntryPoint:="GetWindowLongPtr", CharSet:=CharSet.Auto)> _
Private Function GetWindowLongPtr64(ByVal hWnd As HandleRef, ByVal nIndex As Integer) As IntPtr
End Function

Private Function GetWindowLong(ByVal hWnd As HandleRef, ByVal nIndex As Integer) As IntPtr
    If (IntPtr.Size = 4) Then
        Return GetWindowLong32(hWnd, nIndex)
    End If
    Return GetWindowLongPtr64(hWnd, nIndex)
End Function

'See: System.Windows.Forms.UnSafeNativeMethods.SetWindowLong*
<DllImport("user32.dll", EntryPoint:="SetWindowLong", CharSet:=CharSet.Auto)> _
Private Function SetWindowLongPtr32(ByVal hWnd As HandleRef, ByVal nIndex As Integer, ByVal dwNewLong As HandleRef) As IntPtr
End Function

<DllImport("user32.dll", EntryPoint:="SetWindowLongPtr", CharSet:=CharSet.Auto)> _
Private Function SetWindowLongPtr64(ByVal hWnd As HandleRef, ByVal nIndex As Integer, ByVal dwNewLong As HandleRef) As IntPtr
End Function

Private Function SetWindowLong(ByVal hWnd As HandleRef, ByVal nIndex As Integer, ByVal dwNewLong As HandleRef) As IntPtr
    If (IntPtr.Size = 4) Then
        Return SetWindowLongPtr32(hWnd, nIndex, dwNewLong)
    End If
    Return SetWindowLongPtr64(hWnd, nIndex, dwNewLong)
End Function

'See: System.Windows.Forms.Control.SetWindowStyle
Private Sub SetWindowStyle(ByVal form As Form, ByVal flag As Integer, ByVal value As Boolean)
    Dim windowLong As Integer = CInt(CLng(GetWindowLong(New HandleRef(form, form.Handle), -16)))
    Dim ip As IntPtr
    If value Then
        ip = New IntPtr(windowLong Or flag)
    Else
        ip = New IntPtr(windowLong And Not flag)
    End If
    SetWindowLong(New HandleRef(form, form.Handle), -16, New HandleRef(Nothing, ip))
End Sub

<Extension()> _
Public Sub ShowCaption(ByVal form As Form)
    SetWindowStyle(form, &H400000, True)
    ApplyStyleChanges(form)
End Sub

<Extension()> _
Public Sub HideCaption(ByVal form As Form)
    SetWindowStyle(form, &H400000, False)
    ApplyStyleChanges(form)
End Sub

<Extension()> _
Public Function ApplyStyleChanges(ByVal form As Form) As Boolean
    Return SetWindowPos(New HandleRef(form, form.Handle), NullHandleRef, 0, 0, 0, 0, &H37)
End Function
Jules
That looks cool, i'll have a play around with it next week. Thanks for the info!
beakersoft