CB6中使用浏览器控件的一些操作

类别:编程语言 点击:0 评论:0 推荐:

浏览器的表现形式丰富,适度的使用能使界面增色不少,介绍一下CB6中TCppWebBrowser控件的一些用法
分开说比较麻烦,从以前的工程里找了一个完整网页贴过来

网页:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>免费送歌</title>
<style type="text/css">
input {border:1px solid #000000}
<!--
-->
</style>
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-->
</script>
<style type="text/css">
<!--
td {
 font-family: "Arial";
 font-size: 12px;
 color: #990000;
 cursor: default;
}
.jiben {
 font-family: "Arial";
 font-size: 12px;
 font-weight: bold;
 color: #990000;
 cursor: default;
}
-->
</style>
</head>

<body leftmargin="0" topmargin="0" onLoad="MM_preloadImages('image/K-17a.jpg','image/K-11a.jpg')">
<table width="315" height="144" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td height="22"><table width="314" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td width="6"><img src="image/F-1.jpg" width="4" height="25"></td>
          <td> <div align="left">
              <table width="310" border="0" align="right" cellpadding="0" cellspacing="0" background="image/14A.jpg" id="moveform">
                <tr>
                  <td valign="middle" 4>8751免费点歌登陆</td>
                  <td width="195"><div align="right">
                      <table width="64" border="0" cellspacing="0" cellpadding="0">
                        <tr>
                          <td width="29%"><div align="right"><img src="image/A1.jpg" alt="最小化" name="minform" width="20" height="25" id="Image1" onMouseOver="MM_swapImage('Image1','','image/1A.jpg',1)" onMouseOut="MM_swapImgRestore()"></div></td>
                          <td width="32%"><div align="right"><img src="image/A2.jpg" width="20" height="25"></div></td>
                          <td width="27%"><div align="right"><img src="image/A3.jpg" alt="关闭" name="closeform" width="19" height="25" id="Image2" onMouseOver="MM_swapImage('Image2','','image/3A.jpg',1)" onMouseOut="MM_swapImgRestore()"></div></td>
                          <td width="12%"><div align="right"></div></td>
                        </tr>
                      </table>
                    </div></td>
                </tr>
              </table>
            </div></td>
        </tr>
      </table></td>
  </tr>
  <tr>
    <td valign="top"><table width="315" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td width="10"><img src="image/L-1.jpg" width="4" height="198"></td>
          <td width="260" valign="top"><table width="304" border="0" cellpadding="0" cellspacing="0" background="image/K-9.jpg">
              <tr>
                <td colspan="4"><table width="305" height="141" border="0" cellpadding="0" cellspacing="0">
                    <tr>
                      <td height="141" valign="top"> <table width="201" height="132" border="0" align="center" cellpadding="0" cellspacing="0">
                          <tr>
                            <td height="16" colspan="2">&nbsp;</td>
                          </tr>
                          <tr>
                            <td colspan="2" ><div align="center" class="jiben">基
                                本 信 息</div></td>
                          </tr>
                          <tr >
                            <td width="81">手机号码:</td>
                            <td width="120"> <input name="MbNum" type="text" id="MbNum" size="15"></td>
                          </tr>
                          <tr >
                            <td>姓名:</td>
                            <td> <input name="Name" type="text" id="Name" size="15"></td>
                          </tr>
                          <tr >
                            <td>8751ID:</td>
                            <td> <input name="ID" type="text" id="ID" size="15"></td>
                          </tr>
                          <tr >
                            <td>备注信息:</td>
                            <td> <input name="MemTextArea" type="text" id="MemTextArea" size="15"></td>
                          </tr>
                        </table></td>
                    </tr>
                  </table></td>
              </tr>
              <tr>
                <td width="83" height="38">&nbsp;</td>
                <td width="84"><a href="#"><img src="image/K-17.jpg" name="Ok" width="55" height="19" border="0" id="Ok" onMouseOver="MM_swapImage('Ok','','image/K-17a.jpg',1)" onMouseOut="MM_swapImgRestore()"></a></td>
                <td width="79"> <div align="center"><a href="#"><img src="image/K-11.jpg" name="Cancel" width="55" height="19" border="0" id="Cancel" onMouseOver="MM_swapImage('Cancel','','image/K-11a.jpg',1)" onMouseOut="MM_swapImgRestore()"></a></div></td>
                <td width="59"> <div align="left"></div></td>
              </tr>
              <tr>
                <td colspan="4">&nbsp;</td>
              </tr>
              <tr>
                <td height="4" colspan="4"><img src="image/K-8.jpg" width="306" height="4"></td>
              </tr>
            </table></td>
          <td width="45"><img src="image/K-7.jpg" width="4" height="198"></td>
        </tr>
      </table></td>
  </tr>
</table>
<div align="right"></div>
</body>
</html>

.h
#ifndef AddFriendFormH
#define AddFriendFormH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include "SHDocVw_OCX.h"
#include <OleCtrls.hpp>
//---------------------------------------------------------------------------
class TfrmAddFriend : public TForm
{
__published: // IDE-managed Components
        TCppWebBrowser *CppWebBrowserAddFriend;
        void __fastcall FormCreate(TObject *Sender);
        void __fastcall CppWebBrowserAddFriendDocumentComplete(TObject *Sender,
          LPDISPATCH pDisp, Variant *URL);
        void __fastcall FormDestroy(TObject *Sender);
private:
        void MoveFormEvt(DISPID id, VARIANT* pVarResult);
        void MoveForm(void);
        void MaskEvt(DISPID id, VARIANT* pVarResult);
        void SetBrowserStyle(void);
        void CancelEvt(void);
        void ReleaseHook(void);
        void AddFriendOnClick(DISPID id, VARIANT* pVarResult);
        AnsiString GetInputValue(AnsiString itemname);
        void OnKeyPressEvt(DISPID id, VARIANT* pVarResult);
        void SetFocusInput(AnsiString itemname);
        void PutInputValue(AnsiString itemname, AnsiString value); // User declarations
public:  // User declarations
        AnsiString MbNum;
        AnsiString FriendName;
        AnsiString ID8751;
        AnsiString Info;
        __fastcall TfrmAddFriend(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TfrmAddFriend *frmAddFriend;
//---------------------------------------------------------------------------
#endif

.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "AddFriendForm.h"
#include "GlobalFunc.h"
#include "GlobalVar.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "SHDocVw_OCX"
#pragma resource "*.dfm"
TfrmAddFriend *frmAddFriend;
//---------------------------------------------------------------------------
__fastcall TfrmAddFriend::TfrmAddFriend(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TfrmAddFriend::FormCreate(TObject *Sender)
{
        this->CppWebBrowserAddFriend->HandleNeeded();
        this->CppWebBrowserAddFriend->Navigate(WideString(GetResPath("AddFriends.htm")),NULL,NULL,NULL,NULL);

        CutFormRgn(Handle,316,225);
}
//---------------------------------------------------------------------------
void TfrmAddFriend::MoveFormEvt(DISPID id, VARIANT* pVarResult) 
{
        //TODO: Add your source code here
        _di_IDispatch disp;
        System::DelphiInterface<IHTMLDocument2> htmldoc2;
        System::DelphiInterface<IHTMLWindow2> htmlwnd2;
        System::DelphiInterface<IHTMLEventObj> htmlevt;
        System::DelphiInterface<IHTMLElement> htmlelem;

        disp = this->CppWebBrowserAddFriend->Document;
        if(!disp)
                return;

        htmldoc2 = disp;
        htmldoc2->get_parentWindow(&htmlwnd2);
        htmlwnd2->get_event(&htmlevt);

        htmlevt->get_srcElement(&htmlelem);

        WideString _tag;

        htmlelem->get_tagName(&_tag);

        long btn,_x,_y;
        htmlevt->get_button(&btn);
        htmlevt->get_screenX(&_x);
        htmlevt->get_screenY(&_y);

        if(_tag == WideString("TD") && btn == 1)
        {
                ::ReleaseCapture();
                ::SendMessage(Handle,WM_NCLBUTTONDOWN,HTCAPTION,MAKELPARAM(_x,_y));
        }
        pVarResult->vt = VT_BOOL;
        pVarResult->boolVal = FALSE;
}

void TfrmAddFriend::MoveForm(void)
{
        //TODO: Add your source code here
        _di_IDispatch disp;
        System::DelphiInterface<IHTMLDocument2> htmldoc2;
        System::DelphiInterface<IHTMLElementCollection> htmlelemcoll;
        System::DelphiInterface<IHTMLElement> htmlelem;

        disp = this->CppWebBrowserAddFriend->Document;
        if(!disp)
                return;

        htmldoc2 = disp;
        htmldoc2->get_all(&htmlelemcoll);
        htmlelemcoll->item(TVariant("moveform"),TVariant(0),&disp);
        htmlelem = disp;

        TVariant OnfrmMosuseDown;
        OnfrmMosuseDown = CFunctionObject<TfrmAddFriend>::CreateEventFunctionObject(this,&TfrmAddFriend::MoveFormEvt,0);
        htmlelem->put_onmousedown(OnfrmMosuseDown);
}

void TfrmAddFriend::MaskEvt(DISPID id, VARIANT* pVarResult)
{
        //TODO: Add your source code here
        _di_IDispatch disp;
        System::DelphiInterface<IHTMLDocument2> htmldoc2;
        System::DelphiInterface<IHTMLWindow2> htmlwnd2;
        System::DelphiInterface<IHTMLEventObj> htmlevt;
        System::DelphiInterface<IHTMLElement> htmlelem;

        disp = this->CppWebBrowserAddFriend->Document;
       
        if(!disp)
                return;

        htmldoc2 = disp;
        htmldoc2->get_parentWindow(&htmlwnd2);
        htmlwnd2->get_event(&htmlevt);

        htmlevt->get_srcElement(&htmlelem);

        WideString _tag;

        htmlelem->get_tagName(&_tag);
        if(_tag == WideString("INPUT"))
                return;

        pVarResult->vt = VT_BOOL;
        pVarResult->boolVal = FALSE;
}

void TfrmAddFriend::CancelEvt(void)
{
        //TODO: Add your source code here
        _di_IDispatch disp;
        System::DelphiInterface<IHTMLDocument2> htmldoc2;
        System::DelphiInterface<IHTMLElement> htmlelem;
        System::DelphiInterface<IHTMLElement2> htmlelem2;

        disp = this->CppWebBrowserAddFriend->Document;
        if(!disp)
                return;

        htmldoc2 = disp;
        htmldoc2->get_body(&htmlelem);
        htmlelem2 = htmlelem;

        TVariant vt;
        vt = CFunctionObject<TfrmAddFriend>::CreateEventFunctionObject(this,&TfrmAddFriend::MaskEvt,0);
        htmlelem->put_onselectstart(vt);
        htmlelem2->put_oncontextmenu(vt);
}

void TfrmAddFriend::SetBrowserStyle(void)
{
        //TODO: Add your source code here
        _di_IDispatch disp;
        System::DelphiInterface<IHTMLDocument2> htmldoc2;
        System::DelphiInterface<IHTMLElement> htmlelem;
        System::DelphiInterface<IHTMLBodyElement> htmlbody;
        System::DelphiInterface<IHTMLStyle> htmlsty;

        disp = this->CppWebBrowserAddFriend->Document;
        if(!disp)
                return;

        htmldoc2 = disp;
        htmldoc2->get_body(&htmlelem);
        htmlbody = htmlelem;
        htmlbody->put_scroll(WideString("no"));
        htmlelem->get_style(&htmlsty);
        htmlsty->put_border(WideString("none"));
}

void TfrmAddFriend::ReleaseHook(void)
{
        //TODO: Add your source code here
        _di_IDispatch disp;
        System::DelphiInterface<IHTMLDocument2> htmldoc2;
        System::DelphiInterface<IHTMLElementCollection> htmlelemcoll;
        System::DelphiInterface<IHTMLElement> htmlelem;
        System::DelphiInterface<IHTMLElement2> htmlelem2;

        disp = this->CppWebBrowserAddFriend->Document;
        if(!disp)
                return;

        htmldoc2 = disp;
        htmldoc2->get_all(&htmlelemcoll);
        htmlelemcoll->item(TVariant("moveform"),TVariant(0),&disp);

        htmlelem = disp;

        VARIANT varNull;
        varNull.vt = VT_NULL;
        htmlelem->put_onmousedown(varNull);

        htmldoc2->get_body(&htmlelem);
        htmlelem->put_onselectstart(varNull);
        htmlelem->put_onclick(varNull);
        htmlelem->put_onkeypress(varNull);

        htmlelem2 = htmlelem;
        htmlelem2->put_oncontextmenu(varNull);
}

AnsiString TfrmAddFriend::GetInputValue(AnsiString itemname)
{
        //TODO: Add your source code here
        AnsiString rtn = "";

        _di_IDispatch disp;
        System::DelphiInterface<IHTMLDocument2> htmldoc2;
        System::DelphiInterface<IHTMLElement> htmlelem;
        System::DelphiInterface<IHTMLElementCollection> htmlelemcoll;

        disp = this->CppWebBrowserAddFriend->Document;
        if(!disp)
                return rtn;

        htmldoc2 = disp;
        htmldoc2->get_all(&htmlelemcoll);
        htmlelemcoll->item(TVariant(itemname),TVariant(0),&disp);
        htmlelem = disp;
        TVariant val;
        htmlelem->getAttribute(WideString("value"),0,&val);
        rtn = val.bstrVal;

        return rtn;
}

void TfrmAddFriend::PutInputValue(AnsiString itemname, AnsiString value)
{
        //TODO: Add your source code here
        _di_IDispatch disp;
        System::DelphiInterface<IHTMLDocument2> htmldoc2;
        System::DelphiInterface<IHTMLElement> htmlelem;
        System::DelphiInterface<IHTMLElementCollection> htmlelemcoll;

        disp = this->CppWebBrowserAddFriend->Document;
        if(!disp)
                return;

        htmldoc2 = disp;

        htmldoc2->get_all(&htmlelemcoll);
        htmlelemcoll->item(TVariant(itemname),TVariant(0),&disp);
        htmlelem = disp;
        htmlelem->setAttribute(WideString("value"),TVariant(value),0);
}

void TfrmAddFriend::AddFriendOnClick(DISPID id, VARIANT* pVarResult)
{
        //TODO: Add your source code here
        _di_IDispatch disp;
        System::DelphiInterface<IHTMLDocument2> htmldoc2;
        System::DelphiInterface<IHTMLWindow2> htmlwnd2;
        System::DelphiInterface<IHTMLEventObj> htmlevt;
        System::DelphiInterface<IHTMLElement> htmlelem;

        disp = this->CppWebBrowserAddFriend->Document;
        if(!disp)
                return;

        htmldoc2 = disp;
        htmldoc2->get_parentWindow(&htmlwnd2);
        htmlwnd2->get_event(&htmlevt);
        htmlevt->get_srcElement(&htmlelem);

        WideString ws;
        bool flag;
        ws.SetLength(64);
        htmlelem->get_tagName(&ws);
        if(ws == WideString("IMG"))
        {
                TVariant val;
                htmlelem->getAttribute(WideString("name"),0,&val);

                AnsiString str;
                str = val.bstrVal;
                if(str == WideString("closeform"))
                {
                        this->Close();
                }
                else if(str == WideString("minform"))
                {
                        this->WindowState = wsMinimized ;
                }
                else if(str == WideString("Ok"))
                {
                        this->MbNum = this->GetInputValue("MbNum");
                        this->FriendName = this->GetInputValue("Name");
                        this->ID8751 = this->GetInputValue("ID");
                        this->Info = this->GetInputValue("MemTextArea");
                        this->ModalResult = mrOk;
                        this->Close();
                }
                else if(str == WideString("Cancel"))
                {
                        this->ModalResult = mrCancel;
                        this->Close();
                }
        }
        pVarResult->vt = VT_BOOL;
        pVarResult->boolVal = FALSE;
}
//---------------------------------------------------------------------------
void __fastcall TfrmAddFriend::CppWebBrowserAddFriendDocumentComplete(
      TObject *Sender, LPDISPATCH pDisp, Variant *URL)
{
        this->CancelEvt();
        this->SetBrowserStyle();
        this->MoveForm();

        _di_IDispatch disp;
        System::DelphiInterface<IHTMLDocument2> htmldoc2;
        System::DelphiInterface<IHTMLElement> htmlelem;

        disp = this->CppWebBrowserAddFriend->Document;
        if(!disp)
                return;

        htmldoc2 = disp;
        htmldoc2->get_body(&htmlelem);

        TVariant vt;
        vt = CFunctionObject<TfrmAddFriend>::CreateEventFunctionObject(this,&TfrmAddFriend::AddFriendOnClick,0);
        htmlelem->put_onclick(vt);

        vt = CFunctionObject<TfrmAddFriend>::CreateEventFunctionObject(this,&TfrmAddFriend::OnKeyPressEvt,0);
        htmlelem->put_onkeypress(vt);

        this->SetFocusInput("MbNum");
}
//---------------------------------------------------------------------------
void TfrmAddFriend::OnKeyPressEvt(DISPID id, VARIANT* pVarResult)
{
        //TODO: Add your source code here
        _di_IDispatch disp;
        System::DelphiInterface<IHTMLDocument2> htmldoc2;
        System::DelphiInterface<IHTMLWindow2> htmlwnd2;
        System::DelphiInterface<IHTMLEventObj> htmlevt;

        disp = this->CppWebBrowserAddFriend->Document;
        if(!disp)
                return;
               
        htmldoc2 = disp;
        htmldoc2->get_parentWindow(&htmlwnd2);
        htmlwnd2->get_event(&htmlevt);

        long key;
        htmlevt->get_keyCode(&key);

        if(int(key) == 13)
        {
                this->MbNum = this->GetInputValue("MbNum");
                this->FriendName = this->GetInputValue("Name");
                this->ID8751 = this->GetInputValue("ID");
                this->Info = this->GetInputValue("MemTextArea");
                this->ModalResult = mrOk;
                this->Close();
                pVarResult->vt = VT_BOOL;
                pVarResult->boolVal = FALSE;
        }
        if(int(key) == 27)
        {
                this->Close();
                pVarResult->vt = VT_BOOL;
                pVarResult->boolVal = FALSE;
        }
}

void TfrmAddFriend::SetFocusInput(AnsiString itemname)
{
        //TODO: Add your source code here
        _di_IDispatch disp;
        System::DelphiInterface<IHTMLDocument2> htmldoc2;
        System::DelphiInterface<IHTMLElementCollection> htmlelemcoll;
        System::DelphiInterface<IHTMLElement2> htmlelem2;

        disp = this->CppWebBrowserAddFriend->Document;
        if(!disp)
                return;

        htmldoc2 = disp;
        htmldoc2->get_all(&htmlelemcoll);
        htmlelemcoll->item(TVariant(itemname),TVariant(0),&disp);

        if(!disp)
                return;

        htmlelem2 = disp;
        htmlelem2->focus();
}


void __fastcall TfrmAddFriend::FormDestroy(TObject *Sender)
{
        this->ReleaseHook();       
}

本文地址:http://com.8s8s.com/it/it24144.htm