就是将如下代码加到listbox里面的ondrawitem事件中吧,我刚才加了,不行procedure TFrmMain.ListBox1DrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState); var Flags: Longint; Data: String; begin {SetLength(ItemHeightList,Items.Count); SetLength(TotalItemHeight,Items.Count);} Canvas.FillRect(Rect); if Index < Self.ListBox1.Items.Count then begin Canvas.Font := Font; {if odSelected in State then Canvas.Font.Color := clSkyBlue;} 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 := Self.ListBox1.Items[Index]; DrawText(Canvas.Handle, PChar(Data), Length(Data), Rect, Flags); {ItemHeightList[Index] := Rect.Top; TotalItemHeight[Index] := Rect.Bottom - Rect.Top;} end; end;
试过了,还是不行.
原码如下:
With Self.ListBox1.Canvas Do
Begin
theRect := Rect(X, Y, X + TextAddWidth, Y + TextAddHeight);
TextRect(theRect, X, Y, Str);
End;
type
TmyListBox = class(TCustomListBox)
{function GetCurrentItem(): TA;
function GetTotalItemLength(): integer;}
private
//procedure CMFontChanged(var Msg: TMessage);
protected
procedure DrawItem(Index: Integer; Rect: TRect;
State: TOwnerDrawState); override;
public
constructor Create(AOwner: TComponent); override;
published
property Align;
property Anchors;
property BevelEdges; .........
property OnDrawItem;
end;procedure TmyListBox.DrawItem(Index: Integer; Rect: TRect;
State: TOwnerDrawState);
var
Flags: Longint;
Data: String;
begin
{SetLength(ItemHeightList,Items.Count);
SetLength(TotalItemHeight,Items.Count);}
Canvas.FillRect(Rect);
if Index < Items.Count then begin
Canvas.Font := Font;
{if odSelected in State then
Canvas.Font.Color := clSkyBlue;}
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);
{ItemHeightList[Index] := Rect.Top;
TotalItemHeight[Index] := Rect.Bottom - Rect.Top;}
end;
end;
Rect: TRect; State: TOwnerDrawState);
var
Flags: Longint;
Data: String;
begin
{SetLength(ItemHeightList,Items.Count);
SetLength(TotalItemHeight,Items.Count);}
Canvas.FillRect(Rect);
if Index < Self.ListBox1.Items.Count then begin
Canvas.Font := Font;
{if odSelected in State then
Canvas.Font.Color := clSkyBlue;}
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 := Self.ListBox1.Items[Index];
DrawText(Canvas.Handle, PChar(Data), Length(Data), Rect, Flags);
{ItemHeightList[Index] := Rect.Top;
TotalItemHeight[Index] := Rect.Bottom - Rect.Top;}
end;
end;
是不是还需要加些什么才能
procedure TfrmSysSetup.inListBox1DrawItem(Control: TWinControl;
Index: Integer; Rect: TRect; State: TOwnerDrawState);
var
Icon: TICon;
SHFileInfo: TSHFileInfo;
begin
with Control as TListbox do
begin
Canvas.FillRect(Rect);//填充以前画的内容
try
Icon := TIcon.Create;
SHGetFileInfo(PChar(bsSkinListBox1.Items.Strings[Index]),
0,
SHFileInfo,
sizeof(SHFileInfo),
SHGFI_ICON or SHGFI_SMALLICON);//取得当前程序的小图标
Icon.Handle := SHFileInfo.hIcon;
Canvas.Draw(Rect.Left, Rect.Top, Icon);//画出这个图标
icon.Free;
except
end;
// erase
// draw item
//显示当前Item的文字
Canvas.TextOut(18, Rect.Top,
bsSkinListBox1.Items[Index]);// InflateRect (Rect, -1, -1);
//处理状态为激活或选中时的样子
if (odFocused in State) and (odSelected in State) then
Canvas.DrawFocusRect(Rect); // Canvas.ClipRect); //
end;
end;
首先,初始化listbox.canvas
With Self.ListBox1.Canvas Do
Begin
Brush.Color := clHotLight;
Brush.Style := bsSolid;
Font.Size := 22; // TextAddWidth := TextWidth(Copy(Self.ComboBox1.Items[1],1,13));
Font.Style := [fsBold];
Font.Color := Self.LabeledEdit2.Color;
Font.Charset := GB2312_CHARSET; TextAddWidth := TextWidth('0123456789ABC');
TextAddHeight := TextHeight(Self.ComboBox1.Items[1]);
End;
其次 ,写屏 当然这都是写成函数供调用的.
With Self.ListBox1.Canvas Do
Begin
theRect := Rect(X, Y, X + TextAddWidth, Y + TextAddHeight);
TextRect(theRect, X, Y, Str);
End;
Self.DoubleBuffered := True;
加入聊DoubleBuffered了
好像没有效也,老兄.