WIN32编写Warkey 实现改建

时间:2022-02-02 05:31:57

下面介绍下,利用WIN32编写自己的Warkey,实现改键,编译出的代码更小巧,希望对感兴趣的朋友有些帮助。主要利用低级键盘和鼠标钩子,无需额外的dll,程序很小巧,主要代码如下。

#ifndef WINVER
#define WINVER 0x0500
#endif

#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0500
#endif

#include <windows.h>
#include <winuser.h>
#include <shellapi.h>
#include <commctrl.h>

#defineWS_MyWinWS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU & (~WS_THICKFRAME)//窗口样式
#define WM_MyTray (WM_USER + 0x64) //定义用户托盘消息
#define ID_MyTray 0x64//定义用户托盘ID
#define ID_TIMER 0x01
#define SEC_MyTime 0x78
#define VK_OEM_3 0xC0//~键对应键码

HINSTANCE hInst=NULL;
HHOOK KeyHook = NULL;
HHOOK MouseHook=NULL;
HWND War3_Hwnd = NULL;//War3窗口句柄
NOTIFYICONDATA NotifyIconData;
HWND HwndHot[6]={NULL,NULL,NULL,NULL,NULL,NULL};//热键句柄
WORD Item[6]={0,0,0,0,0,0};//存储改建热键键码


ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
VOID WINAPI InitializeHotkey(HWND hwndDlg,HWND HwndHot[]);
LRESULT CALLBACK LowLevelMouseProc(INT, WPARAM , LPARAM );
LRESULT CALLBACK LowLevelKeyboardProc(INT, WPARAM, LPARAM);
VOIDWINAPI SetHook(void);
VOID WINAPI UnHook(void);
VOID WINAPI ShowHP(void);
VOID WINAPI AddTrayIcon(HWND);
VOID WINAPI DelTrayIcon(void);

int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg;

MyRegisterClass(hInstance);

if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}

while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return msg.wParam;
}


ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style= CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc= (WNDPROC)WndProc;
wcex.cbClsExtra= 0;
wcex.cbWndExtra= 0;
wcex.hInstance= hInstance;
wcex.hIcon= LoadIcon(hInstance, (LPCTSTR)IDI_WINLOGO);
wcex.hCursor= LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground= (HBRUSH)(COLOR_HIGHLIGHT);
wcex.lpszMenuName= NULL;
wcex.lpszClassName= "MICROPERATION";
wcex.hIconSm= LoadIcon(wcex.hInstance, (LPCTSTR)IDI_WINLOGO);

return RegisterClassEx(&wcex);
}


BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)//构建程序
{
HWND hWnd;

hInst = hInstance;
hWnd=FindWindow("MICROPERATION","MicrOperation");
if (hWnd)
{
ShowWindow(hWnd,SW_RESTORE);
MessageBox(hWnd,"程序已经启动!","提示" ,MB_ICONEXCLAMATION);
return FALSE;
}

hWnd = CreateWindow("MICROPERATION", "MicrOperation", WS_MyWin,
300, 250, 255, 230, NULL, NULL, hInstance, NULL);

if (!hWnd)
{
return FALSE;
}

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

return TRUE;
}


LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)//消息循环主程序
{
switch (message)
{
case WM_SYSCOMMAND:
if (wParam==SC_MINIMIZE)//窗口最小化事件,隐藏程序窗口,安装键盘鼠标钩子
{
ShowWindow(hWnd,SW_HIDE);
SetHook();
for (int i=0;i<6;i++)
Item[i]=LOWORD(SendMessage(HwndHot[i], HKM_GETHOTKEY, NULL, NULL));//存储用户改键信息
break;
}
return DefWindowProc(hWnd, message, wParam, lParam);
case WM_CREATE:
InitializeHotkey(hWnd,HwndHot);
AddTrayIcon(hWnd);
SetTimer (hWnd, ID_TIMER, SEC_MyTime, NULL);
break;
case WM_MyTray:
switch (lParam)
{
case WM_MBUTTONDOWN://鼠标中键托盘图标,退出程序
DestroyWindow(hWnd);
break;
case WM_LBUTTONDOWN://托盘左键,显示程序界面,卸载钩子
UnHook();
ShowWindow(hWnd,SW_RESTORE);
break;
}
break;
case WM_TIMER://计时器显示血条
ShowHP();
break;
case WM_DESTROY:
UnHook();
DelTrayIcon();
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

VOID WINAPI InitializeHotkey(HWND hwndDlg,HWND HwndHot[]) //添加hotkey编辑框
{
HwndHot[0] = CreateWindowEx(0, HOTKEY_CLASS, NULL, WS_CHILD | WS_VISIBLE,
25, 30, 80, 20, hwndDlg, NULL, hInst, NULL);
HwndHot[1] = CreateWindowEx(0, HOTKEY_CLASS, NULL, WS_CHILD | WS_VISIBLE,
145, 30, 80, 20, hwndDlg, NULL, hInst, NULL);
HwndHot[2] = CreateWindowEx(0, HOTKEY_CLASS, NULL, WS_CHILD | WS_VISIBLE,
25, 90, 80, 20, hwndDlg, NULL, hInst, NULL);
HwndHot[3] = CreateWindowEx(0, HOTKEY_CLASS, NULL, WS_CHILD | WS_VISIBLE,
145, 90, 80, 20, hwndDlg, NULL, hInst, NULL);
HwndHot[4] = CreateWindowEx(0, HOTKEY_CLASS, NULL, WS_CHILD | WS_VISIBLE,
25, 150, 80, 20, hwndDlg, NULL, hInst, NULL);
HwndHot[5] = CreateWindowEx(0, HOTKEY_CLASS, NULL, WS_CHILD | WS_VISIBLE,
145, 150, 80, 20, hwndDlg, NULL, hInst, NULL);
for (int i=0;i<6;i++)
SendMessage(HwndHot[i], HKM_SETRULES, (WPARAM)~HKCOMB_NONE, NULL); //设置hotkey控件按键形式
}

LRESULT CALLBACK LowLevelMouseProc(INT nCode, WPARAM wParam, LPARAM lParam)//处理鼠标事件
{
if (nCode >= HC_ACTION)
{
if (wParam == WM_MBUTTONDOWN)//处理中键,对应小键盘4
{
SendMessage(War3_Hwnd,WM_KEYDOWN,VK_NUMPAD4,NULL);
SendMessage(War3_Hwnd,WM_KEYUP,VK_NUMPAD4,NULL);
return FALSE;
}
}
return CallNextHookEx(MouseHook, nCode, wParam, lParam);
}

LRESULT CALLBACK LowLevelKeyboardProc(INT nCode, WPARAM wParam, LPARAM lParam)//处理键盘事件
{
if (nCode >= HC_ACTION)
{
KBDLLHOOKSTRUCT *pKStruct = (KBDLLHOOKSTRUCT*)lParam;
DWORD Code=pKStruct->vkCode;
switch(wParam)
{
case WM_SYSKEYDOWN:
case WM_KEYDOWN:
if(Code == VK_LWIN)//屏蔽左WIN键
{
return TRUE;
}
if (Item[0]!=0 && (Code==Item[0]))//处理物品栏1,对应小键盘7,以下类似
{
SendMessage(War3_Hwnd,WM_KEYDOWN,VK_NUMPAD7,NULL);
SendMessage(War3_Hwnd,WM_KEYUP,VK_NUMPAD7,NULL);
if (Code==VK_OEM_3)//屏蔽~键,防止使用改键,选中小鸡,屏幕移动到小鸡
{
return TRUE;
}
return FALSE;//不屏蔽其他键,以免影响打字聊天
}
if (Item[1]!=0 && (Code==Item[1]))
{
SendMessage(War3_Hwnd,WM_KEYDOWN,VK_NUMPAD8,NULL);
SendMessage(War3_Hwnd,WM_KEYUP,VK_NUMPAD8,NULL);
if (Code==VK_OEM_3)
{
return TRUE;
}
return FALSE;
}
if (Item[2]!=0 && (Code==Item[2]))
{
SendMessage(War3_Hwnd,WM_KEYDOWN,VK_NUMPAD4,NULL);
SendMessage(War3_Hwnd,WM_KEYUP,VK_NUMPAD4,NULL);
if (Code==VK_OEM_3)
{
return TRUE;
}
return FALSE;
}
if (Item[3]!=0 && (Code==Item[3]))
{
SendMessage(War3_Hwnd,WM_KEYDOWN,VK_NUMPAD5,NULL);
SendMessage(War3_Hwnd,WM_KEYUP,VK_NUMPAD5,NULL);
if (Code==VK_OEM_3)
{
return TRUE;
}
return FALSE;
}
if (Item[4]!=0 && (Code==Item[4]))
{
SendMessage(War3_Hwnd,WM_KEYDOWN,VK_NUMPAD1,NULL);
SendMessage(War3_Hwnd,WM_KEYUP,VK_NUMPAD1,NULL);
if (Code==VK_OEM_3)
{
return TRUE;
}
return FALSE;
}
if (Item[5]!=0 && (Code==Item[5]))
{
SendMessage(War3_Hwnd,WM_KEYDOWN,VK_NUMPAD2,NULL);
SendMessage(War3_Hwnd,WM_KEYUP,VK_NUMPAD2,NULL);
if (Code==VK_OEM_3)
{
return TRUE;
}
return FALSE;
}
default:
break;
}
}
return CallNextHookEx(KeyHook, nCode, wParam, lParam);
}

void WINAPI SetHook(void)//安装键盘、鼠标钩子
{
if(KeyHook == NULL)
KeyHook = SetWindowsHookEx(WH_KEYBOARD_LL,LowLevelKeyboardProc,hInst,NULL);
if(MouseHook == NULL)
MouseHook = SetWindowsHookEx(WH_MOUSE_LL,LowLevelMouseProc,hInst,NULL);
}

void WINAPI UnHook(void)//卸载钩子
{
if(KeyHook != NULL)
{
UnhookWindowsHookEx(KeyHook);
KeyHook = NULL;
}
if(MouseHook != NULL)
{
UnhookWindowsHookEx(MouseHook);
MouseHook = NULL;
}
}

void WINAPI ShowHP(void)//显血
{
War3_Hwnd=FindWindow("Warcraft III","Warcraft III");
if (War3_Hwnd!=NULL && War3_Hwnd==GetForegroundWindow())
{
SendMessage(War3_Hwnd,WM_KEYDOWN,VK_OEM_4,NULL);
SendMessage(War3_Hwnd,WM_KEYDOWN,VK_OEM_6,NULL);
}
}

void WINAPI AddTrayIcon(HWND hwnd)//添加托盘
{
NotifyIconData.cbSize = sizeof(NOTIFYICONDATA);
NotifyIconData.hIcon = LoadIcon(NULL,IDI_WINLOGO);
NotifyIconData.hWnd = hwnd;
NotifyIconData.uCallbackMessage = WM_MyTray;
NotifyIconData.uFlags = NIF_MESSAGE|NIF_ICON;
NotifyIconData.uID = ID_MyTray;
Shell_NotifyIcon(NIM_ADD,&NotifyIconData);
}

void WINAPI DelTrayIcon(void)//删除托盘
{
Shell_NotifyIcon(NIM_DELETE, &NotifyIconData);
}