delphi中将webbrowser用作网页编辑器,转

1.在formcreate事件中加入

Application.OnMessage := IEMessageHandler;

这是用作对webbrowser回车事件处理消息

2。打开网页时作相应处理,进入网页编辑状态

webbrowser1.Navigate(htmfile);

while webbrowser1.busy do

Application.ProcessMessages;

(webbrowser1.Document as IHTMLDocument2).designMode :=

'On';

3.加入一按钮,处理点击时使用网页中文字变红

,这对粗

procedure Tformmp3.Button2Click(Sender: TObject);

begin

with webbrowser1.Document as IHTMLDocument2 do

begin

execCommand('ForeColor', False, 'red'); //使选中文字变红

execCommand('Bold', False, 1); //加粗选中文字

webbrowser1.ExecWB

(OLECMDID_SAVE,OLECMDEXECOPT_DONTPROMPTUSER ); //这是对修改后

的网页保存

end;

end;

4.响应回车等的消息处理程序

procedure Tformmp3.IEMessageHandler(var Msg: TMsg; var

Handled: Boolean);

const

StdKeys = [VK_TAB, VK_RETURN]; { 标准键 }

ExtKeys = [VK_DELETE, VK_BACK, VK_LEFT, VK_RIGHT]; { 扩展键

}

fExtended = $01000000; { 扩展键标志 }

begin

Handled := False;

with Msg do

if ((Message >= WM_KEYFIRST) and (Message <= WM_KEYLAST))

and

((wParam in StdKeys) or (GetKeyState(VK_CONTROL) < 0) or

(wParam in ExtKeys) and ((lParam and fExtended) =

fExtended)) then

try

if IsChild(webbrowser1.Handle, hWnd) then

{ 处理所有的浏览器相关消息 }

begin

with webbrowser1.Application as

IOleInPlaceActiveObject do

Handled := TranslateAccelerator(Msg) = S_OK;

if not Handled then

begin

Handled := True;

TranslateMessage(Msg);

DispatchMessage(Msg);

end;

end;

except

end;

end; // IEMessageHandler