c#—WinForm/WPF全屏 隐藏标题栏和任务栏

WinForm:

        /// 
        /// 全屏按钮
        /// 
        /// 
        /// 
        private void button1_Click(object sender, EventArgs e)
        {
            if (this.FormBorderStyle == FormBorderStyle.None)//全屏
            {
                this.WindowState = FormWindowState.Normal;
                this.FormBorderStyle = FormBorderStyle.Sizable;
            }
            else//非全屏
            {
                this.FormBorderStyle = FormBorderStyle.None;
                this.WindowState = FormWindowState.Normal;//先将窗体设置为Nomal,以防最大化时全屏出现的BUG
                this.WindowState = FormWindowState.Maximized;
            }
        }

        /// 
        /// 全屏的快捷功能,F12相当于单击按钮;Esc健,如果全屏则退出全屏
        /// 
        /// 
        /// 
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F12)
            {
                button1.PerformClick();
                e.Handled = true;
            }
            else if (e.KeyCode == Keys.Escape)//esc键盘退出全屏
            {
                if (this.FormBorderStyle == FormBorderStyle.None)//全屏
                {
                    this.WindowState = FormWindowState.Normal;
                    this.FormBorderStyle = FormBorderStyle.Sizable;
                    e.Handled = true;
                }
            }
        }

WPF:

代码逻辑和WinForm是一样的,只是属性名不太一样
        private void main_window_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.F12)
            {
                if (this.WindowStyle == WindowStyle.None)//全屏
                {
                    this.WindowState = WindowState.Normal;
                    this.WindowStyle = WindowStyle.SingleBorderWindow;
                }
                else//非全屏
                {
                    this.WindowStyle = WindowStyle.None;
                    this.WindowState = WindowState.Normal;
                    this.WindowState = WindowState.Maximized;
                }
            }
            else if (e.Key == Key.Escape)
            {
                if (this.WindowStyle == WindowStyle.None)//全屏
                {
                    this.WindowState = WindowState.Normal;
                    this.WindowStyle = WindowStyle.SingleBorderWindow;
                }
            }
        }


不知道为啥不用API任务栏也能隐藏...win7 32位测试可行。

你可能感兴趣的:(c#)