namespace RyzStudio.Windows.ThemedForms { using System; using System.ComponentModel; using System.Drawing; using System.Drawing.Design; using System.Windows.Forms; using Resources = AppLauncher.Properties.Resources; public partial class DialogForm : System.Windows.Forms.Form { protected bool isBusy = false; protected Color topFillColour = Color.FromArgb(15, 15, 15); protected int topFillHeight = 32; protected int borderWidth = 1; private bool windowDragging = false; private Point windowOffset = new Point(); private Point windowSize = new Point(); public DialogForm() : base() { this.InitializeComponent(); this.BackColor = Color.FromArgb(246, 246, 246); this.Padding = new Padding(1); this.topFillColour = Color.FromArgb(51, 51, 51); this.topFillHeight = 32; this.BackColor = Color.FromArgb(248, 249, 250); this.FormBorderStyle = FormBorderStyle.None; imgbxClose.Click += pictureBox3_Click; } protected override void OnClosing(CancelEventArgs e) { base.OnClosing(e); if (this.IsBusy) { e.Cancel = true; } } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); Graphics g = e.Graphics; Rectangle areaTop = new Rectangle(this.DisplayRectangle.Left, this.Padding.Top, this.DisplayRectangle.Width, topFillHeight); Rectangle areaBorder = new Rectangle(this.ClientRectangle.X, this.ClientRectangle.Y, this.ClientRectangle.Width - borderWidth, this.ClientRectangle.Height - borderWidth); // draw header if (topFillHeight > 0) { g.FillRectangle(new SolidBrush(topFillColour), areaTop); } g.DrawRectangle(new Pen(Color.Black, borderWidth), areaBorder); } protected override void OnResize(EventArgs e) { base.OnResize(e); this.Invalidate(); } [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)] public new Color BackColor { get => base.BackColor; set => base.BackColor = value; } [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)] public new FormBorderStyle FormBorderStyle { get => base.FormBorderStyle; set => base.FormBorderStyle = value; } [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)] public new Padding Padding { get => base.Padding; set => base.Padding = value; } [Category("Appearance"), Browsable(true)] public string Description { get { return lblDescription.Text?.Replace("\n", "\\n"); } set { lblDescription.Text = value?.Replace("\\n", "\n"); } } protected virtual bool IsBusy { get => isBusy; set => isBusy = value; } private void pictureBox3_Click(object sender, System.EventArgs e) { MouseEventArgs mouseEvent = (MouseEventArgs)e; if (mouseEvent != null) { if (mouseEvent.Button != MouseButtons.Left) { return; } } this.Close(); } public void SetValue(Label sender, string value) { if (sender.InvokeRequired) { sender.Invoke(new MethodInvoker(() => { sender.Text = value; })); } else { sender.Text = value; } } public void SetValue(GroupBox sender, string value) { if (sender.InvokeRequired) { sender.Invoke(new MethodInvoker(() => { sender.Text = value; })); } else { sender.Text = value; } } public void AddValue(ListBox sender, string value) { if (sender.InvokeRequired) { sender.Invoke(new MethodInvoker(() => { sender.Items.Add(value); })); } else { sender.Items.Add(value); } } public void AddControl(FlowLayoutPanel sender, Control value) { if (sender.InvokeRequired) { sender.Invoke(new MethodInvoker(() => { sender.Controls.Add(value); })); } else { sender.Controls.Add(value); } } public void ClearValues(ListBox sender) { if (sender.InvokeRequired) { sender.Invoke(new MethodInvoker(() => { sender.Items.Clear(); })); } else { sender.Items.Clear(); } } public string GetValue(ListBox sender) { string rv = string.Empty; if (sender.InvokeRequired) { sender.Invoke(new MethodInvoker(() => { rv = (sender.SelectedItem == null) ? string.Empty : sender.SelectedItem.ToString(); })); } else { rv = (sender.SelectedItem == null) ? string.Empty : sender.SelectedItem.ToString(); } return rv; } public string GetValue(TextBox sender) { string rv = string.Empty; if (sender.InvokeRequired) { sender.Invoke(new MethodInvoker(() => { rv = sender.Text.Trim(); })); } else { rv = sender.Text.Trim(); } return rv; } public int GetValue(NumericUpDown sender) { int rv = 0; if (sender.InvokeRequired) { sender.Invoke(new MethodInvoker(() => { rv = (int)sender.Value; })); } else { rv = (int)sender.Value; } return rv; } private void label1_MouseDown(object sender, MouseEventArgs e) { if (e.Button != MouseButtons.Left) { return; } windowDragging = true; windowOffset = e.Location; } private void label1_MouseUp(object sender, MouseEventArgs e) { windowDragging = false; } private void label1_MouseMove(object sender, MouseEventArgs e) { if (windowDragging) { Point currentScreenPos = PointToScreen(e.Location); Location = new Point(currentScreenPos.X - windowOffset.X, currentScreenPos.Y - windowOffset.Y); } } private void label1_MouseClick(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Middle) { this.TopMost = !this.TopMost; } } private void panel1_MouseDown(object sender, MouseEventArgs e) { if (e.Button != MouseButtons.Left) { return; } windowDragging = true; windowOffset = e.Location; windowSize = new Point(this.Width, this.Height); } private void panel1_MouseUp(object sender, MouseEventArgs e) { windowDragging = false; } private void panel1_MouseMove(object sender, MouseEventArgs e) { if (windowDragging) { this.Size = new Size(e.X - windowOffset.X + this.Width, e.Y - windowOffset.Y + this.Height); } } private void panel2_MouseDown(object sender, MouseEventArgs e) { if (e.Button != MouseButtons.Left) { return; } windowDragging = true; windowOffset = e.Location; windowSize = new Point(this.Width, this.Height); } private void panel2_MouseUp(object sender, MouseEventArgs e) { windowDragging = false; } private void panel2_MouseMove(object sender, MouseEventArgs e) { if (windowDragging) { this.Size = new Size(windowSize.X, e.Y - windowOffset.Y + this.Height); } } } }