How to make TWebBrowser Zoom when using ctrl+mousewheel like Internet Explorer does?

不羁岁月 提交于 2019-11-29 03:59:43
Cesar Romero

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;

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!