Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CPaintDC device context question

Status
Not open for further replies.

nhungr

Programmer
Jan 31, 2005
26
CA
In Visual C++, if I create a CPaintDC object in a global function (any function other than the OnPaint message-handler), as follows:

void myfunction(CWnd* pWnd)
{
CPaintDC dc(CWnd* pWnd);

CPen newpen;
CPen* oldpen;
newpen.CreatePen(PS_SOLID,1,RGB(0,0,0));
oldpen=dc.SelectObject(&newpen);
dc.TextOut(100,100,"Some text");
dc.SelectObject(oldpen);
newpen.DeleteObject();

return;
}

and then want to create a new CPaintDC object in a DIFFERENT function, do I have to call the CPaintDC destructor at the end of the first function? If so, then HOW do I call it?

Thanks!
 
>want to create a new CPaintDC object in a DIFFERENT function

Or you could just pass the dc to the functions.

Code:
void otherfunc(CDC& dc)
{

}

voif myfunction(...)
{
  CPaintDC dc(pWnd);

  // ...
  otherfunc(dc);
}

>If so, then HOW do I call it?

Question is how do you call a destructor?
Code:
void someFunc()
{
  //...
  {
     Foo foo;
     foo.doSomething();
  } // Foo::~Foo is called here
  // ...
}  

or

void someFunc()
{
   Foo* foo = new Foo;
   foo->doSomething();
   delete foo; // Foo::~Foo is called here
}

or

#include <memory>
void someFunc()
{
   std::auto_ptr<Foo> foo( new Foo);
   foo->doSomething();
   foo = std::auto_ptr<Foo>(0);// Foo::~Foo is called here
}

/Per

www.perfnurt.se
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top