using System; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; namespace AppLauncher.Windows.Forms { public partial class TitlePanel : UserControl { private bool isDragging = false; private Point windowOffset = new Point(); public TitlePanel() { InitializeComponent(); } protected override void OnLoad(EventArgs e) { base.OnLoad(e); this.BackColor = Color.Black; } 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; label1.MouseDown += parentForm_MouseDown; label1.MouseUp += parentForm_MouseUp; label1.MouseMove += parentForm_MouseMove; } } } [Category("Appearance"), Browsable(true)] public string TitleText { get => label1.Text; set => label1.Text = value; } [Category("Appearance"), Browsable(true)] public bool LabelVisible { get => label1.Visible; set => label1.Visible = value; } public ContextMenuStrip MainMenu { get; set; } = null; public MainForm MainForm { get { if (this.Parent == null) { return null; } if (this.Parent.GetType() != typeof(MainForm)) { return null; } return (MainForm)this.Parent; } } protected void parentForm_MouseDown(object sender, MouseEventArgs e) { if (e.Button != MouseButtons.Left) { return; } isDragging = true; windowOffset = e.Location; } protected void parentForm_MouseUp(object sender, MouseEventArgs e) { isDragging = false; } protected void parentForm_MouseMove(object sender, MouseEventArgs e) { if (isDragging) { Point pos = this.PointToScreen(e.Location); int y = (pos.Y - windowOffset.Y); int x = (pos.X - windowOffset.X); this.Parent.Location = new Point(x, y); //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); } } private void pictureBox1_MouseUp(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Right) { if (this.MainMenu != null) { this.MainMenu.Show(this.PointToScreen(e.Location)); } } } private async void pictureBox1_DoubleClick(object sender, EventArgs e) { if (this.MainForm == null) { return; } await this.MainForm.ToggleSize(); } //protected Point validateFormLocation(int x, int y) //{ // Screen activeScreen = Screen.FromControl(this); // int maxX = (activeScreen.WorkingArea.X + activeScreen.WorkingArea.Width) - this.Width; // int maxY = (activeScreen.WorkingArea.Y + activeScreen.WorkingArea.Height) - this.Height; // if (x < activeScreen.WorkingArea.X) x = activeScreen.WorkingArea.X; // if (y < activeScreen.WorkingArea.Y) y = activeScreen.WorkingArea.Y; // if (x > maxX) x = maxX; // if (y > maxY) y = maxY; // //y = activeScreen.WorkingArea.Y; // return new Point(x, y); //} } }