Microsoft KB Archive/260744

From BetaArchive Wiki
Knowledge Base


How to set an ActiveX control design-time property at run time in Visual C++

Article ID: 260744

Article Last Modified on 12/30/2005



APPLIES TO

  • Microsoft Visual C++ 6.0 Enterprise Edition
  • Microsoft Visual C++ 6.0 Professional Edition
  • Microsoft Visual C++ 6.0 Standard Edition
  • Microsoft Visual C++ 2005 Express Edition
  • Microsoft Visual C++ .NET 2003 Standard Edition
  • Microsoft Visual C++ .NET 2002 Standard Edition



This article was previously published under Q260744

Note Microsoft Visual C++ .NET 2002 and Microsoft Visual C++ .NET 2003 support both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model. The information in this article applies only to unmanaged Visual C++ code. Microsoft Visual C++ 2005 supports both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model.

SUMMARY

Some properties of an ActiveX control may be read-only at run time, which makes dynamic creation difficult. Design mode can be simulated temporarily for property initialization by overriding the control container's OnAmbientProperty handler.

MORE INFORMATION

For ActiveX controls that offer design-time properties, the author of the control probably disallowed dynamic run-time manipulation of these properties on purpose. Take care when you adjust these properties. In some cases, however, the following method may yield useful results. Always carefully test your solution. The control may not be able to switch modes easily, so this technique should be used with care, and only to allow these properties to be set when the control is created dynamically, not as license to modify these settings at will.

The RichTextBox control is an example of a control that offers such properties. Because the Multiline and Scrollbars properties of the RichTextBox control are read-only at run time, this control is almost impossible to create dynamically except in its default form. The solution, in an MFC container, is to override OnAmbientProperty. This function receives, along with a pointer to the control site, the DISPID of the ambient property, and a pointer to the value it should return. This allows the container to convince the control that it is in design mode when the value of DISPID_AMBIENT_USERMODE is queried for.

Following is an example of how to override this function:

BOOL CRichTextDlg::OnAmbientProperty(COleControlSite* pSite, DISPID dispid, VARIANT* pvar) 
{
    if ((dispid == DISPID_AMBIENT_USERMODE) && !m_bInitDone)
    {
        V_VT(pvar) = VT_BOOL;
        V_BOOL(pvar) = 0;
        return TRUE;
    }

    return CDialog::OnAmbientProperty(pSite, dispid, pvar);
}
                

A member variable, m_bInitDone, was added to the class. It determines the result to return from the function. Once initialization of the control has been completed, this variable should be set so that the container reveals its true state.

REFERENCES

For additional information about extensive customization of MFC control containers, click the article number below to view the article in the Microsoft Knowledge Base:

196835 How to Override MFC's Default Control Containment



Additional query words: DISPID_AMBIENT_USERMODE Ambient

Keywords: kbhowto kbcontainer kbctrl KB260744