程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 獲取鼠標的坐標

獲取鼠標的坐標

編輯:C#入門知識

 

【在窗體上獲取相對於屏幕的坐標:】 

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

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

        { 

            UnhookWindowsHookEx(_hookID); 

        } 

 

 

    } 

}   

 

摘自:幸福的豬的專欄

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved