WINFORM 自己写的全屏事件 有办法避免全屏的过程中窗体闪烁吗?
首先相关代码:
private void control_magnify(double fw, double fh)//按长宽比例变动调整控件位置 放大用
{
int x;
int y;
x = Convert.ToInt32(panel1.Size.Width * fw);
panel1.Size = new Size(x, panel1.Size.Height);
//x = Convert.ToInt32(panel2.Location.X * fw);
//y = Convert.ToInt32(panel2.Location.Y * fh);
//panel2.Location = new Point(x, y);
x = Convert.ToInt32(panel2.Size.Width * fw);
y = Convert.ToInt32(panel2.Size.Height * fh);
panel2.Size = new Size(x, y);
//x = Convert.ToInt32(panel3.Location.X * fw);
//y = Convert.ToInt32(panel3.Location.Y * fh);
//panel3.Location = new Point(x, y);
x = Convert.ToInt32(panel3.Size.Width * fw);
y = Convert.ToInt32(panel3.Size.Height * fh);
panel3.Size = new Size(x, y);
foreach (Control ctl in this.Controls)
{
x = Convert.ToInt32(ctl.Location.X * fw);
y = Convert.ToInt32(ctl.Location.Y * fh);
ctl.Location = new Point(x, y);
}
foreach (Control ctl in this.panel2.Controls)
{
if (ctl is PictureBox)
{
x = Convert.ToInt32(ctl.Location.X * fw);
y = Convert.ToInt32(ctl.Location.Y * fh);
ctl.Location = new Point(x, y);
}
}
foreach (Control ctl in this.panel3.Controls)
{
if (ctl is PictureBox)
{
x = Convert.ToInt32(ctl.Location.X * fw);
y = Convert.ToInt32(ctl.Location.Y * fh);
ctl.Location = new Point(x, y);
}
}
}
private void control_minify(double fw, double fh)//按长宽比例变动调整控件位置 缩小用
{
int x;
int y;
x = Convert.ToInt32(panel1.Size.Width / fw);
panel1.Size = new Size(x, panel1.Size.Height);
x = Convert.ToInt32(panel2.Size.Width / fw);
y = Convert.ToInt32(panel2.Size.Height / fh);
panel2.Size = new Size(x, y);
//x = Convert.ToInt32(panel3.Location.X / fw);
//y = Convert.ToInt32(panel3.Location.Y / fh);
//panel3.Location = new Point(x, y);
x = Convert.ToInt32(panel3.Size.Width / fw);
y = Convert.ToInt32(panel3.Size.Height / fh);
panel3.Size = new Size(x, y);
foreach (Control ctl in this.Controls)
{
x = Convert.ToInt32(ctl.Location.X / fw);
y = Convert.ToInt32(ctl.Location.Y / fh);
ctl.Location = new Point(x, y);
}
foreach (Control ctl in this.panel2.Controls)
{
if (ctl is PictureBox)
{
x = Convert.ToInt32(ctl.Location.X / fw);
y = Convert.ToInt32(ctl.Location.Y / fh);
ctl.Location = new Point(x, y);
}
}
foreach (Control ctl in this.panel3.Controls)
{
if (ctl is PictureBox)
{
x = Convert.ToInt32(ctl.Location.X / fw);
y = Convert.ToInt32(ctl.Location.Y / fh);
ctl.Location = new Point(x, y);
}
}
}
private void FullScreen_Click(object sender, EventArgs e)//“全屏”按钮事件
{
double w = this.Size.Width;
double h = this.Size.Height;
fw = SW / w;
fh = SH / h;
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
this.KeyPreview = true;
this.KeyUp += new KeyEventHandler(Form2_KeyUp);
control_magnify(fw, fh);
FullScreen.Click -= new EventHandler(FullScreen_Click);
}
private void Form2_KeyUp(object sender, KeyEventArgs e)//ESC退出全屏
{
if (e.KeyCode == Keys.Escape) //“Esc” 按键退出全屏
{
this.FormBorderStyle = FormBorderStyle.FixedDialog;
this.WindowState = FormWindowState.Normal;
control_minify(fw, fh);
this.KeyUp -= new KeyEventHandler(Form2_KeyUp);
FullScreen.Click += new EventHandler(FullScreen_Click);
}
}
[DllImport("user32.dll")]
private static extern bool AnimateWindow(IntPtr hwnd, int dwTime, int dwFlags);
private const int AW_HOR_POSITIVE = 0x0001;//从左到右打开窗口
private const int AW_HOR_NEGATIVE = 0x0002;//从右到左打开窗口
private const int AW_VER_POSITIVE = 0x0004;//从上到下打开窗口
private const int AW_VER_NEGATIVE = 0x0008;//从下到上打开窗口
private const int AW_CENTER = 0x0010;//从中央打开
private const int AW_HIDE = 0x10000;//隐藏窗体
private const int AW_ACTIVATE = 0x20000;//显示窗体
private const int AW_SLIDE = 0x40000;
private const int AW_BLEND = 0x80000;//淡入淡出效果
//调用
AnimateWindow(this.Handle, 5, AW_VER_NEGATIVE);