procedure Register;
begin
RegisterComponents('Zswang', [THistoryBox]);
end;
{ THistoryBox }
const
cWordWraps: array[Boolean] of Word = (0, DT_WORDBREAK);
constructor THistoryBox.Create(AOwner: TComponent);
begin
inherited;
Style := lbOwnerDrawVariable;
end;
procedure THistoryBox.DrawItem(Index: Integer; Rect: TRect;
State: TOwnerDrawState);
var
vRect: TRect;
S: string;
begin
inherited;
S := Items[Index];
Canvas.FillRect(Rect);
vRect := Rect;
if Odd(Index) then
Canvas.Font.Color := clRed
else Canvas.Font.Color := clBlue;
DrawText(Canvas.Handle, PChar(S), Length(S), vRect,
DT_LEFT or cWordWraps[FWordWrap]);
end;
procedure THistoryBox.MeasureItem(Index: Integer; var Height: Integer);
var
vRect: TRect;
S: string;
begin
inherited;
S := Items[Index];
vRect := ClientRect;
DrawText(Canvas.Handle, PChar(S), Length(S), vRect,
DT_LEFT or cWordWraps[FWordWrap] or DT_CALCRECT);
Height := vRect.Bottom - vRect.Top;
TForm(Parent).Caption := Format('%.6f', [Now]);
end;
procedure THistoryBox.Resize;
var
I: Integer;
begin
inherited;
if not FWordWrap then Exit;
Items.BeginUpdate;
for I := 0 to Items.Count - 1 do Items[I] := Items[I];
Items.EndUpdate;
end;
procedure THistoryBox.SetWordWrap(const Value: Boolean);
begin
if FWordWrap = Value then Exit;
FWordWrap := Value;
Resize;
end;
procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
var
I: Integer;
begin
with TListBox(Control) do
begin
Canvas.FillRect(Rect);
with TStringList.Create do try
Text := Items[Index];
Canvas.Font.Color := clRed;
Canvas.TextOut(Rect.Left, Rect.Top + FCharHeight * 0, Strings[0]);
Canvas.Font.Color := clBlue;
for I := 1 to Pred(Count) do
Canvas.TextOut(Rect.Left, Rect.Top + FCharHeight * I, Strings[I]);
finally
Free;
end;
end;
end;
procedure TForm1.ListBox1MeasureItem(Control: TWinControl; Index: Integer;
var Height: Integer);
begin
Height := (SubStrCount(TrimRight(TListBox(Control).Items[Index]), #13#10) + 1) *
FCharHeight + 1;
end;