Hook sur fenetre (win32)

Description

Comme promis un exemple de hook. C'set tout bete car je n'avais aucune idee de quoi faire.
Il y a une fenetre avec 3 edit. Le hook intercepte les messages WM_RBUTTONUP (releve du bouton droit de la souris). Si fenetre cible est fenetre principale elle se ferme sinon on ecrit "Y a un HOOK" dans fenetre fille, edit dans ce cas.

Source / Exemple :


#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;
}

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.