//测试如下代码~~ unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls;type TForm1 = class(TForm) ComboBox1: TComboBox; Timer1: TTimer; procedure Timer1Timer(Sender: TObject); procedure FormCreate(Sender: TObject); procedure ComboBox1DropDown(Sender: TObject); procedure ComboBox1CloseUp(Sender: TObject); private { Private declarations } FHintWindow: THintWindow; FHintIndex: Integer; public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Timer1Timer(Sender: TObject); var vHandle: THandle; vBuffer: array[0..255] of Char; vIndex: Integer; vPoint: TPoint; I: Integer; vHint: string; begin vPoint := Mouse.CursorPos; vHandle := WindowFromPoint(vPoint); GetClassName(vHandle, vBuffer, SizeOf(vBuffer)); if not SameText(vBuffer, 'ComboLBox') then begin if Assigned(FHintWindow) then Application.HideHint; FHintIndex := -1; Exit; end; vIndex := SendMessage(vHandle, LB_GETCURSEL, 0, 0); if FHintIndex = vIndex then Exit; FHintIndex := vIndex; if vIndex < 0 then Exit; vHint := ComboBox1.Items[vIndex]; if ComboBox1.Canvas.TextWidth(vHint) < ComboBox1.Width then begin if Assigned(FHintWindow) then Application.HideHint; Exit; end; for I := 0 to Application.ComponentCount - 1 do if Application.Components[I] is THintWindow then begin FHintWindow := THintWindow(Application.Components[I]); with FHintWindow do begin ActivateHint( Rect( vPoint.X, vPoint.Y + ComboBox1.ItemHeight, vPoint.X + Canvas.TextWidth(vHint) + 4, vPoint.Y + ComboBox1.ItemHeight + Canvas.TextHeight(vHint) + 4 ), vHint ); end; Break; end; end;procedure TForm1.FormCreate(Sender: TObject); begin Timer1.Enabled := False; Timer1.Interval := 500; end;procedure TForm1.ComboBox1DropDown(Sender: TObject); begin Timer1.Enabled := True; end;procedure TForm1.ComboBox1CloseUp(Sender: TObject); begin Timer1.Enabled := False; end;end.
to kl2000() :调整位置是因为下拉框跑出屏幕了to zswangII: 你怎么改名了?是不是当年的zswang兄啊?呵呵,曾经帮我搞定红绿3D画的。 你上面那代码好复杂哦,而且还用了TIMER,其实可以这样取道INDEX procedure TMain.ComboboxDrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState); var pt:TPoint; begin if odFocused in State then begin // Caption := IntToStr(Index); //Index就是你要的 Caption := TComboBox(Control).Items.Strings[index]+'|'; getcursorpos(pt); application.ActivateHint(pt); end; with TComboBox(Control) do begin Canvas.FillRect(Rect); Canvas.TextOut(Rect.Left + 2, Rect.Top, Items[Index]); end; end;这代码还有问题,你看看先,不过怎样把combobox的编辑框也绘制下
procedure TForm1.BitBtn1Click(Sender: TObject); begin sendmessage(DateTimePicker1.handle, WM_LBUTTONDOWN,//点击 1, //不用管他,1就行了。 (DateTimePicker1.Height div 2)*$10000+DateTimePicker1.Width-8); end;
不明白第二个问题什么意思。
这句可以实现你的第二项.
不过你要把你的最大长度计算出来才可以. 放到OnDropDown
事件就可以
我知道能设置宽度,但怎么调整位置啊?
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;type
TForm1 = class(TForm)
ComboBox1: TComboBox;
Timer1: TTimer;
procedure Timer1Timer(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure ComboBox1DropDown(Sender: TObject);
procedure ComboBox1CloseUp(Sender: TObject);
private
{ Private declarations }
FHintWindow: THintWindow;
FHintIndex: Integer;
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Timer1Timer(Sender: TObject);
var
vHandle: THandle;
vBuffer: array[0..255] of Char;
vIndex: Integer;
vPoint: TPoint;
I: Integer;
vHint: string;
begin
vPoint := Mouse.CursorPos;
vHandle := WindowFromPoint(vPoint);
GetClassName(vHandle, vBuffer, SizeOf(vBuffer));
if not SameText(vBuffer, 'ComboLBox') then
begin
if Assigned(FHintWindow) then
Application.HideHint;
FHintIndex := -1;
Exit;
end;
vIndex := SendMessage(vHandle, LB_GETCURSEL, 0, 0);
if FHintIndex = vIndex then Exit;
FHintIndex := vIndex;
if vIndex < 0 then Exit;
vHint := ComboBox1.Items[vIndex];
if ComboBox1.Canvas.TextWidth(vHint) < ComboBox1.Width then
begin
if Assigned(FHintWindow) then Application.HideHint;
Exit;
end;
for I := 0 to Application.ComponentCount - 1 do
if Application.Components[I] is THintWindow then
begin
FHintWindow := THintWindow(Application.Components[I]);
with FHintWindow do
begin
ActivateHint(
Rect(
vPoint.X,
vPoint.Y + ComboBox1.ItemHeight,
vPoint.X + Canvas.TextWidth(vHint) + 4,
vPoint.Y + ComboBox1.ItemHeight + Canvas.TextHeight(vHint) + 4
),
vHint
);
end;
Break;
end;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
Timer1.Enabled := False;
Timer1.Interval := 500;
end;procedure TForm1.ComboBox1DropDown(Sender: TObject);
begin
Timer1.Enabled := True;
end;procedure TForm1.ComboBox1CloseUp(Sender: TObject);
begin
Timer1.Enabled := False;
end;end.
你上面那代码好复杂哦,而且还用了TIMER,其实可以这样取道INDEX
procedure TMain.ComboboxDrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
var
pt:TPoint;
begin
if odFocused in State then
begin
// Caption := IntToStr(Index); //Index就是你要的
Caption := TComboBox(Control).Items.Strings[index]+'|';
getcursorpos(pt);
application.ActivateHint(pt);
end;
with TComboBox(Control) do
begin
Canvas.FillRect(Rect);
Canvas.TextOut(Rect.Left + 2, Rect.Top, Items[Index]);
end;
end;这代码还有问题,你看看先,不过怎样把combobox的编辑框也绘制下
begin
sendmessage(DateTimePicker1.handle,
WM_LBUTTONDOWN,//点击
1, //不用管他,1就行了。
(DateTimePicker1.Height div 2)*$10000+DateTimePicker1.Width-8);
end;