duilib : 内嵌网页加载完,设置焦点

想模拟鼠标点击网页空白处的效果。

前几天已经搞定的事,可是代码没有入库,连临时库都没入。又去忙别的, 结果今天想起来搞一下,已经完成的代码片段没了。。。

哭了...


有资料上写过思路,但是根据工程的情况,实现上会有所不同。 e.g.  我的当前工程是duilib + win32api.

虽然和前几天整出来的代码不同,但是效果比以前那个好(前几天的效果是用HtmlElement.Click实现的).


代码不及时入库,真杯具啊~


void CMainDlg::SetWebPageFocus(CWebBrowserUI * pWebPage)
{
    /// 设置网页为焦点, 使鼠标滚动时, 可以让网页元素进行滚动
    /// 相当于当网页打开后,先用鼠标在网页中空白处点击一下,再用鼠标滚轮滚动网页
    /// @ref http://www.cnblogs.com/baoconghui/archive/2012/09/08/2676935.html
    /// DuiLib的 CWebBrowserUI 稍有不同

    HRESULT             hr = S_FALSE;
    IWebBrowser2 *      pIWebBrowser2 = NULL;
    IDispatch *         pHtmlDocDisp = NULL;
    IHTMLDocument4 *    pHtmlDoc = NULL;

    if (NULL == pWebPage)
        goto END_SetWebPageFocus;

    // get web browser interface
    pIWebBrowser2 = pWebPage->GetWebBrowser2();
    if (NULL == pIWebBrowser2)
        goto END_SetWebPageFocus;

    // get the IDispatch interface of the document
    hr = pIWebBrowser2->get_Document(&pHtmlDocDisp);
    if (FAILED(hr) || (NULL == pHtmlDocDisp))
        goto END_SetWebPageFocus;

    // Query interface for IHTMLDocument4
    hr = pHtmlDocDisp->QueryInterface (IID_IHTMLDocument4, (void**)&pHtmlDoc);
    if (FAILED(hr) || (NULL == pHtmlDoc))
        goto END_SetWebPageFocus;

    pHtmlDoc->focus();

END_SetWebPageFocus:
    if (NULL != pHtmlDoc)
        pHtmlDoc->Release();

    if (NULL != pHtmlDocDisp)
        pHtmlDocDisp->Release();

    return;
}

<2014-0705>

前面的实现不好使了,  又整理了一个。 区别在于 x->focus();

void CMainDlg::SetWebPageFocus(CWebBrowserUI * pWebPage)
{
	/// 设置网页为焦点, 使鼠标滚动时, 可以让网页元素进行滚动
	/// 相当于当网页打开后,先用鼠标在网页中空白处点击一下,再用鼠标滚轮滚动网页
	/// @ref invalid http://www.cnblogs.com/baoconghui/archive/2012/09/08/2676935.html 
    /// @ref ok http://stackoverflow.com/questions/298932/set-focus-to-embedded-mshtml
	/// DuiLib的 CWebBrowserUI 稍有不同

	HRESULT             hr = S_FALSE;
	IWebBrowser2 *      pIWebBrowser2 = NULL;
	IDispatch *         pHtmlDocDisp = NULL;
	IHTMLDocument2 *    pHtmlDoc2 = NULL;
    IHTMLWindow2 *      pHtmlWindow2 = NULL;

	if (NULL == pWebPage)
		goto END_SetWebPageFocus;

	// get web browser interface
	pIWebBrowser2 = pWebPage->GetWebBrowser2();
	if (NULL == pIWebBrowser2)
		goto END_SetWebPageFocus;

	// get the IDispatch interface of the document
	hr = pIWebBrowser2->get_Document(&pHtmlDocDisp);
	if (FAILED(hr) || (NULL == pHtmlDocDisp))
		goto END_SetWebPageFocus;

	// Query interface for IHTMLDocument2
	hr = pHtmlDocDisp->QueryInterface (IID_IHTMLDocument2, (void**)&pHtmlDoc2);
	if (FAILED(hr) || (NULL == pHtmlDoc2))
		goto END_SetWebPageFocus;

	hr = pHtmlDoc2->get_parentWindow(&pHtmlWindow2);
    if (FAILED(hr) || (NULL == pHtmlWindow2))
        goto END_SetWebPageFocus;

    pHtmlWindow2->focus(); ///< !

END_SetWebPageFocus:
    if (NULL != pHtmlWindow2)
        pHtmlWindow2->Release();

	if (NULL != pHtmlDoc2)
		pHtmlDoc2->Release();

	if (NULL != pHtmlDocDisp)
		pHtmlDocDisp->Release();

    if (NULL != pIWebBrowser2)
        pIWebBrowser2->Release();

	return;
}

<2014-0707>

今天遇到反复切换2个url页面时报错的情况,最后发现,是不能释放得到的COM指针.

没有看到没释放会引起大幅度的内存增加.

void CMainDlg::SetWebPageFocusEx(CWebBrowserUI * pWebPage)
{
	/// 设置网页为焦点, 使鼠标滚动时, 可以让网页元素进行滚动
	/// 相当于当网页打开后,先用鼠标在网页中空白处点击一下,再用鼠标滚轮滚动网页
	/// @ref invalid http://www.cnblogs.com/baoconghui/archive/2012/09/08/2676935.html 
    /// @ref ok http://stackoverflow.com/questions/298932/set-focus-to-embedded-mshtml
	/// DuiLib的 CWebBrowserUI 稍有不同

	HRESULT             hr = S_FALSE;
	IWebBrowser2 *      pIWebBrowser2 = NULL;
	IDispatch *         pHtmlDocDisp = NULL;
	IHTMLDocument2 *    pHtmlDoc2 = NULL;
    IHTMLWindow2 *      pHtmlWindow2 = NULL;

	if (NULL == pWebPage)
		goto END_SetWebPageFocus;

	// get web browser interface
	pIWebBrowser2 = pWebPage->GetWebBrowser2();
	if (NULL == pIWebBrowser2)
		goto END_SetWebPageFocus;

	// get the IDispatch interface of the document
	hr = pIWebBrowser2->get_Document(&pHtmlDocDisp);
	if (FAILED(hr) || (NULL == pHtmlDocDisp))
		goto END_SetWebPageFocus;

	// Query interface for IHTMLDocument2
	hr = pHtmlDocDisp->QueryInterface (IID_IHTMLDocument2, (void**)&pHtmlDoc2);
	if (FAILED(hr) || (NULL == pHtmlDoc2))
		goto END_SetWebPageFocus;

	hr = pHtmlDoc2->get_parentWindow(&pHtmlWindow2);
    if (FAILED(hr) || (NULL == pHtmlWindow2))
        goto END_SetWebPageFocus;

    pHtmlWindow2->focus(); ///< !

END_SetWebPageFocus:
    /// 这里不能释放得到的COM指针,否则运行21~36次的时候报错.
	return;
}

<2015_0905>

将本博文加了一个Demo工程, 对应博文 :  duilib : (工程)内嵌网页加载完,设置焦点



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值