Microsoft KB Archive/201879

= How to determine whether an ActiveX ambient property is implemented =

Article ID: 201879

Article Last Modified on 11/21/2006

-

APPLIES TO

 Microsoft Foundation Class Library 4.2, when used with:  Microsoft Visual C++ 5.0 Enterprise Edition

 Microsoft Visual C++ 6.0 Enterprise Edition

 Microsoft Visual C++ 5.0 Professional Edition

 Microsoft Visual C++ 6.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Standard Edition</li></ul>

 Microsoft Visual C++ .NET 2002 Standard Edition</li></ul>

 Microsoft Visual C++ .NET 2003 Standard Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q201879

<div class="notice_section">

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

<div class="summary_section">

SUMMARY
There may be situations where you want to use the ambient property values when they exist and your own values when the ambient property values don't exist. For example, when the AmbientBackColor isn't implemented, you may want to use the StockBackColor instead of the System Back Color. By design, this can not be accomplished with the COleControl::Ambient... methods.

<div class="moreinformation_section">

MORE INFORMATION
The following methods can be added to your derived COleControl class to provide greater flexibility in using ambient properties than provided by the default class methods. The methods have the following format:

bool Ambient...([in/out]type* name, [in]bool bInit = false);

The return value is true if the ActiveX container implements the ambient property and false for all other cases. The name parameter is the address of a variable that is to be initialized with the ambient property. For AmbientFont, pDisp must be a pointer initialized to NULL; otherwise, it must point to a valid variable. Setting bInit to true causes the variable to be initialized to the default value as determined by MFC if the ambient property isn't implemented.

//***************************************************** // Method definitions for your control's .h file. //*****************************************************

bool AmbientAppearance(short* pAppearance, bool bInit = false); bool AmbientBackColor(OLE_COLOR* pBackColor, bool bInit = false); bool AmbientDisplayName(CString* pDisplayName, bool bInit = false); bool AmbientFont(LPFONTDISP pDisp, bool bInit = false); bool AmbientForeColor(OLE_COLOR* pForeColor, bool bInit = false); bool AmbientLocaleID(LCID* plcid, bool bInit = false); bool AmbientScaleUnits(CString* pStrScaleUnits, bool bInit = false); bool AmbientTextAlign(short* pITextAlign, bool bInit = false); bool AmbientUserMode(BOOL* pbUserMode, bool bInit = false); bool AmbientUIDead(BOOL* pbUIDead, bool bInit = false); bool AmbientShowGrabHandles(BOOL* pbShowGrab, bool bInit = false); bool AmbientShowHatching(BOOL* pbShowHatch, bool bInit = false);

//***************************************************** // Method implementations for your control's .cpp file. //*****************************************************

//***** AmbientAppearance ***** bool CMyOleControl::AmbientAppearance(short* pAppearance,                                     bool bInit) {

ASSERT (pAppearance); if (!GetAmbientProperty(DISPID_AMBIENT_APPEARANCE, VT_I2, pAppearance)) { if (bInit) *pAppearance = 0; return false; }else{ return true; } }

//***** AmbientBackColor ***** bool CMyOleControl::AmbientBackColor(OLE_COLOR* pBackColor,                                    bool bInit) {   ASSERT (pBackColor); if (!GetAmbientProperty(DISPID_AMBIENT_BACKCOLOR, VT_I4, pBackColor)) { if (bInit) *pBackColor = GetSysColor(COLOR_WINDOW); return false; }else{ return true; } }

//***** AmbientDisplayName ***** bool CMyOleControl::AmbientDisplayName(CString* pDisplayName,                                      bool bInit) {   ASSERT (pDisplayName); if (!GetAmbientProperty(DISPID_AMBIENT_DISPLAYNAME, VT_BSTR, pDisplayName)) { if (bInit) *pDisplayName = "CMyOleControl"; return false; }else return true; }

//***** AmbientFont ***** bool CMyOleControl::AmbientFont(LPFONTDISP pDisp,                               bool bInit) {   // pDisp must be NULL. ASSERT (!pDisp);

// Note: Caller MUST Release the font! if (!GetAmbientProperty(DISPID_AMBIENT_FONT, VT_DISPATCH, &pDisp)) { if (bInit) pDisp = NULL; return false; }else{ return true; } }

//***** AmbientForeColor ***** bool CMyOleControl::AmbientForeColor(OLE_COLOR* pForeColor,                                    bool bInit) {   ASSERT (pForeColor); if (!GetAmbientProperty(DISPID_AMBIENT_FORECOLOR, VT_I4, pForeColor)) { if (bInit) *pForeColor = GetSysColor(COLOR_WINDOWTEXT); return false; }else{ return true; } }

//***** AmbientLocaleID ***** bool CMyOleControl::AmbientLocaleID(LCID* plcid, bool bInit) {   ASSERT (plcid); if (!GetAmbientProperty(DISPID_AMBIENT_LOCALEID, VT_I4, plcid)) { if (bInit) *plcid = 0; return false; }else{ return true; } }

//***** AmbientScaleUnits ***** bool CMyOleControl::AmbientScaleUnits(CString* pStrScaleUnits,                                     bool bInit) {   ASSERT (pStrScaleUnits); if (!GetAmbientProperty(DISPID_AMBIENT_SCALEUNITS, VT_BSTR, pStrScaleUnits)) { if (bInit) *pStrScaleUnits = ""; return false; }else{ return true; } }

//***** AmbientTextAlign ***** bool CMyOleControl::AmbientTextAlign(short* piTextAlign, bool bInit) {   ASSERT (piTextAlign); if (!GetAmbientProperty(DISPID_AMBIENT_TEXTALIGN, VT_I2, piTextAlign)) { if (bInit) *piTextAlign = 0; return false; }else{ return true; } }

//***** AmbientUserMode ***** bool CMyOleControl::AmbientUserMode(BOOL* pbUserMode, bool bInit) {   ASSERT(pbUserMode); if (!GetAmbientProperty(DISPID_AMBIENT_USERMODE, VT_BOOL, pbUserMode)) { if (bInit) *pbUserMode = TRUE; return false; }else{ return true; } }

//***** AmbientUIDead ***** bool MyOleControl::AmbientUIDead(BOOL* pbUIDead, bool bInit) {   ASSERT(pbUIDead); if (!GetAmbientProperty(DISPID_AMBIENT_UIDEAD, VT_BOOL, pbUIDead)) { if (bInit) *pbUIDead = FALSE; return false; }else{ return true; } }

//***** AmbientShowGrabHandles ***** bool CMyOleControl::AmbientShowGrabHandles(BOOL* pbShowGrab, bool bInit) {   ASSERT(pbShowGrab); if (!GetAmbientProperty(DISPID_AMBIENT_SHOWGRABHANDLES, VT_BOOL, pbShowGrab)) { if (bInit) *pbShowGrab = TRUE; return false; }else{ return true; } }

//***** AmbientShowHatching ***** bool CMyOleControl::AmbientShowHatching(BOOL* pbShowHatch, bool bInit) {   ASSERT(pbShowHatch); if (!GetAmbientProperty(DISPID_AMBIENT_SHOWHATCHING, VT_BOOL, pbShowHatch)) { if (bInit) *pbShowHatch = TRUE; return false; }else{ return true; } }

<div class="references_section">