J'aimerai créer un événement appuyer sur une touche de type F3 mais cet événement doit se réaliser à chaque fois que j'appuie sur cette touche quelque soit le widget qui a le focus et je ne veux pas à avoir à recopier ce traitement pour chaque objet, voici mon code où je crée une fenêtre avec une zone de texte et un bouton je traite l'action appuyer sur la touche F3 qui fonctionnne au lancement de la fenetre mais dès que j'utilise je suis sur un deux widget, la touche F3 ne réponds plus.
Aidez moi
#include <windows.h>
//#include <windowsx.h>
#include <Shellapi.h> // Pour l'icone
LRESULT CALLBACK toto(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK tutu(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
HINSTANCE g_hInst;
WNDPROC OldScroll[2] ;
LRESULT CALLBACK WndProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
TCHAR szFormat[] = TEXT ("%-16s%04X-%04X %04X-%04X"),
szBuffer[50] ;
static HWND g_hWnd[2];
HINSTANCE hInstance ;
switch(uMsg){
case WM_CREATE:
hInstance = (HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE) ;
g_hWnd[0] = CreateWindow("Edit","Entrer",WS_CHILD|WS_VISIBLE|WS_TABSTOP|ES_UPPERCASE,50,50,60,17,hwnd,(HMENU)0,hInstance,NULL);
g_hWnd[1] = CreateWindow("Button","Entrer",WS_CHILD|WS_VISIBLE|WS_TABSTOP|BS_PUSHBUTTON,50,80,61,17,hwnd,(HMENU)1,hInstance,NULL);
OldScroll[0] = (WNDPROC) SetWindowLong (g_hWnd[0], GWL_WNDPROC, (LONG) toto) ;
OldScroll[1] = (WNDPROC) SetWindowLong (g_hWnd[1], GWL_WNDPROC, (LONG) tutu) ;
return 0;
case WM_SIZE:
MoveWindow(g_hWnd[0],50,50,60,17,TRUE);
MoveWindow(g_hWnd[1],50,80,60,17,TRUE);
SetFocus (hwnd) ;
return 0 ;
case WM_KEYDOWN:
if( wParam == VK_F1)
MessageBox(hwnd,"Test de la touche F1","Test",MB_OK);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
BOOL InitApplication(HINSTANCE g_hInst)
{
WNDCLASSEX wcx;
static TCHAR szAppName[] = TEXT ("Peanuts") ;
// Fill in the window class structure with parameters
// that describe the main window.
wcx.cbSize = sizeof(wcx); // size of structure
wcx.style = CS_HREDRAW |
CS_VREDRAW; // redraw if size changes
wcx.lpfnWndProc = WndProc; // points to window procedure
wcx.cbClsExtra = 0; // no extra class memory
wcx.cbWndExtra = 0; // no extra window memory
wcx.hInstance = g_hInst; // handle to instance
//wcx.hIcon = LoadIcon(NULL,IDI_APPLICATION); // predefined app. icon
wcx.hIcon = ExtractIcon(g_hInst,"peanuts.ico",0); // file icon
wcx.hCursor = LoadCursor(NULL, IDC_ARROW); // predefined arrow
wcx.hbrBackground = (HBRUSH)GetStockObject(
WHITE_BRUSH); // white background brush
wcx.lpszMenuName = "MainMenu"; // name of menu resource
wcx.lpszClassName = "Test"; // name of window class
wcx.hIconSm = (HICON)LoadImage(g_hInst, // small class icon
MAKEINTRESOURCE(5),
IMAGE_ICON,
GetSystemMetrics(SM_CXSMICON),
GetSystemMetrics(SM_CYSMICON),
LR_DEFAULTCOLOR);
// Register the window class.
return RegisterClassEx(&wcx);
}
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
MSG msg;
HWND g_hWnd;
g_hInst = hInstance;
if( !InitApplication(g_hInst) )
{
return 0;
}
g_hWnd = CreateWindow(
"Test", // name of window class
"Essaie", // title-bar string
WS_OVERLAPPEDWINDOW, // top-level window
CW_USEDEFAULT, // default horizontal position
CW_USEDEFAULT, // default vertical position
800, // default width
600, // default height
(HWND) NULL, // no owner window
(HMENU) NULL, // use class menu
g_hInst, // handle to application instance
(LPVOID) NULL); // no window-creation data
if( !g_hWnd )
{
return 0;
}
ShowWindow(g_hWnd, SW_NORMAL);
UpdateWindow(g_hWnd);
while(1)
{
if( PeekMessage(&msg, 0,0,0,PM_REMOVE) )
{
if( msg.message == WM_QUIT )
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (0);
}
LRESULT CALLBACK toto(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
LONG id;
id = GetWindowLong (hwnd, GWL_ID) ;
switch (uMsg)
{
case WM_KEYDOWN :
if (wParam == VK_TAB)
MessageBox(hwnd,"rien","Coucou",MB_OK);
break ;
case WM_SETFOCUS :
//idFocus = id ;
break ;
}
return CallWindowProc (OldScroll[id], hwnd, uMsg, wParam, lParam) ;
}
LRESULT CALLBACK tutu(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
int id = GetWindowLong (hwnd, GWL_ID) ;
int tmpId;
short u;
TCHAR szFormat[] = TEXT ("%-16s%04X-%04X %04X-%04X"),
szBuffer[50] ;
switch (uMsg)
{
case WM_COMMAND:
wsprintf (szBuffer, szFormat, TEXT ("WM_COMMAND"), HIWORD (wParam), LOWORD (wParam),
HIWORD (lParam), LOWORD (lParam)) ;
MessageBox(hwnd,szBuffer,"Test",MB_OK);
SetFocus(hwnd);
break;
case WM_KEYDOWN :
if (wParam == VK_TAB)
u = GetKeyState(VK_SHIFT);
if(u<0)
tmpId = -1;
else
tmpId = 1;
tmpId = (tmpId + id)%2;
SetFocus (GetDlgItem (GetParent (hwnd), tmpId)) ;
break ;
case WM_SETFOCUS :
//idFocus = id ;
break ;
}
return CallWindowProc (OldScroll[id] , hwnd, uMsg, wParam, lParam) ;
}