- #include <windows.h>
-
- HINSTANCE hinst;
- HWND hmain, hed1, hed2, hed3;
- HHOOK hhk = 0;
- char szappname[] = "MsgHook";
- char szEDIT[] = "EDIT";
-
- // le HOOK LOCAL est ICI
- LRESULT CALLBACK GetMsgProc(int nCode, WPARAM wParam, LPARAM lParam)
- {
- MSG *lpmsg;
- lpmsg = (MSG*) lParam;
- if(nCode < 0) goto defRet; // NE JAMAIS TOUCHER DANS CE CAS
- if(lpmsg->message == WM_RBUTTONUP) {
- if(IsChild(hmain, lpmsg->hwnd)) SetWindowText(lpmsg->hwnd, "Y a un HOOK");
- else PostMessage(lpmsg->hwnd, WM_CLOSE, 0, 0);
- return 1;
- }
- defRet:
- return (CallNextHookEx(hhk, nCode, wParam, lParam));
- }
-
- LRESULT CALLBACK AppWndProc(HWND hwnd, UINT mssg, WPARAM wParam, LPARAM lParam)
- {
- switch(mssg) {
- case WM_CREATE:
- hed1 = CreateWindow(szEDIT, 0, WS_CHILD | WS_VISIBLE | WS_BORDER,
- 10, 10, 300, 20, hwnd, (HMENU) 1000, hinst, 0);
- hed2 = CreateWindow(szEDIT, 0, WS_CHILD | WS_VISIBLE | WS_BORDER,
- 10, 60, 300, 20, hwnd, (HMENU) 1001, hinst, 0);
- hed3 = CreateWindow(szEDIT, 0, WS_CHILD | WS_VISIBLE | WS_BORDER,
- 10, 110, 300, 20, hwnd, (HMENU) 1002, hinst, 0);
- hhk = SetWindowsHookEx(WH_GETMESSAGE, GetMsgProc, 0, GetCurrentThreadId());
- return 0;
- case WM_SETFOCUS:
- SetFocus(hed1); return 0;
- case WM_DESTROY:
- if(hhk) UnhookWindowsHookEx(hhk);
- PostQuitMessage(0);
- return 0;
- }
- return DefWindowProc(hwnd, mssg, wParam, lParam);
- }
-
- DWORD InitInstance()
- {
- WNDCLASSEX wndcls;
- RECT rct;
- memset(&wndcls, 0, sizeof(WNDCLASSEX));
- wndcls.cbSize = sizeof(WNDCLASSEX);
- wndcls.lpfnWndProc = AppWndProc;
- wndcls.style = CS_HREDRAW | CS_VREDRAW;
- wndcls.hInstance = hinst;
- wndcls.lpszClassName = szappname;
- wndcls.hbrBackground = (HBRUSH) GetStockObject(GRAY_BRUSH);
- wndcls.hCursor = LoadCursor(0, IDC_ARROW);
- if(!RegisterClassEx(&wndcls)) return 0;
- hmain = CreateWindowEx(0, szappname, szappname, WS_OVERLAPPEDWINDOW | WS_VISIBLE,
- CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
- 0, 0, hinst, 0);
- return (hmain != 0);
- }
-
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, PSTR, int)
- {
- MSG msg;
- hinst = hInstance;
- if(!InitInstance()) return 0;
- ShowWindow(hmain, SW_NORMAL);
- while(GetMessage(&msg, NULL, 0, 0)) {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return 0;
- }
#include <windows.h>
HINSTANCE hinst;
HWND hmain, hed1, hed2, hed3;
HHOOK hhk = 0;
char szappname[] = "MsgHook";
char szEDIT[] = "EDIT";
// le HOOK LOCAL est ICI
LRESULT CALLBACK GetMsgProc(int nCode, WPARAM wParam, LPARAM lParam)
{
MSG *lpmsg;
lpmsg = (MSG*) lParam;
if(nCode < 0) goto defRet; // NE JAMAIS TOUCHER DANS CE CAS
if(lpmsg->message == WM_RBUTTONUP) {
if(IsChild(hmain, lpmsg->hwnd)) SetWindowText(lpmsg->hwnd, "Y a un HOOK");
else PostMessage(lpmsg->hwnd, WM_CLOSE, 0, 0);
return 1;
}
defRet:
return (CallNextHookEx(hhk, nCode, wParam, lParam));
}
LRESULT CALLBACK AppWndProc(HWND hwnd, UINT mssg, WPARAM wParam, LPARAM lParam)
{
switch(mssg) {
case WM_CREATE:
hed1 = CreateWindow(szEDIT, 0, WS_CHILD | WS_VISIBLE | WS_BORDER,
10, 10, 300, 20, hwnd, (HMENU) 1000, hinst, 0);
hed2 = CreateWindow(szEDIT, 0, WS_CHILD | WS_VISIBLE | WS_BORDER,
10, 60, 300, 20, hwnd, (HMENU) 1001, hinst, 0);
hed3 = CreateWindow(szEDIT, 0, WS_CHILD | WS_VISIBLE | WS_BORDER,
10, 110, 300, 20, hwnd, (HMENU) 1002, hinst, 0);
hhk = SetWindowsHookEx(WH_GETMESSAGE, GetMsgProc, 0, GetCurrentThreadId());
return 0;
case WM_SETFOCUS:
SetFocus(hed1); return 0;
case WM_DESTROY:
if(hhk) UnhookWindowsHookEx(hhk);
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, mssg, wParam, lParam);
}
DWORD InitInstance()
{
WNDCLASSEX wndcls;
RECT rct;
memset(&wndcls, 0, sizeof(WNDCLASSEX));
wndcls.cbSize = sizeof(WNDCLASSEX);
wndcls.lpfnWndProc = AppWndProc;
wndcls.style = CS_HREDRAW | CS_VREDRAW;
wndcls.hInstance = hinst;
wndcls.lpszClassName = szappname;
wndcls.hbrBackground = (HBRUSH) GetStockObject(GRAY_BRUSH);
wndcls.hCursor = LoadCursor(0, IDC_ARROW);
if(!RegisterClassEx(&wndcls)) return 0;
hmain = CreateWindowEx(0, szappname, szappname, WS_OVERLAPPEDWINDOW | WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
0, 0, hinst, 0);
return (hmain != 0);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, PSTR, int)
{
MSG msg;
hinst = hInstance;
if(!InitInstance()) return 0;
ShowWindow(hmain, SW_NORMAL);
while(GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}