2011-07-12 28 views
3

我正在使用下面的代碼來動畫一個窗口。 讓我來解釋一下我的程序的視覺結構。我有一個FlowLayoutPanel位於我的Form1之上,還有很多位於FlowLayoutPanel頂部的GroupBox對象。最後,我有一個Button和一個位於GroupBox頂部的隱形RichTextBox對象。C#WinForms AnimateWindow問題

例如:Form1-> FlowLayoutPanel-> GroupBox->按鈕和RichTextBox的(無形)

我想要實現的是,當我點擊按鈕的對象,我希望我的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的父 e.g Effects.Animate(textBox.parent,Effects.Effect.Slide,150,90)的動畫功能;

動畫沒有任何問題。 我不知道它爲什麼它與父母,而不是實際的對象。例如Effects.Animate(textBox,Effects.Effect.Slide,150,90);

+0

歸因在StackExchange站點是必需的。 –

+0

@HansPassant *「需要在StackExchange網站上署名。」*?添加屬性? 'DllImport'?這聽起來像是你想說的,「StackExchange網站需要在應有的地方給出信用」,但「StackExchange網站需要的歸因」並不是這樣說的。也許這是一個錯誤的翻譯? –

+1

這是以前由@HansPassant發佈的代碼。見http://stackoverflow.com/a/6103677/98422我認爲這是他的評論所暗指的。 –

回答

2

我測試了你的代碼,它甚至可以在文本框上工作(也適用於richtextbox,但它變成了黑色,並且只有我類型返回到原始顏色的區域)。

確保您要運行此代碼的控件在調用效果函數之前必須隱藏。例如,我稱之爲Effects.Animate(textBox1,Effects.Effect.Center,1000,120);並且在設計器中將textBox1.Visible設置爲false。

Vijay