我用了Delphi自带的ListBox
使用
sTemp := 'a' + #13#10 + 'b';
ListBox1.Items.Add(sTemp);希望看到的结果是:
----
a
b
----
但实际得到结果是:
----
ab
----请问,各位有没有知道支持一个Item支持多行的ListBox
使用
sTemp := 'a' + #13#10 + 'b';
ListBox1.Items.Add(sTemp);希望看到的结果是:
----
a
b
----
但实际得到结果是:
----
ab
----请问,各位有没有知道支持一个Item支持多行的ListBox
STemp: TStringList;
begin
sTemp := TStringList.Create();
sTemp.Text := 'a' + #13#10 + 'b';
ListBox1.Items.AddStrings(sTemp);
sTemp.Free();
end;
但是,我的意思是,一个Items.Text := 'a' + #13#10 + 'b';
就是说,鼠标选中的那个Items的Text是多行的。
不是说一行字符,一个Item
ListBox1.Items.Text := sTemp;
ListBox1.Items.Text := sTemp;这样加出来的是:'ab'
而不是:
a
b
标题:显示换行的ListBox
说明:如显示聊天记录
日期:2005-02-05
设计:Zswang
支持:[email protected]
//*)//*******begin 修改日志*******//
//2005-02-05 Zswang No.1 新建
//------------------------------------------------------------------------------
//2003-02-07 Zswang No.1 完善 加入自动换行的属性
//*******end 修改日志*******//unit HistoryBox;interfaceuses
Windows, Messages, SysUtils, Classes, Controls, StdCtrls, Graphics, Forms;type
THistoryBox = class(TCustomListBox)
private
FWordWrap: Boolean;
procedure SetWordWrap(const Value: Boolean);
{ private declarations }
protected
{ protected declarations }
procedure DrawItem(Index: Integer; Rect: TRect;
State: TOwnerDrawState); override;
procedure MeasureItem(Index: Integer; var Height: Integer); override;
procedure Resize; override;
public
{ public declarations }
constructor Create(AOwner: TComponent); override;
published
{ published declarations }
property WordWrap: Boolean read FWordWrap write SetWordWrap default True; //2003-02-07 Zswang No.1
property AutoComplete;
property Align;
property Anchors;
property BevelEdges;
property BevelInner;
property BevelKind default bkNone;
property BevelOuter;
property BiDiMode;
property BorderStyle;
property Color;
property Columns;
property Constraints;
property Ctl3D;
property DragCursor;
property DragKind;
property DragMode;
property Enabled;
property ExtendedSelect;
property Font;
property ImeMode;
property ImeName;
property IntegralHeight;
property ItemHeight;
property Items;
property MultiSelect;
property ParentBiDiMode;
property ParentColor;
property ParentCtl3D;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ScrollWidth;
property ShowHint;
property Sorted;
property TabOrder;
property TabStop;
property TabWidth;
property Visible;
property OnClick;
property OnContextPopup;
property OnData;
property OnDataFind;
property OnDataObject;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnDrawItem;
property OnEndDock;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnMeasureItem;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnStartDock;
property OnStartDrag;
end;procedure Register;implementationprocedure Register;
begin
RegisterComponents('Zswang', [THistoryBox]);
end;{ THistoryBox }const
cWordWraps: array[Boolean] of Word = (0, DT_WORDBREAK);constructor THistoryBox.Create(AOwner: TComponent);
begin
inherited;
FWordWrap := True;
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;
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;end.
begin
FHistoryBox := THistoryBox.Create(self);
FHistoryBox.Parent := self;
FHistoryBox.Align := alClient; FCharHeight := FHistoryBox.Canvas.TextHeight('|');
Font.Name := '宋体';
Font.Size := 9; FHistoryBox.Items.Add(
'(2005-01-11 11:13:13) 拉灯(410465996)'#13#10 +
'投我一票吧!'#13#10
);
FHistoryBox.Items.Add(
'(2005-01-11 11:14:56) 拉灯(410465996)'#13#10 +
'那我只好去CSDN灌点水了'#13#10
);
FHistoryBox.Items.Add(
'(2005-02-04 11:12:44) CoolSlob(7975379)'#13#10 +
'明天可以回家了'#13#10
);
FHistoryBox.Items.Add(
'(2005-01-11 11:14:25) 布石(356213398)'#13#10 +
'昨天没有上网吗?'#13#10 +
'看你进天的表现,:)'#13#10
);
FHistoryBox.Items.Add(
'(2005-01-13 17:39:03) Ehomsoft(18270172)'#13#10 +
'在某个月黑风高的晚上'#13#10 +
'我用我的独门武器,刷票机,找了台肉机,把第一页作弊的全刷黑了'#13#10
);
end;