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

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

最佳答案

您不需要自己创建或销毁 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 );

关于运行时的 TWebBrowser 和 FEATURE_BROWSER_EMULATION,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6534614/

10-12 12:35