由于要用到像DevExpress中TdxPopupEdit的功能,但DEV的控件包太大了,没法单独用那个控件,就自己写一个。刚开始就碰到大问题!
我用一个TEdit和一个TSpeedButton模拟下拉框,点击Button后出现下拉框。但是如果下拉框中有控件,比如Edit,则如果下拉框中的Edit一旦获得焦点,程序中其他控件就无法再获得焦点,程序被锁S。如下代码可以模拟这种情况,新建一个工程,然后用以下代码替换Unit1中的代码,再又击窗体空白部分,以使FormCreate生效。由于用到了RxLib中的一个单元,所以您的搜索路径中必须有RxLib2.75的源文件。//=====================================================================unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ToolEdit;type
TxCombo = class(TCustomComboEdit)
protected
public
constructor Create(AOwner: TComponent); override;
end; TxList = class(TPopupWindow)
private
FButton: TButton;
FEdit: TEdit;
protected
function GetValue: Variant; override;
procedure SetValue(const Value: Variant); override;
public
constructor Create(AOwner: TComponent); override;
end; TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
F: TxCombo;
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}{ TxCombo }constructor TxCombo.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
ControlStyle := ControlStyle - [csSetCaption];
ControlState := ControlState + [csCreating];
try
FPopup := TxList.Create(Self);
TPopupWindow(FPopup).OnCloseUp := PopupCloseUp;
finally
ControlState := ControlState - [csCreating];
end;
end;{ TxList }constructor TxList.Create(AOwner: TComponent);
begin
inherited; Width := 100;
Height := 100; FButton := TButton.Create(Self);
with FButton do
begin
Left := 10;
Top := 10;
Parent := Self;
end; FEdit := TEdit.Create(Self);
with FEdit do
begin
Left := 10;
Top := 40;
Parent := Self;
end;
end;function TxList.GetValue: Variant;
beginend;procedure TxList.SetValue(const Value: Variant);
begin
inherited;end;procedure TForm1.FormCreate(Sender: TObject);
begin
F := TxCombo.Create(Self);
F.Parent := Self;
F.Left := 100;
F.Top := 100;
end;end.
//=====================================================================这时如果运行,点击下拉按钮,可以弹出一个下拉窗,里面有一个Button一个Edit,无论你点击下拉窗任何部分,下拉窗将关闭。然后您改变ToolEdit.pas文件中以下部分,注释掉的内容就是我们改变的部分。procedure TCustomComboEdit.WMKillFocus(var Message: TWMKillFocus);
begin
inherited;
FFocused := False;
// PopupCloseUp(FPopup, False);
end;然后再运行程序,如果点击下拉框空白部分,下拉窗将正常关闭,但如果点到下拉框的按钮或输入框中,程序就锁住了。不知道各位遇到过这种情况没有?我觉得可能是和VCL的消息机制有关,但我研究了两天没有结果。本来答应一天就做一个下拉控件的,结果太掉面子了!求各位有经验的帮下忙啊!
我用一个TEdit和一个TSpeedButton模拟下拉框,点击Button后出现下拉框。但是如果下拉框中有控件,比如Edit,则如果下拉框中的Edit一旦获得焦点,程序中其他控件就无法再获得焦点,程序被锁S。如下代码可以模拟这种情况,新建一个工程,然后用以下代码替换Unit1中的代码,再又击窗体空白部分,以使FormCreate生效。由于用到了RxLib中的一个单元,所以您的搜索路径中必须有RxLib2.75的源文件。//=====================================================================unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ToolEdit;type
TxCombo = class(TCustomComboEdit)
protected
public
constructor Create(AOwner: TComponent); override;
end; TxList = class(TPopupWindow)
private
FButton: TButton;
FEdit: TEdit;
protected
function GetValue: Variant; override;
procedure SetValue(const Value: Variant); override;
public
constructor Create(AOwner: TComponent); override;
end; TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
F: TxCombo;
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}{ TxCombo }constructor TxCombo.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
ControlStyle := ControlStyle - [csSetCaption];
ControlState := ControlState + [csCreating];
try
FPopup := TxList.Create(Self);
TPopupWindow(FPopup).OnCloseUp := PopupCloseUp;
finally
ControlState := ControlState - [csCreating];
end;
end;{ TxList }constructor TxList.Create(AOwner: TComponent);
begin
inherited; Width := 100;
Height := 100; FButton := TButton.Create(Self);
with FButton do
begin
Left := 10;
Top := 10;
Parent := Self;
end; FEdit := TEdit.Create(Self);
with FEdit do
begin
Left := 10;
Top := 40;
Parent := Self;
end;
end;function TxList.GetValue: Variant;
beginend;procedure TxList.SetValue(const Value: Variant);
begin
inherited;end;procedure TForm1.FormCreate(Sender: TObject);
begin
F := TxCombo.Create(Self);
F.Parent := Self;
F.Left := 100;
F.Top := 100;
end;end.
//=====================================================================这时如果运行,点击下拉按钮,可以弹出一个下拉窗,里面有一个Button一个Edit,无论你点击下拉窗任何部分,下拉窗将关闭。然后您改变ToolEdit.pas文件中以下部分,注释掉的内容就是我们改变的部分。procedure TCustomComboEdit.WMKillFocus(var Message: TWMKillFocus);
begin
inherited;
FFocused := False;
// PopupCloseUp(FPopup, False);
end;然后再运行程序,如果点击下拉框空白部分,下拉窗将正常关闭,但如果点到下拉框的按钮或输入框中,程序就锁住了。不知道各位遇到过这种情况没有?我觉得可能是和VCL的消息机制有关,但我研究了两天没有结果。本来答应一天就做一个下拉控件的,结果太掉面子了!求各位有经验的帮下忙啊!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货