Use of SaveDC and RestoreDC with TCanvas |
The TCanvas class does not encapsulate the Windows GDI functions of SaveDC and RestoreDC that provide saving and restoring of the device context state. However, use of these functions can be necessary for restoring the device context state if, for example, the clipping region of the device context varies with call of the IntersectClipRect function. ProblemsThere are two problems with using SaveDC and RestoreDC with TCanvas. The first problem is a mismatch between the state of the device context and the TCanvas state after a call to RestoreDC. The second problem is linked with Windows 9x. The problem occurs when, between the SaveDC and RestoreDC calls, the GDI objects (font, pen, or brush) that have been selected into the device context before SaveDC are deleted. This is a common cause of GDI resource leaks in Windows 9x programs. SolutionTo solve the first problem, it is necessary to call the Refresh method of TCanvas after RestoreDC. The Refresh method will return the device context to a default state (with selected stock objects) and will set TCanvas into the deselected state. Thus a correspondence between an actual state of the device context and the TCanvas state will be reached. The second problem is solved with a bit more difficulty. It is necessary to save the Handle property of TCanvas in an additional variable so we can call the Refresh method of TCanvas. Then we can call the SaveDC function with the additional variable as the DC parameter. The Refresh method will set the device context to a default state. Use of the additional variable will allow us to save the device context in this state. The following code demonstrates the common scheme of use of SaveDC and RestoreDC: HDC DC; int SaveIndex; DC = Canvas->Handle; Canvas->Refresh (); SaveIndex = SaveDC (DC); . . // any calls of TCanvas ... . RestoreDC (DC, SaveIndex); Canvas->Refresh (); . . // another calls of TCanvas ... . This scheme guarantees absence of the leakage of the GDI resources and provides the correct work of the TCanvas methods. Nikolay Antonov & Vyatcheslav Baranov. MBLab, 9/17/2001. |
| Home - Company - Sources - Products - Download - Order - Support - Forums - Contact |
| Copyright © 2001-2004 MBLabSoft. All Rights Reserved. |