Thread: Painting stuff, Resizing the Window

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    127

    Painting stuff, Resizing the Window

    Hi,

    I've been experimenting with this simple Generic Windows tutorial program (Windows API Tutorial: Generic), and I was wondering if someone could help me understand something about it.

    Basically in the process of experimenting with it, I've added the following code into main, which makes it draw loads of lines in spirals all over the screen:

    First at the top of int main, I added:

    Code:
    int x1=0, x2=0, y1=0, y2=0;
    Then in main, after "int status;", I added:

    Code:
            HWND hwnd = topWinClass.GetRunningWindow ();
            HDC  _hdc = GetDC (hwnd);
            while ((status = ::GetMessage (&msg, 0, 0, 0)) != 0)
            {
                ::MoveToEx (_hdc, x1, y1, 0);
                ::LineTo (_hdc, x1, y1);
                ::LineTo (_hdc, x2, y2);
                if (x2 < 800 && y2 == 0)
                    x2+=10;
                else if (x2 == 800 && y2 < 800)
                    y2+=10;
                else if (x2 > 0 && y2 == 800)
                    x2-=10;
                else if (x2 == 0 && y2 > 0)
                    y2-=10;
                if (x2 == 0 && y2 > 0 && y2 < 50)
                {
                    x1 += 20;
                    y1 += 20;
                }
                if (status == -1)
                    return -1;
                ::TranslateMessage (&msg);
                ::DispatchMessage (&msg);
            }
    Now, if I run it like that, the lines will draw, but only if I move the mouse around in the process. I think that's due to BeginPaint and EndPaint getting run whenever I do that. But say I get rid of pCtrl->Paint (); in the following part of control.cpp:

    Code:
        case WM_PAINT:
            pCtrl->Paint ();
            return 0;
    Then, the lines get drawn all over the screen without me having to move the mouse around.

    So my questions are:

    1. If I resize the window with pCtrl->Paint() removed, all the previously-drawn lines disappear from the window. Why is this?

    2. If I minimise and then bring the window back up, all my lines disappear, but the original text in the program (just the word generic in the top left of the window) stays there, fine. How come?

    Sorry if this is a long-winded thing to ask about. It just seems like if I can get this stuff worked out, I could start using windows to display stuff instead of just the console, which would be awesome.

    Thanks.

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    First some details...

    GetMessage() monitors the OS msg queue looking for msgs for your app.
    It will wait until the OS has a msg for your app.

    You have put your drawing code in the main msg 'pump'. (it should be in the callback)
    [In this wrappers case I would put it in the 'View::Paint() method, calling Canvas::Line() as required.]
    It will draw lines each time a msg is retuned by GetMessage()

    If there are no msgs for your app there will be no lines.


    Quote Originally Posted by bengreenwood View Post
    Now, if I run it like that, the lines will draw, but only if I move the mouse around in the process.
    Mouse move msgs are pasing thru the msg pump, drawing a line each time.

    Quote Originally Posted by bengreenwood View Post
    Then, the lines get drawn all over the screen without me having to move the mouse around.
    To paint the screen you declare an area 'invalid' (InvalidateRect())
    This generates a WM_PAINT msg which is dispatched to your window
    In WM_PAINT you must call BeginPaint() and EndPaint() to 'validate' the invalid area.
    If you don't the paint msg is not considered processed and will continue to be generated.
    Each time a new line is drawn but the OS never considers the paint message finished.

    Quote Originally Posted by bengreenwood View Post
    1. If I resize the window with pCtrl->Paint() removed, all the previously-drawn lines disappear from the window. Why is this?
    The app redraws the background and non client area (menu, title, etc) on a resize, clearing all previous drawing.
    The OS sends these msgs automatically, your app ingnores most of them (doing default processing).

    Quote Originally Posted by bengreenwood View Post
    2. If I minimise and then bring the window back up, all my lines disappear, but the original text in the program (just the word generic in the top left of the window) stays there, fine. How come?
    This must be with the pCtlr->Paint() still in.

    The Controller objects Paint method calls (thru other objects) a method that writes 'Generic' and underlines it.

    Maximising the app calls for a repaint (of all areas) the same way as a resize does.
    "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
    Sep 2007
    Posts
    127
    Thanks, that's really helpful.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Resizing window causes extreme flicker
    By Gerread in forum Windows Programming
    Replies: 6
    Last Post: 05-22-2007, 02:00 PM
  2. Adding colour & bmps to a Win 32 Window??
    By carey_sizer in forum Windows Programming
    Replies: 4
    Last Post: 09-04-2004, 05:55 PM
  3. no errors but still errors
    By Megatron in forum Windows Programming
    Replies: 7
    Last Post: 01-12-2003, 11:21 PM
  4. Putting stuff in my window...
    By Brian in forum Windows Programming
    Replies: 3
    Last Post: 03-04-2002, 11:37 PM
  5. g_hWndMain = hWnd;error C2065,C2440
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 12-09-2001, 03:36 PM