c#

位置:IT落伍者 >> c# >> 浏览文章

c#中通过设置钩子监视鼠标移动


发布日期:2020年01月02日
 
c#中通过设置钩子监视鼠标移动
这个问题来自论坛提问咨询如何通过设置钩子监视鼠标的移动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 ;

}

}

}

               

上一篇:.net中的List的使用

下一篇:解答关于C#的JavaScript函数的问题