const int MAX_STEP = 5;
while (Width < desiredWidth)
{
int step = Math.Min(MAX_STEP, desiredWidth - Width);
SetBounds(Left - step, Top, Width + step, Height);
Refresh();
Application.DoEvents();
Thread.Sleep(4);
}
在这里,我做了几件事,首先是定义最大步长并设置步长,以便您获得所需的确切宽度。与过度回答相比,这里的最大变化是 SetBounds 它将自动设置所有属性,我的示例程序中没有其他任何事情发生,所以我看到右侧的 gitter 回来并在我设置宽度时形成左边(以前在测试解决方案时)。您可能在其他一些过程中遇到了这个问题,因此您需要引发事件 Application.DoEvents(),就像@banana 提到的那样,您可以执行 Refresh(),这应该会被自动调用,但再次调用它应该不会有什么坏处. 另外,我在desiredWidth 中正确拼写了Width,这让我发疯了!“this.Property”或“this.Method”是“this”的冗余限定符
**注意,查看 ClientSize.Width 因为它通常是人们想要的,您可能还想查看 SetDesktopBounds