Windows Form Customized Message Box

Create two classes inside one class File

  1. Create new class file give name as AnimtedMsgBox and inherit this class with Form Class as shown below:
    1. class AnimtedMsgBox :Form  
    2. {  
    3. }
    Or

    Add WindowsForm and give name as RNTP_MsgBox.cs

    And change class name if you want as given above then.

  2. Add two references to this class:
    1. using System.Text.RegularExpressions;  
    2. using System.Runtime.InteropServices;  
  3. Declare variables inside class as below:
    1. private const int CS_DROPSHADOW = 0x00020000;  
    2. private static AnimtedMsgBox _msgBox;  
    3. private Panel _plHeader = new Panel();  
    4. private Panel _plFooter = new Panel();  
    5. private Panel _plIcon = new Panel();  
    6. private PictureBox _picIcon = new PictureBox();  
    7. private FlowLayoutPanel _flpButtons = new FlowLayoutPanel();  
    8. private Label _lblTitle;  
    9. private Label _lblMessage;  
    10. private List<Button> _buttonCollection = new List<Button>();  
    11. private static DialogResult _buttonResult = new DialogResult();  
    12. private static Timer _timer;  
    13. private static Point lastMousePos;  
    14.   
    15. [DllImport("user32.dll", CharSet = CharSet.Auto)]  
    16. private static extern bool MessageBeep(uint type);  
  4. And add below function to this class:
    1. private AnimtedMsgBox() {  
    2.     this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;  
    3.     this.BackColor = Color.FromArgb(45, 45, 48);  
    4.     this.StartPosition = FormStartPosition.CenterScreen;  
    5.     this.Padding = new System.Windows.Forms.Padding(3);  
    6.     this.Width = 200;  
    7.   
    8.     _lblTitle = new Label();  
    9.     _lblTitle.ForeColor = Color.White;  
    10.     _lblTitle.Font = new System.Drawing.Font("Segoe UI", 18);  
    11.     _lblTitle.Dock = DockStyle.Top;  
    12.     _lblTitle.Height = 50;  
    13.   
    14.     _lblMessage = new Label();  
    15.     _lblMessage.ForeColor = Color.White;  
    16.     _lblMessage.Font = new System.Drawing.Font("Segoe UI", 10);  
    17.     _lblMessage.Dock = DockStyle.Fill;  
    18.   
    19.     _flpButtons.FlowDirection = FlowDirection.RightToLeft;  
    20.     _flpButtons.Dock = DockStyle.Fill;  
    21.   
    22.     _plHeader.Dock = DockStyle.Fill;  
    23.     _plHeader.Padding = new Padding(20);  
    24.     _plHeader.Controls.Add(_lblMessage);  
    25.     _plHeader.Controls.Add(_lblTitle);  
    26.   
    27.     _plFooter.Dock = DockStyle.Bottom;  
    28.     _plFooter.Padding = new Padding(20);  
    29.     _plFooter.BackColor = Color.FromArgb(37, 37, 38);  
    30.     _plFooter.Height = 75;  
    31.     _plFooter.Controls.Add(_flpButtons);  
    32.   
    33.     _picIcon.Width = 32;  
    34.     _picIcon.Height = 32;  
    35.     _picIcon.Location = new Point(30, 50);  
    36.   
    37.     _plIcon.Dock = DockStyle.Left;  
    38.     _plIcon.Padding = new Padding(20);  
    39.     _plIcon.Width = 70;  
    40.     _plIcon.Controls.Add(_picIcon);  
    41.   
    42.     List < Control > controlCollection = new List < Control > ();  
    43.     controlCollection.Add(this);  
    44.     controlCollection.Add(_lblTitle);  
    45.     controlCollection.Add(_lblMessage);  
    46.     controlCollection.Add(_flpButtons);  
    47.     controlCollection.Add(_plHeader);  
    48.     controlCollection.Add(_plFooter);  
    49.     controlCollection.Add(_plIcon);  
    50.     controlCollection.Add(_picIcon);  
    51.   
    52.     foreach(Control control in controlCollection) {  
    53.         control.MouseDown += AnimtedMsgBox_MouseDown;  
    54.         control.MouseMove += AnimtedMsgBox_MouseMove;  
    55.     }  
    56.   
    57.     this.Controls.Add(_plHeader);  
    58.     this.Controls.Add(_plIcon);  
    59.     this.Controls.Add(_plFooter);  
    60. }  
    61.   
    62. private static void AnimtedMsgBox_MouseDown(object sender, MouseEventArgs e) {  
    63.     if (e.Button == MouseButtons.Left) {  
    64.         lastMousePos = new Point(e.X, e.Y);  
    65.     }  
    66. }  
    67.   
    68.   
    69. private static void AnimtedMsgBox_MouseMove(object sender, MouseEventArgs e) {  
    70.     if (e.Button == MouseButtons.Left) {  
    71.         _msgBox.Left += e.X - lastMousePos.X;  
    72.         _msgBox.Top += e.Y - lastMousePos.Y;  
    73.     }  
    74. }  
    75.   
    76. public static DialogResult Show(string message) {  
    77.     _msgBox = new AnimtedMsgBox();  
    78.     _msgBox._lblMessage.Text = message;  
    79.   
    80.     AnimtedMsgBox.InitButtons(Buttons.OK);  
    81.   
    82.     _msgBox.ShowDialog();  
    83.     MessageBeep(0);  
    84.     return _buttonResult;  
    85. }  
    86.   
    87. public static DialogResult Show(string message, string title) {  
    88.     _msgBox = new AnimtedMsgBox();  
    89.     _msgBox._lblMessage.Text = message;  
    90.     _msgBox._lblTitle.Text = title;  
    91.     _msgBox.Size = AnimtedMsgBox.MessageSize(message);  
    92.   
    93.     AnimtedMsgBox.InitButtons(Buttons.OK);  
    94.   
    95.     _msgBox.ShowDialog();  
    96.     MessageBeep(0);  
    97.     return _buttonResult;  
    98. }  
    99.   
    100. public static DialogResult Show(string message, string title, Buttons buttons) {  
    101.     _msgBox = new AnimtedMsgBox();  
    102.     _msgBox._lblMessage.Text = message;  
    103.     _msgBox._lblTitle.Text = title;  
    104.     _msgBox._plIcon.Hide();  
    105.   
    106.     AnimtedMsgBox.InitButtons(buttons);  
    107.   
    108.     _msgBox.Size = AnimtedMsgBox.MessageSize(message);  
    109.     _msgBox.ShowDialog();  
    110.     MessageBeep(0);  
    111.     return _buttonResult;  
    112. }  
    113.   
    114. public static DialogResult Show(string message, string title, Buttons buttons, Icon icon) {  
    115.     _msgBox = new AnimtedMsgBox();  
    116.     _msgBox._lblMessage.Text = message;  
    117.     _msgBox._lblTitle.Text = title;  
    118.   
    119.     AnimtedMsgBox.InitButtons(buttons);  
    120.     AnimtedMsgBox.InitIcon(icon);  
    121.   
    122.     _msgBox.Size = AnimtedMsgBox.MessageSize(message);  
    123.     _msgBox.ShowDialog();  
    124.     MessageBeep(0);  
    125.     return _buttonResult;  
    126. }  
    127.   
    128. public static DialogResult Show(string message, string title, Buttons buttons, Icon icon, AnimateStyle style) {  
    129.     _msgBox = new AnimtedMsgBox();  
    130.     _msgBox._lblMessage.Text = message;  
    131.     _msgBox._lblTitle.Text = title;  
    132.     _msgBox.Height = 0;  
    133.   
    134.     AnimtedMsgBox.InitButtons(buttons);  
    135.     AnimtedMsgBox.InitIcon(icon);  
    136.   
    137.     _timer = new Timer();  
    138.     Size formSize = AnimtedMsgBox.MessageSize(message);  
    139.   
    140.     switch (style) {  
    141.         case AnimtedMsgBox.AnimateStyle.SlideDown:  
    142.             _msgBox.Size = new Size(formSize.Width, 0);  
    143.             _timer.Interval = 1;  
    144.             _timer.Tag = new ApplyAnimation(formSize, style);  
    145.             break;  
    146.   
    147.         case AnimtedMsgBox.AnimateStyle.FadeIn:  
    148.             _msgBox.Size = formSize;  
    149.             _msgBox.Opacity = 0;  
    150.             _timer.Interval = 20;  
    151.             _timer.Tag = new ApplyAnimation(formSize, style);  
    152.             break;  
    153.   
    154.         case AnimtedMsgBox.AnimateStyle.ZoomIn:  
    155.             _msgBox.Size = new Size(formSize.Width + 100, formSize.Height + 100);  
    156.             _timer.Tag = new ApplyAnimation(formSize, style);  
    157.             _timer.Interval = 1;  
    158.             break;  
    159.     }  
    160.   
    161.     _timer.Tick += timer_Tick;  
    162.     _timer.Start();  
    163.   
    164.     _msgBox.ShowDialog();  
    165.     MessageBeep(0);  
    166.     return _buttonResult;  
    167. }  
    168.   
    169. static void timer_Tick(object sender, EventArgs e) {  
    170.     Timer timer = (Timer) sender;  
    171.     ApplyAnimation animate = (ApplyAnimation) timer.Tag;  
    172.   
    173.     switch (animate.Style) {  
    174.         case AnimtedMsgBox.AnimateStyle.SlideDown:  
    175.             if (_msgBox.Height < animate.FormSize.Height) {  
    176.                 _msgBox.Height += 17;  
    177.                 _msgBox.Invalidate();  
    178.             } else {  
    179.                 _timer.Stop();  
    180.                 _timer.Dispose();  
    181.             }  
    182.             break;  
    183.   
    184.         case AnimtedMsgBox.AnimateStyle.FadeIn:  
    185.             if (_msgBox.Opacity < 1) {  
    186.                 _msgBox.Opacity += 0.1;  
    187.                 _msgBox.Invalidate();  
    188.             } else {  
    189.                 _timer.Stop();  
    190.                 _timer.Dispose();  
    191.             }  
    192.             break;  
    193.   
    194.         case AnimtedMsgBox.AnimateStyle.ZoomIn:  
    195.             if (_msgBox.Width > animate.FormSize.Width) {  
    196.                 _msgBox.Width -= 17;  
    197.                 _msgBox.Invalidate();  
    198.             }  
    199.             if (_msgBox.Height > animate.FormSize.Height) {  
    200.                 _msgBox.Height -= 17;  
    201.                 _msgBox.Invalidate();  
    202.             }  
    203.             break;  
    204.     }  
    205. }  
    206.   
    207. private static void InitButtons(Buttons buttons) {  
    208.     switch (buttons) {  
    209.         case AnimtedMsgBox.Buttons.AbortRetryIgnore:  
    210.             _msgBox.InitAbortRetryIgnoreButtons();  
    211.             break;  
    212.   
    213.         case AnimtedMsgBox.Buttons.OK:  
    214.             _msgBox.InitOKButton();  
    215.             break;  
    216.   
    217.         case AnimtedMsgBox.Buttons.OKCancel:  
    218.             _msgBox.InitOKCancelButtons();  
    219.             break;  
    220.   
    221.         case AnimtedMsgBox.Buttons.RetryCancel:  
    222.             _msgBox.InitRetryCancelButtons();  
    223.             break;  
    224.   
    225.         case AnimtedMsgBox.Buttons.YesNo:  
    226.             _msgBox.InitYesNoButtons();  
    227.             break;  
    228.   
    229.         case AnimtedMsgBox.Buttons.YesNoCancel:  
    230.             _msgBox.InitYesNoCancelButtons();  
    231.             break;  
    232.     }  
    233.   
    234.     foreach(Button btn in _msgBox._buttonCollection) {  
    235.         btn.ForeColor = Color.FromArgb(170, 170, 170);  
    236.         btn.Font = new System.Drawing.Font("Segoe UI", 8);  
    237.         btn.Padding = new Padding(3);  
    238.         btn.FlatStyle = FlatStyle.Flat;  
    239.         btn.Height = 30;  
    240.         btn.FlatAppearance.BorderColor = Color.FromArgb(99, 99, 98);  
    241.         btn.Cursor = Cursors.Hand;  
    242.         _msgBox._flpButtons.Controls.Add(btn);  
    243.     }  
    244. }  
    245.   
    246. private static void InitIcon(Icon icon) {  
    247.     switch (icon) {  
    248.         case AnimtedMsgBox.Icon.Application:  
    249.             _msgBox._picIcon.Image = SystemIcons.Application.ToBitmap();  
    250.             break;  
    251.   
    252.         case AnimtedMsgBox.Icon.Exclamation:  
    253.             _msgBox._picIcon.Image = SystemIcons.Exclamation.ToBitmap();  
    254.             break;  
    255.   
    256.         case AnimtedMsgBox.Icon.Error:  
    257.             _msgBox._picIcon.Image = SystemIcons.Error.ToBitmap();  
    258.             break;  
    259.   
    260.         case AnimtedMsgBox.Icon.Info:  
    261.             _msgBox._picIcon.Image = SystemIcons.Information.ToBitmap();  
    262.             break;  
    263.   
    264.         case AnimtedMsgBox.Icon.Question:  
    265.             _msgBox._picIcon.Image = SystemIcons.Question.ToBitmap();  
    266.             break;  
    267.   
    268.         case AnimtedMsgBox.Icon.Shield:  
    269.             _msgBox._picIcon.Image = SystemIcons.Shield.ToBitmap();  
    270.             break;  
    271.   
    272.         case AnimtedMsgBox.Icon.Warning:  
    273.             _msgBox._picIcon.Image = SystemIcons.Warning.ToBitmap();  
    274.             break;  
    275.     }  
    276. }  
    277.   
    278. private void InitAbortRetryIgnoreButtons() {  
    279.     Button btnAbort = new Button();  
    280.     btnAbort.Text = "Abort";  
    281.     btnAbort.Click += ButtonClick;  
    282.   
    283.     Button btnRetry = new Button();  
    284.     btnRetry.Text = "Retry";  
    285.     btnRetry.Click += ButtonClick;  
    286.   
    287.     Button btnIgnore = new Button();  
    288.     btnIgnore.Text = "Ignore";  
    289.     btnIgnore.Click += ButtonClick;  
    290.   
    291.     this._buttonCollection.Add(btnAbort);  
    292.     this._buttonCollection.Add(btnRetry);  
    293.     this._buttonCollection.Add(btnIgnore);  
    294. }  
    295.   
    296. private void InitOKButton() {  
    297.     Button btnOK = new Button();  
    298.     btnOK.Text = "OK";  
    299.     btnOK.Click += ButtonClick;  
    300.   
    301.     this._buttonCollection.Add(btnOK);  
    302. }  
    303.   
    304. private void InitOKCancelButtons() {  
    305.     Button btnOK = new Button();  
    306.     btnOK.Text = "OK";  
    307.     btnOK.Click += ButtonClick;  
    308.   
    309.     Button btnCancel = new Button();  
    310.     btnCancel.Text = "Cancel";  
    311.     btnCancel.Click += ButtonClick;  
    312.   
    313.   
    314.     this._buttonCollection.Add(btnOK);  
    315.     this._buttonCollection.Add(btnCancel);  
    316. }  
    317.   
    318. private void InitRetryCancelButtons() {  
    319.     Button btnRetry = new Button();  
    320.     btnRetry.Text = "OK";  
    321.     btnRetry.Click += ButtonClick;  
    322.   
    323.     Button btnCancel = new Button();  
    324.     btnCancel.Text = "Cancel";  
    325.     btnCancel.Click += ButtonClick;  
    326.   
    327.   
    328.     this._buttonCollection.Add(btnRetry);  
    329.     this._buttonCollection.Add(btnCancel);  
    330. }  
    331.   
    332. private void InitYesNoButtons() {  
    333.     Button btnYes = new Button();  
    334.     btnYes.Text = "Yes";  
    335.     btnYes.Click += ButtonClick;  
    336.   
    337.     Button btnNo = new Button();  
    338.     btnNo.Text = "No";  
    339.     btnNo.Click += ButtonClick;  
    340.   
    341.   
    342.     this._buttonCollection.Add(btnYes);  
    343.     this._buttonCollection.Add(btnNo);  
    344. }  
    345.   
    346. private void InitYesNoCancelButtons() {  
    347.     Button btnYes = new Button();  
    348.     btnYes.Text = "Abort";  
    349.     btnYes.Click += ButtonClick;  
    350.   
    351.     Button btnNo = new Button();  
    352.     btnNo.Text = "Retry";  
    353.     btnNo.Click += ButtonClick;  
    354.   
    355.     Button btnCancel = new Button();  
    356.     btnCancel.Text = "Cancel";  
    357.     btnCancel.Click += ButtonClick;  
    358.   
    359.     this._buttonCollection.Add(btnYes);  
    360.     this._buttonCollection.Add(btnNo);  
    361.     this._buttonCollection.Add(btnCancel);  
    362. }  
    363.   
    364. private static void ButtonClick(object sender, EventArgs e) {  
    365.     Button btn = (Button) sender;  
    366.   
    367.     switch (btn.Text) {  
    368.         case "Abort":  
    369.             _buttonResult = DialogResult.Abort;  
    370.             break;  
    371.   
    372.         case "Retry":  
    373.             _buttonResult = DialogResult.Retry;  
    374.             break;  
    375.   
    376.         case "Ignore":  
    377.             _buttonResult = DialogResult.Ignore;  
    378.             break;  
    379.   
    380.         case "OK":  
    381.             _buttonResult = DialogResult.OK;  
    382.             break;  
    383.   
    384.         case "Cancel":  
    385.             _buttonResult = DialogResult.Cancel;  
    386.             break;  
    387.   
    388.         case "Yes":  
    389.             _buttonResult = DialogResult.Yes;  
    390.             break;  
    391.   
    392.         case "No":  
    393.             _buttonResult = DialogResult.No;  
    394.             break;  
    395.     }  
    396.   
    397.     _msgBox.Dispose();  
    398. }  
    399.   
    400. private static Size MessageSize(string message) {  
    401.     Graphics g = _msgBox.CreateGraphics();  
    402.     int width = 350;  
    403.     int height = 230;  
    404.   
    405.     SizeF size = g.MeasureString(message, new System.Drawing.Font("Segoe UI", 10));  
    406.   
    407.     if (message.Length < 150) {  
    408.         if ((int) size.Width > 350) {  
    409.             width = (int) size.Width;  
    410.         }  
    411.     } else {  
    412.         string[] groups = (from Match m in Regex.Matches(message, ".{1,180}") select m.Value).ToArray();  
    413.         int lines = groups.Length;  
    414.         width = 490;  
    415.         height += (int)(size.Height) * lines;  
    416.     }  
    417.     return new Size(width, height);  
    418. }  
    419.   
    420. protected override CreateParams CreateParams {  
    421.     get {  
    422.         CreateParams cp = base.CreateParams;  
    423.         cp.ClassStyle |= CS_DROPSHADOW;  
    424.         return cp;  
    425.     }  
    426. }  
    427.   
    428. protected override void OnPaint(PaintEventArgs e) {  
    429.     base.OnPaint(e);  
    430.   
    431.     Graphics g = e.Graphics;  
    432.     Rectangle rect = new Rectangle(new Point(0, 0), new Size(this.Width - 1, this.Height - 1));  
    433.     Pen pen = new Pen(Color.FromArgb(0, 151, 251));  
    434.   
    435.     g.DrawRectangle(pen, rect);  
    436. }  
    437.   
    438. public enum Buttons {  
    439.     AbortRetryIgnore = 1,  
    440.         OK = 2,  
    441.         OKCancel = 3,  
    442.         RetryCancel = 4,  
    443.         YesNo = 5,  
    444.         YesNoCancel = 6  
    445. }  
    446.   
    447. public enum Icon {  
    448.     Application = 1,  
    449.         Exclamation = 2,  
    450.         Error = 3,  
    451.         Warning = 4,  
    452.         Info = 5,  
    453.         Question = 6,  
    454.         Shield = 7,  
    455.         Search = 8  
    456. }  
    457.   
    458. public enum AnimateStyle {  
    459.     SlideDown = 1,  
    460.         FadeIn = 2,  
    461.         ZoomIn = 3  
    462. }  
  5. Add one more class to this Class file or make separate class as give below:

    class ApplyAnimation
    {
    public Size FormSize;
    public AnimtedMsgBox.AnimateStyle Style;

    public ApplyAnimation(Size formSize, AnimtedMsgBox.AnimateStyle style)
    {
    this.FormSize = formSize;
    this.Style = style;
    }
    }

That’s it your Customized messagebox is ready, now lets see how to use it.

-To Use this Customized MessageBox

  1. Open new or existing windows form and give reference to this class, In my example reference is
    using RNTP_MsgBox;

  2. Add button and write messagebox code to button click event or any event as given below:

    AnimtedMsgBox.Show("Customized messagebox FadeIn", "title", AnimtedMsgBox.Buttons.OK, AnimtedMsgBox.Icon.Application, AnimtedMsgBox.AnimateStyle.FadeIn);

    Note:

    There are three animation for this customized messagebox:

    AnimtedMsgBox.AnimateStyle.FadeIn
    AnimtedMsgBox.AnimateStyle.SlideDown
    AnimtedMsgBox.AnimateStyle.ZoomIn

    There are new extra icon also avilable for this customized messagebox
    AnimtedMsgBox.Icon.Application
    AnimtedMsgBox.Icon.Shield
    AnimtedMsgBox.Icon.Search