C#窗體全屏功能

最近有朋友讓我給他弄個應用程序全屏的功能,例如銀行的取號程序界面。因此我從網上查詢了一些實現的方法。html

C#應用程序中如何實現全屏幕顯示功能?
 效果就像windows自帶的屏幕保護程序和衆多的遊戲那樣,不管是否設置了「將任務欄保持在其餘窗口的前端」都不顯示任務欄前端

實現方式一windows

在網上找來一些簡單的實現方式:函數

this.FormBorderStyle = FormBorderStyle.None;     //設置窗體爲無邊框樣式
this.WindowState = FormWindowState.Maximized;    //最大化窗體 

而後再設置窗體的位置和大小,例如:Width=1024 Height=768 Left=0 Top=0等size的值this

把以上兩句代碼直接放到Form1_Load的方法中,就能夠了,比較簡單,我就不貼代碼了。spa

參考出處:http://blog.csdn.net/friendan/article/details/7350570.net

============================================================code

實現方式二orm

調用系統的API函數,如user32.dll中的FindWindow和ShowWindow函數,具體代碼以下:htm

        [DllImport("user32.dll", EntryPoint = "ShowWindow")]
        public static extern Int32 ShowWindow(Int32 hwnd, Int32 nCmdShow);

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

代碼以下:

using System;
using System.Windows.Forms;

using System.Drawing;
using System.Runtime.InteropServices;


namespace FullScr
{
    public partial class Form1 : Form
    {

        Boolean m_IsFullScreen = false;//標記是否全屏


        public Form1()
        {
            InitializeComponent();
        }


        private void Form1_Load(object sender, EventArgs e)
        {
        }

        /// <summary>
        ///  全屏按鈕的Click事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            m_IsFullScreen = !m_IsFullScreen;//點一次全屏,再點還原。  
            this.SuspendLayout();
            if (m_IsFullScreen)//全屏 ,按特定的順序執行
            {
                SetFormFullScreen(m_IsFullScreen);
                this.FormBorderStyle = FormBorderStyle.None;
                this.WindowState = FormWindowState.Maximized;
                this.Activate();//
            }
            else//還原,按特定的順序執行——窗體狀態,窗體邊框,設置任務欄和工做區域
            {
                this.WindowState = FormWindowState.Normal;
                this.FormBorderStyle = FormBorderStyle.Sizable;
                SetFormFullScreen(m_IsFullScreen);
                this.Activate();
            }
            this.ResumeLayout(false);
        }

        /// <summary>  
        /// 設置全屏或這取消全屏  
        /// </summary>  
        /// <param name="fullscreen">true:全屏 false:恢復</param>  
        /// <param name="rectOld">設置的時候,此參數返回原始尺寸,恢復時用此參數設置恢復</param>  
        /// <returns>設置結果</returns>  
        public Boolean SetFormFullScreen(Boolean fullscreen)//, ref Rectangle rectOld
        {
            Rectangle rectOld = Rectangle.Empty;
            Int32 hwnd = 0;
            hwnd = FindWindow("Shell_TrayWnd", null);//獲取任務欄的句柄

            if (hwnd == 0) return false;

            if (fullscreen)//全屏
            {
                ShowWindow(hwnd, SW_HIDE);//隱藏任務欄

                SystemParametersInfo(SPI_GETWORKAREA, 0, ref rectOld, SPIF_UPDATEINIFILE);//get屏幕範圍
                Rectangle rectFull = Screen.PrimaryScreen.Bounds;//全屏範圍
                SystemParametersInfo(SPI_SETWORKAREA, 0, ref rectFull, SPIF_UPDATEINIFILE);//窗體全屏幕顯示
            }
            else//還原 
            {
                ShowWindow(hwnd, SW_SHOW);//顯示任務欄
                SystemParametersInfo(SPI_SETWORKAREA, 0, ref rectOld, SPIF_UPDATEINIFILE);//窗體還原
            }
            return true;
        }

        #region user32.dll

        public const Int32 SPIF_UPDATEINIFILE = 0x1;
        public const Int32 SPI_SETWORKAREA = 47;
        public const Int32 SPI_GETWORKAREA = 48;
        public const Int32 SW_SHOW = 5;
        public const Int32 SW_HIDE = 0;

        [DllImport("user32.dll", EntryPoint = "ShowWindow")]
        public static extern Int32 ShowWindow(Int32 hwnd, Int32 nCmdShow);

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

        [DllImport("user32.dll", EntryPoint = "SystemParametersInfo")]
        private static extern Int32 SystemParametersInfo(Int32 uAction, Int32 uParam, ref Rectangle lpvParam, Int32 fuWinIni);

        #endregion

    }
}


參考出處:http://www.cnblogs.com/ArcScofield/archive/2013/01/02/Form_Fullscreen.html

 

實現方式三

窗體最大化,不佔用任務欄,具體實現以下:

this.MaximizedBounds = Screen.PrimaryScreen.WorkingArea; //設置最大化的大小爲工做區域
this.WindowState = FormWindowState.Maximized;

 

出處:https://blog.csdn.net/lwjzjw/article/details/80828353

相關文章
相關標籤/搜索