📄 mousehook.cpp
字号:
/*******************************
* wince keyboard hook
* liqm@ustc.edu
* 2008-11-29 20:57
******************************/
#include <windows.h>
#include <aygshell.h>
#include "pwinuser.h"
HWND g_hwndContent;
HHOOK g_hHook = NULL;
BOOL g_bAppend = FALSE;
#define SZ_WINDOW_CLASS L"HookDemoClass"
#define SZ_WINDOW_TITLE L"HookDemo"
#define MAX_MSG_BUFFER 0x100
#define ALERT(hwnd,info,title) MessageBox(hwnd,info,title,MB_ICONINFORMATION|MB_SETFOREGROUND)
#define LOG_LAST_ERROR(info) ALERT(NULL,info,L"Error")
void AppendMessage(LPWSTR szFormat,...)
{
if(g_hwndContent)
{
WCHAR szBuffer[MAX_MSG_BUFFER ];
va_list ap;
va_start(ap,szFormat);
_vsnwprintf(szBuffer,MAX_MSG_BUFFER,szFormat,ap);
va_end(ap);
SendMessage(g_hwndContent,EM_REPLACESEL ,FALSE,(LPARAM)szBuffer);
}
}
void SwitchPending()
{
if(g_bAppend)
{
AppendMessage(L"Log DISABLED,click to enable,");
g_bAppend = FALSE;
}
else
{
SetWindowText(g_hwndContent,L"");
g_bAppend = TRUE;
AppendMessage(L"Log ENABLED,click to disable,");
}
AppendMessage(L"double click to exit.\r\n");
}
LRESULT MouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
PEVENTMSGMSG pMsg = (PEVENTMSGMSG)lParam;
if(pMsg && g_bAppend)
{
AppendMessage(L"msg 0x%x,param(%d,%d),time %d\r\n",
pMsg->message,LOWORD(pMsg->paramL),HIWORD(pMsg->paramL),pMsg->time);
}
return CallNextHookEx( g_hHook, nCode, wParam, lParam );
}
BOOL InitWindow(HWND hwndParent,HINSTANCE hInstance)
{
RECT rct;
GetClientRect(hwndParent, &rct);
InitCommonControls();
g_hwndContent=CreateWindow(L"Edit", NULL,
WS_CHILD | WS_VISIBLE | ES_MULTILINE | ES_READONLY | ES_WANTRETURN | WS_VSCROLL,
0,rct.top,rct.right,rct.bottom,
hwndParent, (HMENU)0, hInstance, NULL);
if(!g_hwndContent)
{
return FALSE;
}
else
{
EVENTMSG msg = {HC_ACTION};
g_hHook = QASetWindowsJournalHook(WH_JOURNALRECORD, MouseProc, &msg);
SwitchPending();
AppendMessage(L"QASetWindowsJournalHook %s \r\n",g_hHook ? L"success":L"failed");
return TRUE;
}
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITMENUPOPUP:
break;
case WM_CREATE:
if(InitWindow(hWnd,((CREATESTRUCT *)lParam)->hInstance))
{
break;//fail go througt WM_DESTROY
}
case WM_DESTROY:
if(g_hHook)
{
QAUnhookWindowsJournalHook(WH_JOURNALRECORD);
g_hHook = NULL;
}
PostQuitMessage(0);
break;
case WM_ACTIVATE:
if(WA_ACTIVE==LOWORD(wParam))
{
SetFocus(g_hwndContent);
}
break;
case WM_NOTIFY:
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
HWND InitInstance(HINSTANCE hInstance)
{
WNDCLASS wc={0};
wc.style= CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc= (WNDPROC) WndProc;
wc.hInstance= hInstance;
wc.lpszClassName= SZ_WINDOW_CLASS;
RegisterClass(&wc);
HWND hWnd = CreateWindow(SZ_WINDOW_CLASS, SZ_WINDOW_TITLE, WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
if (hWnd)
{
ShowWindow(hWnd, SW_SHOW);//nCmdShow);
UpdateWindow(hWnd);
}
else LOG_LAST_ERROR(L"CreateWindow");
return hWnd;
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPWSTR lpCmdLine,int nCmdShow)
{
HWND hMain = InitInstance (hInstance);
if (hMain)
{
for(MSG msg;GetMessage(&msg, NULL, 0, 0);)
{
if(msg.hwnd == g_hwndContent)
{
switch(msg.message)
{
//avoid any selection
case WM_MOUSEMOVE:
continue;
case WM_LBUTTONDOWN:
SwitchPending();
continue;
case WM_LBUTTONDBLCLK:
PostMessage (hMain, WM_CLOSE, 0, 0);
continue;
}
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
UnregisterClass(SZ_WINDOW_CLASS,hInstance);
}
return 0;
}
//=========end of file=============//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -