Thread: Forcing an update of a window

  1. #1
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681

    Forcing an update of a window

    What I'm trying to do:
    I have a seperate window that displays parts of a bitmap to make a map. Image is attached:
    http://www.mikemill.org/images/map.JPG
    I'm using the reverse video to show where the person is.
    I've gotten all that to work. My problem is that only way I've been able to get the window to refresh itself is to use two ShowWindow commands to hide and then show. I've tried using SendMessage with a WM_PAINT but it won't redraw it.

    Any suggestions?

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    I just encountered that, and I found that UpdateWindow didn't work either. Try this:
    Code:
    InvalidateRect( g_hWnd, NULL, FALSE );
    g_hWnd would obviously be your window handle.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Would I need to put a ValidateRect somewhere then?

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Nope. It's done automatically.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Woohoo, thanks a bunch.

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    try this

    Code:
    GetClientRect(hWnd,&Rect);
    InvalidateRect(hWnd,&Rect,FALSE); // tell windows this is the area that needs a redraw and do not erase the background
    UpdateWindow(hWnd); // send WM_PAINT
    
    //in your WM_PAINT (this should be ALL you do here if you want a fast update)
    
    GetUpdateRect(hWnd, &theRect, 0);
    if (IsRectEmpty(&theRect))
          GetClientRect(hWnd,&theRect);
    hdc=BeginPaint(hWnd); // validate the update region and get its HDC
    // bitblt your screen buffer
    EndPaint(hWnd); // clean up
    "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
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Actually XSquared's way is much easier for my needs. Only took one line of code in the right spot

  8. #8
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Passing NULL as the second parameter to InvalidateRect automatically invalidates the whole window for you.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  9. #9
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>>
    Passing NULL as the second parameter to InvalidateRect automatically invalidates the whole window for you.
    <<<

    Exactly, and in a simple application this isn't a problem, however, if you have a complex application, redrawing the entire screen when potentially only a couple of pixels have changed will give you a recognisable performance hit.

    Sooner or later, you'll have to address this.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  10. #10
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    I know. I was just saying that it does the exact same thing as Novacain's example, just with less code.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  11. #11
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    The PAINTSTRUCT returned by BeginPaint() gives you the rectangle that needs processing.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  12. #12
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Cool. I never actually thought of using the PAINTSTRUCT returned by BeginPaint. I thought it was just there to look pretty.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  13. #13
    jasondoucette.com JasonD's Avatar
    Join Date
    Mar 2003
    Posts
    278
    To clarify:

    UpdateWindow() immediately creates and processes a WM_PAINT message for the current invalid region. From MSDN: "The function sends a WM_PAINT message directly to the window procedure of the specified window, bypassing the application queue. If the update region is empty, no message is sent."

    InvalidateRect() creates an invalid region. It places a WM_PAINT message in the queue.

    This is why UpdateWindow() by itself doesn't do anything. If there's nothing invalid, then it doesn't update anything.

    Also, InvalidateRect() may appear to work, but considering that WM_PAINT is a low priority message, it does not get called immediately. It is called very quickly most of the time, but not when there's a bunch of other things to do.

    The solution?

    Call them both. InvalidateRect() first, then UpdateWindow(). Also, adrianxw is right - you should not invalidate portions of the window that need not be updated. No sense wasting CPU time for things that does not have to do, especially when you care about speed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WM_CAPTION causing CreateWindowEx() to fail.
    By Necrofear in forum Windows Programming
    Replies: 8
    Last Post: 04-06-2007, 08:23 AM
  2. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  3. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 10:17 AM
  4. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  5. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 10:14 PM