使Winforms全屏

时间:2011-01-10 18:03:14

标签: vb.net winforms fullscreen

我需要全屏制作winform。这就是我在网上找到的。

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

我对vb.net相当新,不知道如何做第1步或第2步。有人可以给我一个片段或指向正确的方向吗?

谢谢giodamelio

2 个答案:

答案 0 :(得分:6)

诀窍是获取HwndSource并调用其AddHook()方法。这有效:

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

Winforms表单的相同代码:

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

答案 1 :(得分:0)

对不起,这是在C#(不是VB)中,但它可能对你有用:

以下是我用于具有全屏模式的winforms应用的方法:

    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;
    }

当然,您可能希望根据自己的需要对其进行修改,但希望它能为您提供一个起点。