BEGIN_EVENTSINK_MAP(CMainFrame, CWnd)
//{{AFX_EVENTSINK_MAP(CMainFrame)
ON_EVENT(CMainFrame, IDC_ACTIVEBAR, 71, OnActiveBarClick, VTS_DISPATCH)
//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()
Note: The dispatch id constants are located in the file dispids.h
3- Add event handler to mainfrm.cpp
void CMainFrame::OnActiveBarClick(LPDISPATCH pDisp)
{
ITool *pTool=(ITool *)pDisp;
LONG id;
pTool->get_ToolID(&id);
if (id!=0)
SendMessage(WM_COMMAND,MAKELONG(id,0),NULL);
}
As you see hooking up the toolbar click event to commands is very easy since the
toolid's of tool objects map 1-to-1 to command ids of the WordPad application.
Filling the font name and size combobox
To fill ActiveBar's font name and size combobox objects, we will define a class
to enumerate fonts and use ActiveBars tool.CBList property for filling the combobox
list. The source code is provided in fenum.h and fenum.cpp files.
To fill the font combobox use the following code
void CMainFrame::FillFontCombo()
{
VARIANT v;
CTool tool;
// Get tool by tool name
v.vt=VT_BSTR;
v.bstrVal=SysAllocString(L"FontName");
tool=m_bar.GetTools().Item(&v);
VariantClear(&v);
// Set properties of the combobox
tool.SetCBStyle(2); // sort
tool.SetCBWidth(180); // set combolist width
// Get combolist object of the tool and fill font names
CComboList clist=tool.GetCBList();
clist.Clear();
int cnt,fontCount;
CString name;
fontCount=m_fontEnum.GetFontCount();
for (cnt=0;cntget_Enabled(&bPrevEnabled);
if (bOn && (!bPrevEnabled))
pTool->put_Enabled(VARIANT_TRUE);
if ((!bOn) && bPrevEnabled)
pTool->put_Enabled(VARIANT_FALSE);
}
void CToolCmdUI::SetCheck(int nCheck)
{
ASSERT(nCheck >= 0 && nCheck <= 2); // 0=>off, 1=>on, 2=>indeterminate
CMainFrame *pMainFrame=(CMainFrame *)m_pOther;
ASSERT(pMainFrame!= NULL);
ASSERT_KINDOF(CMainFrame, pMainFrame);
ASSERT(m_nIndex < m_nIndexMax);
VARIANT_BOOL bPrevChecked;
pTool->get_Checked(&bPrevChecked);
if (nCheck==1 && (!bPrevChecked))
pTool->put_Checked(VARIANT_TRUE);
if (nCheck!=1 && bPrevChecked)
pTool->put_Checked(VARIANT_FALSE);
}
void CToolCmdUI::SetText(LPCTSTR)
{
// ignore
}
void TwipsToPointString(LPTSTR lpszBuf, int nTwips)
{
ASSERT(lpszBuf != NULL);
lpszBuf[0] = NULL;
if (nTwips >= 0)
{
// round to nearest half point
nTwips = (nTwips+5)/10;
if ((nTwips%2) == 0)
_stprintf(lpszBuf, _T("%ld"), nTwips/2);
else
_stprintf(lpszBuf, _T("%.1f"), (float)nTwips/2.F);