begin process at 2010 02 10 16:56:01
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

API

 > HOOK SUR FENETRE (WIN32)

HOOK SUR FENETRE (WIN32)


 Information sur la source

Note :
10 / 10 - par 1 personne
10,00 / 10

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10
Catégorie :API Niveau :Débutant Date de création :26/06/2003 Date de mise à jour :26/06/2003 21:00:51 Vu / téléchargé :5 542 / 522

Auteur : BruNews

Ecrire un message privé
Site perso
Ce membre participe au partage de revenus publicitaires
Commentaire sur cette source (8)
Ajouter un commentaire et/ou une note


 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

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


 Fichier Zip

Les Membres Club peuvent télécharger directement un fichier contenu dans le zip sans télécharger le zip en entier !

Télécharger le zip


 Sources du même auteur

Source avec Zip Source avec une capture CALENDRIER (WIN64)
Source avec Zip COMMENTER CODE C <=> ASM (WIN64)
Source avec Zip CHANGEUR DATE FICHIER (WIN32)
Source avec Zip TESTS DE TRIS (WIN32)
Source avec Zip TXT SUPPRIMER LIGNES DOUBLONS (WIN32)

 Sources de la même categorie

Source avec Zip Source avec une capture CALENDRIER (WIN64) par BruNews
Source avec Zip Source avec une capture IMPRESSION EN WIN32 API AVEC OPTIONS par racpp
Source avec Zip Source avec une capture INFOTIP SHELL EXTENSION (BULLE DE L'EXPLORATEUR WINDOWS) (WI... par racpp
Source avec Zip Source avec une capture BROUILLAGE DES FICHIERS JAVASCRIPT ET CSS(WIN32) par gagah1
Source avec Zip Source avec une capture CHANGE CURSEUR par ganjarasta

Commentaires et avis

Commentaire de Anacr0x le 27/06/2003 15:18:36

Super pratique ta source, j'arrive enfin a utiliser les hook (^_^)
Mais sinon il me reste un problème, j'aimerais faire un hook sur la touche entrée, donc je remplace WM_RBUTTONUP par WM_KEYDOWN mais je ne sais pas comment préciser la touche (je crois kil s'agit de la 13), comment faire ?

Commentaire de BruNews le 27/06/2003 20:49:41 administrateur CS

VK_RETURN ou 13 pour ENTER.
Toujours bien etudier s'il n'y a pas une autre solution + simple, comme pour les thread il ne faut pas en mettre partout. Souvent un simple sous classement de fenetre fera parfaitement l'affaire.

Commentaire de Anacr0x le 28/06/2003 18:40:52

merci, mais VK_RETURN ne marche pas, c'est sans doute moi ki m'y prend mal ;)

Commentaire de yserver le 22/02/2006 17:52:05

Merci pour l'exemple. Ca me retire une épine du pied.
Je souhaitais hooker aussi la souris dans une de mes application. Grace a toi je vois mieux le principe.
J'etais en train de me fourvoyer et de perdre beaucoup de temps. Je m'interessais au hook de souris au lieu des message. Normalement cela devrait etre suffisant. Si non je reprendrais mon Hook de souris.
Ce que je souhaite faire c'est dans un conteneur ActiveX : IWebBrowser2 popur enpecher le clic droit ou le remplacer par mon propre menu.

Merci de ta participation

Commentaire de vecchio56 le 25/11/2006 00:24:40 administrateur CS

Dans ce hook, tu interceptes WM_RBUTTONUP
Je souhaiterais faire la même chose, mais avec WM_DRAWITEM et WM_MEASUREITEM.
Visiblement ils ne sont pas interceptés par ce hook, je vois pas pourquoi...
Tu as une idée?

Commentaire de BruNews le 25/11/2006 01:15:57 administrateur CS

Pas le temps de tester mais si tu as une pompe à messages, prends les direct à cet endroit, c'est normalement imparable. J'ai fait ainsi dans un autre exemple de source pour les raccourcis claviers, tout simple à implémenter et va impec.

Commentaire de vecchio56 le 25/11/2006 08:09:28 administrateur CS

Le problème est que je l'utilise pour des menus owner-draw.
Et vu que les menus ont leur propre boucles de messages, je ne peux pas récupérer les WM_DRAWITEM, c'est pourquoi j'ai pensé au hook

Commentaire de open le 10/06/2007 01:17:25

J'ai encore tenté de la compilé cette source ça plante arf, sinon ça a l'air interessant. :)

 Ajouter un commentaire




Nos sponsors


Sondage...

CalendriCode

Février 2010
LMMJVSD
1234567
891011121314
15161718192021
22232425262728

Consulter la suite du CalendriCode

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 1,014 sec (3)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales