unit TestCombobox;interfaceuses
 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;implementationprocedure 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.  

解决方案 »

  1.   

    一开始显示的只有Edit,其实这个是组合控件,要完成Combobox的功能,当Edit获得焦点的时候,我显示Stringgrid,然后我可以用鼠标或者上下左右键选取其中的一个Cell,选好以后我按回车或鼠标双击把Cell的值赋给Edit,同时StringGrid Hide,然后把焦点转到下一个控件大家能不能看看怎么改代码,实现这样的功能
      

  2.   

    怎么定义一个回车事件,使Stringgrid中选中的cell值赋给Edit.怎么定义一个鼠标双击事件,使Stringgrid中选中的cell值赋给Edit
      

  3.   

    处理wm_KeyDown和WM_LBUTTONDBLCLK消息就可以了