1. 新建FormFullscreen窗体
新建FormFullscreen窗体作为全屏显示,属性设置如下:
FormBorderStyle = None;
TopMost = true;
WindowState = Maximized;
2. 启动全屏的原窗体
启动全屏的原窗体中有按钮btnMaxScreen,点击按钮启动全屏。
FormFullscreen fs;
private void btnMaxScreen_Click(object sender, EventArgs e)
{
fs = new FormFullscreen();
fs.Controls.Add(panelScreen); //panelScreen为全屏显示的Control
fs.KeyUp += new KeyEventHandler(control_KeyUp); //FormFullscreen添加按钮监听
AddEventKeyUp(panelScreen);
fs.ShowDialog();
}
/**
递归方法使panelScreen的子控件也添加键盘监听,当焦点落在panelScreen的子控件上时ESC按键也有效
*/
private void AddEventKeyUp(Control p)
{
if (p != null)
{
p.KeyUp += new KeyEventHandler(control_KeyUp);
foreach (Control c in p.Controls)
{
AddEventKeyUp(c);
}
}
}
点击回车退出全屏的实现
private void control_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
if (fs != null)
{
fs.Controls.Clear();
//这句是为了把全屏显示的Control恢复到原窗体中,需根据实际情况修改。
//这里不能和下面的Close顺序错了,要不然会引起错误,因为Close后把控件销毁了。
this.panelContent.Controls.Add(this.panelScreen);
fs.Close();
fs = null;
}
}
}