Thursday, January 20, 2005

 

Some notes on IE ExecWB

The following collections are a little bit messy. However I have no time to rearrange them.

本文根据资料收集整理而成

首先是函数声明,参见
http://msdn.microsoft.com/library/default.asp?url=/workshop/browser/webbrowser/reference/ifaces/iwebbrowser2/execwb.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/vcrefCHtmlViewExecWB.asp
http://msdn.microsoft.com/library/default.asp?url=/workshop/browser/webbrowser/reference/methods/execwb.asp

命令ID列表在
http://msdn.microsoft.com/library/default.asp?url=/workshop/browser/mshtml/reference/commandids.asp

我们常用的命令有

1.实现clipboard use:

ExecWB(OLECMDID_CUT, OLECMDEXECOPT_DONTPROMPTUSER, NULL, NULL);

ExecWB(OLECMDID_COPY,OLECMDEXECOPT_DONTPROMPTUSER,NULL,NULL);

ExecWB(OLECMDID_PASTE,OLECMDEXECOPT_DONTPROMPTUSER,NULL,NULL);

ExecWB(OLECMDID_SELECTALL, OLECMDEXECOPT_DONTPROMPTUSER, NULL, NULL);

2.实现查找:

MSHTML::IHTMLDocument2Ptr pdoc;
_variant_t varinput,varoutput;
IOleCommandTarget *pCmd;
HRESULT hr;
pdoc=GetHtmlDocument();
hr=pdoc.QueryInterface(IID_IOleCommandTarget,&pCmd);
if(SUCCEEDED(hr))
{
varinput=_T("");
hr=pCmd->Exec(&CGID_MSHTML,IDM_FIND,OLECMDEXECOPT_DODEFAULT,&varinput,&varoutput);
}

详见
http://www.codeproject.com/tips/mshtmlfind.asp

3.
How to Print Preview
by Frank W. Wu
from http://www.codeproject.com/miscctrl/wbp.asp

Add print preview functionality
In the print preview method, I start by verifying the WebBrowser control is valid. Then, I retrieve a pointer to the IDispatch interface pointer to the HTMLDocument, from which I query the IOleCommandTarget interface, and store the pointer in lpTarget. You can find information on the IOleCommandTarget interface in MSDN. I won't repeat the instructions here. Finally, I execute the print preview command by calling IOleCommandTarget::Exec with the appropriate parameters. The command id OLECMDID_PRINTPREVIEW is defined in "docobj.h". The GUI of the print preview is handled by the WebBrowser control.

Following is the source code of the method:

void CWBPDlg::OnDemoPrintpreview()
{
// Verify the WebBrowser control is valid.
LPDISPATCH lpDispApp = m_wndBrowser.GetApplication();
if(lpDispApp)
{
// Get the HTMLDocument interface.
LPDISPATCH lpDispDoc = m_wndBrowser.GetDocument();
if (lpDispDoc != NULL)
{
// Get the IOleCommandTarget interface so that
// we can dispatch the command.
LPOLECOMMANDTARGET lpTarget = NULL;
if (SUCCEEDED(lpDispDoc->
QueryInterface(IID_IOleCommandTarget,
(LPVOID*) &lpTarget)))
{
// Execute the print preview command. The
// control will handle the print preview
// GUI.
// OLECMDID_PRINTPREVIEW is defined in
// "docobj.h".
lpTarget->Exec(NULL,
OLECMDID_PRINTPREVIEW, 0, NULL, NULL);
lpTarget->Release();
}
lpDispDoc->Release();
}
lpDispApp->Release();
}
}

Now you can compile and run the application. The GUI of print preview is the same as that you have seen in IE.

Use WebBrowser::ExecWB method - Alexander Tsarfin's contribution
The WebBrowser class implements a wrapper that allows you to execute a command on an OLE object using the IOleCommandTarget::Exec method.

void CWBPDlg::OnDemoPrintpreview()
{
m_wndBrowser.ExecWB(OLECMDID_PRINTPREVIEW,
OLECMDEXECOPT_PROMPTUSER, NULL, NULL);
}

4.实现显示源程序:

MSHTML::IHTMLDocument2Ptr pdoc;
_variant_t varinput,varoutput;
IOleCommandTarget *pCmd;
HRESULT hr;
pdoc=GetHtmlDocument();
hr=pdoc.QueryInterface(IID_IOleCommandTarget,&pCmd);
if(SUCCEEDED(hr))
{
varinput=_T("");
hr=pCmd->Exec(&CGID_MSHTML,IDM_VIEWSOURCE,OLECMDEXECOPT_DODEFAULT,&varinput,&varoutput);
}

5.实现显示Internet选项:

MSHTML::IHTMLDocument2Ptr pdoc;
_variant_t varinput,varoutput;
IOleCommandTarget *pCmd;
HRESULT hr;
pdoc=GetHtmlDocument();
hr=pdoc.QueryInterface(IID_IOleCommandTarget,&pCmd);
if(SUCCEEDED(hr))
{
varinput=_T("");
hr=pCmd->Exec(&CGID_MSHTML,IDM_OPTIONS,OLECMDEXECOPT_DODEFAULT,&varinput,&varoutput);
}

6.
JAVAScript的调用方式
from http://www.csdn.net/develop/article/16/16938.shtm

我对IEWebBrowser这个组件的execWB方法整理了一下﹐希望对大家有所帮助。
1. object id="WebBrowser" width=0 height=0 classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2">/object>
2. 调用方法。
WebBrowser.ExecWB nCmdID, nCmdExecOpt, [pvaIn], [pvaOut]
3. 参数说明。
(a).nCmdID
OLECMDID_OPEN = 1,
OLECMDID_NEW = 2,
OLECMDID_SAVE = 3,
OLECMDID_SAVEAS = 4,
OLECMDID_SAVECOPYAS = 5,
OLECMDID_PRINT = 6,
OLECMDID_PRINTPREVIEW = 7,
OLECMDID_PAGESETUP = 8,
OLECMDID_SPELL = 9,
OLECMDID_PROPERTIES = 10,
OLECMDID_CUT = 11,
OLECMDID_COPY = 12,
OLECMDID_PASTE = 13,
OLECMDID_PASTESPECIAL = 14,
OLECMDID_UNDO = 15,
OLECMDID_REDO = 16,
OLECMDID_SELECTALL = 17,
OLECMDID_CLEARSELECTION = 18,
OLECMDID_ZOOM = 19,
OLECMDID_GETZOOMRANGE = 20
OLECMDID_UPDATECOMMANDS = 21
OLECMDID_REFRESH = 22
OLECMDID_STOP = 23
OLECMDID_HIDETOOLBARS = 24
OLECMDID_SETPROGRESSMAX = 25
OLECMDID_SETPROGRESSPOS = 26
OLECMDID_SETPROGRESSTEXT = 27
OLECMDID_SETTITLE = 28
OLECMDID_SETDOWNLOADSTATE = 29
OLECMDID_STOPDOWNLOAD = 30
上面的关键词都可以在浏览器的菜单里面找到对应的选项﹐大家一看就明白的﹗
(b).nCmdExecOpt
OLECMDEXECOPT_DODEFAULT = 0,
OLECMDEXECOPT_PROMPTUSER = 1,
LECMDEXECOPT_DONTPROMPTUSER = 2,
OLECMDEXECOPT_SHOWHELP = 3
对于这个参数﹐一般来说﹐选1就可以了。

这是调用IE的”另存为”功能的示例﹕

object id="WebBrowser" width=0 height=0 classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2">/object>
A href="javascript:WebBrowser.ExecWB(4,1);">Save-存储/A>

7.
另一种Javascript调用方式
from http://www.csdn.net/develop/Read_Article.asp?Id=14981

在不是js打开的页面上按window.close(),会有提示框,很烦,现在可以不用了,没有提示框直接关闭窗口。

试试下面代码:

object id="WebBrowser" width=0 height=0 classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2">
/object>
input type="button" name="Button" value="关闭窗口" onClick="document.all.WebBrowser.ExecWB(45,1)">

试着改变参数会得到其他一些功能:

WebBrowser.ExecWB(1,1) 打开
WebBrowser.ExecWB(2,1) 关闭现在所有的IE窗口,并打开一个新窗口
WebBrowser.ExecWB(4,1) 保存网页
WebBrowser.ExecWB(6,1) 打印
WebBrowser.ExecWB(6,6) 无选择打印机对话框直接打印
WebBrowser.ExecWB(7,1) 打印预览
WebBrowser.ExecWB(8,1) 打印页面设置
WebBrowser.ExecWB(10,1) 查看页面属性
WebBrowser.ExecWB(15,1) 好像是撤销,有待确认
WebBrowser.ExecWB(17,1) 全选
WebBrowser.ExecWB(22,1) 刷新
WebBrowser.ExecWB(45,1) 关闭窗体无提示

8.
改变字体

this article was contributed by sriram.

a sample code to change the fonts in your web browser using webbrowser control.

void cmybrowser::setfont(int nfontsize)
{ m_pdisp = null; // idispatch pointer
lpolecommandtarget pcmdtarg = null;

m_pdisp = m_webbrowser2.getdocument(); // get the idispatch pointer from webbrowser control
assert(m_pdisp); //check for validity

m_pdisp->queryinterface(iid_iolecommandtarget, (lpvoid*)&pcmdtarg); //query for iid_iolecommandtarget interface
assert(pcmdtarg); // check for validity

variant vafontsize; // input argumentsvariantinit(&vafontsize);
v_vt(&vafontsize) = vt_i4;
v_i4(&vafontsize) = nfontsize; //size of the font you want
variant vafontoutput;

pcmdtarg->exec(null, // execute just change the font
olecmdid_zoom,
olecmdexecopt_promptuser,
&vafontsize,
&vafontoutput);

variantclear(&vafontsize); //clear the variant variable before freeing its memory

if (pcmdtarg) pcmdtarg->release(); // release document's command target
if (m_pdisp) m_pdisp->release(); // release document's dispatch interface
}



<< Home

This page is powered by Blogger. Isn't yours?