Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Visual C++ and MFC Fundamentals Chapter 12: Dialog-Based Windows


11.5.6..Parenthood............................................................................................


After specifying that the control you are creating is a child, which is done by placing the
control on a form or a dialog box or by using or adding the WS_CHILD style, you must
specify what window is the parent of your control. The parenthood of a control is
automatically set when the control is placed on a host: the host becomes the parent and
the parent is responsible for destroying the children when it is closed.

If you are programmatically creating the control, you can specify its parent by passing a
window handle as the pParentWnd argument of the the CreateWindow(), the
CreateWindowEx() functions, the Create(), or the second version of the CreateEx()
member functions. If you are creating the control in a member function or an event of the
parent window that is a descendent of CWnd, you can pass this argument as the this
pointer. Here is an example:

void CSecondDlg::OnFirstControl()
{
// TODO: Add your control notification handler code here
CWnd *First = new CWnd;
CString StrClsName = AfxRegisterWndClass(CS_VREDRAW | CS_HREDRAW,
LoadCursor(NULL, IDC_CROSS),
(HBRUSH)GetStockObject(BLACK_BRUSH)
LoadIcon(NULL, IDI_WARNING));

First->Create(StrClsName, NULL,WS_CHILD | WS_VISIBLE | WS_BORDER,
CRect(20, 20, 120, 60), this, );
}

Specifying the parent as the this pointer indicates that when the parent is destroyed, it
will also make sure the child control is destroyed. If you want the application to be the
parent and owner of the control, use the first version of the CreateEx() method and pass
the handle of your application as the hwndParent argument.

11.5.7..Control Identification..........................................................................


After adding a control to your application, you must identify it. An identifier is not a
string, it is a constant integer that is used to identify the control. Therefore, it is not the
name of the control.

If you have just added the control, Visual C++ would assign a default identifier. If the
control is static, it would have the IDC_STATIC identifier. Every other control gets an
identifier that mimics its class name followed by a number. An example would be
Free download pdf