3

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 fine. 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);

akk kur
  • 361
  • 2
  • 5
  • 14
  • Attribution is required at the StackExchange sites. – Hans Passant Jul 12 '11 at 18:59
  • @HansPassant *"Attribution is required at the StackExchange sites."*? Adding attributes? `DllImport`? It sounds like you're trying to say, "StackExchange sites require giving credit where it is due.", but "Attribution is required at the StackExchange sites" doesn't say that. Perhaps it's a mis-translation? – Ian Boyd Dec 12 '11 at 15:33
  • 1
    This is code previously posted by @HansPassant. See http://stackoverflow.com/a/6103677/98422 I think this is what his comment was alluding to. – Gary McGill Nov 13 '12 at 17:35

1 Answers1

2

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

Vijay Gill
  • 1,508
  • 1
  • 14
  • 16