vb.net 按需显示或隐藏标题栏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1394197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Show or hide a title bar on demand
提问by beakersoft
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.
我想向我的 windows 窗体应用程序(用 vb.net 编写)添加一个选项,该选项将为用户提供隐藏菜单栏和标题栏的选项。我可以做菜单,但我不确定隐藏标题的最佳方法是什么。
I could just change the FormBorderStyle to none, but is that the best way of doing it?
我可以将 FormBorderStyle 更改为 none,但这是最好的方法吗?
Cheers Luke
干杯卢克
采纳答案by DVK
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)
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
但是,在运行时打开/关闭要困难得多,请参阅推理和如何操作的示例here
回答by Contraptor
I just found an easier solution that is working great for me during runtime. This question was posted a long time ago, but maybe somebody else will find this helpful.
我刚刚找到了一个更简单的解决方案,它在运行时非常适合我。这个问题是很久以前发布的,但也许其他人会发现这有帮助。
The eureka for me was learning to set the form's ControlBox property to false. Note too that the text property must be empty.
我的尤里卡正在学习将表单的 ControlBox 属性设置为 false。还要注意 text 属性必须为空。
Dim f As New Form
f.Text = String.Empty
f.ControlBox = False
f.Show(Me)
回答by Jules
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.
为了确保例程在 32 位和 64 位系统上都能正常工作,您需要做一些额外的检查。在这种情况下,我使用反射器来查看框架如何实现 pinvokes。特别是,看看 System.Windows.Forms.SafeNativeMethods 和 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
回答by Gleeson Paul
Actually you can hide the title bar during runtime (i found a way to do this), by hiding the form before you change the borderstyle to 0(/none) and then show it back again.
实际上,您可以在运行时隐藏标题栏(我找到了一种方法),方法是在将边框样式更改为 0(/none) 之前隐藏表单,然后再次显示它。
Sample
样本
If CheckBox1.Checked Then
Hide()
FormBorderStyle = Windows.Forms.FormBorderStyle.Fixed3D
Show()
Else
Hide()
FormBorderStyle = Windows.Forms.FormBorderStyle.None
Show()
End If
I used a checkbox to toggle it from 0 to 1/2/3/4/5. AND it works even if it has a value in TEXT property.
我使用复选框将其从 0 切换到 1/2/3/4/5。即使它在 TEXT 属性中有一个值,它也能工作。
BTW I'm using vb.net 2008.
顺便说一句,我正在使用 vb.net 2008。
I know this question was posted ages ago but i just want to share my answer.
我知道这个问题是很久以前发布的,但我只想分享我的答案。
回答by Basanta Das
Very simple just use as below
非常简单,使用如下
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