Link to home
Start Free TrialLog in
Avatar of Nobuo Miwa
Nobuo Miwa

asked on

Implement customized TWebBrowser of Delphi for C++Builder

Hello Experts,

Please show me implementation for C++Builder of following Delphi code.

uses
  SHDocVw, ActiveX;

type
  TWebBrowser = class(SHDocVw.TWebBrowser, IOleCommandTarget)
  private
    function QueryStatus(CmdGroup: PGUID; cCmds: Cardinal; prgCmds: POleCmd;
      CmdText: POleCmdText): HRESULT; stdcall;
    function Exec(CmdGroup: PGUID; nCmdID, nCmdexecopt: DWORD; 
      const vaIn: OleVariant; var vaOut: OleVariant): HRESULT; stdcall;
  end;

implementation

function TWebBrowser.QueryStatus(CmdGroup: PGUID; cCmds: Cardinal; 
  prgCmds: POleCmd; CmdText: POleCmdText): HRESULT; stdcall;
begin
  // MSDN notes that a command target must implement this function; E_NOTIMPL is not a 
  // valid return value. Be careful to return S_OK, because we notice that context menu 
  // of Web page "Add to Favorites..." becomes disabled. Another MSDN document shows an
  // example with default return value OLECMDERR_E_NOTSUPPORTED.
  // http://msdn.microsoft.com/en-us/library/bb165923(v=vs.80).aspx
  Result := OLECMDERR_E_NOTSUPPORTED;
end;

function TWebBrowser.Exec(CmdGroup: PGUID; nCmdID, nCmdexecopt: DWORD; 
  const vaIn: OleVariant; var vaOut: OleVariant): HRESULT; stdcall;
var
  ShowDialog, InterpretScript: Boolean;
begin
  if CmdGroup = nil then
  begin
    Result := OLECMDERR_E_UNKNOWNGROUP;
    Exit;
  end;

  // MSDN notes that a command target must implement this function; E_NOTIMPL is not a 
  // valid return value. Be careful to return S_OK, because we notice some unhandled
  // commands behave unexpected with S_OK. We assumed that a return value 
  // OLECMDERR_E_NOTSUPPORTED means to use the default behavior.
  Result := OLECMDERR_E_NOTSUPPORTED;

  if IsEqualGUID(CmdGroup^, CGID_DocHostCommandHandler) then
  begin
    // there's a script error in the currently executed script, so
    if nCmdID = OLECMDID_SHOWSCRIPTERROR then
    begin
      ShowDialog := True;
      InterpretScript := False; 

      // Implements an event if you want, so that your application is able to choose the way of handling script errors at runtime.
      if Assigned(OnNotifyScriptError) then
        OnNotifyScriptError(Self, ShowDialog, InterpretScript);

      if ShowDialog then
        Result := S_FALSE
      else
        Result := S_OK;
      vaOut := InterpretScript; // Without setting the variable to true, further script execution will be cancelled.
    end;
  end;
end;

Open in new window


Regards,
Nobuo Miwa
ASKER CERTIFIED SOLUTION
Avatar of Sinisa Vuk
Sinisa Vuk
Flag of Croatia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Nobuo Miwa
Nobuo Miwa

ASKER

Great thanks ! It works !