Ver Mensaje Individual
  #2 (permalink)  
Antiguo 26/10/2005, 10:03
Avatar de Jose_minglein2
Jose_minglein2
 
Fecha de Ingreso: noviembre-2004
Mensajes: 2.344
Antigüedad: 19 años, 6 meses
Puntos: 8
Bien he conseguido detectar la posición, es decir que devuelva la posición x e y del ratón con respecto a la pantalla, pero siempre dentro del formulario, pero no consigo ni detectar el de fuera, ni que se detecte el click, el code con el que he conseguido la x y la "y" es de codeproject:

Código:
static int hHook = 0;
		public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);
		public const int WH_MOUSE = 7;
		HookProc MouseHookProcedure;
		[StructLayout(LayoutKind.Sequential)]
			public class POINT
		{
			public int x;
			public int y;
		}

		[StructLayout(LayoutKind.Sequential)]
			public class MouseHookStruct
		{
			public POINT pt;
			public int hwnd;
			public int wHitTestCode;
			public int dwExtraInfo;
		}
		[DllImport("user32.dll",CharSet=CharSet.Auto,
			 CallingConvention=CallingConvention.StdCall)]
		public static extern int SetWindowsHookEx(int idHook, HookProc lpfn,
			IntPtr hInstance, int threadId);

		//This is the Import for the UnhookWindowsHookEx function.
		//Call this function to uninstall the hook.
		[DllImport("user32.dll",CharSet=CharSet.Auto,
			 CallingConvention=CallingConvention.StdCall)]
		public static extern bool UnhookWindowsHookEx(int idHook);

		//This is the Import for the CallNextHookEx function.
		//Use this function to pass the hook information to the next hook procedure in chain.
		[DllImport("user32.dll",CharSet=CharSet.Auto,
			 CallingConvention=CallingConvention.StdCall)]
		public static extern int CallNextHookEx(int idHook, int nCode,
			IntPtr wParam, IntPtr lParam);

		public static int MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam)
		{
			//Marshall the data from the callback.
			MouseHookStruct MyMouseHookStruct = (MouseHookStruct) Marshal.PtrToStructure(lParam, typeof(MouseHookStruct));

			if (nCode < 0)
			{
				return CallNextHookEx(hHook, nCode, wParam, lParam);

			}
			else
			{
				//Create a string variable that shows the current mouse coordinates.
				String strCaption = "x = " +
					MyMouseHookStruct.pt.x.ToString("d") +
					"  y = " +
					MyMouseHookStruct.pt.y.ToString("d");
				string a = MyMouseHookStruct.wHitTestCode.ToString();
				//You must get the active form because it is a static function.
				Form tempForm = Form.ActiveForm;

				//Set the caption of the form.
				tempForm.Text = a;
				return CallNextHookEx(hHook, nCode, wParam, lParam);
			}
		}