Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Runtime.InteropServices;
- using BF2Statistics.Properties;
- namespace BF2Statistics
- {
- public enum AlertType
- {
- Info, Success, Warning
- }
- public enum AlertPopupStyle
- {
- BottomToTop,
- RightToLeft,
- FadeIn
- }
- public enum AlertCloseStyle
- {
- TopToBottom,
- LeftToRight,
- FadeOut
- }
- public partial class AlertForm : Form
- {
- /// <summary>
- /// The total hieght of all the active alerts, with padding,
- /// for stacking purposes, to prevent overlapping
- /// </summary>
- protected static int TotalHieght = 0;
- /// <summary>
- /// Form Position X
- /// </summary>
- protected int PosLeft = 0;
- /// <summary>
- /// Form Position Y
- /// </summary>
- protected int PosTop = 0;
- /// <summary>
- /// Target hieght of the alert
- /// </summary>
- protected int TargetHeight = 0;
- /// <summary>
- /// Gets or sets the alert type icon
- /// </summary>
- public AlertType AlertType = AlertType.Info;
- /// <summary>
- /// Gets or sets the Alert Popup Direction
- /// </summary>
- public AlertPopupStyle PopupStyle = AlertPopupStyle.BottomToTop;
- /// <summary>
- /// Gets or sets the Alert Close Direction
- /// </summary>
- public AlertCloseStyle CloseStyle = AlertCloseStyle.TopToBottom;
- /// <summary>
- /// Gets or sets the Alert Message
- /// </summary>
- public string AlertMessage
- {
- get { return labelHeader.Text; }
- set { labelHeader.Text = value; }
- }
- /// <summary>
- /// Gets or sets the Alert Contents
- /// </summary>
- public string AlertSubText
- {
- get { return labelDetails.Text; }
- set { labelDetails.Text = value; }
- }
- /// <summary>
- /// Gets or sets the Dock time for the alert (How long it stays open) in milliseconds
- /// </summary>
- public int DockTime
- {
- get { return CloseTimer.Interval; }
- set { CloseTimer.Interval = value; }
- }
- /// <summary>
- /// Indicates whether the Alert is closing
- /// </summary>
- public bool IsClosing { get; protected set; }
- /// <summary>
- /// Prevents window from stealing focus
- /// </summary>
- protected override bool ShowWithoutActivation
- {
- get { return true; }
- }
- public AlertForm()
- {
- InitializeComponent();
- // Rounded form edges
- this.Region = Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 1, 1));
- // Variable setup
- IsClosing = false;
- }
- #region Show Methods
- new public void Show()
- {
- BuildForm();
- base.Show();
- }
- new public void Show(IWin32Window Owner)
- {
- BuildForm();
- base.Show(Owner);
- }
- new public DialogResult ShowDialog()
- {
- throw new Exception("Alerts cannot use the ShowDialog method to display");
- }
- new public DialogResult ShowDialog(IWin32Window Owner)
- {
- throw new Exception("Alerts cannot use the ShowDialog method to display");
- }
- #endregion
- protected void BuildForm()
- {
- // Set heights so multiple alerts can show
- TargetHeight = TotalHieght + this.Height;
- TotalHieght += this.Height;
- // Get form start position
- if (PopupStyle == AlertPopupStyle.FadeIn)
- {
- this.Opacity = 0;
- PosLeft = Screen.GetWorkingArea(this).Width - this.Width - 5;
- PosTop = Screen.GetWorkingArea(this).Height - TargetHeight;
- }
- else if (PopupStyle == AlertPopupStyle.RightToLeft)
- {
- PosLeft = Screen.GetWorkingArea(this).Width - 5;
- PosTop = Screen.GetWorkingArea(this).Height - TargetHeight;
- }
- else
- {
- PosLeft = Screen.GetWorkingArea(this).Width;
- PosTop = Screen.GetWorkingArea(this).Height;
- }
- // Set initial position
- this.Location = new Point(PosLeft, PosTop);
- // Load icon image
- switch (this.AlertType)
- {
- default:
- AlertIconBox.Image = Resources.InfoAlert;
- break;
- case AlertType.Success:
- AlertIconBox.Image = Resources.AlertSuccess;
- break;
- case AlertType.Warning:
- AlertIconBox.Image = Resources.AlertWarning;
- break;
- }
- }
- /// <summary>
- /// Creates the rounded corner for the control
- /// </summary>
- /// <param name="nLeftRect">X coordinate of upper-left corner</param>
- /// <param name="nTopRect">Y coordinate of upper-left corner</param>
- /// <param name="nRightRect">X coordinate of lower-right corner</param>
- /// <param name="nBottomRect">Y coordinate of lower-right corner</param>
- /// <param name="nWidthEllipse">width of ellipse</param>
- /// <param name="nHeightEllipse">height of ellipse</param>
- /// <returns></returns>
- [DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
- private static extern IntPtr CreateRoundRectRgn
- (
- int nLeftRect,
- int nTopRect,
- int nRightRect,
- int nBottomRect,
- int nWidthEllipse,
- int nHeightEllipse
- );
- /// <summary>
- /// Draws the animate when closing
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void CloseTimer_Tick(object sender, EventArgs e)
- {
- // For future Queue messages, Reduce the stack hieght
- if (!IsClosing)
- {
- TotalHieght -= this.Height;
- IsClosing = true;
- }
- switch (CloseStyle)
- {
- case AlertCloseStyle.TopToBottom:
- if (this.Location.Y < PosTop)
- {
- CloseTimer.Interval = 20;
- this.Location = new Point(this.Location.X, this.Location.Y + 6);
- this.Opacity -= 0.050;
- }
- else
- this.Close();
- break;
- case AlertCloseStyle.LeftToRight:
- if (this.Location.X < PosLeft)
- {
- CloseTimer.Interval = 10;
- this.Location = new Point(this.Location.X + 15, this.Location.Y);
- this.Opacity -= 0.050;
- }
- else
- this.Close();
- break;
- case AlertCloseStyle.FadeOut:
- if (this.Opacity > 0)
- {
- CloseTimer.Interval = 10;
- this.Opacity -= 0.050;
- }
- else
- {
- CloseTimer.Enabled = true;
- OpenTimer.Enabled = false;
- }
- break;
- }
- }
- /// <summary>
- /// Draws the animate when opening
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void OpenTimer_Tick(object sender, EventArgs e)
- {
- switch (PopupStyle)
- {
- case AlertPopupStyle.BottomToTop:
- if (this.Location.Y > PosTop - TargetHeight)
- {
- double D = TargetHeight / 18;
- int Increment = (int)Math.Round(D, 0); // Base increment
- this.Location = new Point(PosLeft - this.Width - 5, this.Location.Y - Increment);
- }
- else
- {
- CloseTimer.Enabled = true;
- OpenTimer.Enabled = false;
- }
- break;
- case AlertPopupStyle.RightToLeft:
- if (this.Location.X > PosLeft - this.Width)
- {
- this.Location = new Point(this.Location.X - 10, this.Location.Y);
- }
- else
- {
- CloseTimer.Enabled = true;
- OpenTimer.Enabled = false;
- }
- break;
- case AlertPopupStyle.FadeIn:
- if (this.Opacity < 0.90)
- {
- this.Opacity += 0.050;
- }
- else
- {
- CloseTimer.Enabled = true;
- OpenTimer.Enabled = false;
- this.Refresh();
- }
- break;
- }
- }
- /// <summary>
- /// Creates the background gradient
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void AlertForm_Paint(object sender, PaintEventArgs e)
- {
- using (LinearGradientBrush brush = new LinearGradientBrush(
- this.ClientRectangle,
- Color.Black,
- Color.DimGray,
- 120F))
- {
- e.Graphics.FillRectangle(brush, this.ClientRectangle);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement