你好,欢迎来到电脑编程技巧与维护杂志社! 杂志社简介广告服务读者反馈编程社区  
合订本订阅
 
 
您的位置:杂志经典 / 编程语言
C++Builder下基于CppWebBrowser的“考试系统”设计与实现(四)
 

3.3  HTML试卷的加载

C++Builder中无法像C#一样通过直接修改CppWebBrowserDocument即可刷新页面显示,但可以通过流加载的方式进行刷新。流加载函数如下:

 

//从内存中刷新HTML文档

//CppWB:    TcppWebBrowser对象

//strList     要刷新的页面HTML源码

 

void  TMainForm::LoadStream(TCppWebBrowser *CppWB,TStringList * strList)

{

    IHTMLDocument2 *htm = NULL;

    //加载一个空的页面使得Document完成初始化

    CppWB->Navigate(L"about:blank");

if(CppWB->Document->QueryInterface(IID_IHTMLDocument2,(LPVOID*)&htm))

      {

        IPersistStreamInit *spPsi = NULL;

        if(htm->QueryInterface(IID_IPersistStreamInit, (LPVOID*)&spPsi) && spPsi)

          {

             TMemoryStream* mStream = new TMemoryStream();

             strList->SaveToStream(mStream);

             mStream->Seek(0,0);

             TStreamAdapter *sa = new TStreamAdapter(mStream,soReference);

             spPsi->Load(*sa);

             delete mStream;

          }

       }

 }

 

3.4  HTML试卷评判

对于用户在HTML表单中输入的内容,在评判时需根据对象的名称来获得该表单对象,并由得到的对象指针来访问用户输入的对象值,再将其与标准答案比较,判断正误。

下面即以表单中ID值为“1001001”的对象为例,说明程序中是如何获得表单对象输入值的。

_di_IDispatch disp;

System::DelphiInterface<IHTMLDocument2> htmldoc2;

System::DelphiInterface<IHTMLElement> htmlelem;

System::DelphiInterface<IHTMLElementCollection> htmlelemcoll;

 

disp = this->WebBrowser1->Document;

htmldoc2 = disp;

htmldoc2->get_all(&htmlelemcoll);

AnsiString elementID = "1001001";   //指定对象ID名称

//得到指定对象_di_IDispatch指针

htmlelemcoll->item(TVariant(elementID),TVariant(0),&disp);

//得到IHTMLElement对象

htmlelem = disp;

TVariant val1;

//获得指定对象的Value

htmlelem->getAttribute(WideString("Value"),0,&val1);

//获得用户输入字符串

AnsiString userinput = val1.bstrVal;

 

    在系统评判过程中,就是按照顺序依次访问每个IHTMLElement对象,获取对象的Value值,同对应标准答案比对,完成试卷的评判。

 

3.5  HTML页面自动填充的实现

“专业技术理论考试系统”设计为单机版,当在应用该系统进行个人练习时,有时需要在练习过程中快速的查询答案,为此,作者设计了HTML页面自动填充功能,从而实现辅助练习者记忆知识的目的。

HTML页面的自动填充,一般可以分为按选中表单对象单个填充和所有表单对象自动填充两种方法。

1)对选中对象的填充

_di_IDispatch disp;

System::DelphiInterface<IHTMLDocument2> htmldoc2;

System::DelphiInterface<IHTMLElement> htmlelem;

System::DelphiInterface<IHTMLElement> curelem;

System::DelphiInterface<IHTMLElementCollection> htmlelemcoll;

 

disp = this->WebBrowser1->Document;

htmldoc2 = disp;

htmldoc2->get_activeElement(&htmlelem);

BSTR tagID,tagName;

htmlelem->get_tagName(&tagName);

//判断当前选中的对象是否为可输入类型

if(AnsiString(tagName) == "INPUT") 

{

htmlelem->get_id(&tagID);

int tagIDNum = AnsiString(tagID).ToInt();  

//根据tagIDNum的编码规则得到该对象相应的标准答案

TVariant val1;

val1 = "标准答案";

//设定当前表单对象的值为“标准答案值”

htmlelem->setAttribute(WideString("Value"),val1,0);

}

2)对所有表单对象填充

//HTML试卷中对象的Name值必须按规则命名

for(int i=0;i<HTMLPaper.QuestionUnits->Count;i++)

   {

      TQuestionUnits * curUnits = (TQuestionUnits *)HTMLPaper.QuestionUnits->Items[i];

      for(int j=0;j<curUnits->Questions->Count;j++)

         {

           TQuestion * curQuestion = (TQuestion *)curUnits->Questions->Items[j];

           //填空题所有空遍历

           for(int k=0;k<curQuestion->SelectItems->Count;k++)

              {

                 AnsiString elementID = (AnsiString)((i+1)*100000+(j+1)*100+(k+1));//获得对象ID

                 htmlelemcoll->item(TVariant(elementID),TVariant(0),&disp);

                 htmlelem = disp;

                 TVariant val1;

                 val1 = curQuestion->SelectItems->Strings[k];//当前对象对应答案

                 htmlelem->setAttribute(WideString("Value"),val1,0);

               }

           }

}

  推荐精品文章

·2024年2月目录 
·2024年1月目录
·2023年12月目录
·2023年11月目录
·2023年10月目录
·2023年9月目录 
·2023年8月目录 
·2023年7月目录
·2023年6月目录 
·2023年5月目录
·2023年4月目录 
·2023年3月目录 
·2023年2月目录 
·2023年1月目录 

  联系方式
TEL:010-82561037
Fax: 010-82561614
QQ: 100164630
Mail:gaojian@comprg.com.cn

  友情链接
 
Copyright 2001-2010, www.comprg.com.cn, All Rights Reserved
京ICP备14022230号-1,电话/传真:010-82561037 82561614 ,Mail:gaojian@comprg.com.cn
地址:北京市海淀区远大路20号宝蓝大厦E座704,邮编:100089