This repository has been archived by the owner on Nov 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
EvilWorks.Vcl.GenericControl.pas
264 lines (230 loc) · 6.15 KB
/
EvilWorks.Vcl.GenericControl.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
//
// EvilLibrary by Vedran Vuk 2010-2012
//
// Name: EvilWorks.VCL.GenericControl
// Description: Generic control exposing canvas and OnPaint event.
// File last change date: September 2nd. 2012
// File version: 0.0.9
// Licence: Free.
//
{ TODO: Fix Ctrl3D and border props. }
unit EvilWorks.VCL.GenericControl;
interface
uses
WinApi.Windows,
WinApi.Messages,
System.Classes,
System.SysUtils,
Vcl.Controls,
Vcl.Graphics;
type
{ Forward declarations }
TGenericControl = class;
{ TMouseState }
TMouseState = (msOut, msOver, msDown);
{ Events }
TOnPaint = procedure(aSender: TGenericControl; const aCanvas: TCanvas; const aRect: TRect; const aMouseState: TMouseState) of object;
{ TGenericControl }
TGenericControl = class(TCustomControl)
private
FInvalidating : boolean;
FTransparent : boolean;
FMouseTriggersPaint: boolean;
FOnPaint : TOnPaint;
procedure SetTransparent(const Value: boolean);
protected
FMouseState: TMouseState;
procedure Paint; override;
procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
procedure CMMouseEnter(var Message: TMessage); message CM_MOUSEENTER;
procedure CMMouseLeave(var Message: TMessage); message CM_MOUSELEAVE;
procedure WMEraseBkgnd(var Message: TWMEraseBkgnd); message WM_ERASEBKGND;
procedure CMColorchanged(var Message: TMessage); message CM_COLORCHANGED;
procedure CMFontchanged(var Message: TMessage); message CM_FONTCHANGED;
public
constructor Create(aOwner: TComponent); override;
procedure Assign(aSource: TPersistent); override;
procedure Invalidate; override;
property Canvas;
published
property Align;
property Anchors;
property AutoSize;
property BevelEdges;
property BevelInner;
property BevelKind;
property BevelOuter;
property BevelWidth;
property BiDiMode;
property BorderWidth;
property Color;
property Constraints;
property Ctl3D;
property DockSite;
property DoubleBuffered;
property DragCursor;
property DragKind;
property DragMode;
property Enabled;
property Font;
property Padding;
property ParentBackground;
property ParentBiDiMode;
property ParentColor;
property ParentCtl3D;
property ParentDoubleBuffered;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ShowHint;
property TabOrder;
property TabStop;
property Touch;
property UseDockManager default True;
property Visible;
property MouseTriggersPaint: boolean read FMouseTriggersPaint write FMouseTriggersPaint default True;
property Transparent : boolean read FTransparent write SetTransparent default False;
property OnAlignInsertBefore;
property OnAlignPosition;
property OnCanResize;
property OnClick;
property OnConstrainedResize;
property OnContextPopup;
property OnDockDrop;
property OnDockOver;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDock;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnGesture;
property OnGetSiteInfo;
property OnMouseActivate;
property OnMouseDown;
property OnMouseEnter;
property OnMouseLeave;
property OnMouseMove;
property OnMouseUp;
property OnResize;
property OnStartDock;
property OnStartDrag;
property OnUnDock;
property OnPaint: TOnPaint read FOnPaint write FOnPaint;
end;
implementation
{ TGenericControl }
constructor TGenericControl.Create(aOwner: TComponent);
begin
inherited;
FMouseTriggersPaint := True;
FMouseState := msOut;
ControlStyle := ControlStyle + [csAcceptsControls];
end;
procedure TGenericControl.Assign(aSource: TPersistent);
begin
inherited;
if (aSource is TGenericControl) then
begin
MouseTriggersPaint := TGenericControl(aSource).MouseTriggersPaint;
Transparent := TGenericControl(aSource).Transparent;
OnPaint := TGenericControl(aSource).OnPaint;
end;
end;
procedure TGenericControl.Paint;
begin
if (csDesigning in ComponentState) then
begin
Canvas.FillRect(ClientRect);
Exit;
end;
if (Assigned(FOnPaint)) then
begin
FOnPaint(Self, Canvas, ClientRect, FMouseState);
Exit;
end;
end;
procedure TGenericControl.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
inherited;
FMouseState := msDown;
if (FMouseTriggersPaint) then
Invalidate;
end;
procedure TGenericControl.MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
inherited;
FMouseState := msOver;
if (FMouseTriggersPaint) then
Invalidate;
end;
procedure TGenericControl.Invalidate;
var
i: integer;
b: boolean;
begin
if (FInvalidating = False) then
begin
FInvalidating := True;
Parent.Invalidate;
for i := 0 to Self.ControlCount - 1 do
Self.Controls[i].Invalidate;
FInvalidating := False;
end;
inherited Invalidate;
end;
procedure TGenericControl.SetTransparent(const Value: boolean);
begin
if (FTransparent = Value) then
Exit;
FTransparent := Value;
if (FTransparent) then
begin
SetWindowLong(Handle, GWL_EXSTYLE, GetWindowLong(Handle, GWL_EXSTYLE) or WS_EX_TRANSPARENT);
SetBkMode(Canvas.Handle, WinApi.Windows.Transparent);
ControlStyle := ControlStyle - [csOpaque];
end
else
begin
SetWindowLong(Handle, GWL_EXSTYLE, GetWindowLong(Handle, GWL_EXSTYLE) and not WS_EX_TRANSPARENT);
SetBkMode(Canvas.Handle, WinApi.Windows.OPAQUE);
ControlStyle := ControlStyle + [csOpaque];
end;
Invalidate;
end;
procedure TGenericControl.CMColorchanged(var Message: TMessage);
begin
Canvas.Brush.Color := Color;
Invalidate;
end;
procedure TGenericControl.CMFontchanged(var Message: TMessage);
begin
Canvas.Font.Assign(Font);
end;
procedure TGenericControl.CMMouseEnter(var Message: TMessage);
begin
inherited;
FMouseState := msOver;
if (FMouseTriggersPaint) then
Invalidate;
end;
procedure TGenericControl.CMMouseLeave(var Message: TMessage);
begin
inherited;
FMouseState := msOut;
if (FMouseTriggersPaint) then
Invalidate;
end;
procedure TGenericControl.WMEraseBkgnd(var Message: TWMEraseBkgnd);
begin
if (FTransparent) then
begin
SetBkMode(message.DC, WinApi.Windows.TRANSPARENT);
message.Result := 0;
end
else
inherited;
end;
end.