-
Notifications
You must be signed in to change notification settings - Fork 2
/
GPWinControl.pas
228 lines (193 loc) · 5.77 KB
/
GPWinControl.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
{*****************************************************************************}
{ }
{ GDI + Controls }
{ http://lummie.co.uk }
{ Version: 1.0.3 }
{ }
{ Copyright (c) 2005 Matt harrison (http://www.lummie.co.uk) }
{ }
{*****************************************************************************}
unit GPWinControl;
interface
uses
SysUtils, Classes, windows, Controls, messages, GDIPAPI, GDIPOBJ, GDIPUTIL, graphics;
type
TGDIPPaint = procedure (Canvas : TGPGraphics) of object;
TGDIPWinControl = class(TWinControl)
private
{ Private declarations }
FCanvas : TGPGraphics;
FBuffer : TGPBitmap;
FTransparent: boolean;
function GetCanvas: TGPGraphics;
procedure SetTransparent(const Value: boolean);
protected
{ Protected declarations }
procedure AllocateCanvas; virtual;
procedure DeAllocateCanvas; virtual;
procedure WMPaint(var Message: TWMPaint); message WM_PAINT;
procedure CreateWnd; override;
procedure RenderChecked(Canvas : TGPGraphics; FGColor, BGColor: TGPColor);
procedure Paint(Canvas : TGPGraphics); virtual;
procedure WMEraseBkgnd(var Message: TWMEraseBkgnd); message WM_ERASEBKGND;
procedure WMWindowPosChanging(var Message: TWMWindowPosChanging); message WM_WINDOWPOSCHANGING;
property Canvas : TGPGraphics read GetCanvas;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property Transparent : boolean read FTransparent write SetTransparent;
published
{ Published declarations }
end;
TGDIPPaintEvent = procedure(Canvas : TGPGraphics) of object;
TGDIPCanvas = class(TGDIPWinControl)
private
FOnPaint: TGDIPPaintEvent;
protected
procedure Paint(Canvas : TGPGraphics); override;
public
constructor Create(AOwner: TComponent); override;
published
property OnPaint : TGDIPPaintEvent read FOnPaint write FOnPaint;
property Transparent;
property Align;
property Anchors;
property Canvas;
property DragKind;
property DragCursor;
property DragMode;
property MouseCapture;
property ParentShowHint;
property PopupMenu;
property OnContextPopup;
property OnClick;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDock;
property OnEndDrag;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnMouseWheel;
property OnMouseWheelDown;
property OnMouseWheelUp;
property OnResize;
property OnStartDock;
property OnStartDrag;
end;
implementation
uses
GDIPExtensions;
{ TGDIPWinControl }
procedure TGDIPWinControl.AllocateCanvas;
var
recreateBuffer : boolean;
begin
recreateBuffer := false;
if not assigned(FCanvas) then recreateBuffer := true;
if not assigned(FBuffer) then
recreateBuffer := true
else
begin
if cardinal(width) <> FBuffer.GetWidth then recreateBuffer := true;
if cardinal(height) <> FBuffer.GetHeight then recreateBuffer := true;
end;
if recreateBuffer then
begin
DeAllocateCanvas;
if FTransparent then
FBuffer := TGPBitmap.Create(width,height,PixelFormat32bppARGB)
else
FBuffer := TGPBitmap.Create(width,height,PixelFormat32bppRGB);
FCanvas := TGPGraphics.Create(FBuffer);
end;
end;
constructor TGDIPWinControl.Create(AOwner: TComponent);
begin
inherited;
FCanvas := nil;
DoubleBuffered := true;
controlstyle := controlstyle - [csOpaque] + [csParentBackground];
color := clnavy;
end;
procedure TGDIPWinControl.CreateWnd;
begin
inherited;
SetWindowLong(Parent.Handle, GWL_STYLE, GetWindowLong(Parent.Handle, GWL_STYLE) and not WS_CLIPCHILDREN);
end;
procedure TGDIPWinControl.DeAllocateCanvas;
begin
FreeAndNil(FCanvas);
FreeAndNil(FBuffer);
end;
destructor TGDIPWinControl.Destroy;
begin
DeAllocateCanvas;
inherited;
end;
function TGDIPWinControl.GetCanvas: TGPGraphics;
begin
result := FCanvas;
end;
procedure TGDIPWinControl.Paint(Canvas: TGPGraphics);
begin
RenderChecked(canvas,MakeColor($FF,255,255,255),MakeColor($FF,212,212,212));
end;
procedure TGDIPWinControl.RenderChecked(Canvas : TGPGraphics; FGColor, BGColor: TGPColor);
var
ABrush : TGPHatchBrush;
begin
ABrush := TGPHatchBrush.create(HatchStyleLargeCheckerBoard,FGColor,bgColor) ;
Canvas.FillRectangle(ABrush,makeRect(Clientrect));
ABrush.free;
end;
procedure TGDIPWinControl.SetTransparent(const Value: boolean);
begin
FTransparent := Value;
end;
procedure TGDIPWinControl.WMEraseBkgnd(var Message: TWMEraseBkgnd);
begin
// do not change
end;
procedure TGDIPWinControl.WMPaint(var Message: TWMPaint);
var
ps: PAINTSTRUCT;
gr : TGPGraphics;
r : TRect;
begin
AllocateCanvas;
BeginPaint(handle,ps);
r := Boundsrect;
// draw checks
Paint(FCanvas);
// blit to window
gr := TGPGraphics.create(Handle,false);
gr.DrawImage(FBuffer,0,0,FBuffer.GetWidth,FBuffer.GetHeight);
gr.free;
endpaint(handle,ps);
message.result := 0;
end;
procedure TGDIPWinControl.WMWindowPosChanging(
var Message: TWMWindowPosChanging);
var
Rect : TRect;
begin
rect := clientrect;
invalidaterect(handle,@rect,false);
end;
{ TGDIPCanvas }
constructor TGDIPCanvas.Create(AOwner: TComponent);
begin
inherited;
FOnPaint := nil;
FTransparent := true;
end;
procedure TGDIPCanvas.Paint(Canvas: TGPGraphics);
begin
//if csDesigning in componentstate then
inherited;
if assigned(FOnPaint) then FOnPaint(Canvas);
end;
end.