This repository has been archived by the owner on Sep 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
APPUTIL.PAS
executable file
·181 lines (158 loc) · 4.75 KB
/
APPUTIL.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
unit AppUtil;
interface
uses classes, sysUtils, forms, controls, winTypes, graphics
;
function AppPath: string;
function GetTopMostForm(FormClass: TFormClass): TForm;
function ValidForm(FormClass: TFormClass): TForm;
function ClientToClient(Src, Dest: TControl; const Point: TPoint): TPoint;
procedure SafeSetFocus(Control: TWinControl);
procedure ShowAndBringToFront(Form: TForm);
function CreateWinControlBitmap(Control: TWinControl): Graphics.TBitmap;
implementation
uses WinProcs, Messages, SDS
;
function AppPath: string;
begin
result := extractFilePath(application.exeName);
end;
{ returns the instance of this form class which was most recently used. This is determined }
{ by the order of screens.forms, which is ordered by z-order. }
function GetTopMostForm(formClass: TFormClass): TForm;
var
i: integer;
begin
if (application.mainForm.formStyle = fsMDIForm) then begin
for i := 0 to (application.mainForm.mdiChildCount - 1) do begin
if (application.mainForm.mdiChildren[i] is formClass) then begin
result := application.mainForm.mdiChildren[i];
exit;
end;
end;
end else begin
for i := 0 to (screen.formCount - 1) do begin
if (screen.forms[i] is formClass) then begin
result := screen.forms[i];
exit;
end;
end;
end;
result := nil;
end;
function ValidForm(FormClass: TFormClass): TForm;
begin
result := GetTopMostForm(FormClass);
if (result = nil) then
result := FormClass.Create(Application);
end;
function ClientToClient(Src, Dest: TControl; const Point: TPoint): TPoint;
begin
result := src.clientToScreen(point);
result := dest.screenToClient(result);
end;
{ For use when the control might be another notebook page - will prevent }
{ the "unable to focus invisible or disabled control" exception }
procedure SafeSetFocus(Control: TWinControl);
begin
control.show;
control.setFocus;
end;
procedure ShowAndBringToFront(Form: TForm);
begin
if (form.windowState = wsMinimized) then
form.windowState := wsNormal;
form.show;
form.bringToFront;
end;
{$ifdef ver90}
function CreateWinControlBitmap(Control: TWinControl): Graphics.TBitmap;
var
ScreenDC, PrintDC: HDC;
OldBits, PrintBits: HBITMAP;
PaintLParam: Longint;
procedure PrintHandle(Handle: HWND);
var
R: TRect;
Child: HWND;
SavedIndex: Integer;
begin
if IsWindowVisible(Handle) then
begin
SavedIndex := SaveDC(PrintDC);
WinProcs.GetClientRect(Handle, R);
MapWindowPoints(Handle, Control.Handle, R, 2);
with R do
begin
SetWindowOrgEx(PrintDC, -Left, -Top, nil);
IntersectClipRect(PrintDC, 0, 0, Right - Left, Bottom - Top);
end;
SendMessage(Handle, WM_ERASEBKGND, PrintDC, 0);
SendMessage(Handle, WM_PAINT, PrintDC, PaintLParam);
Child := GetWindow(Handle, GW_CHILD);
if Child <> 0 then
begin
Child := GetWindow(Child, GW_HWNDLAST);
while Child <> 0 do
begin
PrintHandle(Child);
Child := GetWindow(Child, GW_HWNDPREV);
end;
end;
RestoreDC(PrintDC, SavedIndex);
end;
end;
begin
Result := nil;
ScreenDC := GetDC(0);
PaintLParam := 0;
try
PrintDC := CreateCompatibleDC(ScreenDC);
try
PrintBits := CreateCompatibleBitmap(ScreenDC, Control.ClientWidth, Control.ClientHeight);
try
OldBits := SelectObject(PrintDC, PrintBits);
try
{ Clear the contents of the bitmap }
FillRect(PrintDC, Control.ClientRect, Control.Brush.Handle);
{ Paint form into a bitmap }
PrintHandle(Control.Handle);
finally
SelectObject(PrintDC, OldBits);
end;
Result := Graphics.TBitmap.Create;
Result.Handle := PrintBits;
PrintBits := 0;
except
Result.Free;
if PrintBits <> 0 then DeleteObject(PrintBits);
raise;
end;
finally
DeleteDC(PrintDC);
end;
finally
ReleaseDC(0, ScreenDC);
end;
end;
{$else}
function CreateWinControlBitmap(Control: TWinControl): Graphics.TBitmap;
begin
Result := Graphics.TBitmap.Create;
try
Result.Width := Control.ClientWidth;
Result.Height := Control.ClientHeight;
Result.Canvas.Brush := Control.Brush;
Result.Canvas.FillRect(Control.ClientRect);
Result.Canvas.Lock;
try
Control.PaintTo(Result.Canvas.Handle, 0, 0);
finally
Result.Canvas.Unlock;
end;
except
Result.Free;
raise;
end;
end;
{$endif ver90}
end.