Der Trick besteht darin, die HwndSource zu erhalten und ihre AddHook() - Methode aufzurufen. Dies funktioniert:
Imports System.Windows.Interop
Class Window1
Protected Overrides Sub OnSourceInitialized(ByVal e As System.EventArgs)
MyBase.OnSourceInitialized(e)
DirectCast(PresentationSource.FromVisual(Me), HwndSource).AddHook(AddressOf WndProc)
End Sub
Private Const WM_SYSCOMMAND As Integer = &H112
Private Const SC_MAXIMIZE As Integer = &HF030
Private Function WndProc(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wp As IntPtr, ByVal lp As IntPtr, ByRef handled As Boolean) As IntPtr
If msg = WM_SYSCOMMAND AndAlso wp.ToInt32() = SC_MAXIMIZE Then
Me.ResizeMode = ResizeMode.NoResize
Me.WindowStyle = WindowStyle.None
Me.WindowState = WindowState.Maximized
handled = True
End If
End Function
End Class
Der gleiche Code für eine WinForms Form:
Public Class Form1
Private Const WM_SYSCOMMAND As Integer = &H112
Private Const SC_MAXIMIZE As Integer = &HF030
Protected Overrides Sub WndProc(ByRef m As Message)
If m.Msg = WM_SYSCOMMAND AndAlso m.WParam.ToInt32() = SC_MAXIMIZE Then
Me.FormBorderStyle = FormBorderStyle.None
Me.WindowState = FormWindowState.Maximized
Return
End If
MyBase.WndProc(m)
End Sub
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
'' Restore window when the user presses Escape
If Me.WindowState = FormWindowState.Maximized AndAlso keyData = Keys.Escape Then
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.Sizable
Me.WindowState = FormWindowState.Normal
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
End Class
Vielen Dank. Können Sie mir ein Beispiel dafür geben, wie ich die Taskleiste ein- und ausblenden würde? – giodamelio
Haben Sie es versucht? Sie müssen die Taskleiste nicht ausblenden, ein randloses Fenster darf sie überlappen. –
Sein gibt mir viel errors.' ‚OnSourceInitialized‘ ist kein Mitglied von ‚System.Windows.Forms.Form'' – giodamelio