![]() |
| トップ | ソフトウェア | その他 | 掲示板 | リンク | 連絡 |
TWebBrowserで表示されているデータの指定文字列を指定文字列で置換します。
まずusesにSHDocVw_TLB, MSHTML_TLBを追加し、以下の関数を定義してください。
function TForm1.ReplaceString(const Find,Replace: String; WholeWord,
MatchCase, Down: Boolean; confirm: Boolean): Boolean;
var
HTML: IHTMLDocument2;
textRange: OLEVariant;
Option: Integer;
begin
Result := False;
if WebBrowser1.Document = nil then
Exit;
try
HTML := WebBrowser1.Document as IHTMLDocument2;
textRange := HTML.selection.createRange;
textRange.select();
Option := 0;
if WholeWord then
Option := Option + 2;
if MatchCase then
Option := Option + 4;
if Down then
begin
textRange.moveStart('character');
textRange.moveEnd('textedit');
Result := textRange.findText(Find, 1, Option);
end else
begin
textRange.moveEnd('word', -1);
Result := textRange.findText(Find, -1, Option);
end;
if Result then
begin
textRange.select();
if confirm then
begin
try
if MessageDlg('置き換えてよろしいですか?'+#13#10,
mtConfirmation,[mbYes,mbNo],0) = mrYes then
textRange.text := Replace;
except
Result := False;
end;
end else
begin
try
textRange.text := Replace;
except
Result := False;
end;
end;
end;
except
end;
end;
|
これの使い方ですが、Find,Replaceにそれぞれ検索キーと置換文字、WholeWordをTrueで単語単位検索、MatchCaseをTrueで大文字小文字を区別する、DownをTrueで下方検索、confirmをTrueで置換前に確認メッセージを表示するとなっています。
また、所謂全て置換を行うときは以下のように上記関数を回してください。
var
Hit: Boolean;
begin
Hit := False;
while ReplaceString(Edit1.Text,'置き換えテキスト',false,false,true,false) do
begin
Hit := true;
end;
end; |
