Advertisement
Guest User

Alert Form

a guest
Jul 10th, 2013
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.24 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Drawing.Drawing2D;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Windows.Forms;
  10. using System.Runtime.InteropServices;
  11. using BF2Statistics.Properties;
  12.  
  13. namespace BF2Statistics
  14. {
  15.     public enum AlertType
  16.     {
  17.         Info, Success, Warning
  18.     }
  19.  
  20.     public enum AlertPopupStyle
  21.     {
  22.         BottomToTop,
  23.         RightToLeft,
  24.         FadeIn
  25.     }
  26.  
  27.     public enum AlertCloseStyle
  28.     {
  29.         TopToBottom,
  30.         LeftToRight,
  31.         FadeOut
  32.     }
  33.  
  34.     public partial class AlertForm : Form
  35.     {
  36.         /// <summary>
  37.         /// The total hieght of all the active alerts, with padding,
  38.         /// for stacking purposes, to prevent overlapping
  39.         /// </summary>
  40.         protected static int TotalHieght = 0;
  41.  
  42.         /// <summary>
  43.         /// Form Position X
  44.         /// </summary>
  45.         protected int PosLeft = 0;
  46.  
  47.         /// <summary>
  48.         /// Form Position Y
  49.         /// </summary>
  50.         protected int PosTop = 0;
  51.  
  52.         /// <summary>
  53.         /// Target hieght of the alert
  54.         /// </summary>
  55.         protected int TargetHeight = 0;
  56.  
  57.         /// <summary>
  58.         /// Gets or sets the alert type icon
  59.         /// </summary>
  60.         public AlertType AlertType = AlertType.Info;
  61.  
  62.         /// <summary>
  63.         /// Gets or sets the Alert Popup Direction
  64.         /// </summary>
  65.         public AlertPopupStyle PopupStyle = AlertPopupStyle.BottomToTop;
  66.  
  67.         /// <summary>
  68.         /// Gets or sets the Alert Close Direction
  69.         /// </summary>
  70.         public AlertCloseStyle CloseStyle = AlertCloseStyle.TopToBottom;
  71.  
  72.         /// <summary>
  73.         /// Gets or sets the Alert Message
  74.         /// </summary>
  75.         public string AlertMessage
  76.         {
  77.             get { return labelHeader.Text; }
  78.             set { labelHeader.Text = value; }
  79.         }
  80.  
  81.         /// <summary>
  82.         /// Gets or sets the Alert Contents
  83.         /// </summary>
  84.         public string AlertSubText
  85.         {
  86.             get { return labelDetails.Text; }
  87.             set { labelDetails.Text = value; }
  88.         }
  89.  
  90.         /// <summary>
  91.         /// Gets or sets the Dock time for the alert (How long it stays open) in milliseconds
  92.         /// </summary>
  93.         public int DockTime
  94.         {
  95.             get { return CloseTimer.Interval; }
  96.             set { CloseTimer.Interval = value; }
  97.         }
  98.  
  99.         /// <summary>
  100.         /// Indicates whether the Alert is closing
  101.         /// </summary>
  102.         public bool IsClosing { get; protected set; }
  103.  
  104.         /// <summary>
  105.         /// Prevents window from stealing focus
  106.         /// </summary>
  107.         protected override bool ShowWithoutActivation
  108.         {
  109.             get { return true; }
  110.         }
  111.  
  112.         public AlertForm()
  113.         {
  114.             InitializeComponent();
  115.  
  116.             // Rounded form edges
  117.             this.Region = Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 1, 1));
  118.  
  119.             // Variable setup
  120.             IsClosing = false;
  121.         }
  122.  
  123.         #region Show Methods
  124.  
  125.         new public void Show()
  126.         {
  127.             BuildForm();
  128.             base.Show();
  129.         }
  130.  
  131.         new public void Show(IWin32Window Owner)
  132.         {
  133.             BuildForm();
  134.             base.Show(Owner);
  135.         }
  136.  
  137.         new public DialogResult ShowDialog()
  138.         {
  139.             throw new Exception("Alerts cannot use the ShowDialog method to display");
  140.         }
  141.  
  142.         new public DialogResult ShowDialog(IWin32Window Owner)
  143.         {
  144.             throw new Exception("Alerts cannot use the ShowDialog method to display");
  145.         }
  146.  
  147.         #endregion
  148.  
  149.         protected void BuildForm()
  150.         {
  151.             // Set heights so multiple alerts can show
  152.             TargetHeight = TotalHieght + this.Height;
  153.             TotalHieght += this.Height;
  154.  
  155.             // Get form start position
  156.             if (PopupStyle == AlertPopupStyle.FadeIn)
  157.             {
  158.                 this.Opacity = 0;
  159.                 PosLeft = Screen.GetWorkingArea(this).Width - this.Width - 5;
  160.                 PosTop = Screen.GetWorkingArea(this).Height - TargetHeight;
  161.             }
  162.             else if (PopupStyle == AlertPopupStyle.RightToLeft)
  163.             {
  164.                 PosLeft = Screen.GetWorkingArea(this).Width - 5;
  165.                 PosTop = Screen.GetWorkingArea(this).Height - TargetHeight;
  166.             }
  167.             else
  168.             {
  169.                 PosLeft = Screen.GetWorkingArea(this).Width;
  170.                 PosTop = Screen.GetWorkingArea(this).Height;
  171.             }
  172.  
  173.             // Set initial position
  174.             this.Location = new Point(PosLeft, PosTop);
  175.  
  176.             // Load icon image
  177.             switch (this.AlertType)
  178.             {
  179.                 default:
  180.                     AlertIconBox.Image = Resources.InfoAlert;
  181.                     break;
  182.                 case AlertType.Success:
  183.                     AlertIconBox.Image = Resources.AlertSuccess;
  184.                     break;
  185.                 case AlertType.Warning:
  186.                     AlertIconBox.Image = Resources.AlertWarning;
  187.                     break;
  188.             }
  189.         }
  190.  
  191.  
  192.         /// <summary>
  193.         /// Creates the rounded corner for the control
  194.         /// </summary>
  195.         /// <param name="nLeftRect">X coordinate of upper-left corner</param>
  196.         /// <param name="nTopRect">Y coordinate of upper-left corner</param>
  197.         /// <param name="nRightRect">X coordinate of lower-right corner</param>
  198.         /// <param name="nBottomRect">Y coordinate of lower-right corner</param>
  199.         /// <param name="nWidthEllipse">width of ellipse</param>
  200.         /// <param name="nHeightEllipse">height of ellipse</param>
  201.         /// <returns></returns>
  202.         [DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
  203.         private static extern IntPtr CreateRoundRectRgn
  204.         (
  205.             int nLeftRect,
  206.             int nTopRect,
  207.             int nRightRect,
  208.             int nBottomRect,
  209.             int nWidthEllipse,
  210.             int nHeightEllipse
  211.          );
  212.  
  213.         /// <summary>
  214.         /// Draws the animate when closing
  215.         /// </summary>
  216.         /// <param name="sender"></param>
  217.         /// <param name="e"></param>
  218.         private void CloseTimer_Tick(object sender, EventArgs e)
  219.         {
  220.             // For future Queue messages, Reduce the stack hieght
  221.             if (!IsClosing)
  222.             {
  223.                 TotalHieght -= this.Height;
  224.                 IsClosing = true;
  225.             }
  226.  
  227.             switch (CloseStyle)
  228.             {
  229.                 case AlertCloseStyle.TopToBottom:
  230.                     if (this.Location.Y < PosTop)
  231.                     {
  232.                         CloseTimer.Interval = 20;
  233.                         this.Location = new Point(this.Location.X, this.Location.Y + 6);
  234.                         this.Opacity -= 0.050;
  235.                     }
  236.                     else
  237.                         this.Close();
  238.                     break;
  239.                 case AlertCloseStyle.LeftToRight:
  240.                     if (this.Location.X < PosLeft)
  241.                     {
  242.                         CloseTimer.Interval = 10;
  243.                         this.Location = new Point(this.Location.X + 15, this.Location.Y);
  244.                         this.Opacity -= 0.050;
  245.                     }
  246.                     else
  247.                         this.Close();
  248.                     break;
  249.                 case AlertCloseStyle.FadeOut:
  250.                     if (this.Opacity > 0)
  251.                     {
  252.                         CloseTimer.Interval = 10;
  253.                         this.Opacity -= 0.050;
  254.                     }
  255.                     else
  256.                     {
  257.                         CloseTimer.Enabled = true;
  258.                         OpenTimer.Enabled = false;
  259.                     }
  260.                     break;
  261.             }
  262.         }
  263.  
  264.         /// <summary>
  265.         /// Draws the animate when opening
  266.         /// </summary>
  267.         /// <param name="sender"></param>
  268.         /// <param name="e"></param>
  269.         private void OpenTimer_Tick(object sender, EventArgs e)
  270.         {
  271.             switch (PopupStyle)
  272.             {
  273.                 case AlertPopupStyle.BottomToTop:
  274.                     if (this.Location.Y > PosTop - TargetHeight)
  275.                     {
  276.                         double D = TargetHeight / 18;
  277.                         int Increment = (int)Math.Round(D, 0); // Base increment
  278.                         this.Location = new Point(PosLeft - this.Width - 5, this.Location.Y - Increment);
  279.                     }
  280.                     else
  281.                     {
  282.                         CloseTimer.Enabled = true;
  283.                         OpenTimer.Enabled = false;
  284.                     }
  285.                     break;
  286.                 case AlertPopupStyle.RightToLeft:
  287.                     if (this.Location.X > PosLeft - this.Width)
  288.                     {
  289.                         this.Location = new Point(this.Location.X - 10, this.Location.Y);
  290.                     }
  291.                     else
  292.                     {
  293.                         CloseTimer.Enabled = true;
  294.                         OpenTimer.Enabled = false;
  295.                     }
  296.                     break;
  297.                 case AlertPopupStyle.FadeIn:
  298.                     if (this.Opacity < 0.90)
  299.                     {
  300.                         this.Opacity += 0.050;
  301.                     }
  302.                     else
  303.                     {
  304.                         CloseTimer.Enabled = true;
  305.                         OpenTimer.Enabled = false;
  306.                         this.Refresh();
  307.                     }
  308.                     break;
  309.             }
  310.         }
  311.  
  312.         /// <summary>
  313.         /// Creates the background gradient
  314.         /// </summary>
  315.         /// <param name="sender"></param>
  316.         /// <param name="e"></param>
  317.         private void AlertForm_Paint(object sender, PaintEventArgs e)
  318.         {
  319.             using (LinearGradientBrush brush = new LinearGradientBrush(
  320.                 this.ClientRectangle,
  321.                 Color.Black,
  322.                 Color.DimGray,
  323.                 120F))
  324.             {
  325.                 e.Graphics.FillRectangle(brush, this.ClientRectangle);
  326.             }
  327.         }
  328.     }
  329. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement