C# WinForm窗体最大化
效果图:
核心代码:
1. 控制类
2.[按ESC返回]信息窗口类
3.如何调用
核心代码:
1. 控制类
窗体最大化控制类
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication2
{
public class Pb_C_MaxFrm
{
Form _MaxFrom;
ESCMsgFrm _ESCFrm;
FormBorderStyle _oriFrmBorderStyle;
FormWindowState _oriFrmWindowState;
bool _bIsMax = false;
public Pb_C_MaxFrm()
{
}
public Pb_C_MaxFrm(Form maxForm)
{
setMaxForm(maxForm);
}
public bool IsMax
{
get { return _bIsMax; }
}
public void setMaxForm(Form maxForm)
{
_MaxFrom = maxForm;
//记录窗体的原始状态
_oriFrmBorderStyle = maxForm.FormBorderStyle;
_oriFrmWindowState = maxForm.WindowState;
_MaxFrom.KeyDown+=new KeyEventHandler(_MaxFrom_KeyDown);
}
bool CheckData()
{
return _MaxFrom == null ? false : true;
}
void _MaxFrom_KeyDown(object sender, KeyEventArgs e)
{
//按下[F11]键执行全屏化
if (e.KeyCode == Keys.F11)
Show();
else if (e.KeyCode == Keys.Escape)//按下[Esc]键返回原始窗体
unShow();
}
public void Show()
{
if (!CheckData())
return;
#region 窗体最大化
if (_MaxFrom.WindowState != FormWindowState.Maximized || _MaxFrom.FormBorderStyle!= FormBorderStyle.None)
{
_MaxFrom.FormBorderStyle = FormBorderStyle.None;
_MaxFrom.WindowState = FormWindowState.Maximized;
#region 启动闪屏
if (_ESCFrm == null)
{
_ESCFrm = new ESCMsgFrm();
_ESCFrm.FormClosed += new FormClosedEventHandler(_ESCFrm_FormClosed);
_ESCFrm.Owner = _MaxFrom;
}
_ESCFrm.Show();
#endregion
}
#endregion
_bIsMax = true;
}
public void unShow()
{
if (!CheckData())
return;
if (_ESCFrm != null)
_ESCFrm.Close();
//返回到初始状态
_MaxFrom.FormBorderStyle = _oriFrmBorderStyle;
_MaxFrom.WindowState = _oriFrmWindowState;
_bIsMax = false;
}
void _ESCFrm_FormClosed(object sender, FormClosedEventArgs e)
{
_ESCFrm = null;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication2
{
public class Pb_C_MaxFrm
{
Form _MaxFrom;
ESCMsgFrm _ESCFrm;
FormBorderStyle _oriFrmBorderStyle;
FormWindowState _oriFrmWindowState;
bool _bIsMax = false;
public Pb_C_MaxFrm()
{
}
public Pb_C_MaxFrm(Form maxForm)
{
setMaxForm(maxForm);
}
public bool IsMax
{
get { return _bIsMax; }
}
public void setMaxForm(Form maxForm)
{
_MaxFrom = maxForm;
//记录窗体的原始状态
_oriFrmBorderStyle = maxForm.FormBorderStyle;
_oriFrmWindowState = maxForm.WindowState;
_MaxFrom.KeyDown+=new KeyEventHandler(_MaxFrom_KeyDown);
}
bool CheckData()
{
return _MaxFrom == null ? false : true;
}
void _MaxFrom_KeyDown(object sender, KeyEventArgs e)
{
//按下[F11]键执行全屏化
if (e.KeyCode == Keys.F11)
Show();
else if (e.KeyCode == Keys.Escape)//按下[Esc]键返回原始窗体
unShow();
}
public void Show()
{
if (!CheckData())
return;
#region 窗体最大化
if (_MaxFrom.WindowState != FormWindowState.Maximized || _MaxFrom.FormBorderStyle!= FormBorderStyle.None)
{
_MaxFrom.FormBorderStyle = FormBorderStyle.None;
_MaxFrom.WindowState = FormWindowState.Maximized;
#region 启动闪屏
if (_ESCFrm == null)
{
_ESCFrm = new ESCMsgFrm();
_ESCFrm.FormClosed += new FormClosedEventHandler(_ESCFrm_FormClosed);
_ESCFrm.Owner = _MaxFrom;
}
_ESCFrm.Show();
#endregion
}
#endregion
_bIsMax = true;
}
public void unShow()
{
if (!CheckData())
return;
if (_ESCFrm != null)
_ESCFrm.Close();
//返回到初始状态
_MaxFrom.FormBorderStyle = _oriFrmBorderStyle;
_MaxFrom.WindowState = _oriFrmWindowState;
_bIsMax = false;
}
void _ESCFrm_FormClosed(object sender, FormClosedEventArgs e)
{
_ESCFrm = null;
}
}
}
2.[按ESC返回]信息窗口类
按ESC返回
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication2
{
public partial class ESCMsgFrm : Form
{
public ESCMsgFrm()
{
InitializeComponent();
}
#region 窗体效果(暂时未用)
/// <summary>
/// 设置窗体的四角为圆角
/// </summary>
void setCircleCorner()
{
#region 圆角蒙板
System.Drawing.Drawing2D.GraphicsPath GraphicsPath = new System.Drawing.Drawing2D.GraphicsPath();
#region 左上
GraphicsPath.AddRectangle(new Rectangle(0, 0, 5, 5));
GraphicsPath.AddRectangle(new Rectangle(10, 0, 2, 1));
GraphicsPath.AddRectangle(new Rectangle(9, 0, 1, 2));
GraphicsPath.AddRectangle(new Rectangle(8, 0, 1, 2));
GraphicsPath.AddRectangle(new Rectangle(7, 0, 1, 3));
GraphicsPath.AddRectangle(new Rectangle(6, 0, 1, 4));
GraphicsPath.AddRectangle(new Rectangle(5, 0, 1, 5));
GraphicsPath.AddRectangle(new Rectangle(0, 5, 5, 1));
GraphicsPath.AddRectangle(new Rectangle(0, 6, 4, 1));
GraphicsPath.AddRectangle(new Rectangle(0, 7, 3, 1));
GraphicsPath.AddRectangle(new Rectangle(0, 8, 2, 1));
GraphicsPath.AddRectangle(new Rectangle(0, 9, 2, 1));
GraphicsPath.AddRectangle(new Rectangle(0, 10, 1, 1));
GraphicsPath.AddRectangle(new Rectangle(0, 11, 1, 1));
#endregion
#region 右上
GraphicsPath.AddRectangle(new Rectangle(Width - 11, 0, 2, 1));
GraphicsPath.AddRectangle(new Rectangle(Width - 9, 0, 2, 2));
GraphicsPath.AddRectangle(new Rectangle(Width - 7, 0, 1, 3));
GraphicsPath.AddRectangle(new Rectangle(Width - 6, 0, 1, 4));
GraphicsPath.AddRectangle(new Rectangle(Width - 5, 0, 5, 5));
GraphicsPath.AddRectangle(new Rectangle(Width - 4, 5, 4, 1));
GraphicsPath.AddRectangle(new Rectangle(Width - 3, 6, 5, 1));
GraphicsPath.AddRectangle(new Rectangle(Width - 2, 7, 2, 2));
GraphicsPath.AddRectangle(new Rectangle(Width - 1, 9, 1, 2));
#endregion
#region 左下
GraphicsPath.AddRectangle(new Rectangle(0, Height - 5, 5, 1));
GraphicsPath.AddRectangle(new Rectangle(0, Height - 6, 4, 1));
GraphicsPath.AddRectangle(new Rectangle(0, Height - 7, 3, 1));
GraphicsPath.AddRectangle(new Rectangle(0, Height - 9, 2, 2));
GraphicsPath.AddRectangle(new Rectangle(0, Height - 12, 1, 3));
GraphicsPath.AddRectangle(new Rectangle(0, Height - 4, 5, 4));
GraphicsPath.AddRectangle(new Rectangle(5, Height - 4, 1, 4));
GraphicsPath.AddRectangle(new Rectangle(6, Height - 3, 1, 3));
GraphicsPath.AddRectangle(new Rectangle(7, Height - 2, 2, 2));
GraphicsPath.AddRectangle(new Rectangle(9, Height - 1, 3, 1));
#endregion
#region 右下
GraphicsPath.AddRectangle(new Rectangle(Width - 9, Height - 1, 2, 1));
GraphicsPath.AddRectangle(new Rectangle(Width - 7, Height - 2, 1, 2));
GraphicsPath.AddRectangle(new Rectangle(Width - 6, Height - 3, 1, 3));
GraphicsPath.AddRectangle(new Rectangle(Width - 5, Height - 4, 1, 4));
GraphicsPath.AddRectangle(new Rectangle(Width - 4, Height - 4, 4, 4));
GraphicsPath.AddRectangle(new Rectangle(Width - 4, Height - 5, 5, 1));
GraphicsPath.AddRectangle(new Rectangle(Width - 3, Height - 6, 3, 1));
GraphicsPath.AddRectangle(new Rectangle(Width - 2, Height - 8, 2, 2));
GraphicsPath.AddRectangle(new Rectangle(Width - 1, Height - 11, 1, 3));
#endregion
System.Drawing.Region RegionX = new Region();
RegionX.Xor(GraphicsPath);
this.Region = RegionX;
#endregion
}
/// <summary>
/// 圆角透明
/// </summary>
/// <param name="sender"></param>
/// <param name="p_1"></param>
/// <param name="p_2"></param>
private void Type(Control sender, int p_1, double p_2)
{
GraphicsPath oPath = new GraphicsPath();
oPath.AddClosedCurve(new Point[] { new Point(0, sender.Height / p_1),
new Point(sender.Width / p_1, 0),
new Point(sender.Width - sender.Width / p_1, 0),
new Point(sender.Width, sender.Height / p_1),
new Point(sender.Width, sender.Height - sender.Height / p_1),
new Point(sender.Width - sender.Width / p_1, sender.Height),
new Point(sender.Width / p_1, sender.Height),
new Point(0, sender.Height - sender.Height / p_1) },
(float)p_2);
sender.Region = new Region(oPath);
}
#endregion
private void ESCMsgFrm_Load(object sender, EventArgs e)
{
this.ShowInTaskbar = false;//不在任务栏里显示
this.StartPosition = FormStartPosition.CenterScreen;
this.Opacity = 1;
//以下两句话可以保证窗体显示时只显示字体,因为背景色已被设置为透明
//设置窗体背景色为银色
this.BackColor = Color.Silver;
this.TransparencyKey = Color.Silver;//将窗体中银色部分设置为透明
timer1.Interval = 300;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (this.Opacity > 0)
{
this.Opacity -= 0.17;
}
else
{
this.Opacity = 0;
this.Close();
}
}
private void ESCMsgFrm_FormClosing(object sender, FormClosingEventArgs e)
{
timer1.Stop();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication2
{
public partial class ESCMsgFrm : Form
{
public ESCMsgFrm()
{
InitializeComponent();
}
#region 窗体效果(暂时未用)
/// <summary>
/// 设置窗体的四角为圆角
/// </summary>
void setCircleCorner()
{
#region 圆角蒙板
System.Drawing.Drawing2D.GraphicsPath GraphicsPath = new System.Drawing.Drawing2D.GraphicsPath();
#region 左上
GraphicsPath.AddRectangle(new Rectangle(0, 0, 5, 5));
GraphicsPath.AddRectangle(new Rectangle(10, 0, 2, 1));
GraphicsPath.AddRectangle(new Rectangle(9, 0, 1, 2));
GraphicsPath.AddRectangle(new Rectangle(8, 0, 1, 2));
GraphicsPath.AddRectangle(new Rectangle(7, 0, 1, 3));
GraphicsPath.AddRectangle(new Rectangle(6, 0, 1, 4));
GraphicsPath.AddRectangle(new Rectangle(5, 0, 1, 5));
GraphicsPath.AddRectangle(new Rectangle(0, 5, 5, 1));
GraphicsPath.AddRectangle(new Rectangle(0, 6, 4, 1));
GraphicsPath.AddRectangle(new Rectangle(0, 7, 3, 1));
GraphicsPath.AddRectangle(new Rectangle(0, 8, 2, 1));
GraphicsPath.AddRectangle(new Rectangle(0, 9, 2, 1));
GraphicsPath.AddRectangle(new Rectangle(0, 10, 1, 1));
GraphicsPath.AddRectangle(new Rectangle(0, 11, 1, 1));
#endregion
#region 右上
GraphicsPath.AddRectangle(new Rectangle(Width - 11, 0, 2, 1));
GraphicsPath.AddRectangle(new Rectangle(Width - 9, 0, 2, 2));
GraphicsPath.AddRectangle(new Rectangle(Width - 7, 0, 1, 3));
GraphicsPath.AddRectangle(new Rectangle(Width - 6, 0, 1, 4));
GraphicsPath.AddRectangle(new Rectangle(Width - 5, 0, 5, 5));
GraphicsPath.AddRectangle(new Rectangle(Width - 4, 5, 4, 1));
GraphicsPath.AddRectangle(new Rectangle(Width - 3, 6, 5, 1));
GraphicsPath.AddRectangle(new Rectangle(Width - 2, 7, 2, 2));
GraphicsPath.AddRectangle(new Rectangle(Width - 1, 9, 1, 2));
#endregion
#region 左下
GraphicsPath.AddRectangle(new Rectangle(0, Height - 5, 5, 1));
GraphicsPath.AddRectangle(new Rectangle(0, Height - 6, 4, 1));
GraphicsPath.AddRectangle(new Rectangle(0, Height - 7, 3, 1));
GraphicsPath.AddRectangle(new Rectangle(0, Height - 9, 2, 2));
GraphicsPath.AddRectangle(new Rectangle(0, Height - 12, 1, 3));
GraphicsPath.AddRectangle(new Rectangle(0, Height - 4, 5, 4));
GraphicsPath.AddRectangle(new Rectangle(5, Height - 4, 1, 4));
GraphicsPath.AddRectangle(new Rectangle(6, Height - 3, 1, 3));
GraphicsPath.AddRectangle(new Rectangle(7, Height - 2, 2, 2));
GraphicsPath.AddRectangle(new Rectangle(9, Height - 1, 3, 1));
#endregion
#region 右下
GraphicsPath.AddRectangle(new Rectangle(Width - 9, Height - 1, 2, 1));
GraphicsPath.AddRectangle(new Rectangle(Width - 7, Height - 2, 1, 2));
GraphicsPath.AddRectangle(new Rectangle(Width - 6, Height - 3, 1, 3));
GraphicsPath.AddRectangle(new Rectangle(Width - 5, Height - 4, 1, 4));
GraphicsPath.AddRectangle(new Rectangle(Width - 4, Height - 4, 4, 4));
GraphicsPath.AddRectangle(new Rectangle(Width - 4, Height - 5, 5, 1));
GraphicsPath.AddRectangle(new Rectangle(Width - 3, Height - 6, 3, 1));
GraphicsPath.AddRectangle(new Rectangle(Width - 2, Height - 8, 2, 2));
GraphicsPath.AddRectangle(new Rectangle(Width - 1, Height - 11, 1, 3));
#endregion
System.Drawing.Region RegionX = new Region();
RegionX.Xor(GraphicsPath);
this.Region = RegionX;
#endregion
}
/// <summary>
/// 圆角透明
/// </summary>
/// <param name="sender"></param>
/// <param name="p_1"></param>
/// <param name="p_2"></param>
private void Type(Control sender, int p_1, double p_2)
{
GraphicsPath oPath = new GraphicsPath();
oPath.AddClosedCurve(new Point[] { new Point(0, sender.Height / p_1),
new Point(sender.Width / p_1, 0),
new Point(sender.Width - sender.Width / p_1, 0),
new Point(sender.Width, sender.Height / p_1),
new Point(sender.Width, sender.Height - sender.Height / p_1),
new Point(sender.Width - sender.Width / p_1, sender.Height),
new Point(sender.Width / p_1, sender.Height),
new Point(0, sender.Height - sender.Height / p_1) },
(float)p_2);
sender.Region = new Region(oPath);
}
#endregion
private void ESCMsgFrm_Load(object sender, EventArgs e)
{
this.ShowInTaskbar = false;//不在任务栏里显示
this.StartPosition = FormStartPosition.CenterScreen;
this.Opacity = 1;
//以下两句话可以保证窗体显示时只显示字体,因为背景色已被设置为透明
//设置窗体背景色为银色
this.BackColor = Color.Silver;
this.TransparencyKey = Color.Silver;//将窗体中银色部分设置为透明
timer1.Interval = 300;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (this.Opacity > 0)
{
this.Opacity -= 0.17;
}
else
{
this.Opacity = 0;
this.Close();
}
}
private void ESCMsgFrm_FormClosing(object sender, FormClosingEventArgs e)
{
timer1.Stop();
}
}
}
3.如何调用
调用类
public partial class Form2 : Form
{
Pb_C_MaxFrm m;
public Form2()
{
InitializeComponent();
m = new Pb_C_MaxFrm();
m.setMaxForm(this);
}
private void Form2_Load(object sender, EventArgs e)
{
}
}
public partial class Form2 : Form
{
Pb_C_MaxFrm m;
public Form2()
{
InitializeComponent();
m = new Pb_C_MaxFrm();
m.setMaxForm(this);
}
private void Form2_Load(object sender, EventArgs e)
{
}
}