简体   繁体   English

InvalidateRect和RedrawWindow之间的区别

[英]Difference between InvalidateRect and RedrawWindow

When I want to redraw a window, is there any preferred function to call between InvalidateRect and RedrawWindow? 当我想重绘窗口时,InvalidateRect和RedrawWindow之间是否有任何首选函数?

For instance, are these two calls equal: (win would be a HWND) 例如,这两个调用是否相等:(win将是HWND)
RedrawWindow(win, NULL, NULL, RDW_INVALIDATE);
InvalidateRect(win, NULL, NULL);

The main question(s) : When should I use one or the other? 主要问题 :我什么时候应该使用其中一个? Are there any differences that happen in the background? 背景中是否存在任何差异? (different WM_messages / focus / order / priorities..) (不同的WM_messages /焦点/顺序/优先级..)

The reason that I want to redraw the window is because I send a new image to it that I want it to display, meaning the content of the window is no longer valid. 我想重绘窗口的原因是因为我向它发送了一个我希望它显示的新图像,这意味着窗口的内容不再有效。

InvalidateRect does not immediately redraw the window. InvalidateRect不会立即重绘窗口。 It simply "schedules" a future redraw for a specific rectangular area of the window. 它只是“安排”窗口特定矩形区域的未来重绘。 Using InvalidateRect you may schedule as many areas as you want, making them accumulate in some internal buffer. 使用InvalidateRect您可以根据需要调度多个区域,使它们在某个内部缓冲区中累积 The actual redrawing for all accumulated scheduled areas will take place later, when the window has nothing else to do. 所有累积的预定区域的实际重绘将在稍后进行,此时窗口没有其他任何操作。 (Of course, if the window is idle at the moment when you issue the InvalidateRect call, the redrawing will take place immediately). (当然,如果在您发出InvalidateRect调用时窗口处于空闲状态,则重绘将立即进行)。

You can also force an immediate redraw for all currently accumulated invalidated areas by calling UpdateWindow . 您还可以通过调用UpdateWindow强制立即重绘所有当前累积的无效区域。 But, again, if you are not in a hurry, explicitly calling UpdateWindow is not necessary, since once the window is idle it will perform a redraw for all currently invalidated areas automatically. 但是,再次,如果您不赶时间,则不必显式调用UpdateWindow ,因为一旦窗口空闲,它将自动为所有当前无效的区域执行重绘。

RedrawWindow , on the other hand, is a function with a much wider and flexible set of capabilities. 另一方面, RedrawWindow是一个具有更广泛和灵活的功能集的函数。 It can be used to perform invalidation scheduling (ie the same thing InvalidateRect does) or it can be used to forcefully perform immediate redrawing of the specified area, without doing any "scheduling". 它可用于执行无效调度(即InvalidateRect执行的操作),或者可用于强制执行指定区域的立即重绘,而无需执行任何“调度”。 In the latter case calling RedrawWindow is virtually equivalent to calling InvalidateRect and then immediately calling UpdateWindow . 在后一种情况下,调用RedrawWindow实际上等同于调用InvalidateRect然后立即调用UpdateWindow

RedrawWindow(win, NULL, NULL, RDW_INVALIDATE); and InvalidateRect(win, NULL, NULL); InvalidateRect(win, NULL, NULL); are equivalent. 是等价的。 Both functions invalidate the window. 这两个函数使窗口无效。 The WM_PAINT occurs at the normal time (no other messages in the application queue) in both cases. 在两种情况下, WM_PAINT都在正常时间发生(应用程序队列中没有其他消息)。

If you want the paint to be done immediately then calling either RedrawWindow(win, NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW) or InvalidateRect followed by an UpdateWindow will do that. 如果你想立即完成绘制,那么调用RedrawWindow(win, NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW)InvalidateRect RedrawWindow(win, NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW)然后调用UpdateWindow就可以了。

RedrawWindow simply gives more options with the RDW_* bits. RedrawWindow只是为RDW_ *位提供了更多选项。 If all you want is to invalidate the window without the immediate paint then calling InvalidateRect seems cleaner. 如果你想要的是在没有立即绘画的情况下使窗口无效,那么调用InvalidateRect似乎更清晰。

I don't like just giving links, but the MSDN gives you all the information you need and it would be a waste of time to re-type it all here. 我不喜欢只提供链接,但MSDN会为您提供所需的所有信息,在此处重新输入所有信息将是浪费时间。

RedrawWindow RedrawWindow

InvalidateRect InvalidateRect

In short, yes there are differences. 简而言之,是的,存在差异。 The question is, why do you want to redraw the window? 问题是,为什么要重绘窗口? Is it because the contents are no longer valid? 是因为内容不再有效吗? If so, use InvalidateRect , otherwise use RedrawWindow . 如果是这样,请使用InvalidateRect ,否则使用RedrawWindow

RedrawWindow repaints the window immediately. RedrawWindow立即重新绘制窗口。 InvalidateRect only marks the window to be repainted on the next WM_PAINT message. InvalidateRect仅标记要在下一个WM_PAINT消息上重新绘制的窗口。 But WM_PAINT messages have lower priority than other messages, so the repainting won't be immediately if your app is busy handling other messages. 但WM_PAINT消息的优先级低于其他消息,因此如果您的应用忙于处理其他消息,则不会立即重新绘制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM