开发IE插件Toolbar

免费教你开发IE插件Toolbar的文章可真是少见。还好我在www.codeproject.com里找到一篇,http://www.codeproject.com/atl/ietoolbartutorial.asp。不过还是花了一天的时间才自己编写一个Google Search Toolbar。
如果你下载了Internet Explorer Toolbar (Deskband) Tutorial的源代码后编译不通过,也不用奇怪,我就是耽搁在这些地方。先说说都有哪些编译问题。
1.编译的问题
如果遇到下面的问题,就说明commctrl.h的版本低。我们完全可以不用TBSTYLE_EX_MIXEDBUTTONS,用TBSTYLE_EX_DRAWDDARROWS就行。另外的三个删掉,换成
TBSTYLE_BUTTON | TBSTYLE_AUTOSIZE,这个问题就解决了
error C2065: 'TBSTYLE_EX_MIXEDBUTTONS' : undeclared identifier
error C2065: 'BTNS_BUTTON' : undeclared identifier
error C2065: 'BTNS_AUTOSIZE' : undeclared identifier
error C2065: 'BTNS_SHOWTEXT' : undeclared identifier
如果遇到下面问题,把工程文件(dsp)里面的/D _ATL_MIN_CRT删掉。
Linking...
Creating library ReleaseUMinDependency/MotleyFool.lib and object ReleaseUMinDependency/MotleyFool.exp
LIBCMT.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
ReleaseUMinDependency/MotleyFool.dll : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

MotleyFool.dll - 2 error(s), 0 warning(s)
如果你自己重新编写一个新的项目实现IE Toolbar的话,注意一下2个地方。
1.注意CMFToolbar的消息映射顺序。CHAIN_MSG_MAP_MEMBER一定要在WM_CREATE的前面。
BEGIN_MSG_MAP(CToolbarWnd)
CHAIN_MSG_MAP_MEMBER(m_EditWnd)
MESSAGE_HANDLER(WM_CREATE, OnCreate)
END_MSG_MAP()
2.注意CReflectionWnd的消息映射顺序。WM_CREATE一定要在CHAIN_MSG_MAP_MEMBER的前面。
BEGIN_MSG_MAP(CReflectWnd)
MESSAGE_HANDLER(WM_CREATE, OnCreate)
CHAIN_MSG_MAP_MEMBER(m_ToolbarWnd)
END_MSG_MAP()
2.Google Search Toolbar
开发IE插件Toolbar
http://www.codeproject.com/atl/ietoolbartutorial.asp有非常详细的开发步骤,在这里就不重复了。本节的目的是实现Google Search功能。创建ColimasBar工程,
创建CColimasBar类,IE Plugin接口,继承
public CComObjectRootEx<CComSingleThreadModel>,
public CComCoClass<CColimasBar, &CLSID_ColimasBar>,
public IDeskBand,
public IInputObject,
public IObjectWithSite,
public IDispatchImpl<IColimasBar, &IID_IColimasBar, &LIBID_IEBASELib>
创建CEditWnd类,输入栏控件,继承
public CWindowImpl<CEditWnd>
创建CReflectWnd类,消息传递空间,继承
public CWindowImpl<CEditWnd>
创建CToolbarWnd类,Toolbar控件,继承
public CWindowImpl<CEditWnd>
修改Toolbar的Title:
const WCHAR TITLE_CColimasBar[] = L"Google";
修改Button的Title:
TCHAR* pCaption = _T("Search!");
增加Button点击事件的Google Search处理函数GetValue
void CToolbarWnd::GetValue()
{
// if we have a web browser pointer then try to navigate to google site to retrieve search
if (m_pBrowser)
{
VARIANT vEmpty;
VariantInit(&vEmpty);
m_pBrowser->Stop();
_bstr_t bsSite;
// if the user has entered url then append them to the edit
if (m_EditWnd.GetWindowTextLength())
{
WCHAR *bstr = NULL;
m_EditWnd.GetWindowText(&bstr);
UINT i= WideCharToMultiByte(CP_UTF8,0,bstr,-1,NULL,0,NULL,NULL); //双字节转换
char *strB=new char[i];
WideCharToMultiByte (CP_UTF8,0,bstr,-1,strB,i,NULL,NULL); //转换为ASCII
UINT len=i;
char* tmp=new char[len*3];
tmp[0]='\0';
for(i=0;i<len-1;i++)
{
byte j= (unsigned char)strB[i]; sprintf(tmp,"%s%%%x",tmp,j); //转换为16进制。
}
bsSite = L"http://www.google.com/search?hl=zh-CN&q=";
//Google Search字符串
bsSite += tmp;
bsSite += "&rls=com.microsoft:en-US&ie=utf8&oe=utf8";
SysFreeString(bstr);
delete[] strB;
}
else
bsSite = "http://www.google.com/";
// have the webrowser navigate to the site URL requested depending on user input.
m_pBrowser->Navigate(bsSite, &vEmpty, &vEmpty, &vEmpty, &vEmpty);
}
}
开发IE插件Toolbar
本文涉及到的所有知识请参照http://www.codeproject.com/atl/ietoolbartutorial.asp