.NET AnimateWindow

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

当我调用AnimateWindow时,Label控件中的文本只在动画效果结束后出现在Form上。 我希望它与表格的其余部分逐渐淡入。 我怀疑它与Form更新其子控件时有关。 我认为在AnimateWindow调用之后,Form不会告诉其子级更新。 我在设置标签的文本字段后尝试粘贴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上隐藏。 然后我将吐司的大小设置回原来的大小。 最后,我在吐司上打电话给AnimateWindow。 是的,它有效,但它是一个黑客…任何更好的想法?

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

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

尝试在整个应用程序中禁用兼容的文本呈现,或者禁止标签强制它使用GDI。

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