Help Center

CWindow.AddControlmethod

Adds a control to the window.

WindowsmethodCWindow.incdocumented

Syntax

FUNCTION AddControl (BYREF wszClassName AS WSTRING, BYVAL hParent AS HWND = NULL, BYVAL cID AS LONG_PTR = 0, BYREF wszTitle AS WSTRING = "", BYVAL x AS LONG = 0, BYVAL y AS LONG = 0, BYVAL nWidth AS LONG = 0, BYVAL nHeight AS LONG = 0, BYVAL dwStyle AS LONG = -1, BYVAL dwExStyle AS LONG = -1, BYVAL lpParam AS LONG_PTR = 0) AS HWND

Parameters

NameDescription
wszClassNameThe window class name. The class name can be any name registered with RegisterClass or RegisterClassEx, provided that the module that registers the class is also the module that creates the window. The class name can also be any of the predefined system class names.
hParentA handle to the parent or owner window of the control being created. If this parameter is omitted, the handle of the main window beloging to the CWindow class is used.
cIDThe control identifier, an integer value used to notify its parent about events. The application determines the control identifier; it must be unique for all controls with the same parent window.
wszTitleThe window name. If the window style specifies a title bar, the window title is displayed in the title bar. When creating controls, such as buttons, check boxes, and static controls, use wszTitle to specify the text of the control. When creating a static control with the SS_ICON style, use wszTitle to specify the icon name or identifier. To specify an identifier, use the syntax "#num".
xThe x-coordinate of the upper-left corner of the window relative to the upper-left corner of the parent window's client area.
yThe initial y-coordinate of the upper-left corner of the window relative to the upper-left corner of the parent window's client area.
nWidthThe width of the control.
nHeightThe height of the control.
dwStyleThe window styles of the control being created.
dwExStyleThe extended window styles of the control being created.
lpParamOptional. Pointer to a value to be passed to the window through the CREATESTRUCT structure (lpCreateParams member) pointed to by the lParam param of the WM_CREATE message. This message is sent to the created window by this function before it returns.

Return value

If the method succeeds, the return value is a handle to the new control.

If the method fails, the return value is NULL. To get extended error information, call GetLastError.

This method typically fails for one of the following reasons:

  • An invalid parameter value
  • The system class was registered by a different module
  • The WH_CBT hook is installed and returns a failure code
  • If the control is not registered or its window window procedure fails WM_CREATE or WM_NCCREATE.

Description

Adds a control to the window.

Remarks

For the "BITMAPBUTTON", "BITMAPLABEL", "ICONBUTTON" and "ICONLABEL" controls you must pass in the wszTitle parameter the name of the bitmap in the resource file (.RES). If the image resource uses an integral identifier, the name should begin with a number symbol (#) followed by the identifier in an ASCII format, e.g., "#998". Otherwise, use the text identifier name for the image.

Reference

  • Include file CWindow.inc
  • Defined in AfxNova/CWindow.inc:1555
  • Documented in Windows/Windows GUI/CWindow Class.md
  • Topic: CWindow Class