What does the CS_CLASSDC & CS_OWNDC class style do?

關於這兩個類型的解釋可以參考這兩個地方:

 

(1)兩篇出自The Old New Thing的很有意思的文章 CS_CLASSDCCS_OWNDC

 

(2)Feng Yuan的Windows Graphics Programming Win32 GDI and DirectDraw® 第五章第二節

Class Device Context

The CS_CLASSDC flag in WNDCLASS's style field tells the window management module to create a device context shareable by all windows of the particular class. Such a device context is called class device context. The actual device context is created when the first window instance of that class is created, and initialized once to its default values.

When GetDC, GetWindowDC, or BeginPaint is called on a window belonging to such a class, the device context attached with the window class is returned, with an updated display rectangle, visible region, and empty clipping region. All other attributes in the class device context are kept the same—for example, logical pen, text color, mapping mode, etc. After finishing drawing, ReleaseDC or EndPaint returns the device context to the window class, without destroying it or resetting its attributes. A class device context is destroyed only when the last window of the class is destroyed.

If you have ever heard that ReleaseDC and EndPaint can be omitted for class device context because they do nothing, forget about it. This kind of suggestion should be considered harmful, because it can cause big trouble for a small gain. For one thing, EndPaint restores the caret turned off by BeginPaint.

Class device context is useful for control windows that are drawn using the same attribute values, because it minimizes the time required to prepare a device context for drawing and releasing afterward. Another benefit of class device context is its minimum memory usage.

Class device context is provided only for backward compatibility. Its advantage is diminished by today's larger RAM and faster CPU, and by the protected address space design of Win32 processes. Class device context is not recommended in Win32 programming.

Private Device Context

The CS_OWNDC flag in WNDCLASS's style field tells the window management module to create one device context for each window created using this class. So every window will have a dedicated device context during the lifetime of the window—that is, a private device context.

A private device context gets initialized once to its default values. Each call to GetDC, GetWindowDC, or BeginPaint retrieves a window's private device context, with a new display rectangle and visible region. The application can then set device context's attribute and issue drawing commands. ReleaseDC or EndPaint returns the device context to the window, without changing the device context, so the next time a device context is requested, attributes like pen and brush are still the same.

The MSDN documentation on private device context is not clear (check the Private Display Device Contexts portion). It mentions that the application must retrieve the handle only once, and then it goes on to say the application could call BeginPaint to incorporate the update region.

Private device context goes to an extreme to improve performance by sacrificing memory resource. A device context uses three types of resource: a GDI handle, memory in the user application's address space, and memory in system kernel address space. Private device context is useful only for windows with complicated settings that take a long time to prepare, and for windows that need frequent updating. It's recommended only when performance considerations are much more important than memory and GDI handle resource usage.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章