Warum zeichnet sich das Fenster nicht neu???



  • Hallo zusammen,

    habe es mitlerweile geschaft ein Fenster auf den Bildschirm zu zaubern
    welches sich in der groesse nicht veraendern laesst und nur ein X zum
    schliessen aufweist.
    Hatte mich in letzter Zeit hier mal nach dem Thema Timer erkundigt und
    das auch soweit hinbekommen. Getestet mit der MessageBox. 😃

    Nun mein Problem:
    Ich habe hier des oefteren gelesen das

    InvalidateRect(hwnd, NULL, TRUE);
    

    ein Fenster neu zeichnet, oder zumindestens dafuer sorgt das die WM_PAINT
    Message an das Fenster geschickt wird.

    Im folgenden Code sollte sich der angezeigte Text sekuendlich von A nach B und
    dann nach C aendern und dann wieder von vorn anfangen.
    Tut er aber nicht, sondern es steht immer nur das A da! 😡

    Jemand eine Idee??? 😕

    #include <windows.h>
    
    /*  Declare Windows procedure  */
    LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
    
    /*  Make the class name into a global variable  */
    char szClassName[ ] = "WindowsApp";
    
    int WINAPI
    WinMain (HINSTANCE hThisInstance,
             HINSTANCE hPrevInstance,
             LPSTR lpszArgument,
             int nFunsterStil)
    
    {
        HWND hwnd;               /* This is the handle for our window */
        MSG messages;            /* Here messages to the application are saved */
        WNDCLASSEX wincl;        /* Data structure for the windowclass */
    
        /* The Window structure */
        wincl.hInstance = hThisInstance;
        wincl.lpszClassName = szClassName;
        wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
        wincl.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;   /* Catch double-clicks */
        wincl.cbSize = sizeof (WNDCLASSEX);
    
        /* Use default icon and mouse-pointer */
        wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
        wincl.lpszMenuName = NULL;                 /* No menu */
        wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
        wincl.cbWndExtra = 0;                      /* structure or the window instance */
        /* Use actual Windows's color as the background of the window */
        wincl.hbrBackground = (HBRUSH) GetSysColorBrush(COLOR_WINDOW); //rueckgabewert von GetSysColorBrush wird gecastet
    
        /* Register the window class, and if it fails quit the program */
        if (!RegisterClassEx (&wincl))
            return 0;
    
        /* The class is registered, let's create the program*/
        hwnd = CreateWindowEx (
               0,                   /* Extended possibilites for variation */
               szClassName,         /* Classname */
               "Nicos Alter 4",       /* Title Text */
               WS_BORDER | WS_SYSMENU, /* thin border, size can't be change with close button */
               CW_USEDEFAULT,       /* Windows decides the position */
               CW_USEDEFAULT,       /* where the window ends up on the screen */
               200,                 /* The programs width */
               120,                 /* and height in pixels */
               HWND_DESKTOP,        /* The window is a child-window to desktop */
               NULL,                /* No menu */
               hThisInstance,       /* Program Instance handler */
               NULL                 /* No Window Creation data */
               );
    
        /* Make the window visible on the screen */
        ShowWindow (hwnd, nFunsterStil);
    
        /* Run the message loop. It will run until GetMessage() returns 0 */
        while (GetMessage (&messages, NULL, 0, 0))
        {
            /* Translate virtual-key messages into character messages */
            TranslateMessage(&messages);
            /* Send message to WindowProcedure */
            DispatchMessage(&messages);
        }
    
        /* The program return-value is 0 - The value that PostQuitMessage() gave */
        return messages.wParam;
    
    }
    
    /*  This function is called by the Windows function DispatchMessage()  */
    
    LRESULT CALLBACK
    WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    
    HDC hdc;		/* A device context used for drawing */
    PAINTSTRUCT ps;	/* Also used during window drawing */
    RECT rc;		/* A rectangle used during drawing */
    
    char cText = 'A';
    const UINT TimerID = 1;    
    const int iDelay = 1000;        //    = Zeit einstellen
    
        switch (message)                  /* handle the messages */
        {
            case WM_CREATE:
                {
                	SetTimer(hwnd, TimerID, iDelay, NULL); //Timer setzen
                	return 0;
    
                }
    
            case WM_TIMER:
               { 
    
                    if(cText == 'C')
                    	cText = 'A';
    
                    else
                    	cText++;
    
                    InvalidateRect(hwnd, NULL, TRUE); 
                	return 0;
                	break;
    
               }    
            case WM_DESTROY:
                {
                	KillTimer(hwnd, TimerID);	//nicht vergessen den Timer zu loeschen!!!
                 	PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
                	break;
                }
    
            case WM_PAINT:
                {
                	/* The window needs to be painted (redrawn). */
                	hdc = BeginPaint (hwnd, &ps);
                	GetClientRect (hwnd, &rc);
    
                    //Hintergrund der Schriftart transparent machen
                    SetBkMode(hdc,TRANSPARENT);	
    
                    //den Text ausgeben
                    DrawText (hdc, (LPCTSTR) &cText, -1, &rc, 
                   	DT_SINGLELINE | DT_CENTER | DT_VCENTER );
    
                   	EndPaint (hwnd, &ps);
                    return 0;
                    break;
            }    
            default:                      /* for messages that we don't deal with */
                return DefWindowProc (hwnd, message, wParam, lParam);
        }
    
        return 0;
    }
    

    Vielen Dank fuer eventuelle Tipps!!! 👍

    PS
    IDE ist DEV C++ falls das wichtig ist



  • schon mal den Debugger genommen ?

    Oder nimm mal UpdateWindow wenn Du eh die client area komplett neu zeichen willst ...

    MfG
    RB



  • Hi,

    UpdateWindow(hwnd);
    

    hat genau den selben Effekt, den, dass sich gar nichts tut.

    Mit der Fehlersuche habe ich auch nichts finden koennen.
    Sowohl bei:

    InvalidateRect(hwnd, NULL, TRUE);
    

    als auch bei:

    UpdateWindow(hwnd);
    

    laeuft das Programm ganz normal weiter.
    Danach laeuft die folgende while Schleife ein paar mal(undefiniert lange!)
    durch.

    while (GetMessage (&messages, NULL, 0, 0))
        {
            /* Translate virtual-key messages into character messages */
            TranslateMessage(&messages);
            /* Send message to WindowProcedure */
            DispatchMessage(&messages);
        }
    

    keine Ahnung was das ist. 😞



  • Dein char cText ist nicht statisch, verliert also jedesmal beim verlassen der WndProc mittels return ihren Wert.

    Ganz logisch, das dann nach der Zeile

    char cText = 'A';
    

    nur ein 'A' ausgegeben wird, oder?



  • Juhu,

    das wars! Danke!

    (habe jetzt nebenbei auch den Sinn von static verstanden) 😃


Anmelden zum Antworten