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.
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:
- 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)
- 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) - Still running I dynamically created a TWebBrowser and loaded same web page as in step 1 - but user agent still showed MSIE 7.0
- After restarting the application the user agent was 9.0 from the beginning
So it seems like you would have to restart the application.
来源:https://stackoverflow.com/questions/6534614/twebbrowser-and-feature-browser-emulation-at-runtime