Newer
Older
Launcheroid / V3 / FItem.cs
@Ivan Dominguez Ivan Dominguez 11 days ago 3 KB Lancheroid 3 to 4
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Launcheroid
{
    public partial class FItem : Form
    {
        private FItem()
        {
            InitializeComponent();
        }

        private static bool Open(LRD3.Item item, string import)
        {
            bool saved;
            using (FItem f = new FItem())
            {
                f.LoadItem(item);
                f.LinkToPath(import);
                saved = f.ShowDialog() == DialogResult.OK;
                if (saved)
                    f.SaveItem(item);
            }
            return saved;
        }

        public static bool Execute(LRD3.Item item)
        {
            return Open(item, null);
        }

        public static LRD3.Item Import(string path)
        {
            LRD3.Item i = new LRD3.Item();
            if (Open(i, path))
                return i;
            return null;
        }

        private void LoadItem(LRD3.Item item)
        {
            tbName.Text = Text = item.Name;
            tbPath.Text = item.Path;
            tbDirectory.Text = item.InitialFolder;
            tbArguments.Text = item.Arguments;
            pbIcon.Image = item.Image;
        }

        private void SaveItem(LRD3.Item item)
        {
            item.Name = tbName.Text;
            item.Path = tbPath.Text;
            item.InitialFolder = tbDirectory.Text;
            item.Arguments = tbArguments.Text;
            item.Image = pbIcon.Image;
        }

        private void LinkToPath(string path)
        {
            // TODO if it is a link (.lnk) open link and recover information
            tbName.Text = Text = Path.GetFileNameWithoutExtension(path);
            tbPath.Text = path;
            tbDirectory.Text = Path.GetDirectoryName(path);
            try
            {
                pbIcon.Image = Icon.ExtractAssociatedIcon(path).ToBitmap();
            }
            catch
            {
                pbIcon.Image = null;
            }
        }

        private void BtOk_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
        }

        private void TbName_TextChanged(object sender, EventArgs e)
        {
            Text = tbName.Text;
        }

        private void OpenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Image i = FImageLoad.Open();
            if (i != null)
                pbIcon.Image = i;
        }

        private void DeleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pbIcon.Image = null;
        }

        private void FItem_DragEnter(object sender, DragEventArgs e)
        {
            if (!e.Data.GetDataPresent(DataFormats.FileDrop))
                return;
            e.Effect = DragDropEffects.Copy;
        }

        private void FItem_DragDrop(object sender, DragEventArgs e)
        {
            string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
            if (files.Length != 1)
                return;
            Point cp = PointToClient(new Point(e.X, e.Y));
            Control dropTarget = GetChildAtPoint(cp);
            if (dropTarget == pbIcon)
                pbIcon.Image = FImageLoad.Open(files[0]);
            else
                LinkToPath(files[0]);
        }

        private void BtPathGet_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog ofd = new OpenFileDialog())
            {
                ofd.Filter = "Executables|*.exe;*.com;*.lnk|All files|*.*";
                if (ofd.ShowDialog() == DialogResult.OK)
                    LinkToPath(ofd.FileName);
            }
        }

        private void ContextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {
            deleteToolStripMenuItem.Enabled = pbIcon.Image != null;
        }
    }
}