Thread: Question About Using WM_PAINT

  1. #1

    Question Question About Using WM_PAINT

    I'm just getting into Windows programming and have a question about using WM_PAINT. You're supposed to do all your drawing in there right? But aren't you also supposed to declare your variables in WinMain? So if I've declared an object in WinMain, how do I send it to WndProc so WM_PAINT can draw it? There must be a more elegant solution than global variables...


  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Easiest way is to declare the variables in the scope of the windows procedure have the option of making them static so they wont go out of scope with each message that's processed.

    A more fiddly way of doing this without resorting to globals is via the last parameter of the CreateWindow() can cast a pointer to an object or struct to (LPVOID)....then in your windows procedure, pick up the lParam of the WM_CREATE message and cast it to LPCREATESTRUCT.....then copy this pointer to a static LPCREATESTRUCT (say "s_lpcs" for instance)....then whenever you need access to the object pointer it can be found from s_lpcs->lpCreateParams....dont forget to cast back to the pointer you require........

    All in all its easier to declare locally, or keep the vars global......... this is where MFC (and other C++ wrappers) shines as least privilage & class scope keep things real neat and controllable....

  3. #3
    train spotter
    Join Date
    Aug 2001
    near a computer
    >>You're supposed to do all your drawing in there right?

    No. You should only copy your DC to the DC returned (or in the paintstruct) with a BitBlt().
    All other drawing should be finished and only then the result displayed with a WM_PAINT msg call.
    This gives a smooth update without flickering.

    Create your drawing objects, CompatibleDC's, CompatibleBitmap's on your WM_CREATE or WM_INIT msg. Thus keeping them in the callback. Remember any variable in the callback must be static (as Fordy said).

    Try a search for paint functions here as there is plenty of code posted.
    "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

  4. #4
    Ok, thanks guys!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM