C#WinForms AnimateWindow问题

时间:2011-07-12 18:53:03

标签: c# winforms winapi

我使用以下代码为窗口设置动画。 让我稍微解释一下我程序的视觉结构。我有一个位于Form1顶部的FlowLayoutPanel以及位于FlowLayoutPanel顶部的许多GroupBox对象。最后,我有一个Button和一个位于GroupBox顶部的不可见的RichTextBox对象。

例如: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);

1 个答案:

答案 0 :(得分:2)

我测试了你的代码,它甚至可以在文本框上工作(也可以在richtextbox上工作,但它变成黑色,只有i类型返回原始颜色的区域)。

在调用效果函数之前,确保必须隐藏要运行此代码的控件。例如,我调用了Effects.Animate(textBox1,Effects.Effect.Center,1000,120);并且textBox1.Visible在设计器中设置为false。

维杰

相关问题