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.
Add the FileName property.
1. In the ClassView tab of the Project Workspace window, expand DemoMFCLib and click _DDemoMFC with the right mouse button and select Add > Add Property.
2. Enter FileName in the Property Name field.
3. Select the “Get/Set methods” option.
4. Choose BSTR from the Property Type drop-down list.
5. Be sure the Parameters list is empty.
Add the Background property.
1. In the ClassView tab of the Project Workspace window, expand DemoMFCLib and click _DDemoMFC with the right mouse button and select Add > Add Property.
2. Enter Background in the Property Name field.
3. Select the option “Get/Set methods”.
4. Choose OLE_COLOR from the Property Type drop-down list.
5. Leave the Parameters field empty.
Add new private member variables.
1. In the ClassView tab of the Project Workspace window, click CDemoMFCCtrl with the right mouse button.
2. Choose Add > Add Variable from the menu that appears.
3. Enter CString 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 name m_Background in Variable Name and click OK.
Modify the CDemoMFCCtrl constructor.
In the file DemoMFCCtl.cpp, modify the CDemoMFCCtrl constructor as follows to initialize the member variables m_FileName and m_Background:
CDemoMFCCtrl()
// Added for Views.
: m_Ctrl(0), m_FileName(), m_Background(-1)
// End Added for Views.
{
InitializeIIDs(&IID_DDemoMFC, &IID_DDemoMFCEvents);
 
// TODO: Initialize your control’s instance data here.
}
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(const char*);
2. Add the definition DemoCtrl::setFileName to the file DemoCtrl.cpp:
void
DemoCtrl::setFileName(const char* filename)
{
if (!filename || !*filename)
return;
IlvManager* manager = getManager();
if (!manager)
return;
manager->initReDraws();
manager->deleteAll(IlvTrue, IlvFalse);
manager->read(filename);
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 CDemoMFCCtrl::SetFileName and CDemoMFCCtrl::GetFileName generated by the wizard in the file DemoMFCCtl.cpp as follows:
BSTR CDemoMFCCtrl::GetFileName()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
CString strResult;
// TODO: Add your property handler here
strResult = m_FileName;
return strResult.AllocSysString();
}
void CDemoMFCCtrl::SetFileName(LPCTSTR lpszNewValue)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
// TODO: Add your property handler here
// Added for Views.
if (!lpszNewValue || !*lpszNewValue)
return;
if (m_FileName != lpszNewValue) {
m_FileName = lpszNewValue;
if (m_Ctrl && m_FileName.GetLength())
m_Ctrl->setFileName(lpszNewValue);
}
// End Added for Views.
SetModifiedFlag();
}
2. Modify the member functions CDemoMFCCtrl::SetBackground and CDemoMFCCtrl::GetBackground generated by the wizard in the file DemoMFCCtl.cpp:
OLE_COLOR CDemoMFCCtrl::GetBackground()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
// TODO: Add your dispatch handler code here
return m_Background;
}
 
void CDemoMFCCtrl::SetBackground(OLE_COLOR nNewValue)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
// TODO: Add your property handler code here
if (m_Background != nNewValue) {
m_Background = nNewValue;
if (m_Ctrl && (m_Background != -1))
m_Ctrl->setBackground(m_Background);
}
SetModifiedFlag();
}
Modify the CDemoMFCCtrl::OnCreate member function.
Modify the member function CDemoMFCCtrl::OnCreate in the file DemoMFCCtl.cpp to set the file name and/or the background color when the corresponding properties are defined:
int CDemoMFCCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (COleControl::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
// 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;
}
Modify the project settings.
1. Open the project property pages.
2. In the Project Settings dialog box, select All Configurations and click the General tab.
3. For Character Set, select use Multi-byte Character Set.
Test the properties.
With Internet Explorer, you can modify the file test.htm 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
Note: installed the ILVHOME or ILVPATH environment variable path.
<HTML>
<HEAD>
<TITLE>Test page for object DemoMFCCtl</TITLE>
</HEAD>
 
<SCRIPT LANGUAGE = JavaScript>
 
function load(value)
{
DemoMFCCtrl.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="DemoMFCCtrl"
CLASSID="CLSID:3D31E7B8-400B-
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