Windows 表单平滑调整大小

Smooth resize for Windows Form

我正在为具有 FixedSingle FormBorderStyle 的表单重新创建调整大小功能,使用以下方法更新 size/position:

private void resizeBottom()
{
    this.SuspendLayout();
    this.SetBoundsCore(this.Location.X, this.Location.Y, this.Width, Cursor.Position.Y - this.Location.Y, BoundsSpecified.Size);
}

…(other resize helpers)...

protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified)
{
    base.SetBoundsCore(x, y, width, height, specified);
    this.ResumeLayout();
}

但是,当我尝试调整 window 的大小时,我得到了很多伪像,特别是如果我以正确的速度调整大小时,看起来 window 被“涂抹”了屏幕。我试过启用双缓冲,但似乎没有什么不同。

我做了一些测试,得到的结果与使用以下代码以常规方式(通过单击并拖动 window 边缘)调整表单大小时发生的结果非常相似:

private void resizeBottom()
{
    SetBounds(Location.X, Location.Y, Width, Cursor.Position.Y + 30 - Location.Y);
}

protected override void SetBoundsCore(int x, int y, int width, int height,
                                      BoundsSpecified specified)
{
    base.SetBoundsCore(x, y, width, height, specified);
    Update();
}

Update() 使表格立即重绘。

注意:我用的是Cursor.Position.Y + 30,否则只能把表格变小,因为鼠标往下移就离开了window