Trouver une ressource
Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum.
Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !
UTILISATION DES TOUCHES TAB, ENTREE ET ECHAP DANS UNE FENÊTRE WIN32.
Information sur la source
Description
On rencontre souvent des questions dans le forum portant sur l'utilisation de la touche TAB pour naviguer entre les différents contrôles d'une fenêtre. Le présent code source montre comment ajouter cette fonctionnalité en utilisant la fonction IsDialogMessage() dans la boucle des messages après avoir donné le style WS_TABSTOP aux contrôles parmi lesquels on veut naviguer. Je profite de ce code source pour ajouter une autre fonctionnalité souvent demandée aussi. Il s'agit de l'utilisation de la touche ENTREE pour actionner un bouton quelque soit le contrôle ayant le focus. Pour cela il suffit de lui donner IDOK comme identificateur. Pareil pour la touche ECHAP ayant comme identificateur IDCANCEL. La fonction IsDialogMessage() ,vue plus haut, est indispensable dans tous les cas. J'espère que ce sera utile pour certains. Pour tester l'exécutable renommez le en ToucheTab.exe
Source
- #include <windows.h>
-
- LRESULT CALLBACK WinProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- // Déclaration des variables en static:
- static HWND hedit1,hedit2,hedit3,hok,hquitter;
- switch(message)
- {
- case WM_CREATE:
- {
- // Création des contrôles:
- hedit1=CreateWindowEx(WS_EX_CLIENTEDGE,"edit","",WS_CHILD | WS_VISIBLE | WS_TABSTOP,20,20,100,20,hwnd,0,0,0);
- hedit2=CreateWindowEx(WS_EX_CLIENTEDGE,"edit","",WS_CHILD | WS_VISIBLE | WS_TABSTOP,20,60,100,20,hwnd,0,0,0);
- hedit3=CreateWindowEx(WS_EX_CLIENTEDGE,"edit","",WS_CHILD | WS_VISIBLE | WS_TABSTOP,20,100,100,20,hwnd,0,0,0);
- hok=CreateWindowEx(0,"button","Ok",WS_CHILD | WS_VISIBLE | WS_TABSTOP ,150,40,100,20,hwnd,(HMENU)IDOK,0,0);
- hquitter=CreateWindowEx(0,"button","Quitter",WS_CHILD | WS_VISIBLE | WS_TABSTOP,150,80,100,20,hwnd,(HMENU)IDCANCEL,0,0);
- // Changement de la police des contrôles:
- HFONT font=(HFONT)GetStockObject(DEFAULT_GUI_FONT);
- HWND child=FindWindowEx(hwnd,0,0,0);
- do
- {
- SendMessage(child,WM_SETFONT,(WPARAM)font,0);
- child=FindWindowEx(hwnd,child,0,0);
- }while(child);
- // Mettre le focus sur le premier EDIT:
- SetFocus(hedit1);
- return 0;
- }
- case WM_CLOSE:
- // Détruire la fenêtre:
- DestroyWindow(hwnd);
- return 0;
- case WM_DESTROY:
- // Forcer la sortie de la boucle des messages:
- PostQuitMessage(0);
- return 0;
- case WM_COMMAND:
- // Clic sur le bouton Ok ou appui sur la touche ENTREE:
- if((HWND)lParam==hok) MessageBox(hwnd,"Bouton Ok utilisé","Touche TAB",0);
- // Clic sur le bouton Quitter ou appui sur la touche ECHAP:
- if((HWND)lParam==hquitter)SendMessage(hwnd,WM_CLOSE,0,0);
- return 0;
- default:
- break;
- }
- // Appeler la procedure par défaut de la fenêtre:
- return DefWindowProc(hwnd,message, wParam, lParam);
- }
-
- int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hprev, LPSTR cmd, int show)
- {
- // Déclaration et initialisation de la structure WNDCLASSEX:
- WNDCLASSEX wc;
- ZeroMemory(&wc,sizeof(WNDCLASSEX));
- wc.cbSize=sizeof(WNDCLASSEX);
- wc.hInstance=hinst;
- wc.lpszClassName="fenetre";
- wc.lpfnWndProc=WinProc;
- wc.hCursor=LoadCursor(0,IDC_ARROW);
- wc.hIcon=LoadIcon(0,IDI_APPLICATION);
- wc.hbrBackground=(HBRUSH)GetStockObject(LTGRAY_BRUSH);
- // Enregistrement de notre classe de fenêtre:
- RegisterClassEx(&wc);
- // Création de notre fenêtre:
- HWND hwnd=CreateWindowEx(0,"fenetre","Utilisation de la touche TAB",WS_SYSMENU | WS_VISIBLE ,0,0,280,170,0,0,hinst,0);
- // Boucle des messages:
- MSG msg;
- while(GetMessage(&msg,0,0,0))
- {
- if(!IsDialogMessage(hwnd,&msg))
- {
- DispatchMessage(&msg);
- TranslateMessage(&msg);
- }
- }
- // Sortie du programme:
- return 0;
- }
#include <windows.h>
LRESULT CALLBACK WinProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
// Déclaration des variables en static:
static HWND hedit1,hedit2,hedit3,hok,hquitter;
switch(message)
{
case WM_CREATE:
{
// Création des contrôles:
hedit1=CreateWindowEx(WS_EX_CLIENTEDGE,"edit","",WS_CHILD | WS_VISIBLE | WS_TABSTOP,20,20,100,20,hwnd,0,0,0);
hedit2=CreateWindowEx(WS_EX_CLIENTEDGE,"edit","",WS_CHILD | WS_VISIBLE | WS_TABSTOP,20,60,100,20,hwnd,0,0,0);
hedit3=CreateWindowEx(WS_EX_CLIENTEDGE,"edit","",WS_CHILD | WS_VISIBLE | WS_TABSTOP,20,100,100,20,hwnd,0,0,0);
hok=CreateWindowEx(0,"button","Ok",WS_CHILD | WS_VISIBLE | WS_TABSTOP ,150,40,100,20,hwnd,(HMENU)IDOK,0,0);
hquitter=CreateWindowEx(0,"button","Quitter",WS_CHILD | WS_VISIBLE | WS_TABSTOP,150,80,100,20,hwnd,(HMENU)IDCANCEL,0,0);
// Changement de la police des contrôles:
HFONT font=(HFONT)GetStockObject(DEFAULT_GUI_FONT);
HWND child=FindWindowEx(hwnd,0,0,0);
do
{
SendMessage(child,WM_SETFONT,(WPARAM)font,0);
child=FindWindowEx(hwnd,child,0,0);
}while(child);
// Mettre le focus sur le premier EDIT:
SetFocus(hedit1);
return 0;
}
case WM_CLOSE:
// Détruire la fenêtre:
DestroyWindow(hwnd);
return 0;
case WM_DESTROY:
// Forcer la sortie de la boucle des messages:
PostQuitMessage(0);
return 0;
case WM_COMMAND:
// Clic sur le bouton Ok ou appui sur la touche ENTREE:
if((HWND)lParam==hok) MessageBox(hwnd,"Bouton Ok utilisé","Touche TAB",0);
// Clic sur le bouton Quitter ou appui sur la touche ECHAP:
if((HWND)lParam==hquitter)SendMessage(hwnd,WM_CLOSE,0,0);
return 0;
default:
break;
}
// Appeler la procedure par défaut de la fenêtre:
return DefWindowProc(hwnd,message, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hprev, LPSTR cmd, int show)
{
// Déclaration et initialisation de la structure WNDCLASSEX:
WNDCLASSEX wc;
ZeroMemory(&wc,sizeof(WNDCLASSEX));
wc.cbSize=sizeof(WNDCLASSEX);
wc.hInstance=hinst;
wc.lpszClassName="fenetre";
wc.lpfnWndProc=WinProc;
wc.hCursor=LoadCursor(0,IDC_ARROW);
wc.hIcon=LoadIcon(0,IDI_APPLICATION);
wc.hbrBackground=(HBRUSH)GetStockObject(LTGRAY_BRUSH);
// Enregistrement de notre classe de fenêtre:
RegisterClassEx(&wc);
// Création de notre fenêtre:
HWND hwnd=CreateWindowEx(0,"fenetre","Utilisation de la touche TAB",WS_SYSMENU | WS_VISIBLE ,0,0,280,170,0,0,hinst,0);
// Boucle des messages:
MSG msg;
while(GetMessage(&msg,0,0,0))
{
if(!IsDialogMessage(hwnd,&msg))
{
DispatchMessage(&msg);
TranslateMessage(&msg);
}
}
// Sortie du programme:
return 0;
}
Sources de la même categorie
Commentaires
Discussions en rapport avec ce code source
|
Téléchargements
Logiciels à télécharger sur le même thème :
|