Toasts

/* Made by Lonami Exo * (C) LonamiWebs * 22 december 2014 */ using System; using System.Drawing; using System.Windows.Forms; using System.Threading.Tasks; public class Toast : Form { // Toasts won't gain focus with this (theorically) protected override bool ShowWithoutActivation { get { return true; } } const int WS_EX_TOOLWINDOW = 0x00000080; const int WS_EX_TOPMOST = 0x00000008; protected override CreateParams CreateParams { get { var Params = base.CreateParams; Params.ExStyle |= WS_EX_TOOLWINDOW; // Toasts won't show in Alt+TAB with this Params.ExStyle |= WS_EX_TOPMOST; // Toasts won't gain focus (again) return Params; } } public enum ToastLocation { TopLeft = 0, TopCenter = 1, TopRight = 2, BottomLeft = 3, BottomCenter = 4, BottomRight = 5 }; readonly Point FormLocation; int CurStep; const int Steps = 50; const int Speed = 4; const int Timeout = 2000; const int ToastMargin = 40; const int ToastWidth = 300; const int ToastHeight = 110; static int ScreenWidth { get { return Screen.PrimaryScreen.Bounds.Width; } } static int ScreenHeight { get { return Screen.PrimaryScreen.Bounds.Height; } } bool Showing = true; public Toast(ToastLocation location, string title, string details) { switch (location) { case ToastLocation.TopLeft: FormLocation = new Point(ToastMargin, ToastMargin); break; case ToastLocation.TopCenter: FormLocation = new Point(ScreenWidth / 2 - ToastWidth / 2, ToastMargin); break; case ToastLocation.TopRight: FormLocation = new Point(ScreenWidth - ToastWidth - ToastMargin, ToastMargin); break; case ToastLocation.BottomLeft: FormLocation = new Point(ToastMargin, ScreenHeight - ToastHeight - ToastMargin); break; case ToastLocation.BottomCenter: FormLocation = new Point(ScreenWidth / 2 - ToastWidth / 2, ScreenHeight - ToastHeight - ToastMargin); break; case ToastLocation.BottomRight: FormLocation = new Point(ScreenWidth - ToastWidth - ToastMargin, ScreenHeight - ToastHeight - ToastMargin); break; } BackColor = Color.FromArgb(50, 50, 50); FormBorderStyle = FormBorderStyle.None; ClientSize = new Size(ToastWidth, ToastHeight); Opacity = 0; ShowInTaskbar = false; var titleL = new Label { Font = new Font("Microsoft Sans Serif", 20.25F, FontStyle.Regular, GraphicsUnit.Point, 0), ForeColor = Color.White, Location = new Point(12, 9), Size = new Size(275, 38), Text = title }; var contentL = new Label { Font = new Font("Microsoft Sans Serif", 14.25F, FontStyle.Regular, GraphicsUnit.Point, 0), ForeColor = Color.White, Location = new Point(32, 47), Size = new Size(253, 200), Text = details }; Controls.Add(titleL); Controls.Add(contentL); Click += (sender, e) => EndShow(); titleL.Click += (sender, e) => EndShow(); contentL.Click += (sender, e) => EndShow(); BeginShow(); } void BeginShow() { Show(); Location = FormLocation; new TaskFactory().StartNew(() => { while (CurStep <= Steps && Showing) { Invoke(new MethodInvoker(() => Opacity = (float)CurStep / (float)Steps)); CurStep++; BetterSleep.Sleep(Speed); } BetterSleep.Sleep(Timeout); EndShow(); }); } void EndShow() { Showing = false; new TaskFactory().StartNew(() => { while (CurStep >= 0) { Invoke(new MethodInvoker(() => Opacity = (float)CurStep / (float)Steps)); CurStep--; BetterSleep.Sleep(Speed); } Invoke(new MethodInvoker(() => { Hide(); Dispose(); })); }); } }
You do want Android toasts in C#, don't you?

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.