using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Globalization;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace JR.Utils.GUI.Forms
{
public class FlexibleMessageBox
{
#region Public statics
public static double MAX_WIDTH_FACTOR = 0.7;
public static double MAX_HEIGHT_FACTOR = 0.9;
public static Font FONT = SystemFonts.MessageBoxFont;
#endregion
#region Public show functions
public static DialogResult Show(string text)
{
return FlexibleMessageBoxForm.Show(null, text, string.Empty, MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);
}
public static DialogResult Show(IWin32Window owner, string text)
{
return FlexibleMessageBoxForm.Show(owner, text, string.Empty, MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);
}
public static DialogResult Show(string text, string caption)
{
return FlexibleMessageBoxForm.Show(null, text, caption, MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);
}
public static DialogResult Show(IWin32Window owner, string text, string caption)
{
return FlexibleMessageBoxForm.Show(owner, text, caption, MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);
}
public static DialogResult Show(string text, string caption, MessageBoxButtons buttons)
{
return FlexibleMessageBoxForm.Show(null, text, caption, buttons, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);
}
public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons)
{
return FlexibleMessageBoxForm.Show(owner, text, caption, buttons, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);
}
public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
{
return FlexibleMessageBoxForm.Show(null, text, caption, buttons, icon, MessageBoxDefaultButton.Button1);
}
public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
{
return FlexibleMessageBoxForm.Show(owner, text, caption, buttons, icon, MessageBoxDefaultButton.Button1);
}
public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
{
return FlexibleMessageBoxForm.Show(null, text, caption, buttons, icon, defaultButton);
}
public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
{
return FlexibleMessageBoxForm.Show(owner, text, caption, buttons, icon, defaultButton);
}
#endregion
#region Internal form class
private class FlexibleMessageBoxForm : Form
{
#region Constants and P/Invoke
private const int CS_DROPSHADOW = 0x00020000;
private const int WM_NCLBUTTONDOWN = 0xA1;
private const int HT_CAPTION = 0x2;
private const uint FLASHW_TRAY = 0x00000002;
private const uint FLASHW_TIMERNOFG = 0x0000000C;
[StructLayout(LayoutKind.Sequential)]
private struct FLASHWINFO
{
public uint cbSize;
public IntPtr hwnd;
public uint dwFlags;
public uint uCount;
public uint dwTimeout;
}
[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]
private static extern bool ReleaseCapture();
[DllImport("user32.dll")]
private static extern bool FlashWindowEx(ref FLASHWINFO pwfi);
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ClassStyle |= CS_DROPSHADOW;
return cp;
}
}
#endregion
#region Form-Designer generated code
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
private void CloseButton_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
this.Close();
}
private void TitlePanel_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(this.Handle, WM_NCLBUTTONDOWN, (IntPtr)HT_CAPTION, IntPtr.Zero);
}
}
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
button1 = new System.Windows.Forms.Button();
richTextBoxMessage = new System.Windows.Forms.RichTextBox();
FlexibleMessageBoxFormBindingSource = new System.Windows.Forms.BindingSource(components);
panel1 = new System.Windows.Forms.Panel();
pictureBoxForIcon = new System.Windows.Forms.PictureBox();
button2 = new System.Windows.Forms.Button();
button3 = new System.Windows.Forms.Button();
titlePanel = new System.Windows.Forms.Panel();
titleLabel = new System.Windows.Forms.Label();
closeButton = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)FlexibleMessageBoxFormBindingSource).BeginInit();
panel1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxForIcon).BeginInit();
titlePanel.SuspendLayout();
SuspendLayout();
//
// titlePanel
//
titlePanel.BackColor = System.Drawing.Color.FromArgb(20, 24, 29);
titlePanel.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right;
titlePanel.Location = new System.Drawing.Point(6, 6);
titlePanel.Name = "titlePanel";
titlePanel.Size = new System.Drawing.Size(248, 28);
titlePanel.TabIndex = 10;
titlePanel.Controls.Add(closeButton);
titlePanel.Controls.Add(titleLabel);
titlePanel.MouseDown += TitlePanel_MouseDown;
//
// titleLabel
//
titleLabel.AutoSize = false;
titleLabel.Dock = System.Windows.Forms.DockStyle.Fill;
titleLabel.ForeColor = System.Drawing.Color.White;
titleLabel.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Bold);
titleLabel.Location = new System.Drawing.Point(0, 0);
titleLabel.Name = "titleLabel";
titleLabel.Padding = new System.Windows.Forms.Padding(12, 0, 0, 0);
titleLabel.Size = new System.Drawing.Size(218, 28);
titleLabel.TabIndex = 0;
titleLabel.Text = "
";
titleLabel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
titleLabel.MouseDown += TitlePanel_MouseDown;
//
// closeButton
//
closeButton.Dock = System.Windows.Forms.DockStyle.Right;
closeButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
closeButton.FlatAppearance.BorderSize = 0;
closeButton.FlatAppearance.MouseOverBackColor = System.Drawing.Color.FromArgb(200, 60, 60);
closeButton.BackColor = System.Drawing.Color.FromArgb(20, 24, 29);
closeButton.ForeColor = System.Drawing.Color.White;
closeButton.Font = new System.Drawing.Font("Segoe UI", 9F);
closeButton.Location = new System.Drawing.Point(218, 0);
closeButton.Name = "closeButton";
closeButton.Size = new System.Drawing.Size(30, 28);
closeButton.TabIndex = 1;
closeButton.TabStop = false;
closeButton.Text = "✕";
closeButton.Click += CloseButton_Click;
//
// button1
//
button1.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right;
button1.DialogResult = System.Windows.Forms.DialogResult.OK;
button1.Location = new System.Drawing.Point(26, 80);
button1.Name = "button1";
button1.Size = new System.Drawing.Size(75, 28);
button1.TabIndex = 2;
button1.Text = "OK";
button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
button1.FlatAppearance.BorderSize = 0;
button1.FlatAppearance.MouseOverBackColor = System.Drawing.Color.FromArgb(60, 65, 80);
button1.BackColor = System.Drawing.Color.FromArgb(42, 45, 58);
button1.ForeColor = System.Drawing.Color.White;
button1.Font = new System.Drawing.Font("Segoe UI", 9F);
button1.Cursor = System.Windows.Forms.Cursors.Hand;
button1.Visible = false;
//
// richTextBoxMessage
//
richTextBoxMessage.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom
| System.Windows.Forms.AnchorStyles.Left
| System.Windows.Forms.AnchorStyles.Right;
richTextBoxMessage.BackColor = System.Drawing.Color.FromArgb(20, 24, 29);
richTextBoxMessage.ForeColor = System.Drawing.Color.White;
richTextBoxMessage.BorderStyle = System.Windows.Forms.BorderStyle.None;
richTextBoxMessage.DataBindings.Add(new System.Windows.Forms.Binding("Text", FlexibleMessageBoxFormBindingSource, "MessageText", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));
richTextBoxMessage.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 0);
richTextBoxMessage.Location = new System.Drawing.Point(46, 6);
richTextBoxMessage.Margin = new System.Windows.Forms.Padding(0);
richTextBoxMessage.Name = "richTextBoxMessage";
richTextBoxMessage.ReadOnly = true;
richTextBoxMessage.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.Vertical;
richTextBoxMessage.Size = new System.Drawing.Size(190, 20);
richTextBoxMessage.TabIndex = 0;
richTextBoxMessage.TabStop = false;
richTextBoxMessage.Text = "";
richTextBoxMessage.LinkClicked += new System.Windows.Forms.LinkClickedEventHandler(richTextBoxMessage_LinkClicked);
//
// panel1
//
panel1.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom
| System.Windows.Forms.AnchorStyles.Left
| System.Windows.Forms.AnchorStyles.Right;
panel1.BackColor = System.Drawing.Color.FromArgb(20, 24, 29);
panel1.Controls.Add(pictureBoxForIcon);
panel1.Controls.Add(richTextBoxMessage);
panel1.Location = new System.Drawing.Point(6, 34);
panel1.Name = "panel1";
panel1.Size = new System.Drawing.Size(248, 59);
panel1.TabIndex = 1;
//
// pictureBoxForIcon
//
pictureBoxForIcon.BackColor = System.Drawing.Color.Transparent;
pictureBoxForIcon.Location = new System.Drawing.Point(15, 15);
pictureBoxForIcon.Name = "pictureBoxForIcon";
pictureBoxForIcon.Size = new System.Drawing.Size(32, 32);
pictureBoxForIcon.TabIndex = 8;
pictureBoxForIcon.TabStop = false;
//
// button2
//
button2.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right;
button2.DialogResult = System.Windows.Forms.DialogResult.OK;
button2.Location = new System.Drawing.Point(107, 80);
button2.Name = "button2";
button2.Size = new System.Drawing.Size(75, 28);
button2.TabIndex = 3;
button2.Text = "OK";
button2.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
button2.FlatAppearance.BorderSize = 0;
button2.FlatAppearance.MouseOverBackColor = System.Drawing.Color.FromArgb(60, 65, 80);
button2.BackColor = System.Drawing.Color.FromArgb(42, 45, 58);
button2.ForeColor = System.Drawing.Color.White;
button2.Font = new System.Drawing.Font("Segoe UI", 9F);
button2.Cursor = System.Windows.Forms.Cursors.Hand;
button2.Visible = false;
//
// button3
//
button3.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right;
button3.DialogResult = System.Windows.Forms.DialogResult.OK;
button3.Location = new System.Drawing.Point(188, 80);
button3.Name = "button3";
button3.Size = new System.Drawing.Size(75, 28);
button3.TabIndex = 0;
button3.Text = "OK";
button3.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
button3.FlatAppearance.BorderSize = 0;
button3.FlatAppearance.MouseOverBackColor = System.Drawing.Color.FromArgb(60, 65, 80);
button3.BackColor = System.Drawing.Color.FromArgb(42, 45, 58);
button3.ForeColor = System.Drawing.Color.White;
button3.Font = new System.Drawing.Font("Segoe UI", 9F);
button3.Cursor = System.Windows.Forms.Cursors.Hand;
button3.Visible = false;
//
// FlexibleMessageBoxForm
//
AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
BackColor = System.Drawing.Color.FromArgb(25, 25, 30);
ForeColor = System.Drawing.Color.White;
FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
ClientSize = new System.Drawing.Size(260, 115);
Padding = new System.Windows.Forms.Padding(5);
Controls.Add(titlePanel);
Controls.Add(button3);
Controls.Add(button2);
Controls.Add(button1);
Controls.Add(panel1);
MaximizeBox = false;
MinimizeBox = false;
MinimumSize = new System.Drawing.Size(276, 120);
Name = "FlexibleMessageBoxForm";
ShowIcon = false;
SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
Text = "";
Shown += new System.EventHandler(FlexibleMessageBoxForm_Shown);
((System.ComponentModel.ISupportInitialize)FlexibleMessageBoxFormBindingSource).EndInit();
panel1.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)pictureBoxForIcon).EndInit();
titlePanel.ResumeLayout(false);
ResumeLayout(false);
// Apply rounded corners and custom painting
this.Paint += FlexibleMessageBoxForm_Paint;
button1.Paint += RoundedButton_Paint;
button2.Paint += RoundedButton_Paint;
button3.Paint += RoundedButton_Paint;
// Setup hover effects for buttons
SetupButtonHover(button1);
SetupButtonHover(button2);
SetupButtonHover(button3);
Activate();
}
private Button button1;
private BindingSource FlexibleMessageBoxFormBindingSource;
private RichTextBox richTextBoxMessage;
private Panel panel1;
private PictureBox pictureBoxForIcon;
private Button button2;
private Button button3;
private Panel titlePanel;
private Label titleLabel;
private Button closeButton;
#endregion
#region Custom Painting
private Dictionary