This repository has been archived on 2024-08-06. You can view files and clone it, but cannot push or open issues or pull requests.
linear-app-launcher/FizzyLauncher.UI/RyzStudio/Windows/Forms/TForm.cs

433 lines
16 KiB
C#
Raw Normal View History

2021-06-01 08:15:08 +00:00
using System;
2020-05-16 12:25:59 +00:00
using System.ComponentModel;
2020-03-28 02:54:08 +00:00
using System.Drawing;
using System.Windows.Forms;
2021-06-01 08:15:08 +00:00
using Resources = AppLauncher.Properties.Resources;
2020-03-28 02:54:08 +00:00
2021-06-01 08:15:08 +00:00
namespace RyzStudio.Windows.Forms
2020-03-28 02:54:08 +00:00
{
2020-10-21 01:17:24 +00:00
public class TForm : Form
2020-03-28 02:54:08 +00:00
{
2021-06-01 08:15:08 +00:00
protected readonly Color borderColour = Color.FromArgb(200, 200, 200);
2020-05-17 02:24:46 +00:00
protected readonly int borderWidth = 1;
2020-10-20 21:42:40 +00:00
2021-06-01 08:15:08 +00:00
protected readonly int resizeBorderWidth = 4;
2020-10-20 21:42:40 +00:00
2021-06-01 08:15:08 +00:00
protected readonly Color backColour = Color.FromArgb(250, 250, 250);
2020-05-16 12:25:59 +00:00
2020-11-08 23:24:40 +00:00
protected readonly Color titleBackColour = Color.FromArgb(235, 234, 233);
protected readonly Color titleBorderColour = Color.FromArgb(200, 198, 196);
protected readonly Color titleColour = Color.FromArgb(102, 102, 102);
2021-06-01 08:15:08 +00:00
protected readonly int titleBarHeight = 33;
protected readonly bool showTitleBarLine = true;
2020-11-08 23:24:40 +00:00
protected Font titleFont = null;
protected int titleFontTop = 0;
2021-06-01 08:15:08 +00:00
protected readonly Size titleBarIconSize = new Size(48, 32);
protected readonly int titleBarIconMargin = 0;
protected readonly int titleBarIconMarginRight = 0;
protected Image appIcon = null;
protected const int appIconLeft = 12;
protected const int appIconRight = 6;
protected int appIconTop = 0;
2020-11-08 23:24:40 +00:00
protected bool isDragging = false;
protected Point startPosition = new Point();
protected Point startWindowSize = new Point();
2021-06-01 08:15:08 +00:00
protected bool enableMinimise { get; set; } = true;
protected bool enableMaximise { get; set; } = true;
protected bool enableClose { get; set; } = true;
protected bool closeOnMinimise { get; set; } = false;
2020-11-08 23:24:40 +00:00
2021-06-01 08:15:08 +00:00
protected bool isBusy = false;
2020-11-08 23:24:40 +00:00
private IContainer components;
2020-10-21 01:17:24 +00:00
public TForm() : base()
2020-03-28 02:54:08 +00:00
{
2020-05-17 03:31:25 +00:00
InitializeComponent();
2020-03-28 02:54:08 +00:00
if (!this.DesignMode)
{
this.FormBorderStyle = FormBorderStyle.None;
this.StartPosition = FormStartPosition.Manual;
}
2020-05-18 23:17:22 +00:00
2020-11-08 23:24:40 +00:00
this.AutoScaleMode = AutoScaleMode.None;
2020-10-20 21:42:40 +00:00
this.BackColor = backColour;
this.Padding = new Padding(0);
2020-11-08 23:24:40 +00:00
this.DoubleBuffered = true;
2020-10-20 21:42:40 +00:00
2020-11-08 23:24:40 +00:00
this.MouseDown += new MouseEventHandler(form_MouseDown);
this.MouseMove += new MouseEventHandler(form_MouseMove);
this.MouseUp += new MouseEventHandler(form_MouseUp);
this.PreviewKeyDown += new PreviewKeyDownEventHandler(form_PreviewKeyDown);
2020-03-28 02:54:08 +00:00
}
2020-05-17 03:31:25 +00:00
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
2020-11-08 23:24:40 +00:00
this.Text = Application.ProductName;
2020-11-08 23:24:40 +00:00
initialiseLoadComponent();
2020-11-08 23:24:40 +00:00
if (appIcon != null) appIconTop = (int)Math.Floor((decimal)(titleBarHeight - appIcon.Height) / 2) + borderWidth;
titleFont = new Font(this.Font.FontFamily, 10F);
titleFontTop = (int)Math.Floor((decimal)(titleBarHeight - TextRenderer.MeasureText("#", titleFont).Height) / 2) + borderWidth;
2020-05-17 03:31:25 +00:00
}
2020-05-16 12:25:59 +00:00
protected override void OnMouseClick(MouseEventArgs e)
2020-05-10 10:03:55 +00:00
{
2020-05-16 12:25:59 +00:00
base.OnMouseClick(e);
2020-03-28 02:54:08 +00:00
2020-05-17 02:24:46 +00:00
bool isLabel = ((e.Location.X >= 0) && (e.Location.X <= this.Width) && (e.Location.Y >= 0) && (e.Location.Y <= titleBarHeight));
2020-03-28 02:54:08 +00:00
2020-05-16 12:25:59 +00:00
if (e.Button == MouseButtons.Left)
{
if (isLabel)
{
// do nothing
}
else
{
// do nothing
}
}
else if (e.Button == MouseButtons.Right)
{
if (isLabel)
{
if (this.TitleContextMenuStrip != null)
{
this.TitleContextMenuStrip.Show(this, e.Location);
}
}
else
{
// do nothing
}
2020-05-10 10:03:55 +00:00
}
}
2020-03-28 02:54:08 +00:00
2020-05-17 02:24:46 +00:00
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
Rectangle area = new Rectangle(this.DisplayRectangle.X, this.DisplayRectangle.Y, (this.DisplayRectangle.Width - borderWidth), (this.DisplayRectangle.Height - borderWidth));
// border
g.DrawRectangle(new Pen(borderColour, borderWidth), area);
area.Inflate((-1 * borderWidth), (-1 * borderWidth));
2020-11-08 23:24:40 +00:00
g.FillRectangle(new SolidBrush(titleBackColour), area.X, area.Y, (area.Width + area.X), titleBarHeight);
2021-06-01 08:15:08 +00:00
if (showTitleBarLine) g.DrawLine(new Pen(titleBorderColour, 1), area.X, titleBarHeight, (area.Width + area.X), titleBarHeight);
2020-05-17 02:24:46 +00:00
2020-11-08 23:24:40 +00:00
if (!DesignMode)
{
2021-06-01 08:15:08 +00:00
if (appIcon != null) g.DrawImageUnscaled(appIcon, appIconLeft, appIconTop);
2020-05-17 02:24:46 +00:00
2021-06-01 08:15:08 +00:00
int iconPosX = borderWidth + appIconLeft + appIconRight + ((appIcon == null) ? 0 : appIcon.Width);
TextRenderer.DrawText(g, this.Text, titleFont, new Point(iconPosX, titleFontTop), titleColour);
2020-11-08 23:24:40 +00:00
}
2020-05-17 02:24:46 +00:00
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
this.Invalidate();
}
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
2021-06-01 08:15:08 +00:00
public override Color BackColor { get => base.BackColor; set => base.BackColor = backColour; }
2020-05-17 02:24:46 +00:00
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public new FormBorderStyle FormBorderStyle { get => base.FormBorderStyle; set => base.FormBorderStyle = value; }
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
2020-10-20 21:42:40 +00:00
public new Padding Padding { get => base.Padding; set => base.Padding = value; }
2020-05-17 02:24:46 +00:00
[Category("Appearance")]
public ContextMenuStrip TitleContextMenuStrip { get; set; } = null;
2020-11-08 23:24:40 +00:00
//[Browsable(false)]
//public Image AppIcon { get; set; } = null;
2020-10-21 01:17:24 +00:00
2020-11-08 23:24:40 +00:00
//[Browsable(false)]
//public bool IsMaximiseEnabled { get; set; } = false;
2020-05-16 12:25:59 +00:00
2020-11-08 23:24:40 +00:00
//protected Point DefaultLocation
//{
// get
// {
// Point newPosition = new Point(Cursor.Position.X, Cursor.Position.Y);
// newPosition.X -= (this.Width / 2);
// newPosition.Y -= (this.Height / 2);
2020-05-16 12:25:59 +00:00
2020-11-08 23:24:40 +00:00
// newPosition.X = Math.Max(newPosition.X, Screen.PrimaryScreen.WorkingArea.Left);
// newPosition.Y = Math.Max(newPosition.Y, Screen.PrimaryScreen.WorkingArea.Top);
// return newPosition;
// }
//}
2020-05-16 12:25:59 +00:00
2020-05-17 03:31:25 +00:00
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
2020-10-21 01:17:24 +00:00
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(TForm));
2020-05-17 03:31:25 +00:00
this.SuspendLayout();
2020-05-18 23:17:22 +00:00
//
2020-05-17 03:31:25 +00:00
// AForm
2020-05-18 23:17:22 +00:00
//
2020-05-17 03:31:25 +00:00
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
2021-06-01 08:15:08 +00:00
this.Name = "AForm";
2020-05-17 03:31:25 +00:00
this.ResumeLayout(false);
}
2020-11-08 23:24:40 +00:00
private void form_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
2020-11-08 23:24:40 +00:00
isDragging = true;
startPosition = e.Location;
startWindowSize = new Point(this.Width, this.Height);
}
}
2020-11-08 23:24:40 +00:00
private void form_MouseMove(object sender, MouseEventArgs e)
{
2020-11-08 23:24:40 +00:00
if (isDragging)
{
2020-11-08 23:24:40 +00:00
int x = (this.Location.X + (e.Location.X - startPosition.X));
int y = (this.Location.Y + (e.Location.Y - startPosition.Y));
2020-11-08 23:24:40 +00:00
//this.Location = validateFormLocation(x, y);
this.Location = new Point(x, y);
}
2020-11-08 23:24:40 +00:00
}
2020-10-20 21:42:40 +00:00
2020-11-08 23:24:40 +00:00
private void form_MouseUp(object sender, MouseEventArgs e)
{
isDragging = false;
}
2020-11-08 23:24:40 +00:00
private void form_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
2020-05-17 03:31:25 +00:00
{
2020-11-08 23:24:40 +00:00
if (e.KeyCode == Keys.Escape)
2020-05-17 03:31:25 +00:00
{
2020-11-08 23:24:40 +00:00
Application.Exit();
2020-05-17 03:31:25 +00:00
}
}
2020-11-08 23:24:40 +00:00
protected virtual void initialiseLoadComponent()
{
2020-11-08 23:24:40 +00:00
if (DesignMode) return;
// close
TImageBox closeBox = generateToolbarImageBox();
2021-06-01 08:15:08 +00:00
closeBox.Image = closeBox.ImageNormal = (enableClose) ? Resources.titlebar_close : Resources.titlebar_blank;
closeBox.ImageHover = (enableClose) ? Resources.titlebar_close3 : Resources.titlebar_blank;
closeBox.ImageSelected = Resources.titlebar_close3;
closeBox.MouseClick += delegate (object sender, MouseEventArgs e)
{
2021-06-01 08:15:08 +00:00
if (e.Button == MouseButtons.Left)
2020-11-08 23:24:40 +00:00
{
2021-06-01 08:15:08 +00:00
if (enableClose)
2020-11-08 23:24:40 +00:00
{
2021-06-01 08:15:08 +00:00
this.Close();
2020-11-08 23:24:40 +00:00
}
2021-06-01 08:15:08 +00:00
}
};
closeBox.Left = this.DisplayRectangle.Width - closeBox.Width - (titleBarIconMarginRight + borderWidth);
2020-11-08 23:24:40 +00:00
this.Controls.Add(closeBox);
// maximise
TImageBox maximiseBox = generateToolbarImageBox();
2021-06-01 08:15:08 +00:00
maximiseBox.Image = maximiseBox.ImageNormal = (enableMaximise) ? Resources.titlebar_maximise : Resources.titlebar_blank;
maximiseBox.ImageHover = (enableMaximise) ? Resources.titlebar_maximise5 : Resources.titlebar_blank;
maximiseBox.ImageSelected = Resources.titlebar_maximise3;
maximiseBox.MouseClick += delegate (object sender, MouseEventArgs e)
{
2021-06-01 08:15:08 +00:00
if (!(sender is TImageBox)) return;
2020-11-08 23:24:40 +00:00
2021-06-01 08:15:08 +00:00
TImageBox imageBox2 = (sender as TImageBox);
2020-11-08 23:24:40 +00:00
2021-06-01 08:15:08 +00:00
if (imageBox2 == null) return;
2020-11-08 23:24:40 +00:00
2021-06-01 08:15:08 +00:00
if (e.Button == MouseButtons.Left)
{
if (enableMaximise)
2020-11-08 23:24:40 +00:00
{
2021-06-01 08:15:08 +00:00
if (this.WindowState == FormWindowState.Maximized)
2020-11-08 23:24:40 +00:00
{
2021-06-01 08:15:08 +00:00
this.WindowState = FormWindowState.Normal;
imageBox2.Image = imageBox2.ImageNormal = Resources.titlebar_maximise;
2020-11-08 23:24:40 +00:00
}
else
{
2021-06-01 08:15:08 +00:00
this.WindowState = FormWindowState.Maximized;
imageBox2.Image = imageBox2.ImageNormal = Resources.titlebar_maximise5;
2020-11-08 23:24:40 +00:00
}
}
2021-06-01 08:15:08 +00:00
}
else if (e.Button == MouseButtons.Right)
2020-11-08 23:24:40 +00:00
{
2021-06-01 08:15:08 +00:00
this.TopMost = !this.TopMost;
2020-11-08 23:24:40 +00:00
2021-06-01 08:15:08 +00:00
if (this.TopMost)
2020-11-08 23:24:40 +00:00
{
2021-06-01 08:15:08 +00:00
imageBox2.Image = imageBox2.ImageNormal = Resources.titlebar_maximise3;
imageBox2.ImageHover = (enableMaximise) ? Resources.titlebar_maximise5 : Resources.titlebar_maximise3;
2020-11-08 23:24:40 +00:00
}
else
{
2021-06-01 08:15:08 +00:00
imageBox2.Image = imageBox2.ImageNormal = (enableMaximise) ? Resources.titlebar_maximise : Resources.titlebar_blank;
imageBox2.ImageHover = (enableMaximise) ? Resources.titlebar_maximise5 : Resources.titlebar_blank;
2020-11-08 23:24:40 +00:00
}
2021-06-01 08:15:08 +00:00
}
};
maximiseBox.Left = closeBox.Left - maximiseBox.Width - titleBarIconMargin;
this.Controls.Add(maximiseBox);
// minimise
TImageBox minimiseBox = generateToolbarImageBox();
minimiseBox.Image = minimiseBox.ImageNormal = (enableMinimise) ? Resources.titlebar_minimise : Resources.titlebar_blank;
minimiseBox.ImageHover = (enableMinimise) ? Resources.titlebar_minimise5 : Resources.titlebar_blank;
//minimiseBox.ImageSelected = null;
minimiseBox.MouseClick += delegate (object sender, MouseEventArgs e)
2020-11-08 23:24:40 +00:00
{
2021-06-01 08:15:08 +00:00
if (!enableMinimise) return;
if (e.Button != MouseButtons.Left) return;
2020-11-08 23:24:40 +00:00
2021-06-01 08:15:08 +00:00
if (closeOnMinimise)
{
this.Close();
}
else
{
this.WindowState = FormWindowState.Minimized;
}
};
2020-11-08 23:24:40 +00:00
minimiseBox.Left = maximiseBox.Left - minimiseBox.Width - titleBarIconMargin;
this.Controls.Add(minimiseBox);
// resize
UserControl uc1 = new UserControl()
{
Anchor = (AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right),
Height = resizeBorderWidth,
Width = this.DisplayRectangle.Width - resizeBorderWidth,
Left = 0,
Top = this.DisplayRectangle.Height - resizeBorderWidth,
BackColor = Color.Transparent,
Cursor = Cursors.SizeNS
};
uc1.MouseDown += form_MouseDown;
uc1.MouseUp += form_MouseUp;
uc1.MouseMove += delegate (object sender, MouseEventArgs e)
{
if (isDragging)
{
this.Size = new Size(startWindowSize.X, e.Y - startPosition.Y + this.Height);
}
};
uc1.BringToFront();
this.Controls.Add(uc1);
2020-11-08 23:24:40 +00:00
UserControl uc2 = new UserControl()
{
Anchor = (AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom),
Height = this.DisplayRectangle.Height - resizeBorderWidth,
Width = resizeBorderWidth,
Left = this.DisplayRectangle.Width - resizeBorderWidth,
Top = 0,
BackColor = Color.Transparent,
Cursor = Cursors.SizeWE
};
uc2.MouseDown += form_MouseDown;
uc2.MouseUp += form_MouseUp;
uc2.MouseMove += delegate (object sender, MouseEventArgs e)
{
if (isDragging)
{
this.Size = new Size(e.X - startPosition.X + this.Width, startWindowSize.Y);
}
};
uc2.BringToFront();
this.Controls.Add(uc2);
UserControl uc3 = new UserControl()
{
Anchor = (AnchorStyles.Bottom | AnchorStyles.Right),
Height = resizeBorderWidth,
Width = resizeBorderWidth,
Left = this.DisplayRectangle.Width - resizeBorderWidth,
Top = this.DisplayRectangle.Height - resizeBorderWidth,
BackColor = Color.Transparent,
Cursor = Cursors.SizeNWSE
};
uc3.MouseDown += form_MouseDown;
uc3.MouseUp += form_MouseUp;
uc3.MouseMove += delegate (object sender, MouseEventArgs e)
{
if (isDragging)
{
this.Size = new Size((e.X - startPosition.X + this.Width), (e.Y - startPosition.Y + this.Height));
}
};
uc3.BringToFront();
this.Controls.Add(uc3);
}
protected TImageBox generateToolbarImageBox()
{
TImageBox imageBox = new TImageBox();
2021-06-01 08:15:08 +00:00
//imageBox.BackColor = Color.Transparent;
imageBox.BackColorHover = imageBox.BackColorSelected = Color.FromArgb(220, 220, 220);
imageBox.BackgroundImageLayout = ImageLayout.Center;
2020-11-08 23:24:40 +00:00
imageBox.ErrorImage = null;
//imageBox.Image = Resources.close;
//imageBox.ImageHover = Resources.close2;
//imageBox.ImageNormal = Resources.close;
imageBox.ImageSelected = null;
imageBox.IsSelected = false;
//closeBox.Location = new System.Drawing.Point(169, 12);
2021-06-01 08:15:08 +00:00
imageBox.Size = titleBarIconSize;
2020-11-08 23:24:40 +00:00
imageBox.SizeMode = PictureBoxSizeMode.CenterImage;
//imageBox.MouseClick += new MouseEventHandler(closeBox_MouseClick);
//imageBox.Left = this.DisplayRectangle.Width - imageBox.Width - 17;
2021-06-01 08:15:08 +00:00
imageBox.Top = (int)Math.Floor((decimal) (titleBarHeight - titleBarIconSize.Height) / 2) + borderWidth;
2020-11-08 23:24:40 +00:00
imageBox.Anchor = (AnchorStyles.Top | AnchorStyles.Right);
2021-06-01 08:15:08 +00:00
imageBox.Padding = new Padding(0);
2020-11-08 23:24:40 +00:00
return imageBox;
}
2020-11-08 23:24:40 +00:00
//private void exitToolStripMenuItem_Click(object sender, EventArgs e) => this.Close();
//private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
//{
// if (e.Button == MouseButtons.Left)
// {
// this.Visible = !this.Visible;
// }
// //notifyIcon1.Visible = !this.Visible;
//}
2020-03-28 02:54:08 +00:00
}
2020-05-10 10:03:55 +00:00
}