C# WinForms AnimateWindow 问题

标签 c# winforms winapi

我正在使用以下代码为窗口设置动画。 让我稍微解释一下我的程序的视觉结构。我有一个 FlowLayoutPanel 位于我的 Form1 顶部,还有许多 GroupBox 对象位于 FlowLayoutPanel 顶部。最后,我在 GroupBox 的顶部有一个 Button 和一个不可见的 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);

最佳答案

我测试了你的代码,它甚至可以在文本框上工作(也可以在 richtextbox 上工作,但它变成黑色,只有我输入的区域恢复到原来的颜色)。

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

维杰

关于C# WinForms AnimateWindow 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6669496/

相关文章:

c# - 将字符转换为虚拟键码

c# - 反序列化具有未知元素顺序的 XML

c# - 比较列表以检索不匹配的对象属性

c++ - 用 C/C++ 中的值填充 DataGridView ComboBox

.net - 在 VS 2008 中调试设计器处理

SQL 查询在 MS-Access 中工作,但无法以 vb.net 形式实现

c++ - 资源图像不工作 C++

c# - gmail联系人的邮政地址解析

C#图形闪烁

C++ Winapi - MPEG 电影作为动画背景