Reputation: 349
I make a dialog using visual studio 2017 c++ MFC, and web links by CHtmlEditCtrl.
But, the href links not working...
I expect the following behavior.
How can I fix my code?
BOOL CTestDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// ...
CHtmlEditCtrl* htmledit = new CHtmlEditCtrl();
CEdit* edit = (CEdit*)GetDlgItem(IDC_EDIT_HTML);
CRect rc;
edit->GetWindowRect(&rc);
this->ScreenToClient(&rc);
htmledit->Create(0, (WS_CHILD | WS_VISIBLE), rc, this, IDC_EDIT_HTML, 0);
CComPtr<IHTMLDocument2> document;
htmledit->GetDocument(&document);
WaitForComplete(document);
htmledit->SetDocumentHTML(_T("<a href=\"https://www.google.co.jp/\" target=\"_blank\">Google</a><br><a href=\"https://stackoverflow.com/\" target=\"_blank\">stackoverflow</a>"));
WaitForComplete(document);
return TRUE;
}
void CTestDlg::WaitForComplete(IHTMLDocument2* document)
{
BSTR ready;
document->get_readyState(&ready);
while (wcscmp(ready, L"complete"))
{
AfxPumpMessage();
document->get_readyState(&ready);
};
}
I referred to the following site.
Upvotes: 1
Views: 378
Reputation: 31599
CHtmlEditCtrl
overrides NavigateComplete2
and calls SetDesignMode(TRUE)
. If you want a viewer, instead of editor, then override as follows:
class CMyHtmlEditCtrl : public CHtmlEditCtrl
{
virtual void _OnNavigateComplete2(LPDISPATCH, VARIANT FAR*)
{
//SetDesignMode(TRUE);
}
public:
DECLARE_EVENTSINK_MAP()
};
BEGIN_EVENTSINK_MAP(CMyHtmlEditCtrl, CHtmlEditCtrl)
ON_EVENT_REFLECT(CMyHtmlEditCtrl, 252 /* NavigateComplete2 */, _OnNavigateComplete2, VTS_DISPATCH VTS_PVARIANT)
END_EVENTSINK_MAP()
Use this class instead of CHtmlEditCtrl
. You won't need WaitForComplete
anymore because SetDesignMode
is not blocking the calls in OnInitDialog
.
_OnBeforeNavigate
In addition, add #
to the link: href="#http://www.google.com"
so that the browser control doesn't know what to do with the link, and lets you handle it instead. You then convert #http://www.google.com
to http://www.google.com
and open the link.
Declare as CMyHtmlEditCtrl browser;
as class member to avoid leaks.
Example:
class CMyHtmlEditCtrl : public CHtmlEditCtrl
{
public:
virtual void _OnNavigateComplete2(LPDISPATCH, VARIANT FAR*)
{
//SetDesignMode(TRUE);
}
void _OnBeforeNavigate2(LPDISPATCH,
VARIANT* URL, VARIANT*, VARIANT*, VARIANT*, VARIANT*, VARIANT_BOOL*)
{
CString str(V_BSTR(URL));
int pos = str.Find(L'#');
if(pos >= 0)
{
str = str.Mid(pos + 1);
ShellExecute(NULL, L"open", str, NULL, NULL, SW_SHOWNORMAL);
}
}
DECLARE_EVENTSINK_MAP()
};
BEGIN_EVENTSINK_MAP(CMyHtmlEditCtrl, CHtmlEditCtrl)
ON_EVENT_REFLECT(CMyHtmlEditCtrl, 250, _OnBeforeNavigate2, VTS_DISPATCH VTS_PVARIANT VTS_PVARIANT VTS_PVARIANT VTS_PVARIANT VTS_PVARIANT VTS_PBOOL)
ON_EVENT_REFLECT(CMyHtmlEditCtrl, 252, _OnNavigateComplete2, VTS_DISPATCH VTS_PVARIANT)
END_EVENTSINK_MAP()
...
//declare class member for CMyDialog:
CMyHtmlEditCtrl browser;
...
CMyHtmlEditCtrl browser;
BOOL CMyDialog::OnInitDialog()
{
CDialogEx::OnInitDialog();
CWnd* edit = GetDlgItem(IDC_EDIT1);
CRect rc;
edit->GetWindowRect(&rc);
ScreenToClient(&rc);
browser.Create(0, WS_CHILD | WS_VISIBLE, rc, this, 301, 0);
CString html = LR"(<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN">
<html>
<head>
<style type="text/css">
body
{
border:1px solid gray;
}
</style>
</head>
<body>
<a href="#http://www.stackoverflow.com">http://www.stackoverflow.com</a>
</body>
</html>
)";
browser.SetDocumentHTML(html);
return TRUE;
}
Upvotes: 3