大家帮帮忙

s_delhpi 2003-10-18 01:36:18
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.
...全文
45 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
hkbarton 2003-10-19
  • 打赏
  • 举报
回复
处理wm_KeyDown和WM_LBUTTONDBLCLK消息就可以了
s_delhpi 2003-10-19
  • 打赏
  • 举报
回复
怎么定义一个回车事件,使Stringgrid中选中的cell值赋给Edit.怎么定义一个鼠标双击事件,使Stringgrid中选中的cell值赋给Edit
hkbarton 2003-10-18
  • 打赏
  • 举报
回复
上面的代码有什么问题吗?
s_delhpi 2003-10-18
  • 打赏
  • 举报
回复
大家尽量想想办法好吗?谢谢了
s_delhpi 2003-10-18
  • 打赏
  • 举报
回复
一开始显示的只有Edit,其实这个是组合控件,要完成Combobox的功能,当Edit获得焦点的时候,我显示Stringgrid,然后我可以用鼠标或者上下左右键选取其中的一个Cell,选好以后我按回车或鼠标双击把Cell的值赋给Edit,同时StringGrid Hide,然后把焦点转到下一个控件。。。

大家能不能看看怎么改代码,实现这样的功能

5,379

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧