Tips & Tricks

 

1. Change Caption and Icon in Webbrowser's default Dialog-boxes

You can change the content of the caption-bar in TWebbrowsers built-in dialog-boxes. The following sample shows how to replace "Microsoft Internet Explorer" or "Internet Explorer" with the title and icon defined for your project:

Add to private section in your main form:

procedure WMActivate(var Msg: TWMActivate); message WM_ACTIVATE;

Procedure TForm1.WMActivate(var Msg: TWMActivate);
var
  S: String;
  wnd: HWND;
  I: Integer;
begin
  If Msg.Active=0 then
  begin
  wnd := Msg.ActiveWindow;
  SendMessage(wnd, WM_SETICON, ICON_SMALL, Application.Icon.Handle);
  I := GetWindowTextLength(wnd);
  SetLength(S, I + 1);
  GetWindowText(Wnd, PChar(S), I + 1);
  S := StringReplace(S, 'Microsoft ', '', []);
  S := StringReplace(S, 'Internet Explorer', Application.Title, []);
  SetWindowText(Wnd, Pchar(S));
  end;
end;

 


2. Disable built-in Dialog-boxes.

It is not always a good idea to use Silent-property to prevent 'Internet Explorer Script Error'-dialogs and other standard messageboxes from showing up. Instead you can use the following solution. To disable all 'Internet Explorer' messageboxes:

Add to private section in your main form:

procedure WMActivate(var Msg: TWMActivate); message WM_ACTIVATE;


procedure TForm1.WMActivate(var Msg: TWMActivate);
var
  S: String;
  wnd: HWND;
  I: Integer;
begin
  If Msg.Active=0 then
  begin
  wnd := Msg.ActiveWindow;
  I := GetWindowTextLength(wnd);
  SetLength(S, I + 1);
  GetWindowText(Wnd, PChar(S), I + 1);
  If Pos('Internet Explorer', S)>0 then  Sendmessage(wnd,WM_CLOSE,0,0);
  end;
end;



 

3. Keep Contextmenu but disable "View Source"

You do not need to remove the right-click context-menu  if you only want to disable "View Source".

Add the following key to the registry:

HKEY_LOCAL_MACHINE\Software\Microsoft\Internet Explorer\View Source Editor\Editor Name

Set Value of 'Editor Name' to an exe-file, that does nothing but shows a messagebox: "View Source is not available". 



4. Set Print-Options programmatically. (Version 1)

The following sample shows an easy way to programmatically set options for print (number of copies, paper orientation, header, footer, margins etc.). The program opens the Pagesetup and Print dialogs, makes the modifications and close them again before they become visible. You can use the functions EnumChildWindows and GetCtrlId to get the ControlID for other options you want to set programmatically.

 

Add to private section in your main form:

procedure WMActivate(var Msg: TWMActivate); message WM_ACTIVATE;

type

TPrintOptions = record
 
Copies: Integer;    //Number of copies
  Portrait: Boolean;  //Paper orientation
  Left: string;         //Left Margin 
  Top: string;         //Top Margin
  Right: string;       //Right Margin
  Bottom: string;    //Bottom Margin
  Header: string;    //Header string
  Footer: string;    //Footer string
end;

var
PrintOptions: TPrintOptions;
PrintWithOptions: Boolean;

procedure TForm1.WMActivate(var Msg: TWMActivate);
var
  S: string;
  h, wnd: HWND;
  Item, I: Integer;
begin
if PrintWithOptions and (msg.active = 0) then begin
 
PrintWithOptions := false;
  wnd := Msg.ActiveWindow;
  I := GetWindowTextLength(wnd);
  SetLength(S, I + 1);
  GetWindowText(Wnd, PChar(S), I + 1);
  if S = 'Print'#0 then begin
    
SetDlgItemInt(wnd, 1154, PrintOptions.Copies, FALSE);
    SendDlgItemMessage(Wnd, 1, BM_CLICK, 0, 0);
    PrintWithOptions := true;
    end else
  if
S = 'Page Setup'#0 then begin
    if
PrintOptions.Portrait then
    SendDlgItemMessage(Wnd, 1056, BM_CLICK, 0, 0)
    else
    SendDlgItemMessage(Wnd, 1057, BM_CLICK, 0, 0);
    SetDlgItemText(wnd, 8147, PChar(PrintOptions.Header));
    SetDlgItemText(wnd, 8149, PChar(PrintOptions.Footer));
    SetDlgItemText(wnd, 1155, PChar(PrintOptions.Left));
    SetDlgItemText(wnd, 1156, PChar(PrintOptions.Top));
    SetDlgItemText(wnd, 1157, PChar(PrintOptions.Right));
    SetDlgItemText(wnd, 1158, PChar(PrintOptions.Bottom));
    SendDlgItemMessage(Wnd, 1, BM_CLICK, 0, 0);
    PrintWithOptions := true;
  end;
end;
end;



procedure TForm1.Button1Click(Sender: TObject);
begin
if Assigned(EmbeddedWB1.Document) then begin
  with
PrintOptions do begin
    
Copies := 2;
    Portrait := FALSE;
    Footer := 'This is my Footer';
    Header := 'This is my Header';
    Left := '25,0'; 
    Top := '20,0';
    Right := '22,0';
    Bottom := '19,7';
  end;
PrintWithOptions := True;
Embeddedwb1.pagesetup;
embeddedwb1.printsetup;
PrintWithOptions := False;
end else showmessage('No Document to print!');
end;

 

5. Set Print-Options programmatically. (Version 2)

You can specify extended printing information by passing in the SAFEARRAY structure through the VARIANT argument vaIn, when you use ExecWB and OLECMDID_PRINT. This SAFEARRAY data type takes a maximum of five items:
  1. A string (BSTR) that contains a custom header.
  2. A string (BSTR) that contains a custom footer.
  3. An IStream object that contains an HTML file that serves as an "optional header." This is the e-mail header that you see in Microsoft Outlook and Microsoft Outlook Express e-mail messages when you print them. This IStream must point to a full, valid HTML document, not HTML fragments, or it will print incorrectly.
  4. An alternative URL to use for the document. This is only relevant to Outlook and Outlook Express.
  5. A set of printing flags (dwFlags) to configure the printer. This is only relevant to Outlook and Outlook Express.

All versions of Internet Explorer, from version 4.0 onward, support the use of the IWebBrowser2::ExecWB method to customize headers and footers:

procedure TForm1.PrintHeaderFooter(Header, Footer, HTMLHeader: string);
var
  CmdTarget: IOleCommandTarget;
  Stream: IStream;
  v, v1: OleVariant;
  Dummy: Int64;
  Psa: PSafeArray;
begin
  CreateStreamOnHGlobal(0, TRUE, Stream);
  Stream.Write(Pchar(HTMLHeader), length(HTMLHeader), @Dummy);
  Stream.Seek(0, STREAM_SEEK_SET, Dummy);
  SafeArrayCopy(PSafeArray(TVarData(VarArrayOf([Header, Footer, Stream as IUnknown])).VArray), psa);
  TVarData(V1).VType := varArray or varByRef;
  SafeArrayCopy(psa, PSafeArray(TVarData(V1).VArray));
  if Webbrowser1.Document <> nil then
  try
    Webbrowser1.Document.QueryInterface(IOleCommandTarget, CmdTarget);
    if CmdTarget <> nil then
    try
      CmdTarget.Exec(nil, OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER, v1, v);
    finally
      CmdTarget := nil;
    end;
  except end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  HTMLHeader: string;
begin
  HTMLHeader := '<HTML><BODY><FONT SIZE=4><B><CENTER>IE & DELPHI</CENTER></B></FONT><HR></BODY></HTML>';
  PrintHeaderFooter('My Header', 'My Footer', HTMLHeader);
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  webbrowser1.navigate('about:blank');
end;

     

More Info: HOWTO: Print Custom Headers and Footers for a WebBrowser Control


6. Create EmbeddedWB dynamically

procedure TForm1.Button1Click(Sender: TObject);
var
  WB: TEmbeddedWB;
begin
  WB := TEmbeddedWB.Create(nil);
  TControl(WB).Parent := panel1; //or whatever... 
  Wb.Align:=alClient;
  WB.Loaded;
  WB.Visible := True;
end;