C# Mouse dinleyicisi nasıl yapabilirim?

700586

Decapat
Katılım
15 Ağustos 2023
Mesajlar
86
Windows form C# uyugulması üzerinden öreneğin xMousebutton1 tuşuna basıldığında ekran görüntüsü almak istiyorum.
Fakat bunu global bir şekilde yapması lazım form üzerinden değil.
Uygulamanın arkaplandada olsa çalışmasını istiyorum örnek birkaç bir şeyler gösterebilir misiniz?
 
C#:
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace ekranresim
{
    public partial class Form1 : Form
    {
        [DllImport("user32.dll", SetLastError = true)]
        private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);

        [DllImport("user32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool UnhookWindowsHookEx(IntPtr hhk);

        [DllImport("user32.dll", SetLastError = true)]
        private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);

        [DllImport("kernel32.dll", SetLastError = true)]
        private static extern IntPtr GetModuleHandle(string lpModuleName);

        private const int WH_KEYBOARD_LL = 13;
        private const int WM_KEYDOWN = 0x0100;
        private const int VK_XBUTTON1 = 0x05;


        private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
        private event Action XMouseButton1Pressed;

        private IntPtr hookId = IntPtr.Zero;

        public Form1()
        {
            InitializeComponent();

            this.Load += (s, e) => SetHook();
            this.FormClosing += (s, e) => Unhook();
            
            XMouseButton1Pressed += () =>
            {
                CaptureScreen();
            };
        }

        private void SetHook()
        {
            using (ProcessModule module = Process.GetCurrentProcess().MainModule)
            {
                hookId = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProcCallback, GetModuleHandle(module.ModuleName), 0);
            }
        }

        private void Unhook()
        {
            UnhookWindowsHookEx(hookId);
        }

        private IntPtr LowLevelKeyboardProcCallback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
            {
                int vkCode = Marshal.ReadInt32(lParam);
                if (vkCode == VK_XBUTTON1)
                {
                    XMouseButton1Pressed?.Invoke();
                }
            }

            return CallNextHookEx(hookId, nCode, wParam, lParam);
        }

        private void CaptureScreen()
        {
            using (Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height))
            {
                using (Graphics g = Graphics.FromImage(bitmap))
                {
                    g.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, bitmap.Size);
                }
                bitmap.Save("ekranresim.png");
            }
        }
    }
}
 
Bu siteyi kullanmak için çerezler gereklidir. Siteyi kullanmaya devam etmek için çerezleri kabul etmelisiniz. Daha Fazlasını Öğren.…