Thread: InvalidateRect

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    932

    InvalidateRect

    Im trying to refresh one part of the window with InvalidateRect but it wont get refreshed right away. It will be if you move another window over it.

    What am I doing wrong?

    Code:
    #include <windows.h>
    #include <iostream>
    using namespace std;
    
    #pragma comment(linker,"/subsystem:\"console\" /entry:\"WinMainCRTStartup\"")
    
    #define IDB_SOURCE    1008
    
    const RECT rText={20,100,420,25};
    char buf[260]="aaaaaaaaaaaaa";
    HWND hwnd;
    LRESULT CALLBACK WindowFunc(HWND,UINT,WPARAM,LPARAM);
    
    int WINAPI WinMain(HINSTANCE hThisInst,
                       HINSTANCE hPrevInst,
                       LPSTR Args,int WinMode)
    {
        MSG Message;
        WNDCLASS WinClass={0};
    
        char * Class="Find Files";
    
        WinClass.hInstance    =hThisInst;
        WinClass.lpszClassName=Class;
        WinClass.lpfnWndProc  =WindowFunc;
        WinClass.style        =0;
        WinClass.hIcon        =LoadIcon(NULL,IDI_APPLICATION);
        WinClass.hCursor      =LoadCursor(NULL,IDC_ARROW);
        WinClass.lpszMenuName =NULL;
        WinClass.cbClsExtra   =0;
        WinClass.cbWndExtra   =0;
        WinClass.hbrBackground=(HBRUSH) GetStockObject(LTGRAY_BRUSH);
    
        if(!RegisterClass(&WinClass)) return 0;
    
        DWORD dwStyle=(WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU |
                       WS_MINIMIZEBOX | WS_VISIBLE);
        hwnd=CreateWindow(Class,
                            TEXT("Search"),
                            dwStyle,
                            100,100,
                            600,400,
                            NULL,
                            NULL,
                            hThisInst,
                            NULL);
        if(hwnd==NULL)
        {
            //ReportError("CreateWindow");
            return 0;
        }
    
        while(GetMessage(&Message,NULL,0,0))
        {
            TranslateMessage(&Message);
            DispatchMessage(&Message);
        }
        return Message.wParam;
    }
    /// ////////////////////////////////////////////////////
    ///
    LRESULT CALLBACK WindowFunc(HWND hwnd,UINT Message,WPARAM wParam,LPARAM lParam)
    {
        HDC hdc=0;
        PAINTSTRUCT ps;
    
        switch(Message)
        {
        case WM_CREATE:
        {
            CreateWindowEx(0,TEXT("button"),
                           TEXT("Clickme"),
                           WS_VISIBLE | WS_CHILD,
                           20,140,60,20,
                           hwnd,(HMENU)IDB_SOURCE,0,NULL);
    
            return 0;
        }
    
        case WM_COMMAND:
            switch(wParam)
            {
            case IDB_SOURCE:
                sprintf(buf, "bbbbbbbbbbbbbb");
                InvalidateRect(hwnd, &rText, TRUE);
                UpdateWindow(hwnd);
                break;
            }
            return 0;
    
        case WM_PAINT:
        {
            cout << " WM PAINT.\n";
            hdc=BeginPaint(hwnd,&ps);
            TextOut(hdc, 20, 100, buf, strlen(buf));
            EndPaint(hwnd,&ps);
        }
        return 0;
    
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
    
        default:
            return DefWindowProc(hwnd,Message,wParam,lParam);
        }
        return 0;
    }
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Your paint is working, you have just asked for no area to be redrawn.

    Code:
    const RECT rText={20,100,420,25}
    the TOP of this rect is 100 but the bottom is 25, so it has a negative height.

    As you cannot draw to a rect with a negative height (or any area outside the rect you invalidated) your paint fails to show any change.

    Try;

    Code:
    const RECT rText={20,100,420,250};
    
    OR
    
    InvalidateRect(hwnd, NULL, TRUE);
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Thanks, that was it!
    Using Windows 10 with Code Blocks and MingW.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. InvalidateRect();
    By Prashant Tyagi in forum Windows Programming
    Replies: 0
    Last Post: 03-24-2012, 10:10 AM
  2. Invalidaterect
    By Ducky in forum Windows Programming
    Replies: 10
    Last Post: 10-26-2008, 03:12 PM
  3. Is InvalidateRect the correct API?
    By Joelito in forum Windows Programming
    Replies: 6
    Last Post: 05-15-2007, 07:44 AM
  4. using InvalidateRect()
    By agerealm in forum Windows Programming
    Replies: 6
    Last Post: 04-17-2003, 07:43 AM
  5. ValidateRect() and InvalidateRect()
    By Garfield in forum Windows Programming
    Replies: 6
    Last Post: 11-01-2001, 12:04 PM