Creating an Instance of a Control

You can add a standard control by creating an instance of a control using CoCreateInstance.

    To add a standard control using CoCreateInstance

  1. Create an instance of a control with CoCreateInstance, and get a pointer to IASControl.

    Because Windows CE for the Auto PC supports only InProc servers, the dwClsContext parameter of CoCreateInstance must be CLSCTX_INPROC_SERVER.

    The following code example shows how to create a power list box control.

    hr = CoCreateInstance(CLSID_ASPOWERLISTBOX, NULL,
                          CLSCTX_INPROC_SERVER, IID_ASPOWERLISTBOX,
                          (PVOID *) &pPLB);
    if (FAILED(hr))    goto LReturn;
    
  2. Use the IASControl property methods to configure such parameters as color, size, and position.

    The following code example shows how to size and align an IASControl control.

    pPLB->SetBounds(0,18,225,45);
    pPLB->put_Alignment(ASFC_ALIGN_CENTER);
    

    This code example uses an IASControl method, SetBounds, to specify the dimensions of the control in relation to its form. The values passed to SetBounds describe a control whose x-coordinate begins at the left edge of the form and whose y-coordinate begins 18 pixels from the top of the form. The control is 225 x 45 pixels. To specify that the power list box should be centered, the code example uses put_Alignment, which is a method in the IASPowerListBox interface.

  3. Use the form’s IASControl::put_ClassMsgSink, with the sink object’s pointer as a parameter, to assign a class message sink to the form.

    When an action occurs, such as a keystroke or focus change, the form notifies the active control by calling one of several messages, such as IASClassMsgSink::HandleGotFocus or IASClassMsgSink::HandleKeyPress. These notifications are handled by the control, and are not passed to the application. If your application needs to handle such actions, you must implement a class message sink. To assign a class message sink to a form, use IASControl::put_ClassMsgSink.

    The following code example shows how to add a class message sink.

    hr = m_pForm->put_ClassMsgSink((IASClassMsgSink *)this);
    
  4. Add the standard control to the form with IASForm::Add.

    The following code example shows how to use the Add method.

    g_pForm->Add(pPLB, IDC_PLB);