这里有一个很急的问题,愿100分相送。(100分)

  • 主题发起人 主题发起人 billxu
  • 开始时间 开始时间
B

billxu

Unregistered / Unconfirmed
GUEST, unregistred user!
我用TComboBox显示选择的列表时,因为选项字符串太长,
后面的就看不见了,能不能实现象在TTreeView中那样,
看不见全部字符时有一个自动提示Hint显示内容。
 
告诉我是用windows的哪个消息实现的也行。
 
一个符合你要求的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.
 
是不是可以动态的改变HINT呢?
 
to 腾龙:
可我怎么知道现在鼠标移到了哪一行呢
 
combobox1.items[combobox1.itemindex]
 
在DROPDOWN事件中发CB_SETDROPPEDWIDTH 消息让宽度自动适应
 
to hthugm:
大侠能不能说得详细点!!
如何让宽度自动适应?
在下谢过了。
 
procedure TForm1.ComboBox1DropDown(Sender: TObject);
Begin
sendmessage(combobox1.Handle,CB_SETDROPPEDWIDTH,100,0);
//用你認為最大可能的長度替換100
end;
 
多人接受答案了。
 
后退
顶部