11

According to

http://www.rendelmann.info/blog/CommentView,guid,356fbe68-3ed6-4781-90a4-57070a0141da.aspx and http://msdn.microsoft.com/en-us/library/aa770056(v=vs.85).aspx

getting the hosted WebBrowser to zoom using the control key and the mouse wheel should just require calling IWebBrowser2.ExecWB(OLECMDID_OPTICAL_ZOOM, ...) with a pvaIn value of 100,
but after calling it, ctrl+mousewheel still doesn't zoom the content

Code I'm using with Delphi 2007:

const
  OLECMDID_OPTICAL_ZOOM = 63;
var
  pvaIn, pvaOut: OleVariant;
begin
  pvaIn := 100;
  pvaOut := NULL;
  WebBrowser1.ControlInterface.ExecWB(OLECMDID_OPTICAL_ZOOM,
    OLECMDEXECOPT_DONTPROMPTUSER, pvaIn, pvaOut);
end;
TLama
  • 71,521
  • 15
  • 192
  • 348
jasonpenny
  • 2,929
  • 1
  • 22
  • 23
  • A half off topic comment. Have you tried TEmbeddedWB to see if that helps? – Graymatter Aug 01 '12 at 15:52
  • @jasonpenny: 100 is the default size (100%), did you try a bigger value like 120 or 200? – Cesar Romero Aug 02 '12 at 18:16
  • I have tried TEmbeddedWB, it does not work, even with the DOCHOSTUIFLAG_DPI_AWARE.And the issue is that MSDN says that the control will handle the zoom automatically with `CTRL+mouse wheel forward/back` after setting to `100`; explicitly setting the zoom with a value other than 100 does show the page contents zoomed. – jasonpenny Aug 02 '12 at 19:13
  • @jasonpenny I'm not sure how things go in Delphi 2007, but in Delphi 7, mouse wheel events are not always sent to controls. – Marck Aug 08 '12 at 07:50

2 Answers2

8

jasonpenny,

100 is the default value, if you want to change the zoom, you must increase or decrease this value, from 10 up to 1000.

I wrote a test and here is the code:

type
  TFormWebBrowserZoom = class(TForm)
    WebBrowser1: TWebBrowser;
    procedure FormMouseWheelDown(Sender: TObject; Shift: TShiftState; MousePos: TPoint; var Handled: Boolean);
    procedure FormMouseWheelUp(Sender: TObject; Shift: TShiftState; MousePos: TPoint; var Handled: Boolean);
    procedure FormShow(Sender: TObject);
  private
    FZoom: Integer;
    FLastZoom: Integer; 

    procedure ApplyZoom(ZoomValue: Integer);
    procedure DecZoom;
    procedure IncZoom;
  end;

implementation

const
  OLECMDID_OPTICAL_ZOOM = $0000003F;
  MinZoom = 10;
  MaxZoom = 1000;
  ZoomFactor = 20;
  DefaultZoom = 100;

procedure TFormWebBrowserZoom.FormShow(Sender: TObject);
begin
  WebBrowser1.Navigate('http://www.cesarromero.com.br');
  FZoom := DefaultZoom;
  FLastZoom := DefaultZoom;
end;

procedure TFormWebBrowserZoom.ApplyZoom(ZoomValue: Integer);
var
  pvaIn, pvaOut: OleVariant;
begin
  if ZoomValue = FLastZoom then
    Exit;
  FLastZoom := ZoomValue;
  pvaIn := ZoomValue;
  pvaOut := Null;
  WebBrowser1.ControlInterface.ExecWB(OLECMDID_OPTICAL_ZOOM, OLECMDEXECOPT_DONTPROMPTUSER, pvaIn, pvaOut);
end;

procedure TFormWebBrowserZoom.DecZoom;
begin
  System.Dec(FZoom, ZoomFactor);
  if FZoom < MinZoom then
    FZoom := MinZoom;
  ApplyZoom(FZoom);
end;

procedure TFormWebBrowserZoom.IncZoom;
begin
  System.Inc(FZoom, ZoomFactor);
  if FZoom > MaxZoom then
    FZoom := MaxZoom;
  ApplyZoom(FZoom);
end;

procedure TFormWebBrowserZoom.FormMouseWheelDown(Sender: TObject; Shift: TShiftState; MousePos: TPoint; var Handled: Boolean);
begin
  DecZoom;
end;

procedure TFormWebBrowserZoom.FormMouseWheelUp(Sender: TObject; Shift: TShiftState; MousePos: TPoint; var Handled: Boolean);
begin
  IncZoom;
end;
Ken White
  • 117,855
  • 13
  • 197
  • 405
Cesar Romero
  • 3,906
  • 1
  • 24
  • 43
  • I guess the code in the question is not in a MouseWheel event. What I understood was that the question asks why, after the call, the webbrowser automatically does not adjust the zoom itself. – Sertac Akyuz Aug 02 '12 at 18:55
  • @Sertac Akyuz: Exactly, and I just posted the working code, please check that I changed the value of pvaIn := 200; – Cesar Romero Aug 02 '12 at 18:57
  • Cesar's code above does NOT compile, seeing how he has the same procedure TFormWebBrowserZoom.DecZoom; declared twice! – user1527613 Jul 06 '13 at 19:54
  • 1
    @user1527613: It's a simple typo, and reading the code clearly shows that the second `DecZoom` should be `IncZoom` instead. I've edited to fix that issue, and to move the `const` declarations out of the form type declaration and marked them as belonging in the `implementation` section instead for older Delphi versions. – Ken White Jul 06 '13 at 20:37
4

From http://msdn.microsoft.com/en-us/library/cc849094(v=vs.85).aspx#OptInHighDPI:

Making the Web Bigger: DPI Scaling and Internet Explorer 8

Opt-In to High DPI Behavior for Web Browser Controls (WebOCs)

In order to preserve compatibility with previously developed WebOCs, by default Internet Explorer 8 does not render the web content of WebOCs using the Internet Explorer 8 High DPI behavior, but rather uses the Internet Explorer 7 behavior, which scales up fonts specified in absolute values, such as points. To take advantage of the Internet Explorer 8 High DPI behavior in your programs, you need to use a DOCHOSTUIFLAG called DOCHOSTUIFLAG_DPI_AWARE. You use this flag by using the method GetHostInfo, which has a DOCHOSTUIINFO structure as one of its parameters. In turn, DOCHOSTUIINFO has a operator DWORD called dwFlags as one of its members, that can consist of one or more DOCHOSTUIFLAG values. You must include DOCHOSTUIFLAG_DPI_AWARE in dwFlags in order to take advantage of the Internet Explorer 8 High DPI behavior in your WebOC.

The quick and easy way to simulate how the HTML content of your WebOCs will appear once opted -in to the High -DPI behavior is to open the equivalent HTML content (composed in an HTML file) in Internet Explorer 8, and simply check out the rendering at the equivalent zoom settings (120 DPI to 125% zoom, 144 DPI to 150% zoom). We do recommend that you test out the WebOC in actual High DPI scenarios to be completely sure the HTML content renders as you hoped.

Ian Boyd
  • 220,884
  • 228
  • 805
  • 1,125
  • 2
    Do you have a sample that works? Adding `pInfo.dwFlags := pInfo.dwFlags or {DOCHOSTUIFLAG_DPI_AWARE} $40000000;` to TEmbeddedWB.GetHostInfo doesn't enable ctrl+mousewheel Zooming – jasonpenny May 09 '12 at 18:40