【发布时间】:2011-07-12 18:53:03
【问题描述】:
我正在使用以下代码为窗口设置动画。 让我稍微解释一下我的程序的视觉结构。我有一个 FlowLayoutPanel 位于 Form1 的顶部,还有许多 GroupBox 对象位于 FlowLayoutPanel 的顶部。最后,我在 GroupBox 顶部有一个 Button 和一个不可见的 RichTextBox 对象。
ex: Form1->FlowLayoutPanel->GroupBox->Button and RichTextBox(invisible)
我想要实现的是,当我单击 Button 对象时,我希望我的 RichTextBox 向下滑动。我通过在我的主窗体上创建一个按钮和一个 RichTextBox 来尝试它,它工作得非常好。但是,当我在运行时使用 GroupBox 控件尝试相同的操作时,我的 Animate 函数会引发未知异常。
class Effects
{
public enum Effect { Roll, Slide, Center, Blend }
public static void Animate(Control ctl, Effect effect, int msec, int angle)
{
int flags = effmap[(int)effect];
if (ctl.Visible) { flags |= 0x10000; angle += 180; }
else
{
if (ctl.TopLevelControl == ctl) flags |= 0x20000;
else if (effect == Effect.Blend) throw new ArgumentException();
}
flags |= dirmap[(angle % 360) / 45];
bool ok = AnimateWindow(ctl.Handle, msec, flags);
if (!ok) throw new Exception("Animation failed");
ctl.Visible = !ctl.Visible;
}
private static int[] dirmap = { 1, 5, 4, 6, 2, 10, 8, 9 };
private static int[] effmap = { 0, 0x40000, 0x10, 0x80000 };
[DllImport("user32.dll")]
public static extern bool AnimateWindow(IntPtr handle, int msec, int flags);
}
我还注意到,当我使用 RichTextBox 的父级调用 Animate 函数时 例如 Effects.Animate(textBox.parent, Effects.Effect.Slide, 150, 90);
动画没有任何问题。 我不知道为什么它与父对象而不是实际对象一起使用。 例如 Effects.Animate(textBox, Effects.Effect.Slide, 150, 90);
【问题讨论】:
-
StackExchange 站点需要提供署名。
-
@HansPassant “StackExchange 站点需要署名。”?添加属性?
DllImport?听起来您想说“StackExchange 网站需要在应得的地方给予信用。”,但“StackExchange 网站需要署名”并没有这么说。也许是翻译错误? -
这是@HansPassant 之前发布的代码。见stackoverflow.com/a/6103677/98422我认为这就是他的评论所暗示的。