Fragen zur GDI und zum Stil.
-
Hallo,
ich habe ein kleines Manko. Ich habe ein wenig mit der GDI gespielt und wenn ich mein Programm öffne sollten eigentlich die Zeichnungen sofort erscheinen... Doch das tun sie nicht. Erst wenn von Windows aus das Ereignis WM_PAINT aufgerufen wird (also z.B. durch verändern der Größe des Fensters) wird die Zeichnung dargestellt. Wenn ich ihn ein wenig ändere funktioniert es... warum nur?Code:
#include <windows.h> #include <cmath> LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { static TCHAR szAppName[] = TEXT("GDI"); HWND hwnd; MSG msg; WNDCLASS wndclass; wndclass.style = CS_HREDRAW | CS_VREDRAW; wndclass.lpfnWndProc = WndProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = hInstance; wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wndclass.lpszMenuName = NULL; wndclass.lpszClassName = szAppName; if(!RegisterClass(&wndclass)) return 0; hwnd = CreateWindow(szAppName, TEXT("GDI - Grundlagen"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); ShowWindow(hwnd, iCmdShow); UpdateWindow(hwnd); while(GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRUCT ps; static int xPos, yPos; const int DIM = 10000; const double double_PI = 2 * 3.14159; static POINT points[DIM]; for(int i = 0; i < DIM; i++) //Es geht um diesen Teil { points[i].x = i * xPos / DIM; points[i].y = (int)(yPos / 2 * (1 - sin(double_PI * i / DIM))); } switch(message) { case WM_SIZE: xPos = LOWORD(lParam); yPos = HIWORD(lParam); case WM_PAINT: hdc = BeginPaint(hwnd, &ps); MoveToEx(hdc, 0, 0, NULL); LineTo(hdc, xPos, yPos); MoveToEx(hdc, 0, yPos, NULL); LineTo(hdc, xPos, 0); Polyline(hdc, &points[0], DIM); EndPaint(hwnd, &ps); return 0; case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hwnd, message, wParam, lParam); }veränderter Code:
#include <windows.h> #include <cmath> LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { static TCHAR szAppName[] = TEXT("GDI"); HWND hwnd; MSG msg; WNDCLASS wndclass; wndclass.style = CS_HREDRAW | CS_VREDRAW; wndclass.lpfnWndProc = WndProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = hInstance; wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wndclass.lpszMenuName = NULL; wndclass.lpszClassName = szAppName; if(!RegisterClass(&wndclass)) return 0; hwnd = CreateWindow(szAppName, TEXT("GDI - Grundlagen"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); ShowWindow(hwnd, iCmdShow); UpdateWindow(hwnd); while(GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRUCT ps; static int xPos, yPos; const int DIM = 10000; const double double_PI = 2 * 3.14159; static POINT points[DIM]; switch(message) { case WM_SIZE: xPos = LOWORD(lParam); yPos = HIWORD(lParam); case WM_PAINT: hdc = BeginPaint(hwnd, &ps); MoveToEx(hdc, 0, 0, NULL); LineTo(hdc, xPos, yPos); MoveToEx(hdc, 0, yPos, NULL); LineTo(hdc, xPos, 0); for(int i = 0; i < DIM; i++) //Es geht um diesen Teil { points[i].x = i * xPos / DIM; points[i].y = (int)(yPos / 2 * (1 - sin(double_PI * i / DIM))); } Polyline(hdc, &points[0], DIM); EndPaint(hwnd, &ps); return 0; case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hwnd, message, wParam, lParam); }Wenn ich die for-Schleife also in das WM_PAINT Ereignis setze, dann funktioniert die ganze Sache... Aber ich dachte bei UpdateWindow(my_handle_to_window) wird die WndProc mit WM_PAINT als message-Parameter (der 2. Parameter) angegeben (hoffe ihr versteht den Satz
).Und jetzt zu dem Stil:
Schreib man folgenden Kram immer in die WndProc oder sollte ich das lieber auslagern in einer Funktion oder so? :const int DIM = 10000; const double double_PI = 2 * 3.14159; static POINT points[DIM]; for(int i = 0; i < DIM; i++) //Es geht um diesen Teil { points[i].x = i * xPos / DIM; points[i].y = (int)(yPos / 2 * (1 - sin(double_PI * i / DIM))); }Hoffe ihr könnt meine Fragen beantworten

Gruß Speedy_92
-
Kann denn keiner helfen?
-
1. Man errechnet solche Datennatürlich nur einmal, wenn sich die Parameter ändern. Dh.h bei Die bei Größenänderung, also bei Entreffen WM_SIZE Nachricht. Ales andere wäre Resourcenverschwendung.
2. Es wird ur in WM_PAINT gezeichnet, das ist eben so. Ich sehe allerdings kein Problem warum icht auch sofort die Zeichnung erscheinen soll, denn UpdateWindow wird ja sofort nach Erzeugen der Fenster aufgerufen.
3. Der Fallthrough aus WM_SIZE nach WM_PAINT ist definitv falsch.