萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> ASP.NET 程式設計

C# WinForm 完整螢幕模式實作 (隱藏工作列、無邊框、F11 切換)

前言

WinForm 真正的全螢幕需:隱藏工作列、移除表單邊框、最大化視窗、處理多螢幕、支援 F11/ESC 切換。本文提供可直接使用的 `FullScreenForm` 基底類別。

完整基底類別

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

public class FullScreenForm : Form
{
    private bool _isFullScreen;
    private Rectangle _oldBounds;
    private int _taskbarHwnd;

    #region Win32 API
    [DllImport("user32.dll")] private static extern int FindWindow(string lpClassName, string lpWindowName);
    [DllImport("user32.dll")] private static extern int ShowWindow(int hwnd, int nCmdShow);
    [DllImport("user32.dll")] private static extern int SystemParametersInfo(int uAction, int uParam, ref Rectangle lpvParam, int fuWinIni);
    private const int SW_HIDE = 0;
    private const int SW_SHOW = 5;
    private const int SPI_GETWORKAREA = 0x0030;
    private const int SPI_SETWORKAREA = 0x002F;
    private const int SPIF_UPDATEINIFILE = 0x01;
    #endregion

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        _taskbarHwnd = FindWindow("Shell_TrayWnd", null);
        KeyPreview = true; // 讓表單接收鍵盤事件
    }

    protected override void OnKeyDown(KeyEventArgs e)
    {
        base.OnKeyDown(e);
        if (e.KeyCode == Keys.F11) ToggleFullScreen();
        else if (e.KeyCode == Keys.Escape && _isFullScreen) ToggleFullScreen();
    }

    public void ToggleFullScreen()
    {
        _isFullScreen = !_isFullScreen;
        SetFullScreen(_isFullScreen);
    }

    private void SetFullScreen(bool fullscreen)
    {
        if (fullscreen)
        {
            // 記錄原始狀態
            _oldBounds = this.Bounds;
            _oldWindowState = this.WindowState;
            _oldFormBorderStyle = this.FormBorderStyle;

            // 隱藏工作列
            if (_taskbarHwnd != 0) ShowWindow(_taskbarHwnd, SW_HIDE);
            // 設定工作區為全螢幕
            Rectangle fullArea = Screen.FromControl(this).Bounds;
            SystemParametersInfo(SPI_SETWORKAREA, 0, ref fullArea, SPIF_UPDATEINIFILE);

            // 表單全螢幕
            this.FormBorderStyle = FormBorderStyle.None;
            this.WindowState = FormWindowState.Maximized;
            this.Bounds = Screen.FromControl(this).Bounds;
        }
        else
        {
            // 還原工作列
            if (_taskbarHwnd != 0) ShowWindow(_taskbarHwnd, SW_SHOW);
            // 還原工作區
            SystemParametersInfo(SPI_SETWORKAREA, 0, ref _oldBounds, SPIF_UPDATEINIFILE);

            // 還原表單
            this.FormBorderStyle = _oldFormBorderStyle;
            this.WindowState = _oldWindowState;
            this.Bounds = _oldBounds;
        }
    }

    private FormWindowState _oldWindowState;
    private FormBorderStyle _oldFormBorderStyle;

    protected override void OnFormClosing(FormClosingEventArgs e)
    {
        if (_isFullScreen) SetFullScreen(false); // 確保關閉時還原工作列
        base.OnFormClosing(e);
    }
}

使用方式

public partial class MainForm : FullScreenForm
{
    public MainForm()
    {
        InitializeComponent();
        // 可在按鈕點擊呼叫 ToggleFullScreen()
    }
}

注意事項

  • 多螢幕:`Screen.FromControl(this)` 取得表單所在螢幕,支援多螢幕獨立全螢幕。
  • 工作列隱藏:`SystemParametersInfo(SPI_SETWORKAREA)` 讓最大化視窗覆蓋工作列區域;`ShowWindow(SW_HIDE)` 隱藏工作列本身。兩者結合才是真全螢幕。
  • 關閉還原:`OnFormClosing` 強制還原,避免使用者關閉視窗後工作列消失。
  • DPI 感知:專案屬性 → Application → Manifest 勾選 "Enable DPI Awareness" 或在 `app.manifest` 加入 `true`,否則高 DPI 下座標計算偏移。
  • 安全性:隱藏工作列屬系統級操作,需管理員權限或用戶信任,建議僅在自家 kiosk 模式應用使用。

現代替代:WPF / WinUI 3

WPF 只需 `WindowState=Maximized; WindowStyle=None; ResizeMode=NoResize;` 即可真全螢幕,不需 P/Invoke。新專案建議考慮 WPF 或 WinUI 3。

copyright © 萬盛學電腦網 all rights reserved