Thread: Painting

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    15

    Painting

    HI, I have a simple question about painting in a standard win32 application window.
    How do I go about updating my display.
    At the moment I have a WM_PAINT message handler which will paint a line, the lines position is given by x.
    So i want the lines position to change when x changes
    I've tried doing a SendMessage(hWnd, WM_PAINT, 0, 0);
    but this won't repaint the line. The only time the line chagnes is if the window leaves the foreground then comes back up.

    Soym question is, how do you go about updating the window with the new paint information.

    Thanks in advance

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Moved: Windows programming question.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    to send a paint

    InvalidateRect()//set the area to redraw
    UpdateWindow() //post msg directly to windows callback

    remember to 'clear' the last line with something like

    FillRect()

    get the area/rect from the PAINTSTRUCT member

    Create a brush with

    CreateSolidBrush()

    Clean the brush up after use with

    DeleteObject()
    "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
    Registered User
    Join Date
    Dec 2006
    Posts
    15
    Thanks for that, works really well.
    I have another problem, I want for my drawings to be saved, but I need to refresh and clear the buffer i'm drawing in. I'm trying to have a line drawn when I hold down the mouse button then release in a different location. It works fine, but I want it to be saved there on the screen.
    My current function draws the line as I'm holding down the button so you see as you draw, and because of that the screen needs to refresh.
    Am i able to setup two different layers to draw on, and have the top one transparent so I see the back layer where everything will accumulate(like paint) and have the front where only the current line is being drawn on?

    Thanks again

  5. #5
    Registered User pronecracker's Avatar
    Join Date
    Oct 2006
    Location
    netherlands
    Posts
    158
    What

  6. #6
    Registered User pronecracker's Avatar
    Join Date
    Oct 2006
    Location
    netherlands
    Posts
    158
    Please explain.

  7. #7
    Registered User pronecracker's Avatar
    Join Date
    Oct 2006
    Location
    netherlands
    Posts
    158
    If I understand well, you want to draw outside the window? That's rather advanced, you can get an appropriate DC with GetDC(NULL), but it's hard to manage drawings outside the window because they will be overdrawn by other programs, and you won't receive a WM_PAINT message when you should repaint your stuff.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Simple line drawing - you typically set the raster operation to "invert" (see SetROP2()) while dragging the line. The cool thing about this is that if you draw a line twice this way, it disappears again.

    On releasing, you store the line object in your data structures and whenever a WM_PAINT arrives, you redraw everything visible out of these structures.

    In particular, you don't rely on Windows saving anything for you. It doesn't, and it wasn't designed to do so.
    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

  9. #9
    Registered User
    Join Date
    Dec 2006
    Posts
    15
    thanks CornedBeef.
    So If I draw a line one the screen, and want it to stay there, even after it has changed... how can I use the SetROP() to stop the current window being writen over?
    I wasn't quite sure from the msdn description.
    Thanks

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You misunderstand me. You only use the ROP while drawing the temporary line, so you can easily erase it again.

    You cannot stop a window from being written over. Any attempt is extremely bad behaviour on your part.

    What you do is, you store enough information in your program to be able to recreate the window's contents at any time, and when a WM_PAINT is sent, you do so.
    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

  11. #11
    Registered User
    Join Date
    Apr 2007
    Posts
    26
    Ok, I have a possible solution if i understand the question right. How about creating a second compantible DC that you draw on when you want and then on release of the mouse button do a bitblt from the temporary DC to the perment DC. Would that work?

  12. #12
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    You could save the line coords in an array,
    add the new line to the array,
    clear the screen
    use PolyLine() to draw the array (not 100% if that would work as required).
    OR
    draw the lines one by one from the array.

    OR

    Use a buffer DC.
    Create a memory DC (double buffer) in the WM_CREATE
    draw the lines to this buffer
    in the WM_PAINT BitBlt() the mem DC to the DC returned from BeginPaint() using the RECT in the PAINTSTRUCT
    return the default objects to the DC and delete in WM_CLOSE
    "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

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Which of the two main approaches (store the lines and redraw them vs blit a back buffer to the window) you use mainly depends on your application. Is it a vector drawing app or a bitmap drawing app?
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. mouse messages, painting, tabs
    By ZeroG in forum Windows Programming
    Replies: 0
    Last Post: 06-20-2005, 03:51 AM
  2. smoother painting
    By underthesun in forum Windows Programming
    Replies: 2
    Last Post: 01-22-2005, 03:46 AM
  3. Painting with Tex on a Win32 Application
    By webzest in forum C++ Programming
    Replies: 5
    Last Post: 08-16-2004, 03:04 PM
  4. My newest digital painting - work in progress
    By jdinger in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 07-17-2004, 08:22 PM
  5. Question about painting
    By Ariod in forum Windows Programming
    Replies: 10
    Last Post: 08-24-2003, 01:36 PM