TWebBrowser and FEATURE_BROWSER_EMULATION at runtime

给你一囗甜甜゛ 提交于 2019-11-29 02:40:56

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 );

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.

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