TEST - formatting your own code blocks Example2

  // Your C# code here

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.IO;
using System.Linq;
using System.Collections.Generic;

namespace MafutaGameLauncher
{
    public partial class Form1 : Form
    {
        [DllImport("user32.dll")]
        public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);

        [DllImport("user32.dll")]
        public static extern bool UnregisterHotKey(IntPtr hWnd, int id);

        private const int HOTKEY_ID = 1;
        private const int MOD_CTRL = 0x0002;
        private const int VK_G = 0x47;

        private Dictionary gamePaths = new Dictionary();
        
        public Form1()
        {
            InitializeComponent();

            this.FormClosing += Form1_FormClosing;

            RegisterHotKey(this.Handle, HOTKEY_ID, MOD_CTRL, VK_G);

            // Add games to the list
            PopulateGameList();
        }

        private void PopulateGameList()
        {
            string gameDirectory = @"D:\GAMES";
            string[] files = Directory.GetFiles(gameDirectory, "*.exe", SearchOption.AllDirectories);

            foreach (string file in files)
            {
                string fileName = Path.GetFileName(file);
                if (!fileName.StartsWith("Unity"))
                {
                    listBox1.Items.Add(fileName);
                    gamePaths[fileName] = file;
                }
            }

            listBox1.Sorted = true;
        }

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == 0x0312 && m.WParam.ToInt32() == HOTKEY_ID)
            {
                // Toggle visibility when the hotkey is pressed
                this.Visible = !this.Visible;
            }
            base.WndProc(ref m);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex >= 0)
            {
                string? selectedGame = listBox1.SelectedItem?.ToString();
                if (selectedGame != null && gamePaths.TryGetValue(selectedGame, out string? fullPath))
                {
                    Process.Start(fullPath);
                }
            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            try
            {
                System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo
                {
                    FileName = "https://mafuta.itch.io/",
                    UseShellExecute = true
                });
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error opening the URL: " + ex.Message);
            }
        }

        private void Form1_FormClosing(object? sender, FormClosingEventArgs e)
        {
            UnregisterHotKey(this.Handle, HOTKEY_ID);
        }

        private void exitButton_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
  

Comments

Popular posts from this blog

Clone Command Bar Button in Model Driven App: Order & OrderDetails

Model-Driven Apps: Modern Command Bar Customization

Knowledge shared is power squared!