我终于可以解决它了。下面的答案和this 问题中的答案确实对我有所帮助。在那里,您还可以找到有关此问题的更多信息。
如果有人有同样的问题,这是我的代码。我不认为没有我的项目代码的其余部分它会编译,但它应该给出一个想法,如何在浏览器帮助对象中实现这样的对话框:
标题:
#include "atlbase.h"
#include "atlwin.h"
#include "resources/resource.h"
#include <string>
class NotificationBar : public CDialogImpl<NotificationBar>
{
public:
NotificationBar();
enum { IDD = IDD_NOTIFICATIONBAR };
BEGIN_MSG_MAP(CMyDialog)
COMMAND_HANDLER(IDC_CANCEL, BN_CLICKED, OnBnClickedCancel)
END_MSG_MAP()
void show(const std::string &message);
void show();
void fitSize();
void hide();
void setText(const std::string &text);
private:
LRESULT OnBnClickedCancel(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
bool isShown;
};
来源:
#include "NotificationBar.hpp"
NotificationBar::NotificationBar()
:isShown(false)
{
}
void NotificationBar::show(const std::string &message)
{
show();
setText(message);
}
void NotificationBar::show()
{
if(isShown)
{
fitSize();
return;
}
isShown=true;
WebBrowser webbrowser(BrowserHelperObject::getInstance().getBrowser());
//Create dialog
Create(webbrowser.getCurrentTabHwnd());
//Set dialog size
fitSize();
ShowWindow(SW_SHOWNORMAL);
}
void NotificationBar::hide()
{
if(!isShown) return;
ShowWindow(SW_HIDE);
DestroyWindow();
isShown=false;
fitSize();
}
void NotificationBar::fitSize()
{
//This method is highly non portable. I is possible that it will not work in future versions of
//Internet explorer. It is dependend on the layout of the IE window and on the class names
//of its child windows (status bar, document view, ...).
//If the plugin gets some strange layout on future versions of IE or doesn't show a message at all,
//check and change this function.
WebBrowser webbrowser(BrowserHelperObject::getInstance().getBrowser());
CWindow tab(webbrowser.getCurrentTabHwnd());
CWindow child(FindWindowEx(tab,NULL,_T("Shell DocObject View"),_T("")));
CWindow statusbar(FindWindowEx(tab,NULL,_T("msctls_statusbar32"),_T("")));
RECT statusbarrect;
statusbar.GetWindowRect(&statusbarrect);
RECT documentrect;
tab.GetClientRect(&documentrect);
documentrect.bottom-=(statusbarrect.bottom-statusbarrect.top);
if(isShown)
{
//Request document window rect
static const unsigned int DLGHEIGHT=50;
RECT dialogrect=documentrect;
documentrect.top+=DLGHEIGHT;
dialogrect.bottom=dialogrect.top+DLGHEIGHT;
//Shrink document window
MoveWindow(&dialogrect);
}
child.MoveWindow(&documentrect);
}
LRESULT NotificationBar::OnBnClickedCancel(WORD, WORD, HWND , BOOL&)
{
hide();
return 0;
}
void NotificationBar::setText(const std::string &text)
{
if(0==SetDlgItemText(IDC_TEXT, CA2W(text.c_str())))
ieaddon::util::bho::BrowserHelperObject::getInstance().ErrorMessageBox("Error",ieaddon::util::cast::IntToStr(GetLastError()));
}
函数webbrowser.getCurrentTabHwnd()返回当前标签窗口:
HWND WebBrowser::getCurrentTabHwnd()
{
CComPtr<IServiceProvider> pServiceProvider;
if (!SUCCEEDED(_browser->QueryInterface(IID_PPV_ARGS(&pServiceProvider))))
throw std::exception("QueryInterface for IID_IServiceProvider failed in WebBrowser::getCurrentTabHwnd()");
CComPtr<IOleWindow> pWindow;
if (!SUCCEEDED(pServiceProvider->QueryService(SID_SShellBrowser, IID_PPV_ARGS(&pWindow))))
throw std::exception("QueryService for SID_SShellBrowser, IID_IOleWindow failed in WebBrowser::getCurrentTabHwnd()");
HWND hwndBrowser = NULL;
if (!SUCCEEDED(pWindow->GetWindow(&hwndBrowser)))
throw std::exception("GetWindow failed in WebBrowser::getCurrentTabHwnd()");
return hwndBrowser;
}
您还必须在每次调整浏览器窗口大小时调用 NotificationBar::fitSize()。为此,您可以在 IE 的 DocumentComplete 事件中使用 IHTMLWindow3::attachEvent(_T("onresize"),...)。
在 DocumentComplete 处理程序中获取 IHTMLWindow3 实例的方法如下:
- IWebBrowser2::getDocument() 返回 IHTMLDocument2
- IHTMLDocument2::get_parentWindow() 返回 IHTMLWindow2
- IHTMLWindow2::QueryInterface(IID_IHTMLWindow3,...) 成功