刚找了点资料,这个和楼主说的一样,需要通过IServiceProvider的QueryService
而在WebBrowser里面没有OnQueryService事件,所以只有通过它的其他事件,比如OnNewWindow2,设置它的ppDisp为楼主的IHTMLEditHost接口
下面是全文
http://www.euromind.com/iedelphi/HTMLEdit/HTMLEdit2.htm
IHTMLEditHost provides you a way to control how elements are resized and moved when the user grabs and drags the handles on a control element. For instance, you could cause an element to resize or move by specific increments (a snap-to-grid feature), limit resizing to a minimum or maximum size, or constrain the area to which an element can be moved.
Whenever the MSHTML Editor is activated, it looks to see if an IHTMLEditHost is available. The Editor first asks the host application for an IServiceProvider interface. If it finds IServiceProvider, it will query for an IHTMLEditHost interface pointer.
In EmbeddedWB you can use OnQueryService to connect TEditHost:
function TForm1.EmbeddedWB1QueryService(const rsid, iid: TGUID;
out Obj: IUnknown): HRESULT;
begin
Result := S_OK;
if
IsEqualGUID(rsid, SID_SHTMLEDitHost) and (ActiveDesigner <> nil)
then
obj := EditHost1
else
result := E_NOTIMPL;
end;
The IHTMLEditHost interface will remain active until the Editor is turned off or the WebBrowser navigates to a new page. At this point, the Editor releases its pointer to any IHTMLEditHost interface it holds.
TEditHost consists of one method, OnSnapRect. This method has three arguments:
pIElement: a pointer to an IHTMLElement interface for the element that is being moved or resized.
prcNew: a RECT which initially contains the element's size and position. Use this RECT to control how MSHTML resizes or moves the element.
eHandle: an ELEMENT_CORNER enumeration value. The ELEMENT_CORNER enumeration value tells you which handle the user has grabbed to resize the element, if any. If no handle has been grabbed—if eHandle is set to ELEMENT_CORNER_NONE—the element is being moved rather than resized.
The following code demonstrates how to process each of the nine ELEMENT_CORNER values:
function TForm1.EditHost1SnapRect(const pIElement: IHTMLElement;
var prcNew: tagRECT; eHandle: TOleEnum): HRESULT;
begin
case eHandle of
ELEMENT_CORNER_NONE: // Code for moving the element
ELEMENT_CORNER_TOP: // Code for resizing the element
ELEMENT_CORNER_LEFT: // Code for resizing the element
ELEMENT_CORNER_BOTTOM: // Code for resizing the element
ELEMENT_CORNER_RIGHT: // Code for resizing the element
ELEMENT_CORNER_TOPLEFT: // Code for resizing the element
ELEMENT_CORNER_TOPRIGHT: // Code for resizing the element
ELEMENT_CORNER_BOTTOMLEFT: // Code for resizing the element
ELEMENT_CORNER_BOTTOMRIGHT: // Code for resizing the element
end;
result := S_OK;
end;
Since IHTMLEditHost can't be "turned off" during the editing, TEditHost has a propery Enabled that can be turned on and off. If TEdithost is not Enabled the OnSnapRect Event is not called.
Edithost1.Enabled:=FALSE;
另外还有这个控件的代码
//***********************************************************
// TEdithost ver 1.00 (Jan. 14, 2000) *
// *
// For Delphi 4/5 *
// Freeware Component *
// by *
// Per Linds?Larsen *
// per.lindsoe@larsen.dk *
// *
// Documentation and updated versions: *
// *
// http://www.euromind.com/iedelphi *
//***********************************************************
unit Edithost;
interface
uses
mshtml_tlb, Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
const
SID_SHTMLEditHost: TGUID = (D1: $3050F6A0; D2: $98B5; D3: $11CF; D4: ($BB, $82, $00, $AA, $00, $BD, $CE, $0B));
type
TSnapRect = function (const pIElement: IHTMLElement; var prcNew: tagRECT; eHandle: _ELEMENT_CORNER): HResult of object;
TEditHost = class(TComponent, IUnknown, IHTMLEditHost)
private
{ Private declarations }
FSnapRect : TSnapRect;
FEnabled : Boolean;
protected
{ Protected declarations }
function SnapRect(const pIElement: IHTMLElement; var prcNew: tagRECT; eHandle: _ELEMENT_CORNER): HResult; stdcall;
public
{ Public declarations }
published
{ Published declarations }
property OnSnapRect : TSnapRect read FSnapRect write FSnapRect;
property Enabled : Boolean read FEnabled write FEnabled;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Samples', [TEditHost]);
end;
{ TEditHost }
function TEditHost.SnapRect(const pIElement: IHTMLElement;
var prcNew: tagRECT; eHandle: _ELEMENT_CORNER): HResult;
begin
Result:=S_OK;
If Assigned(FSnapRect) and FEnabled then
Result:=FSnapRect(pIElement, prcNew, eHandle);
end;
end.
看看对楼主有帮助不,这种问题通常都是自己解决的