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

c#中通過設置鉤子監視鼠標移動

2022-06-13   來源: .NET編程 
這個問題來自論壇提問咨詢如何通過設置鉤子監視鼠標的移動C#的大致代碼如下

  

  

  using System;
using SystemWindowsForms;
using SystemRuntimeInteropServices;
namespace WindowsApplication
{
public partial class Form : Form
{
public Form()
{
InitializeComponent();
}

private void Form_Load(object sender EventArgs e)
{
WinHook hook = new WinHook();
hookonMouseChange += new EventHandler(hook_onMouseChange);
hookSetHook();
}

  void hook_onMouseChange(object sender EventArgs e)
{
thisText = CursorPositionToString();
}
}
public class WinHook
{

  [DllImport(kernel)]
public static extern int GetCurrentThreadId();

  [DllImport(userCharSet = CharSetAuto CallingConvention =

  CallingConventionStdCall)]
public static extern int SetWindowsHookEx(
HookType idHook
HOOKPROC lpfn
int hmod
int dwThreadId);

  public enum HookType
{
WH_GETMESSAGE =
}

  public delegate int HOOKPROC(int nCode int wParam int lParam);

  public event SystemEventHandler onMouseChange;

  public void SetHook()
{
SetWindowsHookEx(HookTypeWH_GETMESSAGE
new HOOKPROC(thisMyKeyboardProc)

GetCurrentThreadId());
}

  public int MyKeyboardProc(int nCode int wParam int lParam)
{
if (onMouseChange != null)
{
onMouseChange(null null);
}
return ;
}
}
}


From:http://tw.wingwit.com/Article/program/net/201311/12209.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.