本例效果圖:
代碼文件:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, OleCtrls, SHDocVw, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
WebBrowser1: TWebBrowser;
Panel1: TPanel;
Button1: TButton;
CheckBox1: TCheckBox;
procedure FormCreate(Sender: TObject);
procedure CheckBox1Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(WebBrowser1.OleObject.document.body.innerText);
end;
procedure TForm1.CheckBox1Click(Sender: TObject);
begin
{這裡是讓整個頁面可編輯, 也可以單獨編輯某個元素}
WebBrowser1.OleObject.document.body.contentEditable := CheckBox1.Checked;
{有些浏覽器需要這樣設置: document.designMode = "On";}
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Panel1.Align := alTop;
CheckBox1.Anchors := [akTop, akRight];
Button1.Anchors := CheckBox1.Anchors;
CheckBox1.Caption := '編輯';
Button1.Caption := '顯示網頁文本';
WebBrowser1.Align := alClIEnt;
WebBrowser1.Navigate('http://www.cnblogs.com/del/archive/2009/04/16/1437417.Html');
end;
end.
窗體文件:
object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClIEntHeight = 385
ClIEntWidth = 632
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
Position = poScreenCenter
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object WebBrowser1: TWebBrowser
Left = 8
Top = 48
Width = 563
Height = 321
TabOrder = 0
ControlData = {
4C000000303A00002D2100000000000000000000000000000000000000000000
000000004C000000000000000000000001000000E0D057007335CF11AE690800
2B2E126208000000000000004C0000000114020000000000C000000000000046
8000000000000000000000000000000000000000000000000000000000000000
00000000000000000100000000000000000000000000000000000000}
end
object Panel1: TPanel
Left = 0
Top = 1
Width = 633
Height = 32
Caption = 'Panel1'
TabOrder = 1
object Button1: TButton
Left = 440
Top = 4
Width = 99
Height = 25
Caption = 'Button1'
TabOrder = 0
OnClick = Button1Click
end
object CheckBox1: TCheckBox
Left = 552
Top = 8
Width = 73
Height = 17
Caption = 'CheckBox1'
TabOrder = 1
OnClick = CheckBox1Click
end
end
end