2009-09-08 24 views
6

Quiero agregar una opción a mi aplicación de formulario de Windows (escrita en vb.net) que le dará al usuario la opción de ocultar la barra de menú y la barra de título. Puedo hacer el menú, pero no estoy seguro de cuál es la mejor manera de ocultar el título.Mostrar u ocultar una barra de título bajo demanda

Podría simplemente cambiar el FormBorderStyle a ninguno, pero ¿es esa la mejor manera de hacerlo?

Saludos Lucas

Respuesta

2

From this page, hacerlo en tiempo de la escritura:

form1.borderstyle = 0 (ninguno), 1 (Fixed Single), 2 (Sizeable), 3 (diálogo fijo), 4 (fijo toolwindow), 5 (Sizeable toolwindow)

Sin embargo, para encender/apagar en tiempo de ejecución es mucho más difícil, ver el razonamiento y el ejemplo de cómo hacerlo Here

+0

parece lo que estoy buscando, muchas gracias – beakersoft

+0

la solución de @Contraptor a continuación es la solución correcta a este problema –

2

Para garantizar que la rutina funcione en los sistemas de 32 y 64 bits, debe realizar una pequeña comprobación adicional. En casos como estos, utilizo el reflector para ver cómo el marco implementa los pinvokes. En particular, eche un vistazo a System.Windows.Forms.SafeNativeMethods y System.Windows.Forms.UnSafeNativeMethods.

A continuación se muestra el código que uso que aprovecha los métodos de extensión.

'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 
+0

Eso parece genial, voy a tener un juego con él la próxima semana. Gracias por la info! – beakersoft

+0

¿Por qué subclase la ventana, sin embargo, cuando está en código .NET administrado y hay maneras administradas de lograr lo mismo? – Craig

12

Acabo de encontrar una solución más fácil que funciona muy bien para mí durante el tiempo de ejecución. Esta pregunta fue publicada hace mucho tiempo, pero tal vez alguien más lo encuentre útil.

El eureka para mí estaba aprendiendo a establecer la propiedad ControlBox del formulario en falso. Tenga en cuenta también que la propiedad de texto debe estar vacía.

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

En realidad se puede ocultar la barra de título en tiempo de ejecución (i encontrado una manera de hacer esto), al ocultar el formulario antes de cambiar la borderstyle a 0 (/ ninguno) y luego mostrarlo de nuevo.

Muestra

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

I utilizó una casilla de verificación para cambiar entre los modos de 0 a 1/2/3/4/5. Y funciona incluso si tiene un valor en la propiedad TEXTO.

Por cierto estoy usando vb.net 2008.

Sé que esta pregunta fue publicada hace mucho tiempo, pero sólo quiero compartir mi respuesta.

Cuestiones relacionadas