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/Windows/Forms/AForm.cs

136 lines
4.1 KiB
C#
Raw Normal View History

2020-03-28 02:54: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;
namespace AppLauncher.Windows.Forms
{
public class AForm : Form
{
2020-05-16 12:25:59 +00:00
protected readonly int titleHeight = 56;
protected bool isDragging = false;
protected Point startPosition = new Point();
2020-03-28 02:54:08 +00:00
public AForm() : base()
{
if (!this.DesignMode)
{
this.FormBorderStyle = FormBorderStyle.None;
this.StartPosition = FormStartPosition.Manual;
}
}
protected override void OnPaint(PaintEventArgs e)
2020-03-28 02:54:08 +00:00
{
base.OnPaint(e);
2020-03-28 02:54:08 +00:00
Graphics g = e.Graphics;
Color borderColour = Color.FromArgb(232, 231, 236);
Color menubarColour = Color.FromArgb(237, 240, 247);
Rectangle area = new Rectangle(this.DisplayRectangle.X, this.DisplayRectangle.Y, (this.DisplayRectangle.Width - 1), (this.DisplayRectangle.Height - 1));
// border
g.DrawRectangle(new Pen(borderColour, 1), area);
area.Inflate(-1, -1);
2020-05-16 12:25:59 +00:00
g.FillRectangle(new SolidBrush(menubarColour), area.X, area.Y, (area.Width + area.X), titleHeight);
g.DrawLine(new Pen(borderColour, 1), area.X, (titleHeight + 1), (area.Width + area.X), (titleHeight + 1));
g.DrawImageUnscaled(Properties.Resources.app_icon_24, 17, 17);
TextRenderer.DrawText(e.Graphics, "Launcher", new Font(this.Font.FontFamily, 14F), new Point(58, 17), Color.FromArgb(156, 158, 171));
2020-03-28 02:54:08 +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-16 12:25:59 +00:00
bool isLabel = ((e.Location.X >= 0) && (e.Location.X <= this.Width) && (e.Location.Y >= 0) && (e.Location.Y <= titleHeight));
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-16 12:25:59 +00:00
protected override void OnMouseDown(MouseEventArgs e)
{
2020-05-16 12:25:59 +00:00
base.OnMouseDown(e);
if (e.Button != MouseButtons.Left)
{
return;
}
isDragging = true;
startPosition = e.Location;
}
2020-05-16 12:25:59 +00:00
protected override void OnMouseMove(MouseEventArgs e)
{
2020-05-16 12:25:59 +00:00
base.OnMouseMove(e);
if (isDragging)
{
int x = (this.Location.X + (e.Location.X - startPosition.X));
int y = (this.Location.Y + (e.Location.Y - startPosition.Y));
//this.Location = validateFormLocation(x, y);
this.Location = new Point(x, y);
}
}
2020-05-16 12:25:59 +00:00
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
isDragging = false;
}
protected Point DefaultLocation
{
get
{
Point newPosition = new Point(Cursor.Position.X, Cursor.Position.Y);
newPosition.X -= (this.Width / 2);
newPosition.Y -= (this.Height / 2);
newPosition.X = Math.Max(newPosition.X, Screen.PrimaryScreen.WorkingArea.Left);
newPosition.Y = Math.Max(newPosition.Y, Screen.PrimaryScreen.WorkingArea.Top);
return newPosition;
}
}
public override Color BackColor { get => base.BackColor; set => base.BackColor = Color.FromArgb(254, 254, 254); }
[Category("Appearance")]
public ContextMenuStrip TitleContextMenuStrip { get; set; } = null;
2020-03-28 02:54:08 +00:00
}
2020-05-10 10:03:55 +00:00
}