I've seen quite a few code snippets that always use ReleaseDC after using GetDC. Is this necessary?
I'm just using GetDC() to get the device context of a window by its hWnd.
Printable View
I've seen quite a few code snippets that always use ReleaseDC after using GetDC. Is this necessary?
I'm just using GetDC() to get the device context of a window by its hWnd.
Can't hurt to release it. ;)
After painting with a common device context, the ReleaseDC function must be called to release the device context.
http://msdn.microsoft.com/en-us/library/aa921543.aspx
After painting with a common DC, the ReleaseDC function must be called to release the DC. Class and private DCs do not have to be released. ReleaseDC must be called from the same thread that called GetDC. The number of DCs is limited only by available memory.
http://msdn.microsoft.com/en-us/libr...8VS.85%29.aspx
When in doubt, check the documentation on MSDN
Quote:
Originally Posted by msdn
That's why I was asking, I didn't understand the documentation... I don't know the difference between private and common DC? :confused:
And there is no painting going on... just using it to get the DC handle by hWnd.
Common DC.Quote:
I don't know the difference between private and common DC?
The common DC encapsulates two types of DCs, the window DC and the client DC. Common DCs are created from a pool of DCs that is limited by the amount of memory on the current system. Common DCs are further classified as Client and Window DCs. Client and Window DCs are very similar. The only difference between the two is that a client DC is restricted to the client area of the window, while the window DC gives the application access to both the client and the non-client areas of the window.
Class / Private DCs
It is important not to cache the common DCs in an application. How then can an application create a DC that does not have to be reinitialized each time it is called? This can be done with either a class or private DC. These two DCs can be retrieved in the same manner as the other common DCs and they do not need to be released. However it is good practice to release these DCs as the release functions have no effect, and memory leaks will be prevented if the DCs are ever converted back to common DCs. Class and private DCs are persistent, therefore the state of the DC will remain the same as it was in the previous call to that DC. This will allow a DC to be initialized once. This could save valuable time for applications that have lengthy DC initializations. Given below is a short description of both the class and private DC.
Class DC : A class DC is created for use for a single window class.
Private DC : A private DC is created for one particular window.
Hope this helps...
I don't think whether or not you are painting matters. I think (:confused:) in the context of a common DC (like the one retrieved via a hWnd), the DC is shared so there needs to be some sort of system to stop it getting messed up.