-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
TMHtmlWindow.pas
242 lines (212 loc) · 6.74 KB
/
TMHtmlWindow.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
unit TMHtmlWindow;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, OleCtrls, SHDocVw, StdCtrls, JvGradient, ExtCtrls, TMStruct,
JvExControls;
type
THtmlWindow = class(TForm)
TopPanel: TPanel;
AboutHeader: TLabel;
JvGradient2: TJvGradient;
BottomPanel: TPanel;
CloseBtn: TButton;
WebBrowser: TWebBrowser;
Bevel1: TBevel;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure CloseBtnClick(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure WebBrowserTitleChange(Sender: TObject;
const Text: WideString);
procedure WebBrowserBeforeNavigate2(ASender: TObject;
const pDisp: IDispatch; const URL, Flags, TargetFrameName, PostData,
Headers: OleVariant; var Cancel: WordBool);
procedure WebBrowserDocumentComplete(ASender: TObject;
const pDisp: IDispatch; const URL: OleVariant);
private
FSrc: String;
FHeader: String;
FHtmlActions: TStringList;
FHtmlWindowFlags: THtmlWindowFlags;
procedure SetSrc(const Value: String);
procedure SetHeader(const Value: String);
procedure SetHtmlWindowFlags(const Value: THtmlWindowFlags);
protected
procedure CreateParams(var Params: TCreateParams); override;
public
property Header: String read FHeader write SetHeader;
property Src: String read FSrc write SetSrc;
property HtmlActions: TStringList read FHtmlActions write FHtmlActions;
property HtmlWindowFlags: THtmlWindowFlags read FHtmlWindowFlags write SetHtmlWindowFlags;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure SetPosAndDimensions(ALeft, ATop, AWidth, AHeight: Integer);
end;
var
HtmlWindow: THtmlWindow;
implementation
uses
TMMsgs, JclStrings, TMMain;
{$R *.dfm}
var
HookID: THandle;
MouseHookEnabled: Boolean;
function MouseProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
var
szClassName: array[0..255] of Char;
const
ie_name = 'Internet Explorer_Server';
begin
case nCode < 0 of
True:
Result := CallNextHookEx(HookID, nCode, wParam, lParam)
else
case wParam of
WM_RBUTTONDOWN,
WM_RBUTTONUP:
begin
GetClassName(PMOUSEHOOKSTRUCT(lParam)^.HWND, szClassName, SizeOf(szClassName));
if (lstrcmp(@szClassName[0], @ie_name[1]) = 0) and MouseHookEnabled then
Result := HC_SKIP
else
Result := CallNextHookEx(HookID, nCode, wParam, lParam);
end
else
Result := CallNextHookEx(HookID, nCode, wParam, lParam);
end;
end;
end;
{ THtmlWindow }
procedure THtmlWindow.CloseBtnClick(Sender: TObject);
begin
Close;
end;
constructor THtmlWindow.Create(AOwner: TComponent);
begin
inherited;
{ Hook the window to prevent the TWebBrowser popup menu }
HookID := SetWindowsHookEx(WH_MOUSE, MouseProc, 0, GetCurrentThreadId());
end;
procedure THtmlWindow.CreateParams(var Params: TCreateParams);
begin
inherited;
if hwfAlwaysOnTop in HtmlWindowFlags then
Params.WndParent := GetDesktopWindow
else
Params.WndParent := Application.Handle;
end;
destructor THtmlWindow.Destroy;
begin
{ Unhook the window }
if HookID <> 0 then
UnHookWindowsHookEx(HookID);
{ Other de-inits }
HtmlWindow := nil;
inherited;
end;
procedure THtmlWindow.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caFree;
end;
procedure THtmlWindow.SetPosAndDimensions(ALeft, ATop, AWidth, AHeight: Integer);
begin
if AWidth > 0 then
ClientWidth := AWidth;
if AHeight > 0 then
begin
ClientHeight := AHeight;
if BottomPanel.Visible then
ClientHeight := ClientHeight + BottomPanel.Height;
if TopPanel.Visible then
ClientHeight := ClientHeight + TopPanel.Height;
end;
if ALeft >= 0 then
Left := ALeft
else if ALeft < 0 then
Left := Screen.Width + ALeft;
if ATop >= 0 then
Top := ATop
else if ATop < 0 then
Top := Screen.Height + ATop;
end;
procedure THtmlWindow.SetHeader(const Value: String);
begin
FHeader := Value;
AboutHeader.Caption := ' ' + Value;
end;
procedure THtmlWindow.SetHtmlWindowFlags(const Value: THtmlWindowFlags);
begin
FHtmlWindowFlags := Value;
{ Apply the flags }
if hwfMaximized in HtmlWindowFlags then
WindowState := wsMaximized
else
WindowState := wsNormal;
if hwfNoResize in HtmlWindowFlags then
BorderStyle := bsDialog
else
BorderStyle := bsSizeable;
MouseHookEnabled := not (hwfEnableContextMenu in HtmlWindowFlags);
BottomPanel.Visible := not (hwfNoCloseButton in HtmlWindowFlags);
TopPanel.Visible := not (hwfNoHeader in HtmlWindowFlags);
if hwfAlwaysOnTop in HtmlWindowFlags then
FormStyle := fsStayOnTop
else
FormStyle := fsNormal;
// SetWindowPos(Handle, HWND_TOPMOST, 0, 0, 0, 0,
// SWP_NOACTIVATE + SWP_NOSIZE + SWP_NOMOVE)
// else
// SetWindowPos(Handle, HWND_NOTOPMOST, 0, 0, 0, 0,
// SWP_NOACTIVATE + SWP_NOSIZE + SWP_NOMOVE);
end;
procedure THtmlWindow.SetSrc(const Value: String);
begin
FSrc := Value;
WebBrowser.Navigate(Value);
end;
procedure THtmlWindow.WebBrowserBeforeNavigate2(ASender: TObject;
const pDisp: IDispatch; const URL, Flags, TargetFrameName, PostData,
Headers: OleVariant; var Cancel: WordBool);
var
I: Integer;
begin
{ Intercept and handle action URLs }
if StrIPos('action:', Trim(URL)) = 1 then
begin
Cancel := True;
if HtmlActions.Find( Trim(StrAfter('action:', Trim(URL))), I) then
(HtmlActions.Objects[I] as TTMMultiAction).ExecuteAction
else
raise Exception.CreateFmt(SHtmlUnknownAction, [Trim(StrAfter('action:', Trim(URL)))]);
end;
if SameText('close:', Trim(URL)) then
begin
Cancel := True;
Close;
end;
end;
procedure THtmlWindow.WebBrowserDocumentComplete(ASender: TObject;
const pDisp: IDispatch; const URL: OleVariant);
begin
{ Apply some flags }
if hwfNoScrollbars in HtmlWindowFlags then
if not VarIsEmpty(WebBrowser.OleObject.Document) then
begin
WebBrowser.OleObject.Document.Body.Style.OverflowX := 'hidden';
WebBrowser.OleObject.Document.Body.Style.OverflowY := 'hidden';
end;
end;
procedure THtmlWindow.FormShow(Sender: TObject);
begin
if hwfAlwaysOnTop in HtmlWindowFlags then
ShowWindow(Application.Handle, SW_HIDE);
//Prevent application taskbar button from showing up
end;
procedure THtmlWindow.WebBrowserTitleChange(Sender: TObject;
const Text: WideString);
begin
Caption := Text;
end;
initialization
MouseHookEnabled := True;
end.