Introduction
This sampls how to add a drop-down arrow button of Internet Explorer.
regsvr32 debug/dropbutton.dll
Using the code
LRESULT CIEToolbarWnd::OnCheckButton(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
if (m_isIE7)
{
return DefWindowProc(uMsg, wParam, lParam);
}
if (m_ParentWnd.GetButtonID() == (int)wParam)
{
BrowserWndInfo *pInfo = m_ParentWnd.GetBrowserWndInfo();
if (pInfo != NULL)
{
if (pInfo->nButtonState == TBSTATE_CHECKED)
{
lParam = MAKELONG(TRUE, 0);
}
else
{
lParam = MAKELONG(FALSE, 0);
}
}
}
}
LRESULT CIEToolbarWnd::OnAddString(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/)
{
LRESULT lResult = DefWindowProc(uMsg, wParam, lParam);
if (m_isIE7)
{
return lResult;
}
if ((int)wParam == 0)
{
LPWSTR strButtonText = (LPWSTR)lParam;
if (_tcscmp(BUTTON_NAME, strButtonText) == 0)
{
m_buttonStrIndex = lResult;
}
}
return lResult;
}
LRESULT CIEToolbarWnd::OnAddButton(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/)
{
LRESULT lResult = DefWindowProc(uMsg, wParam, lParam);
if (m_isIE7)
{
return lResult;
}
int nButtons = (int)wParam;
LPTBBUTTON pButtons = (LPTBBUTTON)lParam;
for (int i = 0; i < nButtons; i++)
{
if (pButtons[i].iString == m_buttonStrIndex)
{
m_ParentWnd.SetButtonID(pButtons[i].idCommand);
m_ParentWnd.NotifySetButtonInfo(pButtons[i].idCommand);
}
}
return lResult;
}
LRESULT CIEToolbarWnd::OnSetButtonInfo(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/)
{
int iID = (int) wParam;
LPTBBUTTONINFO lptbbi = (LPTBBUTTONINFO)lParam;
int ButtonID = m_ParentWnd.GetButtonID();
if (ButtonID == -1)
{
if (lptbbi->pszText != NULL && wcscmp(lptbbi->pszText, L"DropButton") == 0)
{
ButtonID = iID;
m_ParentWnd.SetButtonID(ButtonID);
}
}
if (ButtonID == iID)
{
lptbbi->dwMask = lptbbi->dwMask | TBIF_STYLE | TBSTYLE_CHECK| TBIF_STATE;
lptbbi->fsStyle = lptbbi->fsStyle|BTNS_DROPDOWN |BTNS_CHECK;
BrowserWndInfo* pInfo = m_ParentWnd.GetBrowserWndInfo();
if (pInfo != NULL && pInfo->nButtonState == TBSTATE_CHECKED)
{
lptbbi->dwMask = lptbbi->dwMask | TBIF_STYLE | TBSTYLE_CHECK| TBIF_STATE;
lptbbi->fsStyle = lptbbi->fsStyle|BTNS_DROPDOWN |BTNS_CHECK;
lptbbi->fsState = TBSTATE_ENABLED|TBSTATE_CHECKED;
}
else
{
lptbbi->dwMask = lptbbi->dwMask | TBIF_STYLE | TBSTYLE_CHECK| TBIF_STATE;
lptbbi->fsStyle = lptbbi->fsStyle|BTNS_DROPDOWN |BTNS_CHECK;
lptbbi->fsState = TBSTATE_ENABLED;
}
}
return DefWindowProc(uMsg, wParam, lParam);
}
void CIEToolbarWnd::SubclassToolbar(IWebBrowser2 *pWB)
{
if (pWB == NULL)
{
return;
}
long hWnd = NULL;
pWB->get_HWND(&hWnd);
int nViewCount = m_arrayBrowserWndInfo.GetSize();
BrowserWndInfo *pBrowserWndInfo = new BrowserWndInfo;
if (pBrowserWndInfo == NULL)
{
return;
}
pBrowserWndInfo->pWebBrowser = pWB;
pBrowserWndInfo->nButtonState = 0;
pBrowserWndInfo->hBrowserWnd = (HWND)hWnd;
m_arrayBrowserWndInfo.Add(pBrowserWndInfo);
if (m_ParentWnd.GetBrowserWndInfo() == NULL)
{
m_ParentWnd.SetBrowserWndInfo(pBrowserWndInfo);
}
if (nViewCount > 0)
{
return;
}
HWND hToolbarWnd = NULL;
if (m_isIE7)
{
ie7SubclassToolbar((HWND)hWnd);
}
else
{
normalSubclassToolbar((HWND)hWnd);
}
}
void CIEToolbarWnd::UnsubclassToolbar(IWebBrowser2 *pWebBrowser)
{
BrowserWndInfo* pBrowseWndInfo = findBrowserWndInfo(pWebBrowser);
if (pBrowseWndInfo != NULL)
{
m_arrayBrowserWndInfo.Remove(pBrowseWndInfo);
m_ParentWnd.SetBrowserWndInfo(NULL);
}
if (m_arrayBrowserWndInfo.GetSize() == 0)
{
if (m_hWnd)
{
UnsubclassWindow();
}
if (m_ParentWnd.m_hWnd)
{
m_ParentWnd.UnsubclassWindow();
}
}
if (pBrowseWndInfo != NULL)
{
delete pBrowseWndInfo;
}
}
void CIEToolbarWnd::SetCurrentBrowserWnd(IWebBrowser2 *pWebBrowser)
{
if (pWebBrowser == NULL)
{
return;
}
BrowserWndInfo* pBrowseWndInfo = findBrowserWndInfo(pWebBrowser);
m_ParentWnd.SetBrowserWndInfo(pBrowseWndInfo);
}
void CIEToolbarWnd::IsIE7(BOOL isIE7)
{
m_isIE7 = isIE7;
}
BOOL CIEToolbarWnd::ie7SubclassToolbar(HWND hBrowserWnd)
{
HWND hCmdbarWnd = FindWindowEx(hBrowserWnd, NULL, L"CommandBarClass", NULL);
if (hCmdbarWnd)
{
HWND hRebarWnd = FindWindowEx(hCmdbarWnd, NULL, L"ReBarWindow32", NULL);
HWND hToolbarWnd = ::GetDlgItem(hRebarWnd, 0x0000A000);
if (hToolbarWnd != NULL)
{
m_ParentWnd.SetToolbarWnd(hToolbarWnd);
SubclassWindow(hToolbarWnd);
m_ParentWnd.SubclassWindow(hCmdbarWnd);
return TRUE;
}
}
return FALSE;
}
BOOL CIEToolbarWnd::normalSubclassToolbar(HWND hBrowserWnd)
{
HWND hCmdbarWnd = ::GetDlgItem(hBrowserWnd, 0x0000A005);
if (hCmdbarWnd)
{
HWND hRebarWnd = FindWindowEx(hCmdbarWnd, NULL, L"ReBarWindow32", NULL);
if (hRebarWnd)
{
HWND hToolbarWnd = ::GetDlgItem(hRebarWnd, 0x0000A000);
if (hToolbarWnd != NULL)
{
m_ParentWnd.SetToolbarWnd(hToolbarWnd);
SubclassWindow(hToolbarWnd);
m_ParentWnd.SubclassWindow(hCmdbarWnd);
return TRUE;
}
}
}
return FALSE;
}
Points of Interest
Did you learn anything interesting/fun/annoying while writing
the code? Did you do anything particularly clever or wild or zany?
History
Keep a running update of any changes or improvements you've
made here.