技术经验解读:【转】c#实现魔兽(warIII)中显血和改键功能(附源码)(不影响聊天打字)

简介: 技术经验解读:【转】c#实现魔兽(warIII)中显血和改键功能(附源码)(不影响聊天打字)

本文来自CSDN博客,转载请标明出处:


在论坛中看到有人提到 这个功能,感觉应该能实现,周末就抽时间写出来了,在这里分享下:


思路:Hook+SendMessage,


首先,因为我们要改的键war3不是自己写的程序,所以只能用Hook来监控键盘的按键:


键盘Hook:


+ expand sourceview plaincopy to clipboardprint?


using System;


using System.Runtime.InteropServices;


using System.Windows.Forms; //代码效果参考:http://hnjlyzjd.com/hw/wz_24387.html


namespace quickey


{


public class KeyboardHook


{


private const int WM_KEYDOWN = 0x100;//按下消息


private const int WM_KEYUP = 0x101;//松开消息


private const int WM_SYSKEYDOWN = 0x104;


private const int WM_SYSKEYUP = 0x105;


//全局事件


public event KeyEventHandler OnKeyDownEvent;


public event KeyEventHandler OnKeyUpEvent;


public event KeyPressEventHandler OnKeyPressEvent;


static int hKeyboardHook = 0;


//鼠标常量


public const int WH_KEYBOARD_LL = 13;


public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);


//声明键盘钩子事件类型


HookProc KeyboardHookProcedure;


///


/// 声明键盘钩子的封送结构类型


///


【StructLayout(LayoutKind.Sequential)】 //代码效果参考:http://hnjlyzjd.com/hw/wz_24385.html


public class KeyboardHookStruct


{


public int vkCode;//表示一个1到254间的虚拟键盘码


public int scanCode;//表示硬件扫描码


public int flags;


public int time;


public int dwExtraInfo;


}


//安装钩子


【DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)】


public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);


//下一个钩子


【DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)】


public static extern int CallNextHookEx(int idHook, int nCode, Int32 wParam, IntPtr lParam);


//卸载钩子


【DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)】


public static extern bool UnhookWindowsHookEx(int idHook);


private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)


{


if ((nCode >= 0) && (OnKeyDownEvent != null || OnKeyUpEvent != null || OnKeyPressEvent != null))


{


KeyboardHookStruct MyKBHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));


//引发OnKeyDownEvent


if (OnKeyDownEvent != null && (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN))


{


Keys keyData = (Keys)MyKBHookStruct.vkCode;


KeyEventArgs e = new KeyEventArgs(keyData);


OnKeyDownEvent(this, e);


}


}


return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);


}


public void Start()


{


if (hKeyboardHook == 0)


{


KeyboardHookProcedure = new HookProc(KeyboardHookProc);


//hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProcedure, Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly().GetModules()【0】), 0);


using (System.Diagnostics.Process curProcess = System.Diagnostics.Process.GetCurrentProcess())


using (System.Diagnostics.ProcessModule curModule = curProcess.MainModule)


hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProcedure,GetModuleHandle(curModule.ModuleName), 0);


if (hKeyboardHook == 0)


{


Stop();


throw new Exception("Set GlobalKeyboardHook failed!");


}


}


}


public void Stop()


{


bool retKeyboard = true;


if (hKeyboardHook != 0)


{


retKeyboard = UnhookWindowsHookEx(hKeyboardHook);


hKeyboardHook = 0;


}


if (!retKeyboard)


throw new Exception("Unload GlobalKeyboardHook failed!");


}


//构造函数中安装钩子


public KeyboardHook()


{


Start();


}


//析构函数中卸载钩子


~KeyboardHook()


{


Stop();


}


}


}


using System;


using System.Runtime.InteropServices;


using System.Windows.Forms;


namespace quickey


{


public class KeyboardHook


{


private const int WM_KEYDOWN = 0x100;//按下消息


private const int WM_KEYUP = 0x101;//松开消息


private const int WM_SYSKEYDOWN = 0x104;


private const int WM_SYSKEYUP = 0x105;


//全局事件


public event KeyEventHandler OnKeyDownEvent;


public event KeyEventHandler OnKeyUpEvent;


public event KeyPressEventHandler OnKeyPressEvent;


static int hKeyboardHook = 0;


//鼠标常量


public const int WH_KEYBOARD_LL = 13;


public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);


//声明键盘钩子事件类型


HookProc KeyboardHookProcedure;


///


/// 声明键盘钩子的封送结构类型


///


【StructLayout(LayoutKind.Sequential)】


public class KeyboardHookStruct


{


public int vkCode;//表示一个1到254间的虚拟键盘码


public int scanCode;//表示硬件扫描码


public int flags;


public int time;


public int dwExtraInfo;


}


//安装钩子


【DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)】


public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);


//下一个钩子


【DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)】


public static extern int CallNextHookEx(int idHook, int nCode, Int32 wParam, IntPtr lParam);


//卸载钩子


【DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)】


public static extern bool UnhookWindowsHookEx(int idHook);


private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)


{


if ((nCode >= 0) && (OnKeyDownEvent != null || OnKeyUpEvent != null || OnKeyPressEvent != null))


{


KeyboardHookStruct MyKBHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));


//引发OnKeyDownEvent


if (OnKeyDownEvent != null && (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN))


{


Keys keyData = (Keys)MyKBHookStruct.vkCode;


KeyEventArgs e = new KeyEventArgs(keyData);


OnKeyDownEvent(this, e);


}


}


return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);


}


public void Start()


{


if (hKeyboardHook == 0)


{


KeyboardHookProcedure = new HookProc(KeyboardHookProc);


//hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProcedure, Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly().GetModules()【0】), 0);


using (System.Diagnostics.Process curProcess = System.Diagnostics.Process.GetCurrentProcess())


using (System.Diagnostics.ProcessModule curModule = curProcess.MainModule)


hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProcedure,GetModuleHandle(curModule.ModuleName), 0);


if (hKeyboardHook == 0)


{


Stop();


throw new Exception("Set GlobalKeyboardHook failed!");


}


}


}


public void Stop()


{


bool retKeyboard = true;


if (hKeyboardHook != 0)


{


retKeyboard = UnhookWindowsHookEx(hKeyboardHook);


hKeyboardHook = 0;


}


if (!retKeyboard)


throw new Exception("Unload GlobalKeyboardHook failed!");


}


//构造函数中安装钩子


public KeyboardHook()


{


Start();


}


//析构函数中卸载钩子


~KeyboardHook()


{


Stop();


}


}


}


创建全局Hook:


view plaincopy to clipboardprint?


KeyboardHook hook = new KeyboardHook();


private void Form1_Load(object sender, EventArgs e)


{


hook.OnKeyDownEvent += new KeyEventHandler(hook_OnKeyDownEvent);


}


void hook_OnKeyDownEvent(object sender, KeyEventArgs e){


//在这里就可以截获到所有的键盘按键了


MessageBox.show(e.keyValue.toString());


}


KeyboardHook hook = new KeyboardHook();


private void Form1_Load(object sender, EventArgs e)


{


hook.OnKeyDownEvent += new KeyEventHandler(hook_OnKeyDownEvent);


}


void hook_OnKeyDownEvent(object sender, KeyEventArgs e){


//在这里就可以截获到所有的键盘按键了


MessageBox.show(e.keyValue.toString());


}


打开war3,在里面按了几下键盘,弹出了看,有好几个对话框,说明可以监测到war3里面的按键,


我的思路是向窗口发送消息,必须找到获取窗口的句柄才行:


view plaincopy to clipboardprint?


【DllImport("USER32.DLL")】


public static extern IntPtr FindWindow(string lpClassName,string lpWindowName);


bool isHookEnable = true;//全局变量,指示Hook是否作用


private const int WM_KEYDOWN = 0x100;


private const int WM_KEYUP = 0x101;


void hook_OnKeyDownEvent(object sender, KeyEventArgs e)


{


if (e.KeyCode == Keys.Scroll)


{


isHookEnable = !isHookEnable;


this.Text = isHookEnable ? "quickey-开启":"quickey-停用";


notifyIcon1.Text = this.Text;


}


if (isHookEnable)


{


IntPtr war3 = FindWindow(null, "Warcraft III");


if (war3 != IntPtr.Zero)


{


MessageBox.show("找到war3了");


}


}


}


【DllImport("USER32.DLL")】


public static extern IntPtr FindWindow(string lpClassName,string lpWindowName);


bool isHookEnable = true;//全局变量,指示Hook是否作用


private const int WM_KEYDOWN = 0x100;


private const int WM_KEYUP = 0x101;


void hook_OnKeyDownEvent(object sender, KeyEventArgs e)


{


if (e.KeyCode == Keys.Scroll)


{


isHookEnable = !isHookEnable;


this.Text = isHookEnable ? "quickey-开启":"quickey-停用";


notifyIcon1.Text = this.Text;


}


if (isHookEnable)


{


IntPtr war3 = FindWindow(null, "Warcraft III");


if (war3 != IntPtr.Zero)


{


MessageBox.show("找到war3了");


}


}


}


运行,按了下弹出“找到war3了”这样就获得了war3窗口的句柄了


,剩下就是向窗口发送按键的消息了:


view plaincopy to clipboardprint?


先声明:


【DllImport("User32.DLL")】


public static extern int SendMessage(IntPtr hWnd,uint Msg, int wParam, int lParam);


if (isHookEnable)


{


IntPtr war3 = FindWindow(null, "Warcraft III");


if (war3 != IntPtr.Zero)


{


if (e.KeyCode == Keys.D)


SendMessage(war3, WM_KEYDOWN, (int)Keys.C, 0);


}


}


先声明:


【DllImport("User32.DLL")】


public static extern int SendMessage(IntPtr hWnd,uint Msg, int wParam, int lParam);


if (isHookEnable)


{


IntPtr war3 = FindWindow(null, "Warcraft III");


if (war3 != IntPtr.Zero)


{


if (e.KeyCode == Keys.D)


SendMessage(war3, WM_KEYDOWN, (int)Keys.C, 0);


}


}


打开war3到创建游戏界面,按了下D,嗯?创建游戏了,说明消息发送成功


这样就可以将截获的按键来发送指定消息来“更改”按键了


view plaincopy to clipboardprint?


private const int KEY_QUOTLEFT = 219;//键盘上 【 键的代码


private const int KEY_QUOTRIGHT = 221;//键盘上 】 键的代码


if (isHookEnable)


{


IntPtr war3 = FindWindow(null, "Warcraft III");


if (war3 != IntPtr.Zero)


{


SetForegroundWindow(war3);//将war3窗口置前,这一句我自己测试时可以不用,但去掉这一句后朋友那里说不行


SendMessage(war3, WM_KEYDOWN, KEY_QUOTLEFT,0);//按下【键就可以显示友军的血了


SendMessage(war3, WM_KEYDOWN, KEY_QUOTRIGHT, 0);


SendMessage(war3, WM_KEYDOWN, int.Parse(hash【e.KeyValue.ToString()】.ToString()), 0);//按下


SendMessage(war3, WM_KEYUP, int.Parse(hash【e.KeyValue.ToString()】.ToString()), 0);//松开


}


}


private const int KEY_QUOTLEFT = 219;//键盘上 【 键的代码


private const int KEY_QUOTRIGHT = 221;//键盘上 】 键的代码


if (isHookEnable)


{


IntPtr war3 = FindWindow(null, "Warcraft III");


if (war3 != IntPtr.Zero)


{


SetForegroundWindow(war3);//将war3窗口置前,这一句我自己测试时可以不用,但去掉这一句后朋友那里说不行


SendMessage(war3, WM_KEYDOWN, KEY_QUOTLEFT,0);//按下【键就可以显示友军的血了


SendMessage(war3, WM_KEYDOWN, KEY_QUOTRIGHT, 0);


SendMessage(war3, WM_KEYDOWN, int.Parse(hash【e.KeyValue.ToString()】.ToString()), 0);//按下


SendMessage(war3, WM_KEYUP, int.Parse(hash【e.KeyValue.ToString()】.ToString()), 0);//松开


}


}


ok,大体 就是这个思路,具体的代码可以到下面下载,


over


界面如下:


在论坛中看到有人提到 这个功能,感觉应该能实现,周末就抽时间写出来了,在这里分享下:


思路:Hook+SendMessage,


首先,因为我们要改的键war3不是自己写的程序,所以只能用Hook来监控键盘的按键:


键盘Hook:


+ expand sourceview plaincopy to clipboardprint?


using System;


using System.Runtime.InteropServices;


using System.Windows.Forms;


namespace quickey


{


public class KeyboardHook


{


private const int WM_KEYDOWN = 0x100;//按下消息


private const int WM_KEYUP = 0x101;//松开消息


private const int WM_SYSKEYDOWN = 0x104;


private const int WM_SYSKEYUP = 0x105;


//全局事件


public event KeyEventHandler OnKeyDownEvent;


public event KeyEventHandler OnKeyUpEvent;


public event KeyPressEventHandler OnKeyPressEvent;


static int hKeyboardHook = 0;


//鼠标常量


public const int WH_KEYBOARD_LL = 13;


public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);


//声明键盘钩子事件类型


HookProc KeyboardHookProcedure;


///


/// 声明键盘钩子的封送结构类型


///


【StructLayout(LayoutKind.Sequential)】


public class KeyboardHookStruct


{


public int vkCode;//表示一个1到254间的虚拟键盘码


public int scanCode;//表示硬件扫描码


public int flags;


public int time;


public int dwExtraInfo;


}


//安装钩子


【DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)】


public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);


//下一个钩子


【DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)】


public static extern int CallNextHookEx(int idHook, int nCode, Int32 wParam, IntPtr lParam);


//卸载钩子


【DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)】


public static extern bool UnhookWindowsHookEx(int idHook);


private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)


{


if ((nCode >= 0) && (OnKeyDownEvent != null || OnKeyUpEvent != null || OnKeyPressEvent != null))


{


KeyboardHookStruct MyKBHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));


//引发OnKeyDownEvent


if (OnKeyDownEvent != null && (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN))


{


Keys keyData = (Keys)MyKBHookStruct.vkCode;


KeyEventArgs e = new KeyEventArgs(keyData);


OnKeyDownEvent(this, e);


}


}


return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);


}


public void Start()


{


if (hKey

相关文章
|
1月前
|
开发框架 C# iOS开发
基于C#开源、功能强大、灵活的跨平台开发框架 - Uno Platform
基于C#开源、功能强大、灵活的跨平台开发框架 - Uno Platform
|
1月前
|
人工智能 开发框架 前端开发
C#/.NET/.NET Core技术前沿周刊 | 第 12 期(2024年11.01-11.10)
C#/.NET/.NET Core技术前沿周刊 | 第 12 期(2024年11.01-11.10)
|
1月前
|
人工智能 开发框架 安全
C#/.NET/.NET Core技术前沿周刊 | 第 13 期(2024年11.11-11.17)
C#/.NET/.NET Core技术前沿周刊 | 第 13 期(2024年11.11-11.17)
|
1月前
|
网络协议 Unix Linux
精选2款C#/.NET开源且功能强大的网络通信框架
精选2款C#/.NET开源且功能强大的网络通信框架
|
2月前
|
监控 前端开发 安全
C#一分钟浅谈:文件上传与下载功能实现
【10月更文挑战第2天】在Web应用开发中,文件的上传与下载是常见需求。本文从基础入手,详细讲解如何在C#环境下实现文件上传与下载。首先介绍前端表单设计及后端接收保存方法,使用`<input type="file">`与`IFormFile`接口;接着探讨错误处理与优化策略,如安全性验证和路径管理;最后讲解文件下载的基本步骤,包括确定文件位置、设置响应头及发送文件流。此外,还提供了进阶技巧,如并发处理、大文件分块上传及进度监控,帮助开发者构建更健壮的应用系统。
149 15
|
3月前
|
SQL API 定位技术
基于C#使用winform技术的游戏平台的实现【C#课程设计】
本文介绍了基于C#使用WinForms技术开发的游戏平台项目,包括项目结构、运行截图、实现功能、部分代码说明、数据库设计和完整代码资源。项目涵盖了登录注册、个人信息修改、游戏商城列表查看、游戏管理、用户信息管理、数据分析等功能。代码示例包括ListView和ImageList的使用、图片上传、图表插件使用和SQL工具类封装,以及高德地图天气API的调用。
基于C#使用winform技术的游戏平台的实现【C#课程设计】
|
2月前
|
开发框架 Cloud Native .NET
10 个 C# 关键字和功能
10 个 C# 关键字和功能
59 8
|
2月前
|
人工智能 开发框架 C#
C#/.NET/.NET Core技术前沿周刊 | 第 6 期(2024年9.16-9.22)
C#/.NET/.NET Core技术前沿周刊 | 第 6 期(2024年9.16-9.22)
|
2月前
|
人工智能 开发框架 Cloud Native
C#/.NET/.NET Core技术前沿周刊 | 第 9 期(2024年10.07-10.13)
C#/.NET/.NET Core技术前沿周刊 | 第 9 期(2024年10.07-10.13)
|
2月前
|
存储 安全 算法
C#一分钟浅谈:数据加密与解密技术
【10月更文挑战第3天】在数字化时代,信息安全至关重要。数据加密作为保障信息不被未授权访问的有效手段,通过特定算法将明文转换为密文,确保即使数据被截获也难以解读。本文从基础概念入手,介绍C#中实现数据加密的方法,涵盖对称加密(如AES、DES)与非对称加密(如RSA),并通过具体示例代码演示如何使用`System.Security.Cryptography.Aes`类完成AES加密和解密过程。此外,还强调了密钥管理及安全策略的重要性。
77 4
下一篇
DataWorks