what is the use of "pOldPen" here?
-
the following code is from the examples on the book [code] CPen pen (PS_SOLID, 0, RGB (192, 192, 192)); CPen* pOldPen = dc.SelectObject (&pen); //do some drawing staff here dc.SelectObject (pOldPen); I know once I create a pen, I can do some drawing staff, but why should I add "CPen* pOldPen = dc.SelectObject (&pen)" and "dc.SelectObject (pOldPen)" here? what's the use of that?
-
the following code is from the examples on the book [code] CPen pen (PS_SOLID, 0, RGB (192, 192, 192)); CPen* pOldPen = dc.SelectObject (&pen); //do some drawing staff here dc.SelectObject (pOldPen); I know once I create a pen, I can do some drawing staff, but why should I add "CPen* pOldPen = dc.SelectObject (&pen)" and "dc.SelectObject (pOldPen)" here? what's the use of that?
It's called restoring the DC to its original state.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
It's called restoring the DC to its original state.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
sorry, what is that mean? could you explain it a little detailed? what is the difference if I had not use it?
-
sorry, what is that mean? could you explain it a little detailed? what is the difference if I had not use it?
bloodwinner wrote:
what is the difference if I had not use it?
You would get a "resource leak" (a GDI handle would be lost, and if your app runs for a long time, this leak would accumulate, which isn't good - leaks like this can easily crash Win9x for example). Basically, if a GDI object such as a pen, brush or font is still selected into a DC when it is destroyed, you will get a leak:
void Draw(CDC& dc)
{
// Create a pen
CPen pen;
pen.CreatePen(PS_SOLID, 2, RGB(255,0,0));
// Select the pen into a DC
dc.SelectObject(&pen);
// Draw
...
// LEAK! Pen still selected into DC - remember that when a CPen goes out of scope,
// the underlying pen HANDLE is automatically deleted.
}To avoid this, do as your sample code shows (save the previous object when calling
SelectObject
), or save/restore the DC state like this:void Draw(CDC& dc)
{
// Save the current DC state
int nState = dc.SaveDC();
// Create a pen
CPen pen;
pen.CreatePen(PS_SOLID, 2, RGB(255,0,0));
// Select the pen into a DC
dc.SelectObject(&pen);
// Draw
...
// Restore original DC state (pen, font, brush, etc.).
dc.RestoreDC(nState);
}Last modified: 11hrs 24mins after originally posted --
Kicking squealing Gucci little piggy.
-
bloodwinner wrote:
what is the difference if I had not use it?
You would get a "resource leak" (a GDI handle would be lost, and if your app runs for a long time, this leak would accumulate, which isn't good - leaks like this can easily crash Win9x for example). Basically, if a GDI object such as a pen, brush or font is still selected into a DC when it is destroyed, you will get a leak:
void Draw(CDC& dc)
{
// Create a pen
CPen pen;
pen.CreatePen(PS_SOLID, 2, RGB(255,0,0));
// Select the pen into a DC
dc.SelectObject(&pen);
// Draw
...
// LEAK! Pen still selected into DC - remember that when a CPen goes out of scope,
// the underlying pen HANDLE is automatically deleted.
}To avoid this, do as your sample code shows (save the previous object when calling
SelectObject
), or save/restore the DC state like this:void Draw(CDC& dc)
{
// Save the current DC state
int nState = dc.SaveDC();
// Create a pen
CPen pen;
pen.CreatePen(PS_SOLID, 2, RGB(255,0,0));
// Select the pen into a DC
dc.SelectObject(&pen);
// Draw
...
// Restore original DC state (pen, font, brush, etc.).
dc.RestoreDC(nState);
}Last modified: 11hrs 24mins after originally posted --
Kicking squealing Gucci little piggy.
That type of restore is more costly if I recall correctly. SaveState() saves everything. So, if you're only using a brush for instance, SaveState() will give you lots of overhead. On the other hand, CPU cycles aren't really a scarce resource these days.. :)
-- A Stern Warning of Things to Come
-
That type of restore is more costly if I recall correctly. SaveState() saves everything. So, if you're only using a brush for instance, SaveState() will give you lots of overhead. On the other hand, CPU cycles aren't really a scarce resource these days.. :)
-- A Stern Warning of Things to Come
-
SaveState()??? I have used SaveDC() and RestoreDC() but never SaveState() and RestoreState(). Are you guys mistaken on the name or is this a function I am not aware of?
:laugh: Nah, Rob and I are just senile, and meant SaveDC/RestoreDC. :-D (Unless Rob really do know about some undocumented funcions :~)
-- A Stern Warning of Things to Come
-
SaveState()??? I have used SaveDC() and RestoreDC() but never SaveState() and RestoreState(). Are you guys mistaken on the name or is this a function I am not aware of?
-
SaveState()??? I have used SaveDC() and RestoreDC() but never SaveState() and RestoreState(). Are you guys mistaken on the name or is this a function I am not aware of?
Me too I wrote a program with SaveDC and RestoreDC:)
_**
**_
WhiteSky