C# WinForms AnimateWindow issue
I am using the following code to animate a window. Let me explain the visual structure of my program a little bit. I have one FlowLayoutPanel located on top of my Form1 and also many GroupBox objects located on top the FlowLayoutPanel. And finally I have one Button and an invisible RichTextBox object located on top of the GroupBox.
ex: Form1->FlowLayoutPanel->GroupBox->Button and RichTextBox(invisible)
What I'm trying to achieve is, when I click the Button object, I want my RichTextBox to slide downwards. I tried it by creating one button and a RichTextBox on top of my main Form and it worked perfectly fin开发者_运维技巧e. However, when I try the same thing using the GroupBox controls at runtime, my Animate function throws an unknown exception.
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);
}
I also noticed that when I call the Animate function using RichTextBox's parent e.g Effects.Animate(textBox.parent, Effects.Effect.Slide, 150, 90);
the animation works without any problem. I don't know why it works with the parent and not the actual object. e.g Effects.Animate(textBox, Effects.Effect.Slide, 150, 90);
I tested your code and it works even on the textboxes (worked on richtextbox also but it turned black and only areas where i types returned to their original colour).
Make sure the control you want to run this code on must be hidden before the effect function is called. For example, I called Effects.Animate(textBox1, Effects.Effect.Center, 1000, 120); and the textBox1.Visible was set to false in the designer.
Vijay
精彩评论