1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| #include <windows.h>
#define IDC_BOUTON1 1000
#define IDC_BOUTON2 1001
/* On utilise le pattern SESE (Single Entry, Single Exit)
sinon on ne s'en sort pas. */
LRESULT CALLBACK OnEvent(HWND Handle, UINT Message, WPARAM WParam, LPARAM LParam) {
LRESULT ret = 0;
switch (Message) {
case WM_DESTROY :
PostQuitMessage(0);
break;
case WM_COMMAND:
switch (LOWORD(WParam)) {
case IDC_BOUTON1:
MessageBox(NULL, TEXT("Vous avez poussé le Bouton1 !"), TEXT("Bouton1"), 0);
break;
case IDC_BOUTON2:
MessageBox(0, TEXT("Vous avez poussé le Bouton2 !"), TEXT("Bouton2"), 0);
break;
default:
ret = DefWindowProc(Handle, Message, WParam, LParam);
break;
}
break;
default:
ret = DefWindowProc(Handle, Message, WParam, LParam);
break;
}
return ret;
}
INT WINAPI WinMain(HINSTANCE Instance, HINSTANCE, LPSTR, INT) {
WNDCLASS WC;
WC.style = 0;
WC.lpfnWndProc = &OnEvent;
WC.cbClsExtra = 0;
WC.cbWndExtra = 0;
WC.hInstance = Instance;
WC.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WC.hCursor = LoadCursor(NULL, IDC_ARROW);
WC.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_BACKGROUND);
WC.lpszMenuName = 0;
WC.lpszClassName = TEXT("Exercice");
RegisterClass(&WC);
HWND Window = CreateWindow(
TEXT("Exercice"), TEXT("Exemple"), WS_SYSMENU | WS_OVERLAPPEDWINDOW | WS_VISIBLE,
0, 0, 800, 600, NULL, NULL, Instance, NULL
);
HWND bouton1 = CreateWindow(
TEXT("BUTTON"), TEXT("Bouton1"), WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS ,
85, 50, 100, 50, Window, reinterpret_cast<HMENU>(IDC_BOUTON1), Instance, NULL
);
HWND bouton2 = CreateWindow(
TEXT("BUTTON"), TEXT("Bouton2"), WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS,
185, 50, 100, 50, Window, reinterpret_cast<HMENU>(IDC_BOUTON2), Instance, NULL
);
//Pourquoi une boucle d'attente active?
/*MSG Message;
Message.message = ~WM_QUIT;
while (Message.message != WM_QUIT) {
if (PeekMessage(&Message, NULL, 0, 0, PM_REMOVE))
TranslateMessage(&Message);
DispatchMessage(&Message);
}
*/
//DestroyWindow(Window); Tu fais ça mal. La fenêtre soit se détruire elle-même et juste prévenir qu'elle est détruite.
MSG Message;
while (GetMessage(&Message, NULL, 0, 0) > 0)
{
TranslateMessage(&Message); //Traduit WM_KEYDOWN en WM_CHAR
DispatchMessage(&Message);
}
UnregisterClass("Exercice", Instance);
return 0;
} |
Partager