-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathWin.Webbrowser.pas
145 lines (121 loc) · 3.99 KB
/
Win.Webbrowser.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
unit Win.WebBrowser;
interface
(*
Special thanks to David Esperalta, aka Dec @ http://www.clubdelphi.com/foros/member.php?u=4681
http://www.clubdelphi.com/foros/showthread.php?p=507565
*)
uses
System.Win.Registry;
type
{$REGION 'TInternetExplorerVersion'}
{$SCOPEDENUMS ON}
/// <summary> Version Numbers for Windows Internet Explorer </summary>
TInternetExplorerVersion = (
/// <summary> Internet Explorer 11 </summary>
IE11,
/// <summary> Internet Explorer 10 </summary>
IE10,
/// <summary> Internet Explorer 9 </summary>
IE9,
/// <summary> Internet Explorer 8 </summary>
IE8,
/// <summary> Internet Explorer 7 </summary>
IE7
);
{$SCOPEDENUMS OFF}
{$ENDREGION}
{$REGION 'TInternetExplorerVersionHelper'}
TInternetExplorerVersionHelper = record helper for TInternetExplorerVersion
public
/// <summary> Returns the Flag specified by Windows API for the given Internet Explorer Version </summary>
function Value: Integer;
end;
{$ENDREGION}
{$REGION 'TWinWebBrowserEmulation'}
/// <summary> Class that tweaks the Windows Registry to enable TWebBrowser emulation support </summary>
TWinWebBrowserEmulation = class
strict private
/// <summary> Creates and returns a TRegistry pointing to the FEATURE_BROWSER_EMULATION Key </summary>
function OpenWebBrowserEmulationRegistry(out ARegistry: TRegistry): Boolean;
strict protected
/// <summary> Returns the full Key Path to the FEATURE_BROWSER_EMULATION </summary>
function GetFeatureBrowserEmulationRegistryKey: string; virtual;
/// <summary> Returns the Name of the Application Executable </summary>
function GetExeName: string; virtual;
public
/// <summary> Tweaks the Windows Registry allowing TWebBrowser Support for the given Internet Explorer Version </summary>
procedure EnableBrowserEmulation(const Version: TInternetExplorerVersion);
/// <summary> Restores any changes done to the Windows Registry </summary>
procedure RestoreBrowserEmulation;
end;
{$ENDREGION}
implementation
uses
Winapi.Windows,
System.SysUtils;
{$REGION 'TWinWebBrowserEmulation'}
function TWinWebBrowserEmulation.GetExeName: string;
begin
Result := ExtractFileName(ParamStr(0));
end;
function TWinWebBrowserEmulation.GetFeatureBrowserEmulationRegistryKey: string;
begin
Result := 'Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION';
end;
function TWinWebBrowserEmulation.OpenWebBrowserEmulationRegistry(out ARegistry: TRegistry): Boolean;
begin
Result := False;
ARegistry := TRegistry.Create;
try
ARegistry.RootKey := HKEY_CURRENT_USER;
Result := ARegistry.OpenKey(GetFeatureBrowserEmulationRegistryKey, True);
finally
if not Result then
ARegistry.Free;
end;
end;
procedure TWinWebBrowserEmulation.RestoreBrowserEmulation;
var
Registry: TRegistry;
begin
if not OpenWebBrowserEmulationRegistry(Registry) then
Exit;
try
if Registry.ValueExists(GetExeName) then
Registry.DeleteKey(GetExeName);
Registry.CloseKey
finally
Registry.Free;
end;
end;
procedure TWinWebBrowserEmulation.EnableBrowserEmulation(const Version: TInternetExplorerVersion);
var
Registry: TRegistry;
begin
if not OpenWebBrowserEmulationRegistry(Registry) then
Exit;
try
if not Registry.ValueExists(GetExeName) then
Registry.WriteInteger(GetExeName, Version.Value);
Registry.CloseKey
finally
Registry.Free;
end;
end;
{$ENDREGION}
{$REGION 'TInternetExplorerVersionHelper'}
function TInternetExplorerVersionHelper.Value: Integer;
begin
// Values from http://msdn.microsoft.com/en-us/library/ee330730(VS.85).aspx#browser_emulation
case Self of
TInternetExplorerVersion.IE11: Result := 11000;
TInternetExplorerVersion.IE10: Result := 10000;
TInternetExplorerVersion.IE9: Result := 9000;
TInternetExplorerVersion.IE8: Result := 8000;
TInternetExplorerVersion.IE7: Result := 7000;
else
raise Exception.Create('TInternetExplorerVersionHelper.Value: Unknown value');
end;
end;
{$ENDREGION}
end.