程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> Delphi中使用TWebBrowser編輯網頁

Delphi中使用TWebBrowser編輯網頁

編輯:Delphi

本例效果圖:

代碼文件:

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved