How to get MouseClickcoordiantes outside the Form

Hello guys, I am beginner in .Net Programming and need Help. I need a code in c#, that will give me coordinates of a Mouse Click outside the Form in TextBox and then to simulate Mouse Click on that coordinates. Its easy inside the Form but i don't know how to do that outside the Form. I have found articles on msdn but cannot understand how to use it.
dave_bulacAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
That's not an easy task.  To best help you, how and why are you going to use this code?...there may be other ways to accomplish your goal.

Without more info, you might need:
(1) Low Level Mouse Hook via WH_MOUSE_LL to detect a click anywhere on the screen.
(2) SendInput() API to click the mouse somewhere on the screen.
dave_bulacAuthor Commented:
I have written a code for this but it's not working. it says : "Unable to find an entry point named 'SetcursorPos' in DLL 'user32.dll'."    can someone Help me with this ?

code :
[DllImport("user32.dll",EntryPoint="SetcursorPos")]
        private static extern void SetCursorPos(int x, int y);

        [DllImport("user32.dll")]
        public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

        private const int MOUSE_LEFTDOWN = 0x0202;
        private const int MOUSE_LEFTUP = 0x0203;
        public void Clicker(int x, int y)
        {
            SetCursorPos(x, y);
            this.Refresh();
            Application.DoEvents();
            mouse_event(MOUSE_LEFTDOWN,x,y,0,0);
            mouse_event(MOUSE_LEFTUP,x,y,0,0);
        }
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
You don't need an API to set the mouse location, just use the built-in .Net Cursor.Position property:
Cursor.Position = new Point(x, y);

Open in new window

dave_bulacAuthor Commented:
You are right, thanks for Help. But what about my main Question ? I have read about low level Mouse hooks but didn'y understand how to get coordinates. I now how to Install Hook, Uninstall and sen information to another Hook. But How to get coordinates i donn't know.  

No One knows book or link where will be detailed explained this task ?
it_saigeDeveloperCommented:
Here is an example program that provides the coordinates:
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace EE_Q28713090
{
	class Program
	{
		static NativeMethods.LowLevelMouseProcDelegate fProcDelegate;
		static IntPtr fMouseHook;

		static void Main(string[] args)
		{
			Hook();
			Application.Run();
			Unhook();
		}

		/// <summary>Get the name of the executing assembly.  ex : If this function currently executed in this library "C:\MyProgram\MyLibrary.dll",
		/// this function will return "MyLibrary.dll"</summary>
		/// <returns>Name of executing assembly</returns>
		public static string GetExecutingAssembly()
		{
			try
			{
				// Get current assembly with filename
				var currentAssembly = Assembly.GetEntryAssembly().Location;
				// Initialize length
				var nbCharToKeep = currentAssembly.Length;
				// Decrease length until an escape caracter is found
				while (currentAssembly[nbCharToKeep - 1] != '\\')
					nbCharToKeep -= 1;

				// Return the dir including the last escape
				return currentAssembly.Substring(nbCharToKeep, currentAssembly.Length - nbCharToKeep);
			}
			catch (Exception ex)
			{
				Debug.WriteLine(string.Format("{0}: Exception reported in {1} - {2} [{3}]", MethodBase.GetCurrentMethod().Name, ex.Source, ex, ex.Message));
				return string.Empty;
			}
		}

		/// <summary>Get the current path of the executing assembly.  ex : If this function currently executed in this library "C:\MyProgram\MyLibrary.dll",
		/// this function will return "C:\MyProgram\"</summary>
		/// <returns>Current path of the executing assembly</returns>
		public static string GetExecutingAssemblyDirectory()
		{
			string result = string.Empty;
			try
			{
				result = new FileInfo(Assembly.GetEntryAssembly().Location).DirectoryName;
				if (result[result.Length - 1] != '\\')
					result = string.Format(@"{0}\", result);
			}
			catch (Exception ex)
			{
				Debug.WriteLine(string.Format("{0}: Exception reported in {1} - {2} [{3}]", MethodBase.GetCurrentMethod().Name, ex.Source, ex, ex.Message));
				result = string.Empty;
			}
			return result;
		}

		private static void Hook()
		{
			if (fProcDelegate != null)
				throw new InvalidOperationException("Cannot hook more than once");

			using (Process current = Process.GetCurrentProcess())
			using (ProcessModule module = current.MainModule)
			{
				fProcDelegate = NativeMethods.LowLevelMouseProc;
				fMouseHook = NativeMethods.SetWindowsHookEx(NativeMethods.WH_MOUSE_LL, fProcDelegate, NativeMethods.GetModuleHandle(module.ModuleName), 0);
			}

			if (fMouseHook == IntPtr.Zero)
				throw new Win32Exception();
		}

		private static void Unhook()
		{
			if (fProcDelegate == null)
				return;

			bool ok = NativeMethods.UnhookWindowsHookEx(fMouseHook);
			if (!ok)
				throw new Win32Exception();

			fProcDelegate = null;
		}
	}

	static class NativeMethods
	{
		public const int WH_MOUSE_LL = 14;

		/// <summary>Retrieves a module handle for the specified module. The module must have been loaded by the calling process.</summary>
		/// <param name="lpModuleName">The name of the loaded module (either a .dll or .exe file). If the file name extension is omitted, the default library extension .dll is appended. The file name string can include a trailing point character (.) to indicate that the module name has no extension. The string does not have to specify a path. When specifying a path, be sure to use backslashes (\), not forward slashes (/). The name is compared (case independently) to the names of modules currently mapped into the address space of the calling process. If this parameter is NULL, GetModuleHandle returns a handle to the file used to create the calling process (.exe file).</param>
		/// <returns>If the function succeeds, the return value is a handle to the specified module. If the function fails, the return value is NULL. To get extended error information, call GetLastError.</returns>
		/// <remarks>The GetModuleHandle function does not retrieve handles for modules that were loaded using the LOAD_LIBRARY_AS_DATAFILE flag.</remarks>
		[DllImport("kernel32", CharSet = CharSet.Auto, SetLastError = true)]
		public static extern IntPtr GetModuleHandle(string lpModuleName);

		/// <summary>Installs an application-defined hook procedure into a hook chain.</summary>
		/// <param name="idHook">The type of hook procedure to be installed.</param>
		/// <param name="lpfn">A pointer to the hook procedure. If the dwThreadId parameter is zero or specifies the identifier of a thread created by a different process, the lpfn parameter must point to a hook procedure in a DLL. Otherwise, lpfn can point to a hook procedure in the code associated with the current process.</param>
		/// <param name="hMod">A handle to the DLL containing the hook procedure pointed to by the lpfn parameter. The hMod parameter must be set to NULL if the dwThreadId parameter specifies a thread created by the current process and if the hook procedure is within the code associated with the current process.</param>
		/// <param name="dwThreadId">The identifier of the thread with which the hook procedure is to be associated. For desktop apps, if this parameter is zero, the hook procedure is associated with all existing threads running in the same desktop as the calling thread.</param>
		/// <returns>If the function succeeds, the return value is the handle to the hook procedure. If the function fails, the return value is NULL. To get extended error information, call GetLastError.</returns>
		/// <remarks>You would install a hook procedure to monitor the system for certain types of events. These events are associated either with a specific thread or with all threads in the same desktop as the calling thread.</remarks>
		[DllImport("user32", EntryPoint = "SetWindowsHookEx", SetLastError = true)]
		public static extern IntPtr SetWindowsHookEx(int idHook, LowLevelMouseProcDelegate lpfn, IntPtr hMod, uint dwThreadId);

		/// <summary>Removes a hook procedure installed in a hook chain by the SetWindowsHookEx function.</summary>
		/// <param name="hHook">A handle to the hook to be removed. This parameter is a hook handle obtained by a previous call to SetWindowsHookEx.</param>
		/// <returns>If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError.</returns>
		[DllImport("user32", EntryPoint = "UnhookWindowsHookEx", SetLastError = true)]
		[return: MarshalAs(UnmanagedType.Bool)]
		public static extern bool UnhookWindowsHookEx(IntPtr hHook);

		/// <summary>Passes the hook information to the next hook procedure in the current hook chain. A hook procedure can call this function either before or after processing the hook information.</summary>
		/// <param name="hHook">This parameter is ignored.</param>
		/// <param name="nCode">The hook code passed to the current hook procedure. The next hook procedure uses this code to determine how to process the hook information.</param>
		/// <param name="wParam">The wParam value passed to the current hook procedure. The meaning of this parameter depends on the type of hook associated with the current hook chain.</param>
		/// <param name="lParam">The lParam value passed to the current hook procedure. The meaning of this parameter depends on the type of hook associated with the current hook chain.</param>
		/// <returns>This value is returned by the next hook procedure in the chain. The current hook procedure must also return this value. The meaning of the return value depends on the hook type. For more information, see the descriptions of the individual hook procedures.</returns>
		[DllImport("user32", EntryPoint = "CallNextHookEx", SetLastError = true)]
		public static extern IntPtr CallNextHookEx(IntPtr hHook, int nCode, IntPtr wParam, IntPtr lParam);

		/// <summary>Application-defined callback function used with the SetWindowsHookEx function. The system calls this function every time a new mouse input event is about to be posted into a thread input queue. The mouse input can come from the local mouse driver or from calls to the mouse_event function. If the input comes from a call to mouse_event, the input was "injected".</summary>
		/// <param name="nCode">A code the hook procedure uses to determine how to process the message. If nCode is less than zero, the hook procedure must pass the message to the CallNextHookEx function without further processing and should return the value returned by CallNextHookEx.</param>
		/// <param name="wParam">The identifier of the mouse message.</param>
		/// <param name="lParam">A pointer to a MSLLHOOKSTRUCT structure.</param>
		/// <returns>If nCode is less than zero, the hook procedure must return the value returned by CallNextHookEx. If nCode is greater than or equal to zero, and the hook procedure did not process the message, it is highly recommended that you call CallNextHookEx and return the value it returns; otherwise, other applications that have installed WH_KEYBOARD_LL hooks will not receive hook notifications and may behave incorrectly as a result. If the hook procedure processed the message, it may return a nonzero value to prevent the system from passing the message to the rest of the hook chain or the target window procedure.</returns>
		public static IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam)
		{
			try
			{
				LowLevelMouseHookStructure structure = (LowLevelMouseHookStructure)Marshal.PtrToStructure(lParam, typeof(LowLevelMouseHookStructure));
				Console.WriteLine("Point = {0}; Flag = {1}; Message = {2}", structure.Point, structure.flags, (LowLevelMouseMessageType)wParam);
			}
			catch (Exception) { /* Squelched */ ;}

			return CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam);
		}

		/// <summary>Point Structure</summary>
		[StructLayout(LayoutKind.Sequential)]
		public struct POINT
		{
			/// <summary>The x</summary>
			public int X;
			/// <summary>The y</summary>
			public int Y;

			/// <summary>Initializes a new instance of the <see cref="POINT"/> struct.</summary>
			/// <param name="x">The x.</param>
			/// <param name="y">The y.</param>
			public POINT(int x, int y)
			{
				X = x;
				Y = y;
			}

			/// <summary>Initializes a new instance of the <see cref="POINT"/> struct.</summary>
			/// <param name="point">The point.</param>
			public POINT(Point point) : this(point.X, point.Y) { ;}

			/// <summary>Performs an implicit conversion from <see cref="POINT"/> to <see cref="Point"/>.</summary>
			/// <param name="point">The point.</param>
			/// <returns>The result of the conversion.</returns>
			public static implicit operator Point(POINT point)
			{
				return new Point(point.X, point.Y);
			}

			/// <summary>Performs an implicit conversion from <see cref="Point"/> to <see cref="POINT"/>.</summary>
			/// <param name="point">The point.</param>
			/// <returns>The result of the conversion.</returns>
			public static implicit operator POINT(Point point)
			{
				return new POINT(point.X, point.Y);
			}

			/// <summary>Returns a <see cref="System.String" /> that represents this instance.</summary>
			/// <returns>A <see cref="System.String" /> that represents this instance.</returns>
			public override string ToString()
			{
				return ((Point)this).ToString();
			}
		}

		/// <summary>Structure that contains information about a low-level mouse input event</summary>
		[StructLayout(LayoutKind.Sequential)]
		public struct LowLevelMouseHookStructure
		{
			/// <summary>The x- and y-coordinates of the cursor, in screen coordinates.</summary>
			public POINT Point;
			/// <summary>The message for the event.</summary>
			/// <remarks>If the message is WM_MOUSEWHEEL, the high-order word of this member is the wheel delta. The low-order word is reserved. A positive value indicates that the wheel was rotated forward, away from the user; a negative value indicates that the wheel was rotated backward, toward the user. One wheel click is defined as WHEEL_DELTA, which is 120.  If the message is WM_XBUTTONDOWN, WM_XBUTTONUP, WM_XBUTTONDBLCLK, WM_NCXBUTTONDOWN, WM_NCXBUTTONUP, or WM_NCXBUTTONDBLCLK, the high-order word specifies which X button was pressed or released, and the low-order word is reserved. This value can be one or more of the following values. Otherwise, mouseData is not used.</remarks>
			[MarshalAs(UnmanagedType.U4)]
			public uint Data;
			[MarshalAs(UnmanagedType.U4)]
			public LowLevelMouseHookFlags flags;
			/// <summary>The time stamp for this message.</summary>
			[MarshalAs(UnmanagedType.U4)]
			public uint Time;
			/// <summary>The additional information field</summary>
			public UIntPtr ExtraInfo;
		}

		/// <summary>Enum that contains the low-level mouse flags</summary>
		[Flags]
		public enum LowLevelMouseHookFlags : uint
		{
			/// <summary>No flags set</summary>
			None = 0x00,
			/// <summary>Event-injected flag.</summary>
			Injected = 0x1,
			/// <summary>Event-injected from a process running at a lower integrity level flag.</summary>
			LowerLevelInjected = 0x2
		}

		/// <summary>Enum that contains the low-level mouse message types</summary>
		public enum LowLevelMouseMessageType : uint
		{
			MouseMove = 0x200,
			LeftButtonDown = 0x201,
			LeftButtonUp = 0x202,
			LeftButtonDoubleClick = 0x203,
			RightButtonDown = 0x204,
			RightButtonUp = 0x205,
			RightButtonDoubleClick = 0x206,
			MiddleButtonDown = 0x207,
			MiddleButtonUp = 0x208,
			MiddleButtonDoubleClick = 0x209,
			MouseWheel = 0x20A,
			XButtonDown = 0x20B,
			XButtonUp = 0x20C,
			XButtonDoubleClick = 0x20D,
			MouseHorizontalWheel = 0x20E
		}

		/// <summary>Delegate for the low level mouse hook</summary>
		/// <param name="nCode">A code the hook procedure uses to determine how to process the message. If nCode is less than zero, the hook procedure must pass the message to the CallNextHookEx function without further processing and should return the value returned by CallNextHookEx.</param>
		/// <param name="wParam">The identifier of the mouse message.</param>
		/// <param name="lParam">A pointer to a KBDLLHOOKSTRUCT structure.</param>
		/// <returns>If nCode is less than zero, the hook procedure must return the value returned by CallNextHookEx. If nCode is greater than or equal to zero, and the hook procedure did not process the message, it is highly recommended that you call CallNextHookEx and return the value it returns; otherwise, other applications that have installed WH_KEYBOARD_LL hooks will not receive hook notifications and may behave incorrectly as a result. If the hook procedure processed the message, it may return a nonzero value to prevent the system from passing the message to the rest of the hook chain or the target window procedure.</returns>
		public delegate IntPtr LowLevelMouseProcDelegate(int nCode, IntPtr wParam, [In] IntPtr lParam);
	}
}

Open in new window

Which produces the following output -Capture.JPG-saige-

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.