你是指过长的时候显示一个hint? 嘿嘿,看看下面这个效果
界面上扔个combobox,所有属性默认
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
ComboBox1: TComboBox;
procedure ComboBox1DrawItem(Control: TWinControl;
Index: Integer;
Rect: TRect;
State: TOwnerDrawState);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
procedure ComboBox1CloseUp(Sender: TObject);
private
{ Private declarations }
_hint:THintWindow;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.ComboBox1DrawItem(Control: TWinControl;
Index: Integer;
Rect: TRect;
State: TOwnerDrawState);
var
arect:trect;
tpt:tpoint;
strwidth:integer;
begin
with combobox1do
begin
Canvas.FillRect(rect);
Canvas.TextOut(rect.Left+2,rect.Top,Items[index]);
strwidth:=canvas.TextWidth(items[index]);
if (strwidth<Width-4) or (odComboBoxEdit in state) then
_hint.ReleaseHandle
else
if (odSelected in state) then
begin
tpt:=clienttoscreen(point(rect.Left,rect.Top+height));
arect.Left:=tpt.X;
arect.Top:=tpt.Y+rect.Bottom-rect.Top;
arect.Right:=arect.Left+strwidth+4;
arect.Bottom:=arect.Top+rect.Bottom-rect.Top;
_hint.ActivateHint(arect,items[index])
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
i,j:byte;
str:string;
begin
_hint:=THintWindow.Create(self);
combobox1.Items.Clear;
for i:=0 to 9do
begin
str:='';
setlength(str,31);
for j:=1 to 30do
str[j]:=inttostr(i)[1];
str[31]:='A';
{测试数据,检测是否显示全最后一个字符}
combobox1.Items.Add(str);
end;
combobox1.Items.Add('短字符');
combobox1.Style:= csOwnerDrawFixed;
end;
procedure TForm1.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
_hint.Free;
end;
procedure TForm1.ComboBox1CloseUp(Sender: TObject);
begin
_hint.ReleaseHandle;
end;
end.