|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 ebloke 于 2015-11-11 20:57 编辑
新手学习~请多关照,不足多多指出~
- #include "stdafx.h"
- LRESULT CALLBACK MyWndProc(HWND,UINT,WPARAM,LPARAM);
- int APIENTRY WinMain(HINSTANCE hInstance,
- HINSTANCE hPrevInstance,
- LPSTR lpCmdLine,
- int nCmdShow)
- {
- // TODO: Place code here.
- const char *szTitle = "SDK-简单HOOK程序";
- const char *szWindowClass = "MyWindowClass";
- MSG msg;
- WNDCLASSEX wcs;
- wcs.cbSize = sizeof(WNDCLASSEX);
- wcs.style = CS_HREDRAW|CS_VREDRAW;
- wcs.lpfnWndProc = (WNDPROC)MyWndProc;
- wcs.cbClsExtra = 0;
- wcs.cbWndExtra = 0;
- wcs.hInstance =hInstance;
- wcs.hIcon = LoadIcon(NULL,IDI_APPLICATION);
- wcs.hCursor = LoadCursor(NULL,IDC_CROSS);
- wcs.hbrBackground = (HBRUSH)::GetStockObject(WHITE_BRUSH);
- wcs.lpszClassName = szWindowClass;
- wcs.lpszMenuName = NULL;
- wcs.hIconSm = LoadIcon(NULL,IDI_APPLICATION);
- RegisterClassEx(&wcs);
- HWND hwnd = CreateWindow(szWindowClass,szTitle,WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT,0,300,200,NULL,NULL,hInstance,NULL);
- if (!hwnd)
- {
- return FALSE;
- }
- ShowWindow(hwnd,nCmdShow);
- UpdateWindow(hwnd);
- while (GetMessage(&msg,NULL,0,0))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return msg.wParam;
- }
- HHOOK g_hKeyHook = NULL;//保存钩子句柄
- LRESULT CALLBACK MyKeyBoardProc(int code, WPARAM wParam, LPARAM lParam)
- {
- char buf[2]={0};
- buf[0] = (char)wParam;
- if (lParam>>31 == 0)
- {
- ::MessageBox(NULL,buf,"钩子过程:您按下了", MB_ICONINFORMATION);
- }else
- {
- ::MessageBox(NULL,buf,"钩子过程:您松开了",MB_ICONINFORMATION);
- }
- return ::CallNextHookEx(g_hKeyHook,code,wParam,lParam); //传递给下一个HOOK处理
- }
- LRESULT CALLBACK MyWndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
- {
- char buf[2]={0}; //定义一个缓冲区接收字符输入
- switch (message)
- {
- case WM_CREATE:
- //创建窗口完毕后安装钩子。 GetCurrentThreadId获取当前线程一个唯一的线程标识符
- g_hKeyHook = SetWindowsHookEx(WH_KEYBOARD, MyKeyBoardProc,NULL,::GetCurrentThreadId());
- break;
- case WM_CHAR: //窗口过程接收的消息
- buf[0]=(char)wParam;
- if (lParam>>31 == 0)
- {
- ::MessageBox(NULL,buf,"窗口过程:您按下了",MB_ICONINFORMATION);
- }else
- {
- ::MessageBox(NULL,buf,"窗口过程:您松开了",MB_ICONINFORMATION);
- }
- break;
- case WM_DESTROY:
- ::UnhookWindowsHookEx(g_hKeyHook); //关闭钩子
- PostQuitMessage(0);
- break;
- default:
- return DefWindowProc(hwnd,message,wParam,lParam);
- }
- return 0;
- }
|
评分
-
查看全部评分
|