对不起呀!自从上次回贴以后,我就再也找不到这个贴咯.
今天才来自大富翁关于这个贴的回应,真对不起了.下面是原码,在我这儿调试通过.
自认为比HOOK好得多.如果有什么不明白,可以EMAIL:zengxiaoxun@hotmail.com.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, MSHTML, OleCtrls, SHDocVw, StdCtrls, ActiveX;
type
_SELECTION_TYPE = TOleEnum;
const
SELECTION_TYPE_None = $00000000;
SELECTION_TYPE_Caret = $00000001;
SELECTION_TYPE_Text = $00000002;
SELECTION_TYPE_Control = $00000003;
SELECTION_TYPE_Max = $7FFFFFFF;
SID_SHTMLEditServices: TGUID ='{3050F7F9-98B5-11CF-BB82-00AA00BDCE0B}';
type
ISegment = interface(IUnknown)
['{3050F683-98B5-11CF-BB82-00AA00BDCE0B}']
function GetPointers(const pIStart: IMarkupPointer; const pIEnd: IMarkupPointer): HResult; stdcall;
end;
IElementSegment = interface(ISegment)
['{3050F68F-98B5-11CF-BB82-00AA00BDCE0B}']
function GetElement(out ppIElement: IHTMLElement): HResult; stdcall;
function SetPrimary(fPrimary: Integer): HResult; stdcall;
function IsPrimary(out pfPrimary: Integer): HResult; stdcall;
end;
ISelectionServicesListener = interface(IUnknown)
['{3050F699-98B5-11CF-BB82-00AA00BDCE0B}']
function BeginSelectionUndo: HResult; stdcall;
function EndSelectionUndo: HResult; stdcall;
function OnSelectedElementExit(const pIElementStart: IMarkupPointer;
const pIElementEnd: IMarkupPointer;
const pIElementContentStart: IMarkupPointer;
const pIElementContentEnd: IMarkupPointer): HResult; stdcall;
function OnChangeType(eType: _SELECTION_TYPE; const pIListener: ISelectionServicesListener): HResult; stdcall;
function GetTypeDetail(out pTypeDetail: WideString): HResult; stdcall;
end;
ISelectionServices = interface(IUnknown)
['{3050F684-98B5-11CF-BB82-00AA00BDCE0B}']
function SetSelectionType(eType: _SELECTION_TYPE; const pIListener: ISelectionServicesListener): HResult; stdcall;
function GetMarkupContainer(out ppIContainer: IMarkupContainer): HResult; stdcall;
function AddSegment(const pIStart: IMarkupPointer; const pIEnd: IMarkupPointer;
out ppISegmentAdded: ISegment): HResult; stdcall;
function AddElementSegment(const pIElement: IHTMLElement; out ppISegmentAdded: IElementSegment): HResult; stdcall;
function RemoveSegment(const pISegment: ISegment): HResult; stdcall;
function GetSelectionServicesListener(out ppISelectionServicesListener: ISelectionServicesListener): HResult; stdcall;
end;
IHTMLEditDesigner = interface(IUnknown)
['{3050F662-98B5-11CF-BB82-00AA00BDCE0B}']
function PreHandleEvent(inEvtDispId: Integer; const pIEventObj: IHTMLEventObj): HResult; stdcall;
function PostHandleEvent(inEvtDispId: Integer; const pIEventObj: IHTMLEventObj): HResult; stdcall;
function TranslateAccelerator(inEvtDispId: Integer; const pIEventObj: IHTMLEventObj): HResult; stdcall;
function PostEditorEventNotify(inEvtDispId: Integer; const pIEventObj: IHTMLEventObj): HResult; stdcall;
end;
IHTMLEditServices = interface(IUnknown)
['{3050F663-98B5-11CF-BB82-00AA00BDCE0B}']
function AddDesigner(const pIDesigner: IHTMLEditDesigner): HResult; stdcall;
function RemoveDesigner(const pIDesigner: IHTMLEditDesigner): HResult; stdcall;
function GetSelectionServices(const pIContainer: IMarkupContainer;
out ppSelSvc: ISelectionServices): HResult; stdcall;
function MoveToSelectionAnchor(const pIStartAnchor: IMarkupPointer): HResult; stdcall;
function MoveToSelectionEnd(const pIEndAnchor: IMarkupPointer): HResult; stdcall;
function SelectRange(const pStart: IMarkupPointer; const pEnd: IMarkupPointer;
eType: _SELECTION_TYPE): HResult; stdcall;
end;
THTMLEditDesigner = class(TInterfacedObject,IHTMLEditDesigner)
private
function PreHandleEvent(inEvtDispId: Integer; const pIEventObj: IHTMLEventObj): HResult; stdcall;
function PostHandleEvent(inEvtDispId: Integer; const pIEventObj: IHTMLEventObj): HResult; stdcall;
function TranslateAccelerator(inEvtDispId: Integer; const pIEventObj: IHTMLEventObj): HResult; stdcall;
function PostEditorEventNotify(inEvtDispId: Integer; const pIEventObj: IHTMLEventObj): HResult; stdcall;
public
constructor Create(ABrowser:TWebBrowser);virtual;
end;
TForm1 = class(TForm)
WebBrowser1: TWebBrowser;
Button1: TButton;
Memo1: TMemo;
procedure WebBrowser1DocumentComplete(Sender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
procedure FormCreate(Sender: TObject);
private
FHTMLEditDesigner:THTMLEditDesigner;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses Math;
{$R *.dfm}
{ THTMLEditDesigner }
constructor THTMLEditDesigner.Create(ABrowser: TWebBrowser);
var
ServiceProvider: IServiceProvider;
HTMLEditServices: IHTMLEditServices;
begin
if Assigned(ABrowser.Document) then
begin
ServiceProvider:=ABrowser.Document as IServiceProvider;
ServiceProvider.QueryService(SID_SHTMLEditServices,IHTMLEditServices,HTMLEditServices);
HTMLEditServices.AddDesigner(Self);
end;
end;
function THTMLEditDesigner.PostEditorEventNotify(inEvtDispId: Integer;
const pIEventObj: IHTMLEventObj): HResult;
begin
Result:=S_OK;
end;
function THTMLEditDesigner.PostHandleEvent(inEvtDispId: Integer;
const pIEventObj: IHTMLEventObj): HResult;
begin
Result:=S_False;
end;
function THTMLEditDesigner.PreHandleEvent(inEvtDispId: Integer;
const pIEventObj: IHTMLEventObj): HResult;
begin
if Assigned(pIEventObj.srcElement) and (pIEventObj.srcElement.tagName<>'BODY') then
Result:=S_OK
else
Result:=S_FALSE;
end;
function THTMLEditDesigner.TranslateAccelerator(inEvtDispId: Integer;
const pIEventObj: IHTMLEventObj): HResult;
begin
Result:=S_False;
end;
procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
begin
if not Assigned(FHTMLEditDesigner) then
FHTMLEditDesigner:=THTMLEditDesigner.Create(WebBrowser1);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
WebBrowser1.Navigate('http://www.delphibbs.com/');
end;
end.