Step 3: Adding Custom Properties to the ActiveX Control
This step shows how to add a file name and a background color to the ActiveX control. The file name is that of the file read by the manager. By default, this file is data/browse.ilv, which we included in the .rc file in Step 2. The background color is that of the manager view.
You are going to:
Add the FileName property.
1. In the ClassView tab of the Project Workspace window, click IDemoAtlCtrl with the right mouse button and select Add > Add Property.
2. Enter FileName in the Property Name field.
3. Choose BSTR from the Property Type drop-down list.
4. Be sure the Parameters field is empty.
5. If necessary, select Get Function, Put Function, and PropPut.
Leave PropPutRef unchecked. The attributes are id and helpstring.
6. Click OK.
Add the Background property.
1. In the ClassView tab of the Project Workspace window, click IDemoAtlCtrl with the right mouse button and select Add > Add Property.
2. Enter Background in the Property Name field.
3. Choose OLE_COLOR in the Property Type drop-down list.
4. Leave the Parameters field empty.
5. If necessary, select Get Function, Put Function, and PropPut.
Leave PropPutRef unckecked. The attributes are id (value 2) and helpstring.
6. Click OK.
Add new private member variables.
1. In the ClassView tab of the Project Workspace window, click CDemoATLCtrl with the right mouse button.
2. Choose Add > Add Variable from the menu that appears.
3. Enter CComBSTR in Variable Type and m_FileName in Variable Name and click OK.
4. Choose Add > Add Variable again.
5. Enter OLE_COLOR in Variable Type and m_Background in Variable Name and click OK.
Modify the CDemoAtlCtrl constructor.
In the file DemoATLCtrl.h, modify the CDemoATLCtrl constructor as follows to initialize the member variables m_FileName and m_Background:
CDemoATLCtrl()
// Added for Views.
: m_Ctrl(0), m_FileName(), m_Background(-1)
// End Added for Views.
{
m_bWindowOnly = TRUE;
}
Add the setFileName member function.
This member function must be added to the DemoCtrl class. It has one parameter of type const char* that represents the new file name. Its purpose is to load the corresponding file.
1. Add the declaration of DemoCtrl::setFileName to the file DemoCtrl.h:
void setFileName(CComBSTR& fileName);
2. Add the definition DemoCtrl::setFileName to the file DemoCtrl.cpp:
void
DemoCtrl::setFileName(CComBSTR& fileName)
{
if (!fileName || !fileName.Length())
return;
IlvManager* manager = getManager();
if (!manager)
return;
manager->initReDraws();
manager->deleteAll(IlvTrue, IlvFalse);
size_t len = wcstombs(0, fileName.m_str, 10000) + 1;
char* path = new char[len];
wcstombs(path, fileName.m_str, len);
manager->read(path);
delete [] path;
manager->fitTransformerToContents(getManagerView());
manager->reDrawViews();
}
Add the setBackground member function.
This member function must be added to the DemoCtrl class. It has one parameter of type OLE_COLOR that represents the new color. Its purpose is to convert the OLE_COLOR into an IlvColor* that Views can interpret and to redraw the view.
1. Add the declaration of DemoCtrl::setBackground to the file DemoCtrl.h:
void setBackground(OLE_COLOR);
2. Add the definition of DemoCtrl::setBackground to the file DemoCtrl.cpp:
void
DemoCtrl::setBackground(OLE_COLOR oleColor)
{
if (oleColor == -1)
return;
IlvManager* manager = getManager();
if (!manager)
return;
COLORREF colRef;
HPALETTE hPal = NULL;
if (getDisplay()->screenDepth() <= 8)
hPal = HPALETTE(getDisplay()->getPaletteHandle());
if (S_OK != OleTranslateColor(oleColor, hPal, &colRef))
return;
IlvIntensity red, blue, green;
getDisplay()->pixelToRGB((unsigned long)(colRef), red, blue, green);
IlvColor* color = getDisplay()->getColor(red, blue, green);
manager->initReDraws();
manager->setBackground(getManagerView(), color);
manager->reDraw();
manager->reDrawViews();
}
Modify the member functions generated by the wizard.
1. Modify the member functions CDemoATLCtrl::put_FileName and CDemoATLCtrl::get_FileName generated by the wizard in the file DemoATLCtrl.cpp as follows:
STDMETHODIMP CDemoATLCtrl::put_FileName(BSTR newVal)
{
// TODO: Add your implementation code here
// Added for Views.
if (!newVal)
return S_OK;
if (m_FileName != newVal) {
m_FileName = newVal;
if (!m_FileName.Length())
m_FileName.Empty();
if (m_Ctrl && !!m_FileName)
m_Ctrl->setFileName(m_FileName);
}
// End Added for Views.
return S_OK;
}
STDMETHODIMP CDemoATLCtrl::get_FileName(BSTR *pVal)
{
// TODO: Add your implementation code here
// Added for Views.
return m_FileName.CopyTo(pVal);
// End Added for Views.
return S_OK;
}
2. Modify the member functions CDemoATLCtrl::put_Background and CDemoATLCtrl::get_Background generated by the wizard in the file DemoATLCtrl.cpp:
STDMETHODIMP CDemoATLCtrl::put_Background(OLE_COLOR newVal)
{
// TODO: Add your implementation code here
// Added for Views.
if (m_Background != newVal) {
m_Background = newVal;
if (m_Ctrl && (m_Background != -1))
m_Ctrl->setBackground(m_Background);
}
// End Added for Views.
return S_OK;
}
STDMETHODIMP CDemoATLCtrl::get_Background(OLE_COLOR *pVal)
{
// TODO: Add your implementation code here
// Added for Views.
*pVal = m_Background;
// End Added for Views.
return S_OK;
}
Add lines to the property map macro.
Add the following lines to the BEGIN_PROP_MAP/END_PROP_MAP macro declaration block in the DemoATLCtrl.h file.
// Added for Views.
PROP_ENTRY_TYPE("FileName", 1, CLSID_NULL, VT_BSTR)
PROP_ENTRY_TYPE("Background", 2, CLSID_NULL, VT_COLOR)
// End Added for Views.
Modify the CDemoATLCtrl::OnCreate member function.
Modify the member function CDemoATLCtrl::OnCreate in the file DemoATLCtrl.h to set the file name and/or the background color when the corresponding properties are defined:
LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
// TODO : Add Code for message handler. Call DefWindowProc if necessary.
// Added for Views.
try {
m_Ctrl = new DemoCtrl(m_hWnd);
// Added for the FileName property.
m_Ctrl->setFileName(m_FileName);
// Added for the Background property.
m_Ctrl->setBackground(m_Background);
} catch (...) {
if (m_Ctrl) {
delete m_Ctrl;
m_Ctrl = 0;
}
}
// End Added for Views.
return 0;
}
Test the properties.
To test the properties, you can use Internet Explorer. With Internet Explorer, you can modify the file DemoATLCtrl.html and use the JavaScript language.
To test the FileName property, we use a combo box that has three predefined choices.
Note: This example assumes that the Views data files are installed in the ILVHOME or ILVPATH environment variable paths.
<HEAD>
<TITLE>ATL 3.0 test page for object DemoATLCtrl</TITLE>
</HEAD>
 
<SCRIPT LANGUAGE = JavaScript>
 
function load(value)
{
DemoATLCtrl.FileName = value;
}
 
</SCRIPT>
<BODY>
 
<SELECT NAME=Files onchange = "load(value)" >
<OPTION Selected Value="data/browse.ilv">World
<OPTION Value="europe.ilv"> Europe
<OPTION Value="africa.ilv"> Africa
</SELECT>
<P>
 
<OBJECT WIDTH=400 HEIGHT=400 ID="DemoATLCtrl" CLASSID="CLSID:3B10417D-3E6C-
11D3-B74F-00C04F68A89D"></OBJECT>
</BODY>
</HTML>
Note: Since the class id is generated, the class id in your file may be different from the one in this example.
Published date: 05/24/2022
Last modified date: 02/24/2022