Library tutorials & articles

Attaching and Detaching Objects

Creation

Creation of most objects involves a two-step process. The first step is to create a C++ object, which is the "wrapper" around the Windows object. The next step is to create the actual Windows object. Some parameterized constructors do both of these steps at once. For example,

CPen pen;

Creates an MFC object, a CPen, but does not associate an HPEN with it. But the constructor

CPen pen(PS_SOLID, 0, RGB(255, 0, 0));

creates an MFC object, a CPen, then creates the underlying Windows object, the HPEN, and attaches that object to the CPen.

You can do this implicitly, by using the Create method (which is sometimes gratuitously renamed, as far as I can tell because the designers of MFC were not C++ experts). For example, to create a pen you can do

CPen pen;
pen.CreatePen(PS_SOLID, 0, RGB(255, 0, 0));

(MFC has CreatePen and CreatePenIndirect, which is silly, because there is no Create method in either the CPen or the CGDIObject superclass).

Comments

  1. 13 Sep 2006 at 07:43

    Sorry posted in wrong thread

  2. 13 Sep 2006 at 06:14
    Hi

    i am new to VC++, while creating a model dialog from a cwnd class i am getting a Assertion error.
    can any one tell me where i did wrong.

    class CAuthen:: public CWnd
    {
      ............
    ..
    public:
     
    CAuthenDlg auth_dlg;
    ..

    public :

     void checkAuthentication();
     

    };

    void CAuthen :: checkAuthentication()
    {
    ..
    ..
    ..

       if(  auth_dlg.DoModal() == IDOK)
        {
           ...
           // some operation goes here
        }
    ..
    ..
    }



































  3. 01 Jan 1999 at 00:00

    This thread is for discussions of Attaching and Detaching Objects.

Leave a comment

Sign in or Join us (it's free).

Joseph M. Newcomer

Want to stay in touch with what's going on? Follow us on twitter!