大家帮帮忙
unit TestCombobox;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls,Grids;
type
TTestCombobox = class;
TMyComboBox=class(TStringGrid)
private
FEdit:TTestCombobox;
protected
procedure CreateParams(var params:TCreateParams);override;
procedure WMSetFocus(var Msg:TWMSetFocus);message WM_SETFOCUS;
public
constructor Create(AOwner:TComponent);override;
{procedure OnSelectCell(Sender: TObject; ACol, ARow: Longint;
var CanSelect: Boolean);}
end;
TTestCombobox = class(TCustomEdit)
private
FMyComboBox:TMyComboBox;
FDowned:Boolean;
procedure ShowCombobox;
procedure HideComboBox;
procedure WM_KeyDown(var Msg:TWMKey);message WM_KEYDOWN;
//procedure OnSelectCell(Sender: TObject; ACol, ARow: Longint;
// var CanSelect: Boolean);
protected
procedure CMCancelMode(var Msg:TCMCancelMode);message CM_CANCELMODE;
procedure WM_LButtonDown(var Msg:TWMMouse);message WM_LBUTTONDOWN;
public
Constructor Create(AOwner:TComponent);override;
destructor Destroy;override;
published
{ Published declarations }
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('XP', [TTestCombobox]);
end;
{ TTestCombobox }
procedure TTestCombobox.WM_KeyDown(var Msg: TWMKey);
begin
case Msg.CharCode of
VK_UP,VK_Down,VK_LBUTTON,VK_Right,VK_Left://你想发送给下拉Grid的按键;
begin
if FDowned then
begin
SendMessage(FMyCombobox.Handle,Msg.Msg,Msg.CharCode,Msg.KeyData);
end
else
Inherited;
//Text:=FMyComboBox.Cells[1,1];
end;
VK_Return:
begin
//Text:='OK'; //FMyComboBox.Cells;
HideComboBox;
end;
else
Inherited;
end;
end;
procedure TTestCombobox.CMCancelMode(var Msg: TCMCancelMode);
begin
inherited;
if FDowned then
begin
HideComboBox;
end;
end;
constructor TTestCombobox.Create(AOwner: TComponent);
Var
i,j : Integer;
begin
inherited Create(AOwner);
FMyComboBox:=TMyComboBox.Create(self);
FMyCombobox.FixedCols := 0;
FMyCombobox.FixedRows := 0;
FDowned:=false;
//*******************//
for i:=0 to 4 do
begin
for j:=0 to 4 do
begin
FMyCombobox.Cells[i,j] := InttoStr(i)+IntToStr(j);
end;
end;
//*******************//
end;
destructor TTestCombobox.Destroy;
begin
FMyComboBox.Free;
FMyComboBox:=nil;
inherited Destroy;
end;
procedure TTestCombobox.HideComboBox;
begin
ShowWindow(FMyCombobox.handle,SW_HIDE);
FDowned:=not FDowned;
end;
procedure TTestCombobox.ShowCombobox;
var
Point:TPoint;
begin
Point.x:=Left;
Point.y:=top+Height;
Point:=Parent.ClienttoScreen(Point);
SetWindowPos(FMyComboBox.Handle,HWND_TOP,Point.x,Point.y,0,0,SWP_NOACTIVATE or SWP_HideWindow or SWP_NOSIZE); ShowWindow(FMyComboBox.Handle,SW_SHOW);
FDowned:=not FDowned;
end;
procedure TTestCombobox.WM_LButtonDown(var Msg: TWMMouse);
begin
if not FDowned then
begin
ShowComboBox;
end
else
begin
HideCombobox;
end;
end;
{ TMyComboBox }
constructor TMyComboBox.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FEdit:=TTestCombobox(AOwner);
ControlStyle:=ControlStyle + [csNoDesignVisible, csReplicatable,
csAcceptsControls];
Width:=134;
Height:=130;
ColCount:=2;
RowCount:=5;
FixedCols:=0;
ScrollBars:=ssNone;
Visible := False;
Parent:=TWinControl(AOwner);
//Parent:=TCustomEdit(TTestCombobox).Parent;
//ScrollBars:=ssNone;
end;
procedure TMyComboBox.CreateParams(var params: TCreateParams);
begin
inherited CreateParams(params);
with Params do
begin
Style:=style or ws_popup;
EXStyle:=EXStyle or WS_EX_CLIENTEDGE or WS_EX_TOOLWINDOW;
end;
end;
procedure TMyComboBox.WMSetFocus(var Msg: TWMSetFocus);
begin
Inherited;
FEdit.SetFocus;
end;
end.