C# 双击全窗口/全屏显示和恢复

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;//DllImport用
 
namespace 全屏显示
{
    public partial class Form1 : Form
    {
        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
 
        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr GetDesktopWindow();
 
        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr GetParent(IntPtr hWnd);
 
        [DllImport("user32.dll", SetLastError = true)]
        static extern int GetSystemMetrics(int nIndex);
 
        [DllImport("user32.dll", EntryPoint = "ShowWindow", CharSet = CharSet.Auto)]
        public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
 
        [DllImport("User32.dll ", EntryPoint = "FindWindow")]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        public int SW_HIDE = 0;
        public int SW_RESTORE = 9;
 
        public bool m_bIsFullScreen=false;//判断是否全屏
        public bool m_bIsFillDock = false;//判断是否全容器
        Rectangle m_RecPanel1 = new Rectangle();//获取原始大小
 
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            m_RecPanel1 = Panel1.Bounds;//获得panel1原始Bounds范围大小
        }
 
        //控件panel1全屏显示
        private void panel1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (!m_bIsFullScreen)
            {
                //获取桌面窗口并设置为父窗口
                IntPtr hDesk = GetDesktopWindow();
                SetParent(this.Panel1.Handle, hDesk);
 
                int width = GetSystemMetrics(0);
                int height = GetSystemMetrics(1);
 
                this.Panel1.Dock = DockStyle.None;//这个很重要,Anchor属性仍可用
                this.Panel1.Bounds = new Rectangle(new Point(0, 0), new Size(width, height));
                ShowWindow(FindWindow("Shell_TrayWnd", null), SW_HIDE);//隐藏Windows任务栏
                //ShowWindow(FindWindow("Button", null), SW_HIDE);
                m_bIsFullScreen = true;
            }
            else
            {
                //获取桌面窗口并设置为父窗口
                IntPtr hDesk = GetDesktopWindow();
                SetParent(this.Panel1.Handle, this.Handle);
 
                int width = GetSystemMetrics(0);
                int height = GetSystemMetrics(1);
                this.Panel1.Dock = DockStyle.None;//这个很重要,Anchor属性仍可用
                this.Panel1.Bounds = m_RecPanel1;
                ShowWindow(FindWindow("Shell_TrayWnd", null), SW_RESTORE);//Shell_TrayWnd是任务栏的类名
                m_bIsFullScreen = false;
            }
        }
 
        //全窗口显示
        //也可以用全屏的方法将   panel2.Bounds=窗口的大小;
        private void panel2_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (!m_bIsFillDock)
            {
                this.panel2.Parent = this;//以窗口为父控件
                this.panel2.Dock = DockStyle.Fill;//填充至整个父控件大小
                panel2.BringToFront();//可以没有,用来保证panel2在最前面,但是还原时候会遮盖原来在上面的控件
                m_bIsFillDock = true;
            }
            else
            {
                this.panel2.Parent = this;
                this.panel2.Dock = DockStyle.None;
                //panel2.BringToFront();
                m_bIsFillDock = false;
            }
        }
    }
}

参考资料
1、隐藏和显示Windows任务栏:http://www.thinksaas.cn/topics/0/237/237013.html

2、C#中使用FindWindow函数详解:http://blog.csdn.net/soief/article/details/4094476

3、Showwindow 及参数:http://blog.csdn.net/bychentufeiyang/article/details/7164171

4、http://blog.csdn.net/u010956619/article/details/51174795

5、http://blog.163.com/wslngcjsdxdr@126/blog/static/16219623020161415256363/

作者:天涯遍地是小草
来源:CSDN
原文:https://blog.csdn.net/u012692537/article/details/56327712
版权声明:本文为博主原创文章,转载请附上博文链接!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值