☆WebBrowserでHTMLを編集する。
今まであまり興味がなく、試したことがなかったのですが、結構すごいですね。
きちんと作れば、高機能なHTMLエディタも簡単につくれそうです。
もちろん、インターフェースとかCOM系のややこしいプログラムを駆使できればの話ですけど(笑)
フォームにButton5個とWebBrowser1個を貼り付けます。
編集機能については、CTRL+C, CTRL+Z等の標準的なものは、プログラムしてなくても使えます。
参考にしたサイト
about.com
How to enable editing of a document in TWebBrowser
MSDN
MSHTML Editing
フォームにButton5個とWebBrowser1個を貼り付けます。
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; Button5: TButton; procedure FormCreate(Sender: TObject); procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure Button4Click(Sender: TObject); procedure Button5Click(Sender: TObject); private public end; var Form1: TForm1; implementation {$R *.dfm} uses MSHTML, ActiveX; // HTMLの設定 procedure TForm1.FormCreate(Sender: TObject); begin WebBrowser1.Navigate('http://hiderin.air-nifty.com/'); Button1.Caption := '編集モード'; Button2.Caption := '閲覧モード'; Button3.Caption := 'コピー'; Button4.Caption := '貼り付け'; Button5.Caption := '保存'; end; // 編集モード procedure TForm1.Button1Click(Sender: TObject); begin (WebBrowser1.Document as IHTMLDocument2).designMode := 'on'; end; // 閲覧モード procedure TForm1.Button2Click(Sender: TObject); begin (WebBrowser1.Document as IHTMLDocument2).designMode := 'off' end; // コピー procedure TForm1.Button3Click(Sender: TObject); begin (WebBrowser1.Document as IHTMLDocument2).execCommand('Copy', True, EmptyParam); end; // 貼り付け procedure TForm1.Button4Click(Sender: TObject); begin (WebBrowser1.Document as IHTMLDocument2).execCommand('Paste', False, EmptyParam); end; // ファイルに保存 //EmptyParamとすると「無題」ファイル名が設定される。 //'test.html'とかファイル名を設定してもいいみたい。 procedure TForm1.Button5Click(Sender: TObject); begin (WebBrowser1.Document as IHTMLDocument2).execCommand('SaveAs', False, EmptyParam); end; initialization OleInitialize(nil); finalization OleUninitialize; end.
編集機能については、CTRL+C, CTRL+Z等の標準的なものは、プログラムしてなくても使えます。
参考にしたサイト
about.com
How to enable editing of a document in TWebBrowser
MSDN
MSHTML Editing
| 固定リンク