运行时的 TWebBrowser 和 FEATURE_BROWSER_EMULATION

发布于 2024-11-17 21:47:32 字数 156 浏览 2 评论 0原文

有没有人尝试在运行时创建和销毁 TWebBrowser 并使用 FEATURE_BROWSER_EMULATION 来切换浏览器模式,然后重新创建 TWebBrowser 以启用切换模式而无需重新启动应用程序?

我想知道是否仅在启动应用程序时或创建 Web 浏览器控件时读取该设置。

Has anyone tried creating and destroying a TWebBrowser at runtime and using FEATURE_BROWSER_EMULATION to switch the browser mode before re-creating the TWebBrowser to enable switching the mode without restarting the application?

I'm wondering if the setting is only read when starting the app, or when the web browser control is created.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一桥轻雨一伞开 2024-11-24 21:47:32

您不需要自己创建或销毁 TEmbeddedWB。我这样做(见下文)是为了为应用程序设置正确的 IE 版本。工作完美。您必须在创建表单之前执行此操作。您可以在初始化语句中执行此操作,例如:

 TIEMode = (iemUnknown, iemIE7, iemIE8, iemIE9, iemIE10);
 // iemUnknown, don't use this as parameter, return result only
 // iemIE10: To run a WebBrowser control in IE10 Standards Mode
 // iemIE9: To run a WebBrowser control in IE9 Standards Mode
 // iemIE8: To run a WebBrowser control in IE8 Standards Mode
 // iemIE7: To run in IE7 Standards Mode

function embeddedWebbrowserMode(bSet : Boolean; Mode: TIEMode; AppName: string = '') : LongInt;
const
  REG_KEY = 'Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION';

var
  Reg: TRegistry;
  Value: LongInt;
  i : LongInt;

begin
  Result:=0;
  if( bSet ) and ( Mode = iemUnknown ) then
   Exit;

  if AppName = '' then
    AppName := ExtractFileName(Application.ExeName);

  if( bSet ) then
   case Mode of
     iemIE7 :  Value := 7000;
     iemIE8 : Value := 8888;
     iemIE9 : Value:=9999;
     else Value:=10001; // IE10 standards mode
   end
  else Value:=0; 

  Reg:=nil;
  try
   Reg := TRegistry.Create();
   Reg.RootKey := HKEY_CURRENT_USER;
    if( Reg.OpenKey(REG_KEY, True) ) then
    begin
      if( bSet ) then
       begin
        Reg.WriteInteger(AppName, Value);
        Result:=Value;
       end
      else Value:=Reg.ReadInteger( AppName );
      Reg.CloseKey;
    end;
  except;
  end;

  if( Assigned( Reg )) then
   FreeAndNil(Reg);

  if( NOT bSet ) and ( Value > 0 ) then
  begin
   i:=Value div 1000;
   if( i >= 7 ) and ( i <= 10 ) then
    begin
        case i of
         7000  : Result:=Byte(iemIE7);
         8888  : Result:=Byte(iemIE8);
         9999  : Result:=Byte(iemIE9);
         10001 : Result:=Byte(iemIE10);
          else begin
                if( i >=10 ) then
                 Result:=Byte(iemIE10);
               end;
        end;
    end;
  end;
end;

function setEmbeddedWebbrowserMode(Mode: TIEMode; AppName: string = '') : boolean;
begin
 Result:=( embeddedWebbrowserMode(TRUE, Mode, AppName ) > 0 ); 
end;

function getEmbeddedWebbrowserMode( AppName: string = '' ) : TIEMode;
begin
 Result:= TIEMode( Byte( embeddedWebbrowserMode(FALSE, iemUnknown, AppName ))); 
end;

如何使用它的示例:

initialization
 setEmbeddedWebbrowserMode( iemIE9 );

You don't need to create or destroy a TEmbeddedWB yourself. I made this (see below) to set the correct IE version for an app. Works perfecctly. You must do this before the form is created. You can do this in a initialization statement, for example:

 TIEMode = (iemUnknown, iemIE7, iemIE8, iemIE9, iemIE10);
 // iemUnknown, don't use this as parameter, return result only
 // iemIE10: To run a WebBrowser control in IE10 Standards Mode
 // iemIE9: To run a WebBrowser control in IE9 Standards Mode
 // iemIE8: To run a WebBrowser control in IE8 Standards Mode
 // iemIE7: To run in IE7 Standards Mode

function embeddedWebbrowserMode(bSet : Boolean; Mode: TIEMode; AppName: string = '') : LongInt;
const
  REG_KEY = 'Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION';

var
  Reg: TRegistry;
  Value: LongInt;
  i : LongInt;

begin
  Result:=0;
  if( bSet ) and ( Mode = iemUnknown ) then
   Exit;

  if AppName = '' then
    AppName := ExtractFileName(Application.ExeName);

  if( bSet ) then
   case Mode of
     iemIE7 :  Value := 7000;
     iemIE8 : Value := 8888;
     iemIE9 : Value:=9999;
     else Value:=10001; // IE10 standards mode
   end
  else Value:=0; 

  Reg:=nil;
  try
   Reg := TRegistry.Create();
   Reg.RootKey := HKEY_CURRENT_USER;
    if( Reg.OpenKey(REG_KEY, True) ) then
    begin
      if( bSet ) then
       begin
        Reg.WriteInteger(AppName, Value);
        Result:=Value;
       end
      else Value:=Reg.ReadInteger( AppName );
      Reg.CloseKey;
    end;
  except;
  end;

  if( Assigned( Reg )) then
   FreeAndNil(Reg);

  if( NOT bSet ) and ( Value > 0 ) then
  begin
   i:=Value div 1000;
   if( i >= 7 ) and ( i <= 10 ) then
    begin
        case i of
         7000  : Result:=Byte(iemIE7);
         8888  : Result:=Byte(iemIE8);
         9999  : Result:=Byte(iemIE9);
         10001 : Result:=Byte(iemIE10);
          else begin
                if( i >=10 ) then
                 Result:=Byte(iemIE10);
               end;
        end;
    end;
  end;
end;

function setEmbeddedWebbrowserMode(Mode: TIEMode; AppName: string = '') : boolean;
begin
 Result:=( embeddedWebbrowserMode(TRUE, Mode, AppName ) > 0 ); 
end;

function getEmbeddedWebbrowserMode( AppName: string = '' ) : TIEMode;
begin
 Result:= TIEMode( Byte( embeddedWebbrowserMode(FALSE, iemUnknown, AppName ))); 
end;

Example how to use it:

initialization
 setEmbeddedWebbrowserMode( iemIE9 );
云朵有点甜 2024-11-24 21:47:32

我尝试了一下,但没有成功。

这就是我所做的:

  1. 使用现有的 TWebBrowser 创建并启动一个应用程序,该应用程序会加载网页
    显示当前用户代理 - 它显示 MSIE 7.0(安装的是 9.0,所以
    嵌入式控件的兼容模式启动)
  2. 同时仍然
    运行我将应用程序添加到 HKCU\Software\Microsoft\Internet
    Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION
    强制执行
    使用版本 9(为我的 exe 名称添加了 9999)
  3. 仍在运行我动态创建
    TWebBrowser 并加载与步骤 1 相同的网页 - 但用户代理
    仍然显示 MSIE 7.0
  4. 重新启动应用程序后,用户
    代理从一开始就是 9.0

所以看来你必须重新启动应用程序。

I tried it and it didn't work.

This is what I did:

  1. Created and started an application with existing TWebBrowser which a loads web page
    that displays the current user agent - it displayed MSIE 7.0 (installed is 9.0, so
    compatibility mode for embedded controls kicked in)
  2. While still
    running I added the application to HKCU\Software\Microsoft\Internet
    Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION
    to enforce
    use of version 9 (added 9999 for my exe name)
  3. Still running I dynamically created
    a TWebBrowser and loaded same web page as in step 1 - but user agent
    still showed MSIE 7.0
  4. After restarting the application the user
    agent was 9.0 from the beginning

So it seems like you would have to restart the application.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文