一个符合你要求的HintCombobox:
{------------------------------------------------------------------------------}
{ SyHintComboBox v1.00 }
{------------------------------------------------------------------------------}
{------------------------------------------------------------------------------}
{ For any questions or comments I can be reached at: }
{ EMail : snap_sun@263.net; snap_sun@163.net }
{------------------------------------------------------------------------------}
{ Date last modified: Nov 14, 2000 }
{------------------------------------------------------------------------------}
{ Usage : Install. Then just drop the component on your form. }
{ For the Hints to work, this ComboBox MUST BE OwnerDraw. }
{ No coding is needed other than the usual 'Add.Item' }
{ Other than the Hints coming up, it works the same as any other }
{ TComboBox. }
{ You also have the option of having the hint stay if the item that }
{ had the last hint goes out of view (The ShowHintWhenHidden property) }
{------------------------------------------------------------------------------}
{ This component is freeware. Do with it what ever you want. but }
{ I will not be held liable for any damage it does (you have the source). }
{------------------------------------------------------------------------------}
unit SyHintComboBox;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, DsgnIntf, stdctrls;
type
TSyHintComboBoxAbout = class(TPropertyEditor)
public
procedure Edit; override;
function GetAttributes: TPropertyAttributes; override;
function GetValue: string; override;
end;
TSyHintComboBox = class(TComboBox)
private
FAbout: TSyHintComboBoxAbout;
FHintWin : THintWindow;
FHintStr : String;
FHintWidth : Integer;
FCmbWidth : Integer;
FTopIndex : Integer;
FBottomIndex : Integer;
CurHintIndex : Integer;
FHintWhenHidden : Boolean;
function GetTopIndex: Integer;
function GetBottomIndex : Integer;
public
procedure DrawItem(Index: Integer; Rect: TRect; State: TOwnerDrawState); override;
procedure Loaded; override;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property TopIndex: Integer read GetTopIndex write FTopIndex;
property BottomIndex : Integer read FBottomIndex write FBottomIndex;
property CurrentIndex : Integer read CurHintIndex write CurHintIndex;
procedure DoExit; override;
Procedure Click; override;
published
property About: TSyHintComboBoxAbout read FAbout write FAbout;
property ShowHintWhenHidden : Boolean read FHintWhenHidden write FHintWhenHidden default false;
end;
procedure Register;
implementation
procedure TSyHintComboBoxAbout.Edit;
begin
Application.MessageBox('TSyHintComboBox component v1.00 for Delphi. (C) 2000 '+
'Sun Yu. This component is freeware.',
'About TSyHintComboBox Component', MB_OK + MB_ICONINFORMATION);
end;
function TSyHintComboBoxAbout.GetAttributes: TPropertyAttributes;
begin
Result:= [paMultiSelect, paDialog, paReadOnly];
end;
function TSyHintComboBoxAbout.GetValue: string;
begin
Result:= '(about)';
end;
constructor TSyHintComboBox.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FHintStr := '';
FHintWidth := 0;
Style := csOwnerDrawFixed;
end;
procedure TSyHintComboBox.Loaded;
begin
FCmbWidth := Width - GetSystemMetrics(SM_CXHTHUMB);
end;
function TSyHintComboBox.GetTopIndex: Integer;
begin
Result := SendMessage(Handle, CB_GETTOPINDEX, 0, 0);
end;
function TSyHintComboBox.GetBottomIndex : Integer;
begin
Result := GetTopIndex + DropDownCount;
end;
procedure TSyHintComboBox.DrawItem(Index: Integer; Rect: TRect; State: TOwnerDrawState);
Var
CurHint : String;
CmbPoint : TPoint;
CmbRect : TRect;
CmbHeight : Integer;
CmbDropHeight : Integer;
begin
Inherited;
FHintStr := Items.Strings[Index];
Canvas.FillRect(Rect);
Canvas.TextOut(Rect.Left, Rect.Top, FHintStr);
If (odSelected In State) Then
begin
If CurHint <> FHintStr Then
begin
CurHint := FHintStr;
If Assigned(FHintWin) Then
FHintWin.ReleaseHandle;
If DroppedDown Then
begin
FTopIndex := GetTopIndex;
FBottomIndex := GetBottomIndex;
CurHintIndex := Items.IndexOf(CurHint);
If (CurHintIndex < FBottomIndex) And (CurHintIndex >= FTopIndex)
Or ShowHintWhenHidden Then
begin
FHintWidth := Canvas.TextWidth(FHintStr);
If FHintWidth > FCmbWidth Then
begin
FHintWin := THintWindow.Create(Self);
FHintWin.Color := clInfoBk;
CmbHeight := Canvas.TextHeight(FHintStr);
CmbDropHeight := CmbHeight * DropDownCount;
cmbPoint := ClientOrigin;
CmbRect.Left := cmbPoint.x + (FCmbWidth div 2);
CmbRect.top := cmbPoint.y + ItemHeight + Rect.Top + 20;
CmbRect.Bottom := CmbRect.Top + CmbHeight + 2;
CmbRect.Right := CmbRect.Left + FHintWidth + 20;
FHintWin.ActivateHint(CmbRect, FHintStr);
end;
end;
end;
end;
end;
end;
Procedure TSyHintComboBox.Click;
begin
if Assigned(FHintWin) then
FHintWin.ReleaseHandle;
end;
Procedure TSyHintComboBox.DoExit;
begin
if Assigned(FHintWin) then
FHintWin.ReleaseHandle;
end;
destructor TSyHintComboBox.Destroy;
begin
If Assigned(FHintWin) Then
FHintWin.ReleaseHandle;
inherited Destroy;
end;
procedure Register;
begin
RegisterComponents('SyGroup', [TSyHintComboBox]);
RegisterPropertyEditor(TypeInfo(TSyHintComboBoxAbout), TSyHintComboBox, 'ABOUT', TSyHintComboBoxAbout);
end;
end.