全局钩子定义
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace WindowsFormsApp1
{
public static class GlobalMousePosition
{
#region 获取鼠标当前位置
public const int WH_MOUSE_LL = 14;
public enum MouseMessages
{
WM_MOUSEMOVE = 0x0200
}
public delegate int LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr SetWindowsHookEx(int idHook, LowLevelMouseProc lpfn, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr GetModuleHandle(string lpModuleName);
#endregion
#region 点击事件
[DllImport("user32.dll")]
static extern bool SetCursorPos(int x, int y);
[DllImport("user32.dll")]
public static extern void mouse_event(uint dwFlags, int dx, int dy, uint dwData, int dwExtraInfo);
// 模拟鼠标点击一次
public static void SimulateMouseClick(int x, int y)
{
SetCursorPos(x, y);
Thread.Sleep(100); // 等待 100 毫秒,确保鼠标移动到指定位置
// 模拟鼠标按下和释放操作(模拟点击)
mouse_event(0x0002, x, y, 0, 0); // 鼠标左键按下
mouse_event(0x0004, x, y, 0, 0); // 鼠标左键释放
}
#endregion
}
}
winfrom窗体代码:文章来源:https://www.toymoban.com/news/detail-743281.html
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Net.Mime.MediaTypeNames;
using static WindowsFormsApp1.GlobalMousePosition;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
private LowLevelMouseProc mouseProc;
private IntPtr mouseHookId = IntPtr.Zero;
public Form1()
{
InitializeComponent();
this.TopMost = true;
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
// 注册全局鼠标钩子
mouseProc = HookCallback;
using (Process curProcess = Process.GetCurrentProcess())
using (ProcessModule curModule = curProcess.MainModule)
{
mouseHookId = SetWindowsHookEx(WH_MOUSE_LL, mouseProc, GetModuleHandle(curModule.ModuleName), 0);
}
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
base.OnFormClosing(e);
// 取消注册全局鼠标钩子
UnhookWindowsHookEx(mouseHookId);
}
private int HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 && MouseMessages.WM_MOUSEMOVE == (MouseMessages)wParam)
{
// 获取鼠标位置
Point mousePosition = Cursor.Position;
// 更新你的全局记录逻辑,比如将当前鼠标位置保存到全局变量或日志中
// ...
labelMousePosition.Text = string.Format("当前鼠标位置:X={0}, Y={1}", mousePosition.X, mousePosition.Y);
}
return CallNextHookEx(mouseHookId, nCode, wParam, lParam);
}
private void button1_Click(object sender, EventArgs e)
{
Clipboard.Clear(); // 清空剪贴板
int x = int.Parse(this.x.Text);
int y = int.Parse(this.y.Text);
string text = this.textbox.Text;
Clipboard.SetText(text);
Thread.Sleep(100);
SimulateMouseClick(x, y);
Thread.Sleep(100);
SendKeys.Send("^(v)");
Thread.Sleep(100);
SendKeys.Send("\t");
}
}
}
效果截图:
文章来源地址https://www.toymoban.com/news/detail-743281.html
到了这里,关于C# 实现全局鼠标钩子操作以及发送键盘事件的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!