windows GetDC() 和 BeginPaint() 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5841299/
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-09-15 16:45:38  来源:igfitidea点击:

Difference between GetDC() and BeginPaint()

windowswinapimfc

提问by Umesha MS

I am working on Win32 UI. I want to know the difference Between GetDC and BeginPaint. When to Use which API and when not to use which API.

我正在研究 Win32 UI。我想知道 GetDC 和 BeginPaint 之间的区别。何时使用哪个 API,何时不使用哪个 API。

回答by Matt

GetDC simply returns the handle to the device context, which can be used any time anywhere to do your own drawing. BeginPaint on the other hand prepares the window for painting, and also provides information on what should be painted (such as whether the background needs repainting and the rect that needs to be painted).

GetDC 只是返回设备上下文的句柄,可以随时随地使用它来进行自己的绘图。另一方面,BeginPaint 准备用于绘制的窗口,并且还提供关于应该绘制什么的信息(例如背景是否需要重新绘制以及需要绘制的矩形)。

Examples of when to use each? BeginPaint is most commonly seen inside WM_PAINT handlers (MSDN: An application should not call BeginPaint except in response to a WM_PAINT message. Each call to BeginPaint must have a corresponding call to the EndPaint function.). GetDC can be used anywhere, so if you want to draw on an external window. Basically anytime thats not in a WM_PAINT handler. BeginPaint and EndPaint also have some affect on the caret. Read msdn for more details.

何时使用每个示例?BeginPaint 最常见于 WM_PAINT 处理程序中(MSDN:应用程序不应调用 BeginPaint,除非响应 WM_PAINT 消息。对 BeginPaint 的每次调用都必须有对 EndPaint 函数的相应调用。)。GetDC 可以在任何地方使用,所以如果你想在外部窗口上绘图。基本上任何时候不在 WM_PAINT 处理程序中。BeginPaint 和 EndPaint 也对插入符号有一些影响。阅读 msdn 了解更多详细信息。

回答by Hans Passant

GetDC() is nota substitute for Begin+EndPaint(). If you try, you'll find that your UI thread starts to burn 100% cpu core and your WM_PAINT handler getting called over and over again.

的GetDC()是不能用于开始+调用EndPaint的替代品()。如果您尝试,您会发现您的 UI 线程开始消耗 100% 的 CPU 内核,并且您的 WM_PAINT 处理程序被一遍又一遍地调用。

The big one is BeginPaint(), it clears the update region of the window. The value of PAINTSTRUCT.rcPaint. WM_PAINT is generated as long as the window has a dirty rectangle, created by an InvalidateRect() call by either the window manager or your program explicitly calling it. BeginPaint() clears it.

最大的是BeginPaint(),它清除窗口的更新区域。PAINTSTRUCT.rcPaint 的值。只要窗口有一个脏矩形,WM_PAINT 就会生成,该矩形由窗口管理器或您的程序显式调用它的 InvalidateRect() 调用创建。BeginPaint() 清除它。

回答by Xion

BeginPaintis intended to be called only in response to WM_PAINTmessage. The device context obtained by it points to the invalidated (to-be-redrawn) area of the window. It should be then released using EndPaint.

BeginPaint旨在仅在响应WM_PAINT消息时调用。它获取的设备上下文指向窗口的失效(待重绘)区域。然后应该使用EndPaint.

GetDCcan be called at any time. The device context obtained by it points to the whole client area of the window. To release it, you should call ReleaseDC.

GetDC可以随时调用。它获取的设备上下文指向窗口的整个客户区。要释放它,您应该调用ReleaseDC.