如何在 WinForms 中平滑过渡面板? [英] How to smoothly transition a panel in WinForms?
问题描述
我正在尝试从表单的左边缘调出菜单.当我这样做时,我希望表单顶部的面板缩小.面板上有按钮,锚点设置为右".
I am trying to bring out a menu from the left edge of a form. As I do it I want a panel at the top of the form to shrink. The panel has buttons on it with anchors set to 'Right'.
面板通过将其向左移动并将宽度减小相同的量来调整大小 - 因此按钮应保持在屏幕上的相同位置.按钮确实留在正确的位置,但看起来很糟糕.即使我使用 .Bounds = new Rectangle(...) 设置面板的新尺寸,它看起来好像我设置了 Left 属性,然后设置了 Width 有时间延迟!我搞砸了 SuspendLayout 没有任何影响.我觉得有一个简单的方法可以解决这个问题.我创建的一个简单示例:
The panel resizes by moving it to the left and reducing the width by the same amount - so the buttons should stay in the same place on screen. The buttons do stay in the correct place but look awful doing it. Even though I set the panel's new dimensions with .Bounds = new Rectangle(...) it looks as if I set the Left property and then the Width with a time delay! I have messed with SuspendLayout with no impact. I feel sure there is a simple way around this. A trivial example I created:
private void StartTest_Click( object sender, EventArgs e )
{
_AmountLeft = PanelToResize.Width / 2; // half of it
_Step = _AmountLeft / 10;
Ticker.Enabled = true;
}
private void Ticker_Tick( object sender, EventArgs e )
{
int this_bit = Math.Min( _AmountLeft, _Step );
// reduce
PanelToResize.Bounds = new Rectangle( PanelToResize.Left + this_bit, PanelToResize.Top, PanelToResize.Width - this_bit, PanelToResize.Height );
if (this_bit == _AmountLeft)
Ticker.Enabled = false;
_AmountLeft -= this_bit;
}
我已将此代码和设计器代码放在此处:https://gist.github.com/anonymous/acf0ec4d5c81dbc5a670
I have placed this code and the designer code here: https://gist.github.com/anonymous/acf0ec4d5c81dbc5a670
推荐答案
在您的表单中尝试以下代码以减少闪烁.
Try the below code in your form to reduce flickering.
protected override CreateParams CreateParams
{
get
{
const int WS_EX_COMPOSITED = 0x02000000;
var cp = base.CreateParams;
cp.ExStyle |= WS_EX_COMPOSITED;
return cp;
}
}
它对整个窗口表面进行双缓冲,包括子控件
Its double-buffer the entire window surface, including the child controls
请参阅此处了解值,包括 WS_EX_COMPOSITED
See here for the values, including WS_EX_COMPOSITED
这篇关于如何在 WinForms 中平滑过渡面板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!