-
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
-
Moved: Windows programming question.
-
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()
-
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
-
-
-
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.
-
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.
-
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
-
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.
-
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?
-
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
-
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?