Thread: Is InvalidateRect the correct API?

  1. #1
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    310

    Is InvalidateRect the correct API?

    I'm trying to change the color of a rectangle in runtime:
    Code:
    // Global
    COLORREF clr;
    
    // WndProc
    case WM_CREATE:
    clr = RGB(255, 255, 255); // Color init of the rectangle
    
    case WM_COMMAND:
    /*
    Calls ChooseColor dialog and pass
    hWnd: the parent handle
    &clr: buffer to the new color
    clr: current color
    */
    if (ColorDLG(hWnd, &clr, clr)) InvalidateRect(hWnd, NULL, FALSE);
    
    case WM_PAINT:
    hdc = BeginPaint(hWnd, &ps);
    hbrush = CreateSolidBrush(clr);
    HBRUSH hbrOld = (HBRUSH)SelectObject(hdc, hbrush);
    Rectangle(hdc, 10, 10, 80, 60);
    SelectObject(hdc, hbrOld);
    DeleteObject(hbrOld);
    DeleteObject(hbrush);
    EndPaint(hWnd, &ps);
    The problem is that the color of the rectangle changes when I minimizad the window, not instantly..any ideas?
    * PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
    * Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Is your WndProc actually missing break/return between all the case statements?

    Also, calling DeleteObject on hbrOld is wrong - you just selected it back into the DC!
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    310
    Quote Originally Posted by CornedBee View Post
    Is your WndProc actually missing break/return between all the case statements?
    No, I use it for clean and small code
    Also, calling DeleteObject on hbrOld is wrong - you just selected it back into the DC!
    Ok...I'll try that...but wouldn't be, at some point, memory leak?
    * PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
    * Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    No. You didn't create the brush that's in the DC when BeginPaint is called, so you're not responsible for deleting it.

    I don't think that's the actual problem, by the way. It's just something I noticed.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    310
    I reduce my code to this:
    Code:
    case WM_PAINT:
    		{
    			PAINTSTRUCT ps;
    			BeginPaint(hWnd, &ps);
    			HBRUSH hbrNew = CreateSolidBrush(clr);
    			HBRUSH hbrOld = (HBRUSH)SelectObject(ps.hdc, hbrNew);
    			Rectangle(ps.hdc, 10, 5, 60, 60);
    			SelectObject(ps.hdc, hbrOld);
    			DeleteObject(hbrNew);
    			EndPaint(hWnd, &ps);
    			return 0;
    		}
    Help???
    Last edited by Joelito; 05-14-2007 at 09:00 PM. Reason: updating code
    * PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
    * Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    if (ColorDLG(hWnd, &clr, clr))
    InvalidateRect(hWnd, NULL, FALSE);

    Is this being called? (ie ColorDLG() returning TRUE?)

    I would test to see if the user changed the colour (ie if(OldCLR!=NewCLR) )


    After you have the colour from the ColourPicker dlg (in the WM_COMMAND) call for a paint msg with

    InvalidateRect() //set area to be redrawn
    UpdateWindow() //post paint msg directly to callback
    Last edited by novacain; 05-14-2007 at 10:25 PM.
    "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

  7. #7
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    310
    Wow.. both:

    InvalidateRect(hWnd, NULL, TRUE);
    UpdateWindow(hWnd);

    Did the trick Thanks!
    * PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
    * Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having some annoying issues with API calls
    By DanFraser in forum C# Programming
    Replies: 7
    Last Post: 07-03-2008, 10:24 AM
  2. How do you toggle keys like Scroll lock without Windows API?
    By animeaholic in forum C++ Programming
    Replies: 7
    Last Post: 09-15-2002, 07:02 PM
  3. more advanced API tutorial
    By bennyandthejets in forum C++ Programming
    Replies: 10
    Last Post: 09-07-2002, 08:31 PM
  4. ValidateRect() and InvalidateRect()
    By Garfield in forum Windows Programming
    Replies: 6
    Last Post: 11-01-2001, 12:04 PM
  5. Win API
    By Marky_Mark in forum Windows Programming
    Replies: 4
    Last Post: 10-23-2001, 12:57 PM