Click to See Complete Forum and Search --> : SelectObject accumalitive?


UnfitElf
September 20th, 2006, 11:39 PM
Hi people,

Im wondering about this and im not clear on what the msdn states..

// Example #1
HGDIOBJ hOld = SelectObject(blaHdc, something1);
SelectObject(blaHdc, something2);
SelectObject(blaHdc, something3);
SelectObject(blaHdc, something4);
// Do something ....

// Clean up
SelectObject(blaHdc, hOld);

//------------------------------------

// Or Example #2

HGDIOBJ hOld = SelectObject(blaHdc, something1);
SelectObject(blaHdc, hOld);
hOld = SelectObject(blaHdc, something2);
SelectObject(blaHdc, hOld);
hOld = SelectObject(blaHdc, something3);
SelectObject(blaHdc, hOld);

//etc.


Do u have to put the original object back into the DC everytime before you want to put a new object in it, or can u simply take a very first returned object then do whatever and put the first returned one back at the end?

To have to replace it with the old one every single time you need to select a new object doesnt really make sence to me, but beter to be safe than sorry ;)

Thanks for any help :)

Boris K K
September 21st, 2006, 10:03 AM
Something in between: You need to preserve the original (first returned object) for each type: bitmap, palette, pen, brush, font.

Subsequent changes of the object of the same type (e.g., using 3 pens and 2 fonts) do not requre additional restoration. The idea is when you stop working with the DC it to has the same pen, brush, font, etc. selected as when you received it.

You can use SaveDC (http://msdn.microsoft.com/library/en-us/gdi/devcons_3exv.asp) and RestoreDC (http://msdn.microsoft.com/library/en-us/gdi/devcons_3883.asp) instead of tracking the changes.

UnfitElf
September 22nd, 2006, 04:32 PM
Thanks very much Boris :) well explained!

ZaccheusUK
September 22nd, 2006, 05:26 PM
You can use SaveDC (http://msdn.microsoft.com/library/en-us/gdi/devcons_3exv.asp) and RestoreDC (http://msdn.microsoft.com/library/en-us/gdi/devcons_3883.asp) instead of tracking the changes.
In all my years of windows programming, I never knew that! Thanks!