i know how to repaint the full window but i don't know how to repaint a pieace of window like i draw a squre using gdi+ than i want to change it's coordinates so i want to repaint the squre not the whole window
anyidea?
i also tried this
RECT rect2;
rect2.left=0;
rect2.top=100;
rect2.right=225;
rect2.bottom=300;
InvadiateRect(hwnd, &rect2, false);
it still repaint the whole window
One way to do this is to call InvalidateRect() with a rectangle that is large enough to cover both the old and new positions of the square you moved. Windows will then call your WM_PAINT handler to repaint the area of the screen that changed.
The UnionRect() function is helpful for calculating this repaint rectangle.
Related
I have outputted an image (bitmap) which is created by Bitblt.
Now I want to get rid of it. How can I do? (Do not use the patch, like FillSolidRect, etc.)
There is no way to "undo" or "erase" a BitBlt or any other drawing output (except in very special cases where you do XOR-based drawing, which you can undo by doing another XOR drawing operation on top of the original).
The only thing you can do is to draw on something else on top of it, which is what you are calling a "patch". Typically, you would draw a solid rectangle of the window's background color. This is precisely what the OnEraseBkgrnd message handler does by default, which runs just before OnPaint. Specifically, it uses your window class's background brush, which is typically a brush that draws using the COLOR_3DFACE (for a dialog) or COLOR_WINDOW (for a window) system color.
Of course, you could always just not do the BitBlt in the first place. All painting code should always go inside of the OnPaint message handler function, so there is no way that you could end up with "stale" graphics. Whenever the window needs repainting, it is going to call this function, and your code inside of that function will repaint the window. If you don't want it to be painted with a bitmap, don't call BitBlt.
If you've done a BitBlt on top of your window using a temporary CDC object (which you generally should not be doing), you can force this to be erased by triggering a repaint of the window. The easiest way is to use the window's InvalidateRect() member function; passing NULL as the pointer to the rectangle to be invalidated will invalidate the window's entire client area, or you can just invalidate the area that you blitted.
am trying to move a custom button whose base class is CWnd, am using MoveWindow() method but each time i move the window the previously drawn window is still visible. i tryed to call InvalidateWindow, InvalidateRect together with UpdateWindow on the parent window but all didn't work. i also tried RedrawWindow nothing happend. how can i properly update the parent window with the new position of the button?
here is my sample code
void CCalendarCtrl::Shift()
{
RECT rc;
m_Up.GetWindowRect(&rc);
rc.top -= 20;
rc.bottom -=20;
m_Up.MoveWindow(&rc,TRUE);
RedrawWindow();
}
Call Invalidate() after movewindow().
https://social.msdn.microsoft.com/Forums/en-US/d6da8041-747c-4b31-b493-343e4516b452/are-the-coordinates-returned-by-cwndgetwindowrect-mfc-and-cwndmovewindow-mfc?forum=vcmfcatl
GetWindowRect returns screen coordinates. MoveWindow works using Client coordinates. So you need to do ScreenToClient(&rc) between the two calls.
For now, I define the three CRect area on the dialog, and then I paint a ID on it with textout function, per CRect for One ID, and I want to use OnLButtonDown, OnMouseMove, and OnLButtonUp the record my mouse cursor and when mouse move the ID will repaint with the cursor.
I try to use Invalidate, but the screen will flash,
I try to use InvalidateRect, but the item will repaint more and more times on screen.
How do I do it ? Have any example.
When you use InvalidateRect, you need to call it twice - once in the old location where you need to erase, and once in the new location where you need to paint. It's OK if the two overlap or even are identical.
well, I got the solution is use return true in OnEraseBkgnd function,
to remove flash screen problem. For now I can use invalidate(true) to repaint
my item which draw in rect...
The problem that I have seems to be trivial, but I cannot find a way to solve it. Here it is. I have a window with some graphics in it.
For simplicity lets say it's a solid green rectangle which fills the entire client area of the window. I want this rectangle to be redrawn and to fill the entire window every time the window changes its size. What I did originally was this. I posted WM_PAINT message from WM_SIZE handler.
It works, but if I move mouse fast I see a bit of unpainted (white) area around the green rectangle (actually one or two sides only, close to where mouse is). My understanding of the problem is that system thread which handles user input (mouse) works faster than my handler of WM_PAINT message. It means that by the time I start drawing an updated rectangle (its size is taken from WM_SIZE), mouse actually moves a little bit and system draws a new window frame which is different from what I'm trying to fill with the green. This creates unfilled areas next to borders which move during resizing.
When I stop resizing, green eventually fills the entire window, but during resizing there is a bit of flickering happening close to borders which is annoying. In order to solve the problem I tried the following.
bool finishedPainting;
RECT windowRect;
case WM_PAINT :
// ..... painting here
finishedPainting = TRUE;
break;
case WM_SIZE :
// .... some actions
// posting WM_PAINT
InvalidateRect(hWnd, NULL, FALSE);
PostMessage(hWnd, WM_PAINT, 0, 0);
break;
case WM_SIZING :
// this supposedly should prevent the system from passing
// new window size to WM_SIZE
if (!finishedPainting) memcpy((void*)lParam, &windowRect, sizeof(windowRect));
else {
// remember current window size for later use
memcpy(&windowRect, (void*)lParam, sizeof(windowRect));
finishedPainting = FALSE;
}
return TRUE;
It doesnt' work. As a slight variation, I also tried this.
bool finishedPainting;
POINT cursorPos;
case WM_PAINT :
// ..... painting here
finishedPainting = TRUE;
break;
case WM_SIZE :
if (!finishedPainting) SetCursorPos(cursorPos.x, cursorPos.y);
else {
finishedPainting = FALSE;
GetCursorPos(&cursorPos);
// .... some actions
InvalidateRect(hWnd, NULL, FALSE);
PostMessage(hWnd, WM_PAINT, 0, 0);
}
break;
This also doesn't work. As far as I understand the solution to the problem lies in somehow slowing the mouse down so that it moves to the next position on the screen (dragging the corner or the side of the window with it) only after the painting is finished.
Any ideas how to achieve this? Or maybe there is something fundamentally wrong with the way I see the problem and solution lies somewhere else?
// ====================================================
Update
I did a few experiments and here is what I found
1) When resizing, the sequence of messages is WM_SIZING - WM_NCPAINT - WM_SIZE - WM_PAINT. This looks a bit strange to me. I would expect WM_SIZE to follow WM_SIZING without interruption by WM_NCPAINT
2) In each message handler I was checking the width of a window during resizing (for simplicity I was only changing width). Surprisingly, the width measured in WM_SIZE turned out to be different from the one in WM_SIZING, but the same as in WM_NCPAINT and WM_PAINT. This is not a problem as such, just a wierd fact.
3) I came to the conclusion that there are two major causes for flicker happening near the window borders. The first one is that WM_NCPAINT comes before WM_PAINT. Imagine that you are stretching your window. The new frame will appear first (WM_NCPAINT comes first), then WM_PAINT fills the client area. A human eye catches that short period of time when the new frame is already on the screen, but it is empty. Even if you specify that you don't want window background to be deleted before repainting, still newly added area is empty and you can see it for a split second. This reason for flicker is best demonstrated when you grab the right window edge and move it quickly to the right. The other reason for flickering effect is less obvious and best seen when you grab the left window edge and move it to the left. During this move you will see unfilled areas along the RIGHT edge. As far as I understand the effect is caused by this. When user is doing resize Windows does the following: A) it sends WM_NCPAINT to draw the new frame, B) it copies the content of the old client area into the new top left window corner (in our case it moved to the left), C) it sends WM_PAINT to fill the new client area. However during stage B for some reason Windows produces those unfilled areas along the right edge, although it seems like it shouldn't because the old content should just stay where it is until it gets repainted over during WM_PAINT.
Ok, the question remains - how to get rid of those artefacts during resizing. As far as I can see now it is impossible to do using standard techniques and functions, because they are caused by the sequence of steps Windows performs during resizing. Swapping WM_NCPAINT and WM_PAINT would probably help, but this seems to be beyond our control (unless there is a simple way to do that which I just don't know about).
You shouldn't post or send WM_PAINT messages yourself. Rather, use ::InvalidateRect to invalidate parts of your window and let Windows decide when to send the WM_PAINT messages.
Windows works this way on purpose. It's generally considered more important to be responsive to the user (i.e. the mouse) than to have a fully up-to-date painted window.
If you're always painting the entire window in your WM_PAINT handler, you can eliminate a lot of flicker by overriding the WM_ERASEBKGND handler and return without doing anything.
If you really insist on preferring window updates over mouse responsiveness, replace the InvalidateRect call with a RedrawWindow call using the RDW_UPDATENOW flag.
Edit: Your observations make sense. WM_SIZING comes before the window is resized, to give you a chance to modify the size and/or position. You might try painting the client area in the WM_NCPAINT handler, even before the border is drawn. After you paint it you can validate the client area to keep it from drawing again.
It's a bad idea to manually post WM_PAINT or WM_SIZE. One weird hacky crazy thing you can do though, is to record the new resized co-ordinates in a RECT in WM_SIZE, Use MoveWindow to change the size of the window back to it's previous one, then manually resize it again using MoveWindow in the WM_PAINT message after you've done the painting.
Another possible solution is to constantly fill your window with the color regardless of whether the screen is resized or not. i.e
// in the WinMain function
if (GetMessage(&msg,NULL,NULL,0))
{
TranslateMessage(&msg,NULL,NULL);
DispatchMessage(&msg,NULL,NULL);
}
else
{
// fill your window with the color in here
}
Or, of course, you can just set the background color of your window to green, instead of doing all the painting yourself:
// Before RegisterClass or RegisterClassEx
// wincl is a WNDCLASS or WNDCLASSEX
wincl.hbrBackground = CreateSolidBrush(RGB(50, 238, 50));
ok i can draw ellipse the problem is this, im trying to draw one ellipse but change its x value to different one. like this i draw one ellipse and the x value is 1 after ten seconds i want it the x value to be 10 but it seems that im creating new ellipse with x value 10. here is my code
while(sd==1)//sd equal 1
{
sf++;//sf equals 1
onPaint(hdc);
InvalidateRect(hWnd,0,true);
}
//on paint function
VOID onPaint(HDC hdc)
{
Graphics graphics(hdc);
Pen pen(Color(255, 0, 0, 255));
graphics.DrawEllipse(&pen,sf , 0, 50, 50);
}
well i thought that invalidate rect will clear everything have been painted and repaint it but it didn't work
If you want to make an animation you are better setting a timer.
Using InvalidateRect as a way to generate WM_PAINTs seems overkill, it will do much more thant that. Instead you can draw directly in OnTimer call, since it is outside a WM_PAINT you will need to get a device context with GetDC.
For example if you can have the function DrawFrame(HDC hDC). OnTimer will update the current position and call DrawFrame, OnPaint will call DrawFrame but will not update the position (that way if you want to stop the animation you will have the last frame draw).
The DrawFrame will clear the background (probably with a FillRect), and draw the circle in the new position. If you have a large area this will flicker, to avoid it as Tom suggested you may use a memory DC and a HBITMAP for the double buffer.
You shouldn't try to draw multiple frames of an animation in one shot.
Save your variable sf somewhere, and in OnPaint(), increment sf, draw a single ellipse, and call Invalidate()
The Invalidate will trigger OnPaint() to be called again.
This should work, but will be very flickery :) You can fix the flicker by double-buffering.
InvalidateRect marks the window as "invalid", but that doesn't cause an erase and repaint to happen right away. The erasing and painting happen only when your message pump is running (e.g., the loop with GetMessage and DispatchMessage). When the message queue runs dry, GetMessage will synthesize WM_ERASEBKGND and WM_PAINT messages for the invalid windows. When those messages are dispatched to the window procedure, the window gets a chance to draw.
Your onPaint function only draws, it doesn't erase. And since your loop never exits, the message pump never gets to run.
For simple animations, the solution is to SetTimer. In your handler for the WM_TIMER messages, update your variables for single frame, call InvalidateRect, and return (which lets the message pump keep running). The erasing and painting message will happen, then the timer will fire again, and you'll get the next frame.