BifaceMcLeOD |
Alload> Je te conseille d'utiliser le fichier "windowsx.h" pour écrire tes procédures de fenêtre en API. Ce fichier définit des tas de macros (les window message crackers) pour encapsuler les procédures de fenêtre, ce qui permet d'une part d'avoir une procédure de fenêtre super courte (un appel de macro sur une ligne par message traité), et d'autre part une fonction spécifique pour chaque message traité par la procédure de fenêtre, façon méthode de classe (chaque fonction ayant un prototype prédéfini en fonction du message qu'elle traite ; autre avantage : ses paramètres sont typés, car la macro a déjà extrait tout ce qu'il fallait des paramètres wParam et lParam).
Par exemple :Code :
- #include <windows.h>
- #include <windowsx.h>
- static char buffer[256];
- BOOL Wnd_OnCreate(HWND hwnd, LPCREATESTRUCT lpCreateStruct)
- {
- wsprintf(buffer, "Window Project." );
- return 0;
- }
- void Wnd_OnPaint(HWND hwnd)
- {
- PAINTSTRUCT ps;
- RECT rect;
- HDC hdc = BeginPaint(hwnd, &ps);
- GetClientRect(hwnd, &rect);
- DrawText(hdc, buffer, -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
- EndPaint(hwnd, &ps);
- wsprintf(buffer,"Window Project." );
- return 0;
- }
- void Wnd_OnLButtonDown(HWND hwnd, BOOL fDoubleClick, int x, int y, UINT keyFlags)
- {
- if (fDoubleClick) {
- wsprintf(buffer,"Vous avez double-cliqué sur la fenêtre." );
- InvalidateRect(hwnd, NULL, TRUE);
- UpdateWindow(hwnd);
- }
- return 0;
- }
- void Wnd_OnDestroy(HWND hwnd) {
- PostQuitMessage(0);
- return 0;
- }
- long WINAPI WndProc(HWND hwnd, UINT message, UINT wParam, long lParam)
- {
- switch (message) {
- return HANDLE_WM_CREATE (hwnd, wParam, lParam, Wnd_OnCreate);
- return HANDLE_WM_PAINT (hwnd, wParam, lParam, Wnd_OnPaint);
- return HANDLE_WM_LBUTTONDOWN(hwnd, wParam, lParam, Wnd_OnLButtonDown);
- return HANDLE_WM_DESTROY (hwnd, wParam, lParam, Wnd_OnDestroy);
- default: return DefWindowProc(hwnd, message, wParam, lParam);
- }
- }
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
- {
- HWND hwnd;
- WNDCLASSEX wndclassex;
- MSG msg;
-
- const char szClassName[] = "Window Class";
- wndclassex.lpszClassName = szClassName;
- wndclassex.cbSize = sizeof(wndclassex);
- wndclassex.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
- wndclassex.hInstance = hInstance;
- wndclassex.lpfnWndProc = WndProc;
- wndclassex.cbClsExtra = 0;
- wndclassex.cbWndExtra = 0;
- wndclassex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- wndclassex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
- wndclassex.hCursor = LoadCursor(NULL, IDC_ARROW);
- wndclassex.hbrBackground = GetStockBrush(WHITE_BRUSH);
- wndclassex.lpszMenuName = NULL;
- if (!RegisterClassEx(&wndclassex))
- return 1;
- hwnd = CreateWindowEx(0,
- szClassName,
- "Window Project",
- WS_OVERLAPPEDWINDOW | WS_VISIBLE,
- 100,
- 100,
- 640,
- 480,
- NULL,
- NULL,
- hInstance,
- NULL);
- ShowWindow(hwnd, nShowCmd);
- while (GetMessage(&msg, NULL, 0, 0)) {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return 0;
- }
|
En plus de tous ces message crackers, "windowsx.h" définit toute une liste de macros de manipulation des contrôles graphiques standard (bouton, liste déroulante, zone de texte, ....) ou qui encapsulent les fonctions noyau et qui sont utiles pour éclaicir le code.
Des trucs tout cons du genre :
au lieu de devoir écrire Code :
- ((GetWindowStyle(hwnd) & (WS_MINIMIZE | WS_MAXIMIZE)) == 0L)
|
ou encore
au lieu de Code :
- (GetKeyState(VK_LBUTTON) < 0)
|
qui sont bien moins clairs à relire. [edit]--Message édité par BifaceMcLeOD--[/edit] |