Win32NET is a class library encapsulated under the. NET of the Win32API, including:
- Net encapsulation of commonly used Win32 APIs
- Mouse, keyboard, hotkey hook module,
- Simulate keyboard input of text (support various characters and text in different languages), simulate mouse click, move, scroll and other operations
- Query system hardware information
How to use this Win32Net library? You can search Win32Net in nuget package management,
Install-Package Win32Net -Version 1.2.0
Or add a quote directly,
<PackageReference Include="Win32Net" Version="1.2.0" /
** How to use the mouse hook: **
First instantiate a mouse hook object, then define a mouse event callback method, and start monitoring. When you no longer need to continue monitoring mouse information, you can cancel monitoring.
win32.Hooks.MouseHook mouseHook = new Hooks.MouseHook();//实例化鼠标钩子对象
mouseHook.LeftDown += MouseHook_LeftDown;//鼠标左键按下的事件监听回调方法
mouseHook.Start();//开始监听
mouseHook.Stop();//停止监听
** How to use keyboard hooks: **
Keyboard hooks are used similar to mouse hooks. First, instantiate a keyboard hook object, then define a keyboard event callback method, and start monitoring. When you no longer need to continue monitoring keyboard information, you can cancel monitoring.
Win32.Hooks.KeyboardHook keyboardHook = new Win32.Hooks.KeyboardHook();
keyboardHook.OnKeyUp += KeyboardHook_OnKeyUp;
keyboardHook.Start();
keyboardHook.Stop();
** How to register global shortcut keys **
//WPF 窗口句柄
//IntPtr hwnd = new WindowInteropHelper(WPFWindow对象).Handle;
//winform 窗口句柄
IntPtr hwnd = this.Handle;
//实例化热键对象,需要一个句柄,用于接收消息
Win32.Hooks.SystemHotKey systemHotKey = new Win32.Hooks.SystemHotKey(hwnd);
//热键id,要求唯一
int hotKeyId = 5000;
//注册Alt+Q快捷键
systemHotKey.AddHotKey(hotKeyId, Win32.KeyModifiers.Alt, Keys.Q,
() =>
{
MessageBox.Show("你按了Alt+Q快捷键");
}
);
//注册ESC快捷键
systemHotKey.AddHotKey(hotKeyId + 1, Win32.KeyModifiers.None, Keys.Escape,
() =>
{
this.Close();
}
);
How to obtain system hardware information
SystemInfo systemInfo = new SystemInfo();
richTextBox1.AppendText("操作系统:" + systemInfo.operatingSystem.Caption + "\n");
richTextBox1.AppendText("系统ID:" + systemInfo.operatingSystem.SerialNumber + "\n");
richTextBox1.AppendText("操作系统平台:" + systemInfo.operatingSystem.OSLevel + "\n");
richTextBox1.AppendText("系统安装时间:" + systemInfo.operatingSystem.InstallDate + "\n");
richTextBox1.AppendText("系统最近启动时间:" + systemInfo.operatingSystem.LastBootUpTime + "\n");
richTextBox1.AppendText("系统时间:" + systemInfo.operatingSystem.LocalDateTime + "\n");
richTextBox1.AppendText("CPU:" + systemInfo.processor.Name + "\n");
richTextBox1.AppendText("CPU厂商:" + systemInfo.processor.Manufacturer + "\n");
richTextBox1.AppendText("CPU序列号:" + systemInfo.processor.SerialNumber + "\n");
richTextBox1.AppendText("物理内存:" + systemInfo.memory.TotalPhysicalMemory + "\n");