views:

298

answers:

3

I am trying to make a notification window like Outlook uses when you receive a new email. I have a modeless form for the notification window with two Label controls on it to display the notification info. To get the fade-in effect, I am p/invoking the Win32 AnimateWindow function. Everything seems to be working alright except for one thing.

When I p/invoke AnimateWindow, the text in the Label controls just appears on the Form after the animation effect is over. I would like it to gradually fade in with the rest of the Form. I suspect that it has something to do with when the Form updates its child controls. I'm thinking that the Form doesn't tell its children to update until after the AnimateWindow call. I have tried sticking Form.Update() calls after setting the text fields for the Labels, but that doesn't help.

Basic steps I'm taking now:

// 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 );
   }
}

Does anyone have any suggestions to get this to work?

EDIT:
I worked out a hack. After the Label text is assigned, I set the toast's size to 0 width and 0 height. Next I call first Show and then Hide on the toast. Then I set the toast's size back to what it was originally. Finally, I call AnimateWindow on the toast. Yeah, it works, but it is a hack... Any better ideas?

+1  A: 

There is a great example of this on the codeproject site.

Darrin Lynn
That's a nice example, but I'm looking for something in .NET.
madhatter160
Sorry this is the example I should have included, it's much better.http://www.codeproject.com/KB/miscctrl/taskbarnotifier.aspx
Darrin Lynn
Great example. Though, it uses custom animation code rather than AnimateWindow and doesn't use Label controls. From what I've read since posting my question it appears that AnimateWindow and WinForms don't play well together. This custom solution fills my needs nicely.
madhatter160
A: 

By default, WinForms controls use GDI+ to render text. I suspect that this is the reason for this behavior.

Try disabling compatible text rendering in the entire application or for the labels to force it to use GDI instead.

SLaks
No luck with either method.
madhatter160
A: 

Read this blog post.

SLaks