C++ InvalidateRect 和 RedrawWindow 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2325894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:03:22  来源:igfitidea点击:

Difference between InvalidateRect and RedrawWindow

c++winapiredraw

提问by Default

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)
RedrawWindow(win, NULL, NULL, RDW_INVALIDATE);
InvalidateRect(win, NULL, NULL);

例如,这两个调用是否相等:(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/focus/order/priorities..)

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.

我想重绘窗口的原因是因为我向它发送了一个我希望它显示的新图像,这意味着窗口的内容不再有效。

回答by AnT

InvalidateRectdoes not immediately redraw the window. It simply "schedules" a future redraw for a specific rectangular area of the window. Using InvalidateRectyou may schedule as many areas as you want, making them accumulatein some internal buffer. 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 InvalidateRectcall, the redrawing will take place immediately).

InvalidateRect不会立即重绘窗口。它只是为窗口的特定矩形区域“安排”未来的重绘。使用InvalidateRect您可以根据需要安排尽可能多的区域,使它们累积在一些内部缓冲区中。当窗口无事可做时,所有累积预定区域的实际重绘将在稍后进行。(当然,如果在您发出InvalidateRect调用的那一刻窗口处于空闲状态,则会立即进行重绘)。

You can also force an immediate redraw for all currently accumulated invalidated areas by calling UpdateWindow. But, again, if you are not in a hurry, explicitly calling UpdateWindowis not necessary, since once the window is idle it will perform a redraw for all currently invalidated areas automatically.

您还可以通过调用强制立即重绘所有当前累积的无效区域UpdateWindow。但是,同样,如果您不着急,UpdateWindow则不需要显式调用,因为一旦窗口空闲,它将自动为所有当前无效区域执行重绘。

RedrawWindow, on the other hand, is a function with a much wider and flexible set of capabilities. It can be used to perform invalidation scheduling (i.e. the same thing InvalidateRectdoes) or it can be used to forcefully perform immediate redrawing of the specified area, without doing any "scheduling". In the latter case calling RedrawWindowis virtually equivalent to calling InvalidateRectand then immediately calling UpdateWindow.

RedrawWindow另一方面,是一个具有更广泛和灵活的功能集的功能。它可以用于执行失效调度(即同样的事情InvalidateRect),也可以用于强制执行指定区域的立即重绘,而不做任何“调度”。在后一种情况下,调用RedrawWindow实际上等同于调用InvalidateRect然后立即调用UpdateWindow

回答by Mike T

RedrawWindow(win, NULL, NULL, RDW_INVALIDATE);and InvalidateRect(win, NULL, NULL);are equivalent. Both functions invalidate the window. The WM_PAINToccurs at the normal time (no other messages in the application queue) in both cases.

RedrawWindow(win, NULL, NULL, RDW_INVALIDATE);并且InvalidateRect(win, NULL, NULL);是等价的。这两个函数都使窗口无效。这两种WM_PAINT情况都发生在正常时间(应用程序队列中没有其他消息)。

If you want the paint to be done immediately then calling either RedrawWindow(win, NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW)or InvalidateRectfollowed by an UpdateWindowwill do that.

如果您希望立即完成绘制,则调用其中一个RedrawWindow(win, NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW)InvalidateRect后跟一个UpdateWindow将执行此操作。

RedrawWindowsimply gives more options with the RDW_* bits. If all you want is to invalidate the window without the immediate paint then calling InvalidateRectseems cleaner.

RedrawWindow只需使用 RDW_* 位提供更多选项。如果您只想在没有立即绘制的情况下使窗口无效,那么调用InvalidateRect似乎更清晰。

回答by Peter Alexander

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

重绘窗口

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

回答by Stefan

RedrawWindow repaints the window immediately. InvalidateRect only marks the window to be repainted on the next WM_PAINT message. 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.

RedrawWindow 立即重新绘制窗口。InvalidateRect 仅在下一个 WM_PAINT 消息上标记要重新绘制的窗口。但是 WM_PAINT 消息的优先级低于其他消息,因此如果您的应用程序忙于处理其他消息,则不会立即重新绘制。