zoukankan      html  css  js  c++  java
  • MFC调用WebBrowser控件直接编写javascript代码及其他WebBrowser相关技巧

    在mfc操作网页的过程中,使用ActiveX控件中的WebBrowser能方便的模拟网页点击等操作

    头文件

    #include <MsHTML.h>    //WEB控件包含头文件

    有时需要直接编写javascript的代码来操作指定网页,可通过以下代码进行操作

    CComVariant vtUrl(L"http://admin:123@172.31.102.12/cgi-bin/va100p.cgi");//指定需要打开的网页
    CComVariant vtEmpty; 
    myva100pwebbrower.Navigate2(&vtUrl, &vtEmpty, &vtEmpty, &vtEmpty, &vtEmpty);//打开指定网页
    上下两部分要分开写,如果在网页还没打开的时候就进行下面的操作会直接报错
    IHTMLDocument2* pHTMLDoc = (IHTMLDocument2*)myva100pwebbrower.get_Document();    
    if ( pHTMLDoc != NULL ) 
    {    
    	IHTMLWindow2* pHTMLWnd;    
    	pHTMLDoc->get_parentWindow( &pHTMLWnd );    
    	if ( pHTMLWnd != NULL )    
    	{  
    		CString js_code;  
    		js_code=L"javascript:document.getElementById('100').value = '11233'; void(0);";//此次为实际写入的javascript代码
    		//js_code.Format(L"goToLocation('%s');",JScode);//JS函数名+参数  
    		VARIANT  ret;    
    		ret.vt = VT_EMPTY;  
    		pHTMLWnd->execScript(js_code.AllocSysString(), L"Javascript", &ret);//执行javascript代码
    	}  
    } 


    在大多数情况下,可以利用控件自带的函数进行网页元素的操作

    CComQIPtr<IHTMLInputElement> spInputElement;
    CComQIPtr<IHTMLDocument3> spDocument = myva100pwebbrower.get_Document();
    CComPtr<IHTMLElement> spElement;
    spDocument->getElementById(L"100", &spElement);//通过ID获得元素对象存在spElement中,这里的ID是100,接下来就可以对该对象进行操作
    if(spElement==NULL)//读入ID之后可以先判断下该ID是否存在
    {
    	MessageBox(L"相应ID不存在");
    	return;
    }
    //如果对象是下拉列表框,则经由下面代码进行选择
    CComVariant vtUrl(L"1-door");
    spElement->setAttribute(L"value",vtUrl); 
    spElement.Release();//操作后要设释放掉元素对象  
    //如果对象是按钮,可用下面方法进行模拟点击
    spElement->click();
    spElement.Release();
    //如果对象是文本框,可用下面方法进行写入
    spInputElement = spElement;
    spInputElement->put_value(myinputbstr);
    spElement.Release();
    //如果对象有value,可用下面方法获得
    BSTR BSvalue;
    spInputElement = spElement;  
    spInputElement->get_value(&BSvalue);//得到对象value放到BSvalue中


    获得网页复选框对象的选择情况

    bool getidcheck(CString myid, CExplorer1 *myweb)//myid为网页控件id,myweb为WebBrowser对象
    {
    	CComQIPtr<IHTMLInputElement> spInputElement;
    	CComQIPtr<IHTMLDocument3> spDocument = myweb->get_Document();
    	CComPtr<IHTMLElement> spElement;
    	spDocument->getElementById(myid.AllocSysString(), &spElement);
    	if (spElement == NULL)
    	{
    		return false;
    	}
    	bool getcheckvalue=false;
    	spInputElement = spElement;
    	VARIANT_BOOL vbgetcheckvalue = 0;
    	spInputElement->get_checked(&vbgetcheckvalue);//获得复选框选择情况放到vbgetcheckvalue中
    	if (vbgetcheckvalue==0)
    	{
    		getcheckvalue = false;
    	}
    	else
    	{
    		getcheckvalue = true;
    	}
    	spElement.Release();
    	return getcheckvalue;
    }

    获得当前的URL(网址)

    CString myweburl= myva100pwebbrower.get_LocationURL();//myva100pwebbrower为WebBrowser对象,myweburl存放着网址

    如果不希望在程序运行时看到插件,可将插件进行隐藏,但可能是的一个bug,一旦在插件的属性中选择隐藏,它就干脆不起作用,这时可以使用下面这句代码进行操作,代码在窗体初始化的时候调用

    ::ShowWindow(myva100pwebbrower.GetSafeHwnd(), SW_HIDE);//其中myva100pwebbrower为WebBrowser对象

    CString NetWorkDlg::getvalue(CString myid)//获得对象value值
    {
    	CComQIPtr<IHTMLInputElement> spInputElement;
    	CComQIPtr<IHTMLDocument3> spDocument = networkweb.get_Document();
    	CComPtr<IHTMLElement> spElement;
    	spDocument->getElementById(myid.AllocSysString(), &spElement);
    	if (spElement == NULL)//读入ID之后可以先判断下该ID是否存在  
    	{
    		MessageBox(L"相应ID不存在");
    		return L"error";
    	}
    	VARIANT myvariant;
    	spElement->getAttribute(L"value", 1, &myvariant);
    	spElement.Release();//操作后要设释放掉元素对象  
    	CString myvalue = myvariant;
    	return myvalue;
    }
    void NetWorkDlg::setvalue(CString myid,CString myinput)//设置对象值
    {
    	CComQIPtr<IHTMLInputElement> spInputElement;
    	CComQIPtr<IHTMLDocument3> spDocument = networkweb.get_Document();
    	CComPtr<IHTMLElement> spElement;
    	spDocument->getElementById(myid.AllocSysString(), &spElement);
    	if (spElement == NULL)//读入ID之后可以先判断下该ID是否存在  
    	{
    		MessageBox(L"相应ID不存在");
    		return;
    	}
    	CComVariant vtUrl;
    	vtUrl = myinput;
    	spElement->setAttribute(L"value", vtUrl);
    	spElement.Release();//操作后要设释放掉元素对象
    }














  • 相关阅读:
    CodeForces 681D Gifts by the List (树上DFS)
    UVa 12342 Tax Calculator (水题,纳税)
    CodeForces 681C Heap Operations (模拟题,优先队列)
    CodeForces 682C Alyona and the Tree (树上DFS)
    CodeForces 682B Alyona and Mex (题意水题)
    CodeForces 682A Alyona and Numbers (水题,数学)
    Virtualizing memory type
    页面跳转
    PHP Misc. 函数
    PHP 5 Math 函数
  • 原文地址:https://www.cnblogs.com/weixinhum/p/3916708.html
Copyright © 2011-2022 走看看