msn淡入淡出窗口

来源:互联网 发布:大数据的重要性 编辑:程序博客网 时间:2024/04/30 08:06

ToastHelpers.cs

 

 

using System;

namespace WeatherAlert
{
    
/// <summary>
    
/// This class describes the arguments used for 
    
/// the event arguments when the user opens the window
    
/// via the Toast Window.
    
/// </summary>

    public class ToastSelectEventArgs : System.EventArgs
    
{
        
/// <summary>
        
/// The object passed back when the user opens his toast.
        
/// </summary>

        private readonly object _toastArgs;

        
/// <summary>
        
/// Retrieve the toast open arguments.
        
/// </summary>

        public object ToastArgs
        
{
            
get
            
{
                
return _toastArgs;
            }

        }


        
/// <summary>
        
/// Single constructor.
        
/// </summary>
        
/// <param name="ToastArgs">The toast arguments.</param>

        public ToastSelectEventArgs( object ToastArgs )
        
{
            _toastArgs 
= ToastArgs;
        }

    }


    
/// <summary>
    
/// Toast open delegate description.
    
/// </summary>

    public delegate void ToastTargetClickHandler( object sender, ToastSelectEventArgs args );
}

Utils.cs

 

using System;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace WeatherAlert
{
    
/// <summary>
    
/// Utility methods of general use.
    
/// </summary>

    public sealed class Utils
    
{
        
/// <summary>
        
/// Hidden constructor.
        
/// </summary>

        private Utils()
        
{
        }


        
/// <summary>
        
/// For some reason, GDI+ dropped support for the 
        
/// rounded rectangle.  Add it back in.
        
/// </summary>
        
/// <param name="rect">The rectangle</param>
        
/// <param name="radius">The radius of the circle that
        
/// inscribes the rounded rect corners.</param>
        
/// <returns>A graphics path surrounding the rounded rect.
        
/// </returns>
        
/// <remarks>
        
/// This method creates a graphics path that can be used to
        
/// draw a rounded rectangle. It is a great way to smooth
        
/// out the rough edges on your windows.
        
/// </remarks>

        public static GraphicsPath CreateRoundedRectPath( Rectangle rect, int radius )
        
{
            GraphicsPath rectPath 
= new GraphicsPath();

            
// Add the line on the top:
            rectPath.AddLine( rect.Left + radius, rect.Top, 
                rect.Right 
- radius, rect.Top );
            
// Add the arc at the top right corner:
            rectPath.AddArc( rect.Right - 2 * radius, rect.Top, 
                radius 
* 2, radius * 2 , 
                
27090 );
            
// Line down the right:
            rectPath.AddLine( rect.Right, rect.Top + radius, 
                rect.Right, rect.Bottom 
- 10 );
            
// Bottom Right quarter circle:
            rectPath.AddArc( rect.Right - radius * 2, rect.Bottom - radius * 2
                radius 
* 2, radius * 2
                
090 );
            
// bottom line:
            rectPath.AddLine( rect.Right - 2 * radius, rect.Bottom , 
                rect.Left 
+ radius, rect.Bottom );
            
// Bottom left quarter circle:
            rectPath.AddArc( rect.Left, rect.Bottom - 2 * radius, 
                
2 * radius, 2 * radius, 9090 );
            
// Up the left side:
            rectPath.AddLine( rect.Left, rect.Bottom - radius, 
                rect.Left, rect.Top 
+ radius );
            
// Upper left arc:
            rectPath.AddArc( rect.Left, rect.Top, 
                
2 * radius, 2 * radius, 18090 );

            
return rectPath;
        }

    }

}

 ToastWindow.cs    form

 

 

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace WeatherAlert
{
    
/// <summary>
    
/// This is a resuable Toast notification window.
    
/// </summary>
    
/// <remarks>
    
/// The behavior is to fade in a toast window containing the 
    
/// message you entered. If the user clicks on the link, an event
    
/// is raised. This is typically used to show the main window,
    
/// or some other more prominent notification. If the user ignores the 
    
/// Toast Window, it fades down. 
    
/// If a new toast window should be displayed while an existing toast 
    
/// is up, the second request is ignored. 
    
/// </remarks>

    public class ToastWindow : System.Windows.Forms.Form
    
{
        
/// <summary>
        
/// The event generated when the user clicks his toast.
        
/// </summary>

        public event ToastTargetClickHandler ToastClick;

        
/// <summary>
        
/// This enumeration keeps track of what the toast window
        
/// is doing.
        
/// </summary>

        private enum ToastState 
        
{
            Nothing,
            FadeUp,
            Visible,
            FadeDown
        }


        
/// <summary>
        
/// Is there a toast window up and running?
        
/// </summary>

        private static bool _toastUp = false;

        
/// <summary>
        
/// Timer to manage the fade in / fade out.
        
/// </summary>

        private System.Windows.Forms.Timer timerFadeIn;

        
/// <summary>
        
/// What is the current Toast Window state?
        
/// </summary>

        private ToastState toastWindowState;

        
/// <summary>
        
/// The parameter to send along when the user clicks his toast.
        
/// </summary>

        private object _toastTargetArgs;

        
/// <summary>
        
/// The message for the toast window.
        
/// </summary>

        private string _message;

        
/// <summary>
        
/// Close button
        
/// </summary>

        private System.Windows.Forms.Button buttonClose;

        
/// <summary>
        
/// Standard included component.
        
/// </summary>

        private System.ComponentModel.IContainer components;

        
/// <summary>
        
/// Default constructor
        
/// </summary>

        public ToastWindow()
        
{
            
//
            
// Required for Windows Form Designer support
            
//
            InitializeComponent();
        }


        
/// <summary>
        
/// Clean up any resources being used.
        
/// </summary>

        protected override void Dispose( bool disposing )
        
{
            
if( disposing )
            
{
                
if(components != null)
                
{
                    components.Dispose();
                }

            }

            
base.Dispose( disposing );
        }


        
Windows Form Designer generated code

        
/// <summary>
        
/// Paint the window.
        
/// </summary>
        
/// <param name="e">unused</param>
        
/// <remarks>
        
/// Draw the text.
        
/// </remarks>

        protected override void OnPaint(PaintEventArgs e)
        
{
            RectangleF boundingRect 
= new RectangleF( ( float ) ClientRectangle.Left,
                ( 
float ) ClientRectangle.Top, 
                ( 
float ) ClientRectangle.Width, ( float ) ClientRectangle.Height );
            StringFormat f 
= new StringFormat();
            f.Alignment 
= StringAlignment.Center;
            f.LineAlignment 
= StringAlignment.Center;
      
            e.Graphics.DrawString( 
this._message, this.Font, Brushes.Blue, boundingRect, f );
        }


        
/// <summary>
        
/// Paint the background.
        
/// </summary>
        
/// <param name="pevent">unused</param>
        
/// <remarks>
        
/// Draw the rounded rect using the gradient brushes.
        
/// </remarks>

        protected override void OnPaintBackground(PaintEventArgs pevent)
        
{
            Graphics g 
= pevent.Graphics;
            g.FillRectangle( Brushes.Lime, 
0,0,this.ClientSize.Width, this.ClientSize.Height );

            System.Drawing.Drawing2D.GraphicsPath path 
= Utils.CreateRoundedRectPath(this.ClientRectangle, 15 );

            
using ( System.Drawing.Drawing2D.LinearGradientBrush b = 
                        
new System.Drawing.Drawing2D.LinearGradientBrush( this.ClientRectangle,
                        Color.SteelBlue, Color.LightBlue, System.Drawing.Drawing2D.LinearGradientMode.Vertical ) )
            
{
                g.FillPath( b, path );
            }

        }


        
/// <summary>
        
/// Load handler.
        
/// </summary>
        
/// <param name="sender">unused</param>
        
/// <param name="e">unused</param>
        
/// <remarks>
        
/// Put the toast window in the right spot,
        
/// Start timers.
        
/// </remarks>

        private void OnLoad(object sender, System.EventArgs e)
        
{
            
lock ( typeof ( ToastWindow ) )
            
{
                System.Diagnostics.Debug.Assert( _toastUp 
== false );
                _toastUp 
= true;
            }

            
// Set the position:
            
// Left is the right of the desktop - our width.
            this.Left = SystemInformation.WorkingArea.Size.Width - this.Size.Width;
            
this.Top = SystemInformation.WorkingArea.Size.Height - this.Size.Height;
            
this.timerFadeIn.Enabled = true;
            
this.toastWindowState = ToastState.FadeUp;
        }


        
/// <summary>
        
/// Mouse Enter handler
        
/// </summary>
        
/// <param name="sender">unused</param>
        
/// <param name="e">Unused</param>
        
/// <remarks>
        
/// If the user moves the mouse over the toast window,
        
/// stop the timers.  Change the opacity to full, and 
        
/// let the user view the toast message.
        
/// And, make the font act like a selected link.
        
/// </remarks>

        protected override void OnMouseEnter(System.EventArgs e)
        
{
            StopFade();
        }


        
/// <summary>
        
/// Mouse Leave handler.
        
/// </summary>
        
/// <param name="sender">unused</param>
        
/// <param name="e">unused</param>

        protected override void OnMouseLeave(System.EventArgs e)
        
{
            StartFade();
        }


        
/// <summary>
        
/// Event handler for when the mouse enters the close box
        
/// </summary>
        
/// <param name="sender">unused</param>
        
/// <param name="e">unused</param>

        private void EnterCloseBox(object sender, System.EventArgs e)
        
{
            StopFade();
        }


        
/// <summary>
        
/// Event handler for when the mouse leaves the close box.
        
/// </summary>
        
/// <param name="sender">uused</param>
        
/// <param name="e">unused</param>

        private void LeaveCloseBox(object sender, System.EventArgs e)
        
{
            StartFade();
        }


        
/// <summary>
        
/// Stop the fade.
        
/// </summary>
        
/// <remarks>
        
/// Stop the fade because we've got the mouse in our window.
        
/// </remarks>

        private void StopFade()
        
{
            
this.Font = new System.Drawing.Font("Microsoft Sans Serif"8.25F);
            
this.Invalidate();
            
this.StartFadeOut();
            
this.timerFadeIn.Enabled = true;
        }


        
/// <summary>
        
/// Start the fade.
        
/// </summary>
        
/// <remarks>
        
/// We start the fade because we've got
        
/// the mouse leaving our window.
        
/// </remarks>

        private void StartFade()
        
{
            
this.Font = new System.Drawing.Font("Microsoft Sans Serif"8.25F);
            
this.Invalidate();
            
this.StartFadeOut();
            
this.timerFadeIn.Enabled = true;
        
        }


        
/// <summary>
        
/// Mouse Up handler.
        
/// </summary>
        
/// <param name="sender">unused</param>
        
/// <param name="e">unused</param>
        
/// <remarks>
        
/// Raise the toast click event. The event sink
        
/// will know what to do.
        
/// </remarks>

        protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
        
{
            
if ( ToastClick != null )
                ToastClick( 
thisnew ToastSelectEventArgs( _toastTargetArgs ) );
            
this.Close();
        }


        
/// <summary>
        
/// Tick handler.  Fade in. Fade out.
        
/// </summary>
        
/// <param name="sender">unused</param>
        
/// <param name="e">unused</param>

        private void FadeTick(object sender, System.EventArgs e)
        
{
            
switch ( this.toastWindowState )
            
{
                
case ToastState.FadeUp:
                    DoFadeIn();
                    
break;
                
case ToastState.Visible:
                    StartFadeOut();
                    
break;
                
case ToastState.FadeDown:
                    DoFadeDown();
                    
break;
            }

        }


        
/// <summary>
        
/// Increase the opacity of the toast window. 
        
/// </summary>
        
/// <remarks>
        
/// Once totally faded in, stay visible for 
        
/// 3 full seconds.
        
/// </remarks>

        private void DoFadeIn()
        
{
            
if ( this.Opacity < 0.99 )
            
{
                
this.Opacity += 0.05;
                
this.Update();
            }

            
else // Stop this timer and start a new one:
            {
                
this.toastWindowState = ToastState.Visible;
                
this.timerFadeIn.Interval = 3000;
            }

        }


        
/// <summary>
        
/// Fade out.
        
/// </summary>
        
/// <remarks>
        
/// Lower the opacity until the window is gone.
        
/// Then close.
        
/// </remarks>

        private void DoFadeDown()
        
{
            
if ( this.Opacity > 0.01 )
            
{
                
this.Opacity -= 0.05;
                
this.Update();
            }

            
else
            
{
                
this.Close();
                
lock ( typeof ( ToastWindow ) ) 
                    _toastUp 
= false;
            }

        }


        
/// <summary>
        
/// Transition from up to fade out.
        
/// </summary>

        private void StartFadeOut()
        
{
            
this.timerFadeIn.Interval = 100;
            
this.toastWindowState = ToastState.FadeDown;
        }


        
/// <summary>
        
/// Close handler.
        
/// </summary>
        
/// <param name="sender">unused</param>
        
/// <param name="e">unused</param>

        private void buttonClose_Click(object sender, System.EventArgs e)
        
{
            
this.Close();
            
lock ( typeof ( ToastWindow ) )
                _toastUp 
= false;
        }


        
/// <summary>
        
/// Public entry point.
        
/// </summary>
        
/// <param name="msg">the message.</param>
        
/// <param name="callbackArgs">the arggs to the callback.</param>
        
/// <param name="ToastLauncher">The optional event handler.</param>
        
/// <remarks>
        
/// If there is no toast window up, make the window and show it.
        
/// If a window is already displayed, ignore this message.
        
/// </remarks>

        public static void ShowToast( string msg, object callbackArgs, 
            ToastTargetClickHandler ToastLauncher )
        
{
            
lock ( typeof (ToastWindow ) )
            
{
                
if ( _toastUp == false )
                
{
                    ToastWindow w 
= new ToastWindow();
                    w._message 
= msg;
                    w._toastTargetArgs 
= callbackArgs;
                    
if ( ToastLauncher != null )
                        w.ToastClick 
+= ToastLauncher;
                    w.Show();
                }

            }

        }


    }

}

 

 

主窗口调用

 

 


//按钮中打开窗口
private void button1_Click(object sender, System.EventArgs e)
        
{
            
            
                ToastWindow.ShowToast(
                    
"Hi"nullnew  ToastTargetClickHandler( toastSelect ) );
            
        }


private static void toastSelect(  object sender, ToastEventArgs args )
            
{
                MessageBox.Show( args.ToastArgs.ToString () );
            }