winform窗體全屏


bool fullscreen = false;
Rectangle rect = new Rectangle();
private void button4_Click(object sender, EventArgs e)
{
    fullscreen = !fullscreen;//循環。點一次全屏,再點還原。
    SetFullScreen(fullscreen, ref rect);
    if (fullscreen)
    {
        this.WindowState = FormWindowState.Maximized;//全屏
    }
    else
    {
        this.WindowState = FormWindowState.Normal;//還原
    }
}

/// <summary>
/// 設置全屏或這取消全屏
/// </summary>
/// <param name="fullscreen">true:全屏 false:恢復</param>
/// <param name="rectOld">設置的時候,此參數返回原始尺寸,恢復時用此參數設置恢復</param>
/// <returns>設置結果</returns>
public static bool SetFullScreen(bool fullscreen, ref Rectangle rectOld)
{
    int Hwnd = 0;
    Hwnd = FindWindow("Shell_TrayWnd", null);
    if (Hwnd == 0) return false;
    if (fullscreen)
    {
        ShowWindow(Hwnd, SW_HIDE);
        Rectangle rectFull = Screen.PrimaryScreen.Bounds;
        SystemParametersInfo(SPI_GETWORKAREA, 0, ref rectOld, SPIF_UPDATEINIFILE);//get
        SystemParametersInfo(SPI_SETWORKAREA, 0, ref rectFull, SPIF_UPDATEINIFILE);//set
    }
    else
    {
        ShowWindow(Hwnd, SW_SHOW);
        SystemParametersInfo(SPI_SETWORKAREA, 0, ref rectOld, SPIF_UPDATEINIFILE);
    }
    return true;
}

[DllImport("user32.dll", EntryPoint = "ShowWindow")]
public static extern int ShowWindow(int hwnd, int nCmdShow);
public const int SW_SHOW = 5; public const int SW_HIDE = 0;

[DllImport("user32.dll", EntryPoint = "SystemParametersInfo")]
private static extern int SystemParametersInfo(int uAction, int uParam, ref Rectangle lpvParam, int fuWinIni);
public const int SPIF_UPDATEINIFILE = 0x1;
public const int SPI_SETWORKAREA = 47;
public const int SPI_GETWORKAREA = 48;

[DllImport("user32.dll", EntryPoint = "FindWindow")]
private static extern int FindWindow(string lpClassName, string lpWindowName);

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 

 

class WindowsFullScreenApi
    {
        [DllImport("user32.dll", EntryPoint = "GetSystemMetrics")]
        public static extern int GetSystemMetrics(int which);
 
        [DllImport("user32.dll")]
        public static extern void
            SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter,
                         int X, int Y, int width, int height, uint flags);
 
        private const int SM_CXSCREEN = 0;
        private const int SM_CYSCREEN = 1;
        private static IntPtr HWND_TOP = IntPtr.Zero;
        private const int SWP_SHOWWINDOW = 64; // 0x0040
 
        public static int ScreenX
        {
            get { return GetSystemMetrics(SM_CXSCREEN); }
        }
 
        public static int ScreenY
        {
            get { return GetSystemMetrics(SM_CYSCREEN); }
        }
 
        public static void SetWinFullScreen(IntPtr hwnd)
        {
            SetWindowPos(hwnd, HWND_TOP, 0, 0, ScreenX, ScreenY, SWP_SHOWWINDOW);
        }
    }

 

調用::全屏

FormBorderStyle = FormBorderStyle.None;
                WindowState = FormWindowState.Maximized;
                WindowsFullScreenApi.SetWinFullScreen(Handle);

 

調用::恢復

FormBorderStyle = FormBorderStyle.Sizable;
                WindowState = FormWindowState.Maximized;//自己控制恢復后是什么狀態

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM