using System.Runtime.InteropServices;
namespace 雙擊全屏顯示或恢復(fù)
{
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)
{
//獲取桌面窗口并設(shè)置為父窗口
IntPtr hDesk = GetDesktopWindow();
SetParent(this.panel1.Handle, hDesk);
int width = GetSystemMetrics(0);
int height = GetSystemMetrics(1);
this.panel1.Dock = DockStyle.None;//這個(gè)很重要,Anchor屬性仍可用
this.panel1.Bounds = new Rectangle(new Point(0, 0), new Size(width, height));
ShowWindow(FindWindow("Shell_TrayWnd", null), SW_HIDE);//隱藏Windows任務(wù)欄
//ShowWindow(FindWindow("Button", null), SW_HIDE);
m_bIsFullScreen = true;
}
else
{
//獲取桌面窗口并設(shè)置為父窗口
IntPtr hDesk = GetDesktopWindow();
SetParent(this.panel1.Handle, this.Handle);
int width = GetSystemMetrics(0);
int height = GetSystemMetrics(1);
this.panel1.Dock = DockStyle.None;//這個(gè)很重要,Anchor屬性仍可用
this.panel1.Bounds = m_RecPanel1;
ShowWindow(FindWindow("Shell_TrayWnd", null), SW_RESTORE);//Shell_TrayWnd是任務(wù)欄的類名
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;//填充至整個(gè)父控件大小
panel2.BringToFront();//可以沒(méi)有,用來(lái)保證panel2在最前面,但是還原時(shí)候會(huì)遮蓋原來(lái)在上面的控件
m_bIsFillDock = true;
}
else
{
this.panel2.Parent = this;
this.panel2.Dock = DockStyle.None;
//panel2.BringToFront();
m_bIsFillDock = false;
}
}
}
}