Get the coordinates of the mouse

Source: Internet
Author: User

 

Obtain the coordinates relative to the screen on the form :]

Method 1:

Public class Win32

{

[StructLayout (LayoutKind. Sequential)]

Public struct POINT

{

Public int X;

Public int Y;

 

Public POINT (int x, int y)

{

This. X = x;

This. Y = y;

}

}

 

[DllImport ("user32.dll", CharSet = CharSet. Auto)]

Public static extern bool GetCursorPos (out POINT pt );

}

 

 

Method 2:

Mouse. GetPosition (this)

 

Method 3:

E. GetPosition (this) in the mouse mousemove event)

 

 

 

 

[Retrieve global mouse coordinates]

Using System;

Using System. Collections. Generic;

Using System. Linq;

Using System. Text;

Using System. Windows;

Using System. Windows. Controls;

Using System. Windows. Data;

Using System. Windows. Documents;

Using System. Windows. Input;

Using System. Windows. Media;

Using System. Windows. Media. Imaging;

Using System. Windows. Navigation;

Using System. Windows. Shapes;

Using System. Diagnostics;

Using System. Runtime. InteropServices;

 

 

Namespace wpfMouse

{

/// <Summary>

/// Interaction logic of MainWindow. xaml

/// </Summary>

Public partial class MainWindow: Window

{

Private LowLevelMouseProc _ proc;

Private IntPtr _ hookID = IntPtr. Zero;

Private MSLLHOOKSTRUCT hookStruct;

Private delegate IntPtr LowLevelMouseProc (int nCode, IntPtr wParam, IntPtr lParam );

 

Public MainWindow ()

{

InitializeComponent ();

PageLoad ();

}

Private void pageLoad ()

{

_ HookID = SetHook (_ proc );

}

Private IntPtr SetHook (LowLevelMouseProc proc)

{

Using (Process curProcess = Process. GetCurrentProcess ())

Using (ProcessModule curModule = curProcess. MainModule)

{

_ Proc = HookCallback;

Return SetWindowsHookEx (WH_MOUSE_LL, _ proc,

GetModuleHandle (curModule. ModuleName), 0 );

}

}

Private IntPtr HookCallback (int nCode, IntPtr wParam, IntPtr lParam)

{

 

If (nCode> = 0 & MouseMessages. WM_MOUSEMOVE = (MouseMessages) wParam)

{

 

HookStruct = (MSLLHOOKSTRUCT) Marshal. PtrToStructure (lParam, typeof (MSLLHOOKSTRUCT ));

// Release

Marshal. FreeCoTaskMem (lParam );

 

LabMouse. Content = hookStruct.pt. x + "," + hookStruct.pt. y;

 

 

}

 

Return CallNextHookEx (_ hookID, nCode, wParam, lParam );

}

Private const int WH_MOUSE_LL = 14;

 

Private enum MouseMessages

{

WM_LBUTTONDOWN = 0x0201,

WM_LBUTTONUP = 0x0202,

WM_MOUSEMOVE = 0x0200,

WM_MOUSEWHEEL = 0x020A,

WM_RBUTTONDOWN = 0x0204,

WM_RBUTTONUP = 0x0205

}

[StructLayout (LayoutKind. Sequential)]

Private struct POINT

{

Public int x;

Public int y;

}

 

[StructLayout (LayoutKind. Sequential)]

Private struct MSLLHOOKSTRUCT

{

Public POINT pt;

Public uint mouseData;

Public uint flags;

Public uint time;

Public IntPtr dwExtraInfo;

}

 

[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: financialas (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 void Window_Closed (object sender, EventArgs e)

{

UnhookWindowsHookEx (_ hookID );

}

 

 

}

}

 

From: happy pig's column

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.