根据需要显示或隐藏标题栏

时间:2009-09-08 14:00:43

标签: vb.net winforms

我想在我的Windows窗体应用程序(用vb.net编写)中添加一个选项,该选项将为用户提供隐藏菜单栏和标题栏的选项。我可以做菜单,但我不确定隐藏标题的最佳方法是什么。

我可以将FormBorderStyle更改为none,但这是最好的方法吗?

干杯 路加

5 个答案:

答案 0 :(得分:13)

我刚刚找到了一个更简单的解决方案,它在运行时非常适合我。这个问题是很久以前发布的,但也许有人会觉得这很有帮助。

对我而言,尤里卡正在学习将表单的ControlBox属性设置为false。另请注意,text属性必须为空。

    Dim f As New Form
    f.Text = String.Empty
    f.ControlBox = False
    f.Show(Me)

答案 1 :(得分:3)

From this page,在写作时这样做:

  

form1.borderstyle = 0(无),1(固定单一),2(大小),3(固定对话框),4(固定工具窗口),5(大小工具窗口)

但是,要在运行时打开/关闭要困难得多,请参阅推理以及如何执行此操作的示例Here

答案 2 :(得分:2)

为了确保例程在32位和64位系统上都能正常工作,您需要做一些额外的检查。 在这些情况下,我使用反射器来了解框架如何实现pinvokes。特别要看一下System.Windows.Forms.SafeNativeMethods和System.Windows.Forms.UnSafeNativeMethods。

以下是我使用的代码,它利用了扩展方法。

'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

答案 3 :(得分:0)

实际上,您可以在运行时隐藏标题栏(我找到了一种方法),在将bordertyle更改为0(/ none)之前隐藏表单,然后再将其显示回来。

样品

If CheckBox1.Checked Then
    Hide()
    FormBorderStyle = Windows.Forms.FormBorderStyle.Fixed3D
    Show()
Else
    Hide()
    FormBorderStyle = Windows.Forms.FormBorderStyle.None
    Show()
End If

我用一个复选框将其从0切换到1/2/3/4/5。即使它在TEXT属性中有值,它也能工作。

BTW我正在使用vb.net 2008。

我知道这个问题很久以前就已发布了,但我只是想分享一下我的答案。

答案 4 :(得分:0)

非常简单,只需按以下方式使用

if Me.ControlBox = True Then 'To hide if already Title bar is there
   Me.Text = String.Empty
   Me.ControlBox = False
Else  ' To show if Title bar is invisible
   Me.Text = "Title"
   Me.ControlBox = True
End If
相关问题