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/TitlePanel.cs

121 lines
3.3 KiB
C#
Raw Normal View History

2020-03-29 14:28:38 +00:00
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace AppLauncher.Windows.Forms
{
public partial class TitlePanel : UserControl
{
2020-04-05 00:32:49 +00:00
private bool isDragging = false;
2020-03-29 14:28:38 +00:00
private Point windowOffset = new Point();
public TitlePanel()
{
InitializeComponent();
}
2020-04-12 00:12:21 +00:00
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.BackColor = Color.Black;
}
2020-03-29 14:28:38 +00:00
protected override void OnParentBindingContextChanged(EventArgs e)
{
base.OnParentBindingContextChanged(e);
if (this.Parent != null)
{
if (this.Parent.GetType() == typeof(MainForm))
{
pictureBox1.MouseDown += parentForm_MouseDown;
pictureBox1.MouseUp += parentForm_MouseUp;
pictureBox1.MouseMove += parentForm_MouseMove;
2020-04-12 00:12:21 +00:00
label1.MouseDown += parentForm_MouseDown;
label1.MouseUp += parentForm_MouseUp;
label1.MouseMove += parentForm_MouseMove;
2020-03-29 14:28:38 +00:00
}
}
}
[Category("Appearance"), Browsable(true)]
2020-04-12 00:12:21 +00:00
public string TitleText { get => label1.Text; set => label1.Text = value; }
[Category("Appearance"), Browsable(true)]
public bool LabelVisible { get => label1.Visible; set => label1.Visible = value; }
2020-03-29 14:28:38 +00:00
2020-04-11 17:43:20 +00:00
public ContextMenuStrip MainMenu { get; set; } = null;
2020-04-12 00:12:21 +00:00
public MainForm MainForm
{
get
{
if (this.Parent == null)
{
return null;
}
if (this.Parent.GetType() != typeof(MainForm))
{
return null;
}
return (MainForm)this.Parent;
}
}
2020-03-29 14:28:38 +00:00
protected void parentForm_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left)
{
return;
}
2020-04-05 00:32:49 +00:00
isDragging = true;
2020-03-29 14:28:38 +00:00
windowOffset = e.Location;
}
protected void parentForm_MouseUp(object sender, MouseEventArgs e)
{
2020-04-05 00:32:49 +00:00
isDragging = false;
2020-03-29 14:28:38 +00:00
}
protected void parentForm_MouseMove(object sender, MouseEventArgs e)
{
2020-04-05 00:32:49 +00:00
if (isDragging)
2020-03-29 14:28:38 +00:00
{
Point pos = this.PointToScreen(e.Location);
int y = Math.Max((pos.Y - windowOffset.Y), Screen.PrimaryScreen.WorkingArea.Y);
y = Math.Min(y, (Screen.PrimaryScreen.WorkingArea.Y + Screen.PrimaryScreen.WorkingArea.Height) - this.Height);
this.Parent.Location = new Point(Screen.PrimaryScreen.WorkingArea.X, y);
}
}
2020-04-11 17:43:20 +00:00
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
if (this.MainMenu != null)
{
this.MainMenu.Show(this.PointToScreen(e.Location));
}
}
}
2020-04-12 00:12:21 +00:00
private async void pictureBox1_DoubleClick(object sender, EventArgs e)
{
if (this.MainForm == null)
{
return;
}
await this.MainForm.ToggleSize();
}
2020-03-29 14:28:38 +00:00
}
}