C# 中winForm 全屏与非全屏 切换闪动问题,大家有没有什么好的办法解决?为什么浏览器的全屏就不会很闪呢?1.
this.WindowState = FormWindowState.Normal;
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized; 2.
if (this.FormBorderStyle == FormBorderStyle.None)
{
this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.WindowState = FormWindowState.Normal;
}
else
{
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
}代码类似如上。
this.WindowState = FormWindowState.Normal;
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized; 2.
if (this.FormBorderStyle == FormBorderStyle.None)
{
this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.WindowState = FormWindowState.Normal;
}
else
{
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
}代码类似如上。
[DllImport("user32.dll", EntryPoint = "LockWindowUpdate")]
public static extern int LockWindowUpdate(int hwndLock);
try
{
LockWindowUpdate(this.Handle.ToInt32());
if (this.FormBorderStyle == FormBorderStyle.None)
{
this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.WindowState = FormWindowState.Normal;
}
else
{
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
}
}
finally
{
LockWindowUpdate(0);
}
{
Rectangle selfRect = new Rectangle();
public Form1()
{
InitializeComponent();
} private void button2_Click(object sender, EventArgs e)
{
Rectangle scrRect = new Rectangle(Screen.GetBounds(this).Location, Screen.GetBounds(this).Size); this.SuspendLayout();
if (this.FormBorderStyle == FormBorderStyle.FixedSingle)
{
selfRect.Location = this.Location;
selfRect.Size = this.Size;
this.FormBorderStyle = FormBorderStyle.None;
this.Location = scrRect.Location;
this.Size = scrRect.Size;
}
else
{
this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.Location = selfRect.Location;
this.Size = selfRect.Size;
}
this.ResumeLayout(true);
}
}
不是很熟悉C#,有些函数可能用的比较笨,但是能满足你的要求
{
Rectangle selfRect = new Rectangle();
public Form1()
{
InitializeComponent();
} private void button2_Click(object sender, EventArgs e)
{
Rectangle scrRect = SystemInformation.WorkingArea; scrRect.Height = scrRect.Height - 2;
this.SuspendLayout();
if (this.FormBorderStyle == FormBorderStyle.FixedSingle)
{
selfRect.Location = this.Location;
selfRect.Size = this.Size;
this.FormBorderStyle = FormBorderStyle.None;
this.Location = scrRect.Location;
this.Size = scrRect.Size;
}
else
{
this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.Location = selfRect.Location;
this.Size = selfRect.Size;
}
this.ResumeLayout(false);
}
不外乎就是改变窗口大小,然后重画里边的元素,这期间不要显示重画的过程,我想我的代码就是这个过程,有layout的2个函数也屏蔽了放大过程中控件的重画问题了。
我认为成熟产品的解决方法就是我这种,只是考虑更详细更健壮而已。
我认为我的代码就是控件很多的窗体也没有多少问题。
其实你原来的代码闪烁严重主要还是,窗体改变border和最大化2个过程都要重画窗体,而且最大化,windows缺省有个动画过程,更加增强了你的闪烁感。
我的代码正好这2个问题都解决了。
this.WindowState = FormWindowState.Normal;
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
this.SetVisibleCore(true);