【问题标题】:How to change form with animation?如何用动画改变形式?
【发布时间】:2011-11-10 12:34:41
【问题描述】:

如何在win应用程序中用动画(如淡入淡出,Cutl Up,...)改变表格?

我有两个表格Form1,Form2。 我在 form1 中,上面有一个按钮,我想在单击按钮时更改表单。

//button click
Form2 f2=new Form2();
this.hide();
f2.show();

【问题讨论】:

  • 现在你的例外是什么?它工作不正常吗?

标签: c# winforms animation


【解决方案1】:

您可以使用AnimateWindow(),定义一个新的表单类并覆盖OnLoad(),以显示具有您想要的效果的表单。

// Console Application
// Load System.Windows.Forms reference.

[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern bool AnimateWindow(IntPtr hwnd, int dwTime, int dwFlags);

class SlidingForm: System.Windows.Forms.Form 
{
    const int AW_ACTIVATE = 0X20000;
    const int AW_SLIDE = 0X40000;
    const int AW_HOR_POSITIVE = 0X1;

    protected override void OnLoad(System.EventArgs e)
    {
        AnimateWindow(this.Handle, 1000, AW_ACTIVATE | AW_SLIDE | AW_HOR_POSITIVE);
        base.OnLoad(e);
    }
}

void Main()
{
    var frm = new SlidingForm().ShowDialog;
}

根据要求隐藏 PictureBox 控件的示例代码:

private void button1_Click(object sender, EventArgs e)
{
   const int AW_HIDE = 0x00010000;
   const int AW_SLIDE = 0x00040000;
   const int AW_HOR_NEGATIVE = 0x00000002;
   const int AW_HOR_POSITIVE = 0x00000001;
   // Toggle show/hide
   AnimateWindow(pictureBox1.Handle, 1000, (pictureBox1.Visible) ? (AW_HIDE | AW_HOR_NEGATIVE) : (AW_HOR_POSITIVE) | AW_SLIDE);
   pictureBox1.Visible ^= true;
}

【讨论】:

  • 谢谢。是否可以将它用于图片框之类的控件?
  • 是的,通常可以将PictureBox 之类的控件句柄传递给AnimateWindow 函数。例如:AnimateWindow(PictureBox1.Handle, 1000, AW_VER_NEGATIVE | AW_SLIDE | AW_HIDE); 将隐藏PictureBox 控件并带有垂直滑动效果。
  • 我使用此代码但不起作用:AnimateWindow(pictureBox1.Handle, 5000, AW_ACTIVATE | AW_BLEND | AW_HOR_POSITIVE);
  • AnimateWindow 返回 bool,而不是 int。用它来抛出一个 Win32Exception ,这样 OP 就知道他用错了。无法激活子窗口。
  • @HansPassant 而BOOL 类型映射到 int,这里使用 int 有什么缺点吗?
【解决方案2】:

没有深入了解代码本身的细节,我发现这个问题处理了一个类似的主题:Simple animation in WinForms

这个问题将我引向了一个名为 dot-net-transitions 的库,我认为它完全符合您的要求。

【讨论】:

  • 例如,我使用此代码作为背景颜色,但没有任何反应:Transition.run(this, "BackColor", Color.Red, new TransitionType_Linear(1000));
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-21
  • 2017-07-15
  • 2012-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多