我正在使用以下代码来动画化一个窗口。
让我解释一下我的程序的视觉结构。我在Form1顶部有一个FlowLayoutPanel,以及位于FlowLayoutPanel顶部的许多GroupBox对象。最后,我在GroupBox顶部放置了一个Button和一个不可见的RichTextBox对象。
例如:Form1->FlowLayoutPanel->GroupBox->Button和RichTextBox(不可见)
我想要实现的是,当我点击Button对象时,我希望我的RichTextBox向下滑动。我尝试创建一个按钮和一个RichTextBox在我的主窗体上,并且它完美地工作。然而,当我尝试在运行时使用GroupBox控件做同样的事情时,我的Animate函数会抛出未知异常。
例如:Form1->FlowLayoutPanel->GroupBox->Button和RichTextBox(不可见)
我想要实现的是,当我点击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);
DllImport
?听起来你是想说,“StackExchange网站要求在应该给予信用的地方进行归属”,但是“在StackExchange网站上需要归属”并没有表达这个意思。也许这是一个误译? - Ian Boyd