熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

獲取鼠標的坐標

2013-11-13 10:23:24  來源: .NET編程 

  view plainprint?

  「在窗體上獲取相對於屏幕的坐標

  方法public class Win { [StructLayout(LayoutKindSequential)] public struct POINT { public int Xpublic int Y

  public POINT(int x int y)

  { thisX = xthisY = y}

  [DllImport(userdll CharSet = CharSetAuto)] public static extern bool GetCursorPos(out POINT pt)}

  方法MouseGetPosition(this)

  方法在鼠標mousemove事件中 eGetPosition(this)

  「獲取全局鼠標坐標」

  using Systemusing SystemCollectionsGenericusing SystemLinqusing SystemTextusing SystemWindowsusing SystemWindowsControlsusing SystemWindowsDatausing SystemWindowsDocumentsusing SystemWindowsInputusing SystemWindowsMediausing SystemWindowsMediaImagingusing SystemWindowsNavigationusing SystemWindowsShapesusing SystemDiagnosticsusing SystemRuntimeInteropServices

  namespace wpfMouse { /// <summary> /// MainWindowxaml 的交互邏輯/// </summary> public partial class MainWindow Window { private LowLevelMouseProc _procprivate  IntPtr _hookID = IntPtrZeroprivate   MSLLHOOKSTRUCT hookStructprivate 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 = ProcessGetCurrentProcess())

  using (ProcessModule curModule = curProcessMainModule)

  { _proc = HookCallbackreturn SetWindowsHookEx(WH_MOUSE_LL _procGetModuleHandle(curModuleModuleName) } private  IntPtr HookCallback(int nCode IntPtr wParam IntPtr lParam)

  {

  if (nCode >= && MouseMessagesWM_MOUSEMOVE == (MouseMessages)wParam)

  {

  hookStruct = (MSLLHOOKSTRUCT)MarshalPtrToStructure(lParam typeof(MSLLHOOKSTRUCT))//釋放MarshalFreeCoTaskMem(lParam)

  labMouseContent = hookStructptx + + hookStructpty

  }

  return CallNextHookEx(_hookID nCode wParam lParam)} private const int WH_MOUSE_LL =

  private enum MouseMessages { WM_LBUTTONDOWN = xWM_LBUTTONUP = xWM_MOUSEMOVE = xWM_MOUSEWHEEL = xAWM_RBUTTONDOWN = xWM_RBUTTONUP = x } [StructLayout(LayoutKindSequential)] private struct POINT { public int xpublic int y}

  [StructLayout(LayoutKindSequential)] private struct MSLLHOOKSTRUCT { public POINT ptpublic uint mouseDatapublic uint flagspublic uint timepublic IntPtr dwExtraInfo}

  [DllImport(userdll CharSet = CharSetAuto SetLastError = true)] private static extern IntPtr SetWindowsHookEx(int idHookLowLevelMouseProc lpfn IntPtr hMod uint dwThreadId)

  [DllImport(userdll CharSet = CharSetAuto SetLastError = true)] [return MarshalAs(UnmanagedTypeBool)] private static extern bool UnhookWindowsHookEx(IntPtr hhk)

  [DllImport(userdll CharSet = CharSetAuto SetLastError = true)] private static extern IntPtr CallNextHookEx(IntPtr hhk int nCodeIntPtr wParam IntPtr lParam)

  [DllImport(kerneldll CharSet = CharSetAuto SetLastError = true)] private static extern IntPtr GetModuleHandle(string lpModuleName)

  private void Window_Closed(object sender EventArgs e)

  { UnhookWindowsHookEx(_hookID)}

  }


From:http://tw.wingwit.com/Article/program/net/201311/13453.html
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.