随笔 - 2146  文章 - 97 评论 - 11767 trackbacks - 253


本例效果图:



代码文件:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, OleCtrls, SHDocVw;

type
  TForm1 = class(TForm)
    WebBrowser1: TWebBrowser;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  WebBrowser1.Align := alTop;
  Button2.Enabled := False;
  Button3.Enabled := False;
  Button4.Enabled := False;
  Button1.Caption := '模拟一个页面';
  Button2.Caption := '假如知道按钮名称';
  Button3.Caption := '假如知道按钮的 ID';
  Button4.Caption := '假如只知道是第几个按钮';
end;

{模拟一个页面}
procedure TForm1.Button1Click(Sender: TObject);
begin
  WebBrowser1.Navigate(
    'about:<head><title>标题</title><body bgcolor=#ff0000>' +
    '<form method="POST" action="http://del.cnblogs.com">' +
    '<input type="submit" value="提交" id="btnID" name="btnName">' +
    '</form></body>');
  Button2.Enabled := True;
  Button3.Enabled := True;
  Button4.Enabled := True;
end;

{假如知道按钮名称, 譬如是: btnName}
procedure TForm1.Button2Click(Sender: TObject);
begin
  WebBrowser1.OleObject.document.all.item('btnName').click;
  //WebBrowser1.OleObject.document.all.item('btnName', 0).click;
  FormCreate(nil);
end;

{假如知道按钮的 ID, 譬如是: btnID}
procedure TForm1.Button3Click(Sender: TObject);
begin
  WebBrowser1.OleObject.document.getElementByID('btnID').click;
  FormCreate(nil);
end;

{假如只知道是第几个按钮, 譬如是第一个}
procedure TForm1.Button4Click(Sender: TObject);
begin
  WebBrowser1.OleObject.document.getElementsByTagName('input').item(0).click;
  FormCreate(nil);
end;

end.


窗体文件:
object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 152
  ClientWidth = 625
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object WebBrowser1: TWebBrowser
    Left = 8
    Top = 8
    Width = 360
    Height = 105
    TabOrder = 0
    ControlData = {
      4C00000035250000DA0A00000000000000000000000000000000000000000000
      000000004C000000000000000000000001000000E0D057007335CF11AE690800
      2B2E126208000000000000004C0000000114020000000000C000000000000046
      8000000000000000000000000000000000000000000000000000000000000000
      00000000000000000100000000000000000000000000000000000000}
  end
  object Button1: TButton
    Left = 8
    Top = 120
    Width = 148
    Height = 25
    Caption = 'Button1'
    TabOrder = 1
    OnClick = Button1Click
  end
  object Button2: TButton
    Left = 162
    Top = 119
    Width = 148
    Height = 25
    Caption = 'Button2'
    TabOrder = 2
    OnClick = Button2Click
  end
  object Button3: TButton
    Left = 316
    Top = 119
    Width = 148
    Height = 25
    Caption = 'Button3'
    TabOrder = 3
    OnClick = Button3Click
  end
  object Button4: TButton
    Left = 470
    Top = 120
    Width = 148
    Height = 25
    Caption = 'Button4'
    TabOrder = 4
    OnClick = Button4Click
  end
end

posted on 2009-01-07 11:08  万一  阅读(...)  评论(...编辑  收藏