using AppLauncher.Models; using System; using System.ComponentModel; using System.Diagnostics; using System.Drawing; using System.IO; using System.Windows.Forms; namespace AppLauncher.Windows.Forms { public partial class TTilePanel : AUserControl { protected bool isDragging = false; protected Point startPosition = new Point(); protected TileModel modelInfo = new TileModel(); public TTilePanel() : base() { InitializeComponent(); this.BackColor = Color.FromArgb(250, 250, 250); this.ContextMenuStrip = contextMenuStrip1; label1.ForeColor = Color.FromArgb(99, 105, 119); label1.Font = new Font(this.Font.FontFamily, 8.25F); this.MouseDown += panel_MouseDown; this.MouseUp += panel_MouseUp; this.MouseMove += panel_MouseMove; pictureBox1.MouseDown += panel_MouseDown; pictureBox1.MouseUp += panel_MouseUp; pictureBox1.MouseMove += panel_MouseMove; label1.MouseDown += panel_MouseDown; label1.MouseUp += panel_MouseUp; label1.MouseMove += panel_MouseMove; } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); Graphics g = e.Graphics; g.DrawRectangle(new Pen(Color.FromArgb(204, 206, 218), 1), new Rectangle(this.DisplayRectangle.X, this.DisplayRectangle.Y, (this.DisplayRectangle.Width - 1), (this.DisplayRectangle.Height - 1))); } [Category("Appearance"), Browsable(true)] public string Title { get => label1.Text; protected set => label1.Text = value; } [Category("Appearance"), Browsable(true)] public Image Image { get => pictureBox1.BackgroundImage; protected set => pictureBox1.BackgroundImage = value; } [Browsable(false)] public TileModel ModelInfo => modelInfo; public TTilePanelLayout PanelContainer { get { if (this.Parent == null) { return null; } if (this.Parent.GetType() != typeof(TTilePanelLayout)) { return null; } return (TTilePanelLayout)this.Parent; } } public void LoadInfo(TileModel model) { this.modelInfo = model; this.Title = model.Title; if (this.modelInfo.IsGroup) { this.Image = Properties.Resources.folder_32; } else { this.Image = model.Icon; if (this.Image == null) { if (File.Exists(model.ProcessFilename)) { try { this.Image = Icon.ExtractAssociatedIcon(model.ProcessFilename)?.ToBitmap(); } catch { } } } } toolTip1.SetToolTip(this, this.Title); toolTip1.SetToolTip(pictureBox1, this.Title); toolTip1.SetToolTip(label1, this.Title); } private void panel_MouseDown(object sender, MouseEventArgs e) { TTilePanelLayout container = this.PanelContainer; if (container == null) { return; } if (e.Button != MouseButtons.Right) { return; } this.BringToFront(); isDragging = true; startPosition = e.Location; } private void panel_MouseUp(object sender, MouseEventArgs e) { isDragging = false; } private void panel_MouseMove(object sender, MouseEventArgs e) { if (isDragging) { TTilePanelLayout layoutPanel = this.PanelContainer; if (layoutPanel == null) { return; } int x = (this.Location.X + (e.Location.X - startPosition.X)); int y = (this.Location.Y + (e.Location.Y - startPosition.Y)); layoutPanel.MoveTile(this, x, y); } } private void panel_MouseClick(object sender, MouseEventArgs e) { if (e.Button != MouseButtons.Left) { return; } if (this.ModelInfo == null) { return; } if (this.ModelInfo.IsGroup) { } else { panel_MouseDoubleClick(sender, e); } } private void panel_MouseDoubleClick(object sender, MouseEventArgs e) { if (e.Button != MouseButtons.Left) { return; } if (this.ModelInfo == null) { return; } if (this.ModelInfo.IsGroup) { return; } if (string.IsNullOrWhiteSpace(this.ModelInfo.ProcessFilename)) { return; } if (!File.Exists(this.ModelInfo.ProcessFilename)) { return; } ProcessStartInfo p = new ProcessStartInfo(); p.FileName = this.ModelInfo.ProcessFilename; p.WindowStyle = this.ModelInfo.ProcessWindowStyle; if (!string.IsNullOrWhiteSpace(this.ModelInfo.ProcessArgument)) p.Arguments = this.ModelInfo.ProcessArgument; if (!string.IsNullOrWhiteSpace(this.ModelInfo.ProcessWorkingDirectory)) p.WorkingDirectory = this.ModelInfo.ProcessWorkingDirectory; if (this.ModelInfo.ProcessAsAdmin) p.Verb = "runas"; try { Process.Start(p); } catch (Exception exc) { MessageBox.Show(exc.Message); } } private void editToolStripMenuItem_Click(object sender, EventArgs e) { if (this.ModelInfo.IsGroup) { EditListTileForm.ShowDialog(this); } else { EditTileForm.ShowDialog(this); } } private void removeToolStripMenuItem_Click(object sender, EventArgs e) { if (this.PanelContainer == null) { return; } this.PanelContainer.Remove(this); } } }