C# 双击鼠标全屏显示或全窗口显示 恢复
|
admin
2024年12月27日 23:59
本文热度 156
|
用控件panel1全屏显示;panel2全窗口显示
通过panel1、panel事件MouseDoubleClick实现。
遮盖panel1上的控件属性的Enabled设置为false,避免捕捉不到双击。
显示界面如下:
代码:
using System.Runtime.InteropServices;
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;
}
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;
this.panel1.Bounds = new Rectangle(new Point(0, 0), new Size(width, height));
ShowWindow(FindWindow("Shell_TrayWnd", 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;
this.panel1.Bounds = m_RecPanel1;
ShowWindow(FindWindow("Shell_TrayWnd", null), SW_RESTORE);
m_bIsFullScreen = false;
}
}
private void panel2_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (!m_bIsFillDock)
{
this.panel2.Parent = this;
this.panel2.Dock = DockStyle.Fill;
panel2.BringToFront();
m_bIsFillDock = true;
}
else
{
this.panel2.Parent = this;
this.panel2.Dock = DockStyle.None;
m_bIsFillDock = false;
}
}
}
}
源代码下载链接:
https://share.weiyun.com/imRUNgZj
该文章在 2024/12/28 11:57:28 编辑过