Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // GRABO Kapitel 3.3
- // Demo-Anwendung, zeichnet verschiedene Rechtecke in zwei Farben
- #include "WinMain.h"
- // hWnd = Fenster (handle)
- // wMessage = Event (INT / Konstante)
- LRESULT CALLBACK WndProc (HWND hWnd, UINT wMessage,
- WPARAM wParam, LPARAM lParam)
- {
- // Zustandsvariable des Fensters, Lebensdauer beachten!
- static POINT m; // Clientmitte
- static POINT p; // markierter Punkt
- static int cyan = FALSE; // Initialisierung zwingend
- switch (wMessage) { // Messageverteilung
- // Fenstererzeugung
- case WM_CREATE: {
- RECT innen; // Rechteck
- GetClientRect(hWnd, &innen);// Größe Client-Area von hWnd ermitteln und in innen speichern
- m.x = innen.right/2; // Rechteck immer ab Clientmitte
- m.y = innen.bottom/2;
- p.x = m.x + 1; // Rechteck erst nur ein Pixel
- p.y = m.y + 1;
- return 0;
- }
- // Sichtbarkeitsänderung
- case WM_PAINT: {
- HDC hDC; // Kontexthandle für Fenster
- PAINTSTRUCT ps; // Datenstruktur für BeginPaint
- HPEN hPen;
- hDC = BeginPaint (hWnd, &ps);
- if (cyan) {
- hPen = CreatePen(PS_SOLID, 0, RGB(0,255,255)); // Pen cyan
- SelectObject(hDC, hPen);
- }
- Rectangle(hDC, m.x, m.y, p.x, p.y); // Rechteck zeichnen
- EndPaint (hWnd, &ps);
- if (cyan) DeleteObject(hPen); // erst wenn nicht in Kontext
- return 0;
- }
- // Druck auf LINKE Maustaste
- case WM_LBUTTONDOWN: { // Rechteckgröße ändern
- p.x = LOWORD(lParam); // Ermitteln Mauskoordinaten
- p.y = HIWORD(lParam);
- InvalidateRect(hWnd, NULL, TRUE); // WM_PAINT erzeugen
- return 0;
- }
- // Druck auf RECHTE Maustaste
- case WM_RBUTTONDOWN: { // Rechteckfarbe ändern
- cyan = !cyan;
- InvalidateRect(hWnd, NULL, TRUE); // Event von Typ WM_PAINT erzeugen
- return 0;
- }
- // Hauptfenster der Anwedung wird geschlossen
- case WM_DESTROY: {
- PostQuitMessage(0); // WM_QUIT erzeugen
- return 0;
- }
- } // end switch
- return DefWindowProc (hWnd, wMessage, wParam, lParam);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement