Making Winforms Fullscreen

2019-01-20 02:55发布

问题:

I need to make a winform full screen. This is what i found online.

1. Hook WinProc to catch WM_SYSCOMMAND

2. Check wParam == SC_MAXIMIZE and then

3. Set my windiw's attributes

Me.ResizeMode = ResizeMode.NoResize

Me.WindowStyle = WindowStyle.None

Me.WindowState = WindowState.Maximized

I am fairly new to vb.net and do not know how to do Steps 1 or 2. Can someone give me a snippet or point me in the right direction?

Thanks giodamelio

回答1:

The trick is to obtain the HwndSource and call its AddHook() method. This works:

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

The same code for a 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


回答2:

Sorry this is in C# (not VB) but perhaps it is still useful to you:

Here is a method that I use for a winforms app that has a full screen mode:

    private void FullScreen(bool Enable)
    {
        SizeChanged -= FormMain_SizeChanged;

        SuspendLayout();
        if (Enable)
        {
            FormBorderStyle = FormBorderStyle.None;
            WindowState = FormWindowState.Maximized;
            if (settings.HideFullScreenCursor)
                Cursor.Hide();
            menuStrip.Visible = false;
        }
        else
        {
            FormBorderStyle = FormBorderStyle.Sizable;
            WindowState = FormWindowState.Normal;
            if (settings.HideFullScreenCursor)
                Cursor.Show();
            menuStrip.Visible = true;
        }
        ResumeLayout();

        SizeChanged += FormMain_SizeChanged;
    }

Of course you will probably want to modify it to suit your needs, but hopefully it gives you a starting point.