只想实现ListBox自动换行,刚刚在这上面搜出了一段控件源码,但我加载报错,该怎么弄?(100分)

  • 主题发起人 主题发起人 del520
  • 开始时间 开始时间
D

del520

Unregistered / Unconfirmed
GUEST, unregistred user!
我想实现listBox能自动换行(注,不是要水平滚动条,是换行),刚才在论坛上搜索,
有一段源码,但装成控件报错,不知是哪儿问题,我还从来没自已做过控件,不懂,但现在
急着用。
源码贴在下面,如果有类似的控件下载下行。

---------------
unit WrapListBox;

interface

uses
Windows, Messages, SysUtils, Classes,
Graphics, Controls, StdCtrls;

type
TWrapListBox = class(TCustomListBox)
private
procedure CMFontChanged(var Msg: TMessage); message CM_FONTCHANGED;
protected
procedure DrawItem(Index: Integer; Rect: TRect;
State: TOwnerDrawState); override;
procedure MeasureItem(Index: Integer; var Height: Integer); override;
public
constructor Create(AOwner: TComponent); override;
published
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 OnEndDock;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnStartDock;
property OnStartDrag;
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('Samples', [TWrapListBox]);
end;

{ TWrapListBox }

procedure TWrapListBox.CMFontChanged(var Msg: TMessage);
begin
Invalidate;
end;

constructor TWrapListBox.Create(AOwner: TComponent);
begin
inherited;
Style := lbOwnerDrawVariable;
end;

procedure TWrapListBox.DrawItem(Index: Integer; Rect: TRect;
State: TOwnerDrawState);
var
Flags: Longint;
Data: String;
begin
Canvas.FillRect(Rect);
if Index < Count then begin
Canvas.Font := Font;
if odSelected in State then
Canvas.Font.Color := clHighlightText;
Flags := DrawTextBiDiModeFlags(
DT_WORDBREAK or DT_VCENTER or DT_NOPREFIX or DT_EDITCONTROL);
if not UseRightToLeftAlignment then
Inc(Rect.Left, 2)
else
Dec(Rect.Right, 2);

Data := Items[Index];
DrawText(Canvas.Handle, PChar(Data), Length(Data), Rect, Flags);
end;
end;

procedure TWrapListBox.MeasureItem(Index: Integer; var Height: Integer);
var
Rt: TRect;
Data: String;
begin
if Index < Count then begin
Canvas.Font := Font;
Data := Items[Index];
Rt := Rect(0, 0, ClientWidth, 600);
DrawText(Canvas.Handle, PChar(Data), Length(Data), Rt,
DT_WORDBREAK or DT_VCENTER or DT_NOPREFIX or DT_CALCRECT or DT_EDITCONTROL);
Height := Rt.Bottom - Rt.Top;
end;
end;

end.
 
我改了一下贴在下面!!!!!希望你能Pass,我的是Pass啦!!!! 呵呵~~~~~

unit WrapListBox;

interface

uses
Windows, Messages, SysUtils, Classes,
Graphics, Controls, StdCtrls;

type
TWrapListBox = class(TCustomListBox)
private
procedure CMFontChanged(var Msg: TMessage); message CM_FONTCHANGED;
protected
procedure DrawItem(Index: Integer; Rect: TRect;
State: TOwnerDrawState); override;
procedure MeasureItem(Index: Integer; var Height: Integer); override;
public
constructor Create(AOwner: TComponent); override;
published
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 ShowHint;
property Sorted;
property TabOrder;
property TabStop;
property TabWidth;
property Visible;

property OnClick;
property OnContextPopup;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDock;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnStartDock;
property OnStartDrag;
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('Netsoft', [TWrapListBox]);
end;

{ TWrapListBox }

procedure TWrapListBox.CMFontChanged(var Msg: TMessage);
begin
Invalidate;
end;

constructor TWrapListBox.Create(AOwner: TComponent);
begin
inherited;
Style := lbOwnerDrawVariable;
end;

procedure TWrapListBox.DrawItem(Index: Integer; Rect: TRect;
State: TOwnerDrawState);
var
Flags: Longint;
Data: String;
begin
Canvas.FillRect(Rect);
if Index < Items.Count then begin
Canvas.Font := Font;
if odSelected in State then
Canvas.Font.Color := clHighlightText;
Flags := DrawTextBiDiModeFlags(
DT_WORDBREAK or DT_VCENTER or DT_NOPREFIX or DT_EDITCONTROL);
if not UseRightToLeftAlignment then
Inc(Rect.Left, 2)
else
Dec(Rect.Right, 2);

Data := Items[Index];
DrawText(Canvas.Handle, PChar(Data), Length(Data), Rect, Flags);
end;
end;

procedure TWrapListBox.MeasureItem(Index: Integer; var Height: Integer);
var
Rt: TRect;
Data: String;
begin
if Index < Items.Count then begin
Canvas.Font := Font;
Data := Items[Index];
Rt := Rect(0, 0, ClientWidth, 600);
DrawText(Canvas.Handle, PChar(Data), Length(Data), Rt,
DT_WORDBREAK or DT_VCENTER or DT_NOPREFIX or DT_CALCRECT or DT_EDITCONTROL);
Height := Rt.Bottom - Rt.Top;
end;
end;

end.
 
谢谢,但我还是没弄上去,你是怎么弄的?

我直接在在 Component -> Install Component 这里安装。

报错: Extpected an indentifier but the end of the file found.
 
接受答案了.
 

Similar threads

I
回复
0
查看
582
import
I
I
回复
0
查看
3K
import
I
I
回复
0
查看
516
import
I
后退
顶部