forked from JBontes/Life32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RightEdit.pas
183 lines (160 loc) · 4.07 KB
/
RightEdit.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
unit RightEdit;
interface
uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls, Menus, DsgnIntF;
type
TMyCustomEdit = class (TCustomEdit)
private
FAlignment: TAlignment;
FOldAlignment : TAlignment;
FTextMargin : integer;
function CalcTextMargin : integer;
procedure CMEnter(var Message: TCMEnter); message CM_ENTER;
procedure CMExit(var Message: TCMExit); message CM_EXIT;
procedure WMPaint(var Message: TWMPaint); message WM_PAINT;
procedure SetAlignment(Value: TAlignment);
protected
property Alignment: TAlignment read FAlignment write SetAlignment default taLeftJustify;
public
constructor Create(AOwner: TComponent); override;
end;
TRightEdit = class (TMyCustomEdit)
published
property Alignment;
property AutoSelect;
property AutoSize;
property BorderStyle;
property Color;
property Ctl3D;
property DragCursor;
property DragMode;
property Enabled;
property Font;
property HideSelection;
property ParentColor;
property ParentCtl3D;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ReadOnly;
property ShowHint;
property TabOrder;
property Text;
property Visible;
property OnChange;
property OnClick;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
end;
procedure Register;
implementation
constructor TMyCustomEdit.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FTextMargin:= CalcTextMargin;
end;
function TMyCustomEdit.CalcTextMargin : integer;
{
This was borrowed from TDBEdit. It calculates a pixel
offset from the edge of the control to the text
(a margin) used in the paint routine.
}
var
DC: HDC;
SaveFont: HFont;
I: Integer;
SysMetrics, Metrics: TTextMetric;
begin
DC:= GetDC(0);
GetTextMetrics(DC, SysMetrics);
SaveFont:= SelectObject(DC, Font.Handle);
GetTextMetrics(DC, Metrics);
SelectObject(DC, SaveFont);
ReleaseDC(0, DC);
I:= SysMetrics.tmHeight;
if I > Metrics.tmHeight then I:= Metrics.tmHeight;
Result:= I div 4;
end;
procedure TMyCustomEdit.SetAlignment(Value: TAlignment);
begin
if FAlignment <> Value then begin
FAlignment:= Value;
Invalidate;
end;
end;
procedure TMyCustomEdit.CMEnter(var Message: TCMEnter);
begin
inherited;
FOldAlignment:= FAlignment;
Alignment:= taLeftJustify;
end;
procedure TMyCustomEdit.CMExit(var Message: TCMExit);
begin
inherited;
Alignment:= FOldAlignment;
end;
procedure TMyCustomEdit.WMPaint(var Message: TWMPaint);
{borrowed from TDBEdit}
{paints the text in the appropriate position}
var
Width, Indent, Left: Integer;
R: TRect;
DC: HDC;
PS: TPaintStruct;
S: string;
Canvas: TControlCanvas;
begin
{let the existing code handle left justify}
if (FAlignment = taLeftJustify) then begin
inherited;
Exit;
end;
Canvas:= TControlCanvas.Create;
try
Canvas.Control:= Self;
DC:= Message.DC;
if DC = 0 then DC:= BeginPaint(Handle, PS);
Canvas.Handle:= DC;
Canvas.Font:= Font;
with Canvas do begin
R:= ClientRect;
if (BorderStyle = bsSingle) then begin
Brush.Color:= clWindowFrame;
FrameRect(R);
InflateRect(R, -1, -1);
end;
Brush.Color:= Color;
Pen.Color:= Color;
S:= Text;
Width:= TextWidth(S);
if BorderStyle = bsNone then Indent:= 0
else Indent:= FTextMargin;
if FAlignment = taRightJustify then Left:= R.Right - Width - Indent
else Left:= (R.Left + R.Right - Width) div 2;
//TextRect(R, Left, Indent, S);
TextOut(Left,Indent,S);
R.Right:= Left;
FillRect(R);
end;
finally
Canvas.Handle:= 0;
if Message.DC = 0 then EndPaint(Handle, PS);
end; {try}
end;
procedure Register;
begin
RegisterComponents('Johan', [TRightEdit]);
end;
end.
{ This code came from Lloyd's help file! }