Probleme mit DrawText(), keine Ausgabe
-
Hallo alle zusammen,
habe ein kleines Problem, das mich doch etwas festhält.
Folgender Code:
void DrawInitText() { HDC hdc ; PAINTSTRUCT ps ; RECT rect ; hdc = BeginPaint (hWnd, &ps) ; GetClientRect (hWnd, &rect) ; DrawText (hdc, TEXT ("Hello, World!"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER) ; TextOut (hdc, 20, 20, L"Ich bin ein Fenster.", 20); // device context, x, y, string, string-Länge EndPaint (hWnd, &ps) ; }Leider bekomme ich keine Ausgabe, wenn ich diese Methode aufrufe. Wo ist mein Denkfehler?
Danke euch und Gruß
Toby
-
Ich denke das muss in WM_PAINT:
#include <windows.h> LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ; int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { static TCHAR szAppName[] = TEXT ("HelloWin") ; 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)) { // UNICODE-Compilierung ist die einzige realistische Fehlermöglichkeit MessageBox (NULL, TEXT ("Programm arbeitet mit Unicode und setzt Windows NT voraus!"), szAppName, MB_ICONERROR) ; return 0 ; } hwnd = CreateWindow (szAppName, // Name der Fensterklasse TEXT ("Das erste echte Programm"), // Fenstertitel WS_OVERLAPPEDWINDOW, // Fensterstil CW_USEDEFAULT, // X-Position des Fensters CW_USEDEFAULT, // Y-Position des Fensters CW_USEDEFAULT, // Fensterbreite CW_USEDEFAULT, // Fensterhöhe NULL, // übergeordnetes Fenster NULL, // Menü hInstance, // Programm-Kopiezähler (Programm-ID) NULL) ; // zusätzliche Parameter 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) { switch (message) { case WM_CREATE: return 0 ; case WM_PAINT: // So funktioniert es bei mir HDC hdc ; PAINTSTRUCT ps ; RECT rect ; hdc = BeginPaint (hwnd, &ps) ; GetClientRect (hwnd, &rect) ; DrawText (hdc, TEXT ("Hello, World!"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER) ; TextOut (hdc, 20, 20, L"Ich bin ein Fenster.", 20); // device context, //x, y, string, string-Länge EndPaint (hwnd, &ps) ; return 0 ; case WM_DESTROY: PostQuitMessage (0) ; return 0 ; } return DefWindowProc (hwnd, message, wParam, lParam) ; }EDIT: Wenn du es außerhalb von WM_PAINT machen willst, kannst du GetDC und ReleaseDC statt Begin und EndPaint verwenden.
-
Danke, gut zu wissen, wofür GetDC and ReleaseDC gut sind und wofür WM_Paint gut ist
-
Hab das jetzt eingebaut.
Die DrawText Methode wird allerdings in gesonderter Klasse (als Singelton) aufgerufen. Habe jetzt mehrere Probleme:
1. Wie funktioniert Resizing? (bitte nicht immer "von Hand" alles neu zeichnen...)
2. Wieso verschwindet der Text wenn ein anderes Fenster überlappt?
3. Wie kann ich eine ScrollBar einbauen, wenn der Text am Ende des Fensters ist?Danke nochmals für eure Hilfe.
-
Das Problem ist, dass wenn ein anderes Fenster über deines kommt oder wenn die Größe geändert wird usw. wird WM_PAINT aufgerufen (zum neuzeichnen). Aber deine Textausgabe ist ja nicht ihn WM_PAINT, von daher wird es auch nicht erneut gezeichnet.
-
evt.steh ich auch auf dem Schlauch, aber wie setz ich dann Klassen in WM_Paint, die will ich ja von überall in meinem Code aus aufrufen können?
Danke und Gruß
Toby