.NET AnimateWindow

时间:2009-09-29 15:25:22

标签: c# .net winforms animation

我正在尝试制作Outlook收到新电子邮件时使用的通知窗口。我有一个通知窗口的无模式窗体,上面有两个Label控件来显示通知信息。为了获得淡入效果,我正在调用Win32 AnimateWindow函数。除了一件事,一切似乎都正常。

当我调用AnimateWindow时,Label控件中的文本在动画效果结束后才会出现在Form上。我希望它与表格的其余部分逐渐淡入。我怀疑它与Form更新其子控件时有关。我认为Form直到AnimateWindow调用之后才会告诉其子级更新。我在设置标签的文本字段后尝试粘贴Form.Update()调用,但这没有帮助。

我现在正在采取的基本步骤:

// Main form of the application.
public class MainForm : Form
{
   // The notification toast.
   protected ToastForm toast;

   // Public method called to show and update the toast.
   public void UpdateToast( string text1, string text2 )
   {
      // Create a new toast form if one does not exist yet.
      if ( this.toast == null )
      {
         this.toast = new ToastForm();
      }

      // Update the toast form's Label controls.
      // Note that this isn't exactly how it's done in my app.  There are fields safely
      // wrapping the Label controls.  I just did it this way here to be concise.
      this.toast.firstLabel.Text = text1;
      this.toast.secondLabel.Text = text2;

      // This doesn't help.
      this.toast.Update();

      // P/invoke the Win32 AnimateWindow function on the toast form.
      User32.AnimateWindow( this.toast.Handle, 750, AnimateWindowFlags.AW_ACTIVATE | AnimateWindowFlags.AW_BLEND );

      // Call Show method on the toast form.  This is needed to get the controls to
      // update at all.
      this.toast.Show( this );
   }
}

有没有人有任何建议可以让它发挥作用?

编辑:
我找到了一个黑客。分配标签文本后,我将toast的大小设置为0宽度和0高度。接下来我打电话给第一个Show然后在toast上隐藏。然后我将吐司的大小设置回原来的大小。最后,我在Toast上打电话给AnimateWindow。是的,它有效,但它是一个黑客...任何更好的想法?

3 个答案:

答案 0 :(得分:2)

codeproject网站上有一个很好的例子。

答案 1 :(得分:0)

默认情况下,WinForms控件使用GDI +来呈现文本。我怀疑这就是这种行为的原因。

尝试禁用兼容的文字呈现in the entire applicationfor the labels以强制它使用GDI。

答案 2 :(得分:0)

只需使用计时器并通过其Opacity属性淡化表单。应该对你有用。