Click here to Skip to main content
15,662,405 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys. I am working on a Project in c# and need to get Mouse Click Coordinates in textbox . Click can be made outside the Screen. I have a code in Internet found but cann't make coordinates bring out in textbox. Here is code.. I was trying to write "" in HookCallBack but it didn't work. Has any one Idea how to do that:
namespace Mouse_Hook
    public partial class Form1 : Form
        public Form1()

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr SetWindowsHookEx(int idHook,
            LowLevelMouseProc lpfn, IntPtr hMod, uint dwThreadId);

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

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

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

        private delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam);
        private static LowLevelMouseProc _proc = HookCallback;

        private const int WH_MOUSE_LL = 14;
        private static IntPtr _hookID = IntPtr.Zero;

        private static IntPtr HookCallback(
        int nCode, IntPtr wParam, IntPtr lParam)
            if (nCode >= 0 &&
                MouseMessages.WM_LBUTTONDOWN == (MouseMessages)wParam)
                MSLLHOOKSTRUCT hookStruct = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));
                Console.WriteLine( + ", " +; 
            return CallNextHookEx(_hookID, nCode, wParam, lParam);

        private static IntPtr SetHook(LowLevelMouseProc proc)
            using (Process curProcess = Process.GetCurrentProcess())
            using (ProcessModule curModule = curProcess.MainModule)
                return SetWindowsHookEx(WH_MOUSE_LL, proc,
                    GetModuleHandle(curModule.ModuleName), 0);

        private struct POINT
            public int x;
            public int y;

        private struct MSLLHOOKSTRUCT
            public POINT pt;
            public uint mouseData;
            public uint flags;
            public uint time;
            public IntPtr dwExtraInfo;

        private enum MouseMessages
            WM_LBUTTONDOWN = 0x0201,
            WM_LBUTTONUP = 0x0202,
            WM_MOUSEMOVE = 0x0200,
            WM_MOUSEWHEEL = 0x020A,
            WM_RBUTTONDOWN = 0x0204,
            WM_RBUTTONUP = 0x0205

        private void Form1_Load(object sender, EventArgs e)
            _hookID = SetHook(_proc);

Updated 16-Sep-15 0:01am

1 solution

An easier way to do this would be to add a WndProc method to your form as described in[^], and just handle the mouse messages that are sent to your Window.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900