当前位置:编程学习 > C#/ASP.NET >>

获取鼠标的坐标

 

【在窗体上获取相对于屏幕的坐标:】 

方法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); 

    } 

 

 

方法2: 

    Mouse.GetPosition(this) 

 

方法3: 

    在鼠标mousemove事件中e.GetPosition(this) 

 

 

 

 

【获取全局鼠标坐标】 

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> 

    /// 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)); 

                //释放 

                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; 

&

补充:软件开发 , C# ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,