[原]combobox和listbox的文字可以右对齐显示

  • 主题发起人 SUNSTONE的Delphi笔记
  • 开始时间
S

SUNSTONE的Delphi笔记

Unregistered / Unconfirmed
GUEST, unregistred user!
0zDUsfP2YFUJNpwIUvuEC0V-A2SAz3IG.jpeg

combobox和listbox的文字可以右对齐显示么?答案是可以的,但是必须使用ComboBox1DrawItem,自行画内容。

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
ComboBox1: TComboBox;
procedure FormCreate(Sender: TObject);
procedure ComboBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
ComboBox1.Style := csOwnerDrawFixed;
end;

procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
var
x: Integer;
txt: string;
begin
with ComboBox1 do
begin
Canvas.FillRect(Rect); //将combobox用现在的brush填充颜色
txt := Items[Index];
x := Rect.Right - Canvas.TextWidth(txt) - 4;
Canvas.TextOut(x, Rect.Top, txt);
end;
end;

end.

作者:sunstone 发表于2009/10/22 8:06:00 原文链接
阅读:1850 评论:0 查看评论

查看更多...
 
最后编辑:

Similar threads

S
回复
0
查看
681
SUNSTONE的Delphi笔记
S
I
回复
0
查看
646
import
I
顶部