void CHtmlradioselView::submit()
{
LPDISPATCH lpdisp=this->GetHtmlDocument();
IHTMLDocument2* spHtmlDocument=NULL;
HRESULT hr=lpdisp->QueryInterface(IID_IHTMLDocument2,(void**)&spHtmlDocument); IHTMLElementCollection* pColl=NULL;
hr=spHtmlDocument->get_all(&pColl);
if(pColl!=NULL&&SUCCEEDED(hr))
{
LPDISPATCH pdisp=NULL;
IHTMLInputElement* pelem=NULL;
long num=0;
pColl->get_length(&num);
for(int i=0;i<num;i++)
{
hr=pColl->item(COleVariant((short)i),COleVariant((short)i),&pdisp);
if(SUCCEEDED(hr)&&pdisp!=NULL)
{
hr=pdisp->QueryInterface(IID_IHTMLInputElement,(void**)&pelem);
if(SUCCEEDED(hr)&&pelem!=NULL)
{
bool bfind=false;
BSTR bstrtype;
pelem->get_type(&bstrtype);
if(wcsicmp(bstrtype,L"submit")==0)
{
IHTMLElement* petmp =NULL;
pelem->QueryInterface(IID_IHTMLElement,(void**)&petmp);
petmp->click();
petmp->Release();
bfind=true;
}
SysFreeString(bstrtype);
pelem->Release();
if(bfind)
break;
}
pdisp->Release();
}
}
pColl->Release();
}
if(spHtmlDocument!=NULL)
spHtmlDocument->Release();
if(lpdisp!=NULL)
lpdisp->Release(); }
{
LPDISPATCH lpdisp=this->GetHtmlDocument();
IHTMLDocument2* spHtmlDocument=NULL;
HRESULT hr=lpdisp->QueryInterface(IID_IHTMLDocument2,(void**)&spHtmlDocument); IHTMLElementCollection* pColl=NULL;
hr=spHtmlDocument->get_all(&pColl);
if(pColl!=NULL&&SUCCEEDED(hr))
{
LPDISPATCH pdisp=NULL;
IHTMLInputElement* pelem=NULL;
long num=0;
pColl->get_length(&num);
for(int i=0;i<num;i++)
{
hr=pColl->item(COleVariant((short)i),COleVariant((short)i),&pdisp);
if(SUCCEEDED(hr)&&pdisp!=NULL)
{
hr=pdisp->QueryInterface(IID_IHTMLInputElement,(void**)&pelem);
if(SUCCEEDED(hr)&&pelem!=NULL)
{
bool bfind=false;
BSTR bstrtype;
pelem->get_type(&bstrtype);
if(wcsicmp(bstrtype,L"submit")==0)
{
IHTMLElement* petmp =NULL;
pelem->QueryInterface(IID_IHTMLElement,(void**)&petmp);
petmp->click();
petmp->Release();
bfind=true;
}
SysFreeString(bstrtype);
pelem->Release();
if(bfind)
break;
}
pdisp->Release();
}
}
pColl->Release();
}
if(spHtmlDocument!=NULL)
spHtmlDocument->Release();
if(lpdisp!=NULL)
lpdisp->Release(); }
我把你的代码COPY过来后,报错。
error C2065: 'IHTMLInputElement' : undeclared identifier
Note, new header file and lib should be put before old ones.
VC menu
Tool->Options
Directory tab
configure the "Include files" settings
make sure ..\ie55_lib\include is put in the head of the list(otherwise you need to move it up manually)
do the same thing for "library files"
如果需要单独安装那里有下载。
我现在用的是IE6。
jiangsheng
有好多啊,到底是哪一个?
你需要学习一些DHTML的知识,MSDN里是用VBScript描述的Document.form1.button.OnClick就是click button了至于捕获事件需要使用Sink或者ActiveX组件连接
Sink的好处是不需要额外的DLL组件,但作起来复杂
下面spDoc是GetDocument得到的IHTMLDocument2
在OnDocumentCompleteWb(LPDISPATCH pDisp, VARIANT FAR* URL) 里面: try{
IUnknown *pUnk;
HRESULT hr = spDoc->QueryInterface(__uuidof(IConnectionPointContainer),
(void **)&pCPC);
if(FAILED(hr)) return;
hr = pCPC->FindConnectionPoint(__uuidof(HTMLDocumentEvents), &pCP);
pCPC->Release();
if (FAILED(hr)) return;
if(!pDocEvent) {
pDocEvent=new IHTMLDocCon();
pDocEvent->Init(GetSafeHwnd(), WM_USER_WB);
}
hr = pDocEvent->QueryInterface(__uuidof(IUnknown), (void **) &pUnk);
if (FAILED(hr)) return;
hr = pCP->Advise(pUnk, &dwConnEvt);
pCP->Release();
pDocEvent->Release();
if (FAILED(hr)) return;
}catch(...){
}pDocEvent就是你作的sink,基类是HTMLDocumentEvents,主要需要实现Invoke方法:
switch(dispIdMember){
case DISPID_HTMLDOCUMENTEVENTS_ONCLICK:
vRet=onclick();
vRet.ChangeType(VT_BOOL);
break;
.............
这里就是实现的捕获点击事件,用个方法,比如sendmessage传回主windows,然后:得到事件类型和事件源:
IHTMLWindow2Ptr curWin=spDoc->GetparentWindow();
IHTMLEventObjPtr event=curWin->Getevent();
IHTMLElementPtr ele=event->GetsrcElement();
然后你就处理吧。
另外一种方法比较简单,是做个activex dll,用向导就成,做个class, 有个默认的方法(ID=0)处理事件,然后在VC里导入(也是#import),实例化一个对象,跟document.form1.button1.Onclick之类的连接到一起, 当然在VC里不是这样写了,要从IHTMLDocument2得到各个IHTMLElement...具体怎么写其实不用查资料,因为VC有自动完成,打个->就能自动列出来那些方法,一琢磨就明白了。MSDN里基本使用VBscript描述的许多东西,不会dhtml是不成的。
你说的方法要安装SDK吗?
我试了,好像不行。
我是指在STDAFX.H中增加你说的那句语句的。
导入MSHTML就能导入足够的头文件了。
加上那句编译没问题吧?我可是从以前的工程里copy的但有些常量还是需要装sdk的,需要那些头文件。
我说的就是编译过不去。
可能是什么原因?
microsoft SDK
http://www.microsoft.com/msdownload/platformsdk/sdkupdate/
IE55 sdk
http://msdn.microsoft.com/downloads/samples/internet/libraries/ie55_lib/
rename("UINT_PTR","wbUINT_PTR") 为了去掉警告错误,可以这样:
#pragma warning( push )
#pragma warning(disable : 4099 4192 4146)
#import "c:\windows\system\mshtml.tlb" no_namespace rename("UINT_PTR","wbUINT_PTR") \
rename("IHTMLControlElement","wbIHTMLControlElement")
#pragma warning( pop )
和没有加一样。
如果是w2000或NT,在系统的system32下有该文件,
直接引入mshtml.tlb也一样。
是直接引入mshtml.dll也一样
virtual void OnBeforeNavigate2(LPCTSTR lpszURL, DWORD nFlags,
LPCTSTR lpszTargetFrameName, CByteArray& baPostedData,LPCTSTR lpszHeaders, BOOL* pbCancel);
按钮所指的URL就放在lpszURL中,根据它你就可以知道是哪个按钮了。