我使用德尔福XE2的TButtonedEdit但我想补充的下拉列表中的历史(如TComboBox)。我知道TComboBox是一个荣耀的TEdit,所以有消息可以发送给TButtonedEdit以添加此功能吗?谢谢。是否可以添加一个历史列表下拉列表到Delphi的TButtonedEdit?
4
A
回答
10
可以使用IAutoComplete2
接口来完成这个任务。
入住此示例代码(适用于TButtonedEdit
和Delphi XE2),从Ken White
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, Winapi.ActiveX, Winapi.ShlObj,
Vcl.Mask, Vcl.ImgList;
type
IACList = interface(IUnknown)
['{77A130B0-94FD-11D0-A544-00C04FD7d062}']
function Expand(pszExpand : POLESTR) : HResult; stdcall;
end;
const
//options for IACList2
ACLO_NONE = 0; // don't enumerate anything
ACLO_CURRENTDIR = 1; // enumerate current directory
ACLO_MYCOMPUTER = 2; // enumerate MyComputer
ACLO_DESKTOP = 4; // enumerate Desktop Folder
ACLO_FAVORITES = 8; // enumerate Favorites Folder
ACLO_FILESYSONLY = 16; // enumerate only the file system
type
IACList2 = interface(IACList)
['{470141a0-5186-11d2-bbb6-0060977b464c}']
function SetOptions(dwFlag: DWORD): HResult; stdcall;
function GetOptions(var pdwFlag: DWORD): HResult; stdcall;
end;
IAutoComplete = interface(IUnknown)
['{00bb2762-6a77-11d0-a535-00c04fd7d062}']
function Init(hwndEdit: HWND; const punkACL: IUnknown;
pwszRegKeyPath, pwszQuickComplete: POLESTR): HResult; stdcall;
function Enable(fEnable: BOOL): HResult; stdcall;
end;
const
//options for IAutoComplete2
ACO_NONE = 0;
ACO_AUTOSUGGEST = $1;
ACO_AUTOAPPEND = $2;
ACO_SEARCH = $4;
ACO_FILTERPREFIXES = $8;
ACO_USETAB = $10;
ACO_UPDOWNKEYDROPSLIST = $20;
ACO_RTLREADING = $40;
type
IAutoComplete2 = interface(IAutoComplete)
['{EAC04BC0-3791-11d2-BB95-0060977B464C}']
function SetOptions(dwFlag: DWORD): HResult; stdcall;
function GetOptions(out pdwFlag: DWORD): HResult; stdcall;
end;
TEnumString = class(TInterfacedObject, IEnumString)
private
type
TPointerList = array[0..0] of Pointer; //avoid bug of Classes.pas declaration TPointerList = array of Pointer;
var
FStrings: TStringList;
FCurrIndex: integer;
public
//IEnumString
function Next(celt: Longint; out elt;
pceltFetched: PLongint): HResult; stdcall;
function Skip(celt: Longint): HResult; stdcall;
function Reset: HResult; stdcall;
function Clone(out enm: IEnumString): HResult; stdcall;
//VCL
constructor Create;
destructor Destroy;override;
end;
TACOption = (acAutoAppend, acAutoSuggest, acUseArrowKey);
TACOptions = set of TACOption;
TACSource = (acsList, acsHistory, acsMRU, acsShell);
TButtonedEdit = class(Vcl.ExtCtrls.TButtonedEdit)
private
FACList: TEnumString;
FAutoComplete: IAutoComplete;
FACEnabled: boolean;
FACOptions: TACOptions;
FACSource: TACSource;
function GetACStrings: TStringList;
procedure SetACEnabled(const Value: boolean);
procedure SetACOptions(const Value: TACOptions);
procedure SetACSource(const Value: TACSource);
procedure SetACStrings(const Value: TStringList);
protected
procedure CreateWnd; override;
procedure DestroyWnd; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property ACEnabled: boolean read FACEnabled write SetACEnabled;
property ACOptions: TACOptions read FACOptions write SetACOptions;
property ACSource: TACSource read FACSource write SetACSource;
property ACStrings: TStringList read GetACStrings write SetACStrings;
end;
TForm1 = class(TForm)
ButtonedEdit1: TButtonedEdit;
ImageList1: TImageList;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm5;
implementation
{$R *.dfm}
uses
System.Win.ComObj;
procedure TForm1.FormCreate(Sender: TObject);
begin
ButtonedEdit1.ACEnabled:=True;
ButtonedEdit1.ACOptions:=[acAutoAppend, acAutoSuggest, acUseArrowKey];
ButtonedEdit1.ACSource:=acsList;
ButtonedEdit1.ACStrings.Add('string 1');
ButtonedEdit1.ACStrings.Add('string 2');
ButtonedEdit1.ACStrings.Add('string 3');
ButtonedEdit1.ACStrings.Add('string 4');
end;
{ TEnumString }
function TEnumString.Clone(out enm: IEnumString): HResult;
begin
Result := E_NOTIMPL;
Pointer(enm) := nil;
end;
constructor TEnumString.Create;
begin
inherited Create;
FStrings := TStringList.Create;
FCurrIndex := 0;
end;
destructor TEnumString.Destroy;
begin
FStrings.Free;
inherited;
end;
function TEnumString.Next(celt: Integer; out elt;
pceltFetched: PLongint): HResult;
var
I: Integer;
wStr: WideString;
begin
I := 0;
while (I < celt) and (FCurrIndex < FStrings.Count) do
begin
wStr := FStrings[FCurrIndex];
TPointerList(elt)[I] := CoTaskMemAlloc(2 * (Length(wStr) + 1));
StringToWideChar(wStr, TPointerList(elt)[I], 2 * (Length(wStr) + 1));
Inc(I);
Inc(FCurrIndex);
end;
if pceltFetched <> nil then
pceltFetched^ := I;
if I = celt then
Result := S_OK
else
Result := S_FALSE;
end;
function TEnumString.Reset: HResult;
begin
FCurrIndex := 0;
Result := S_OK;
end;
function TEnumString.Skip(celt: Integer): HResult;
begin
if (FCurrIndex + celt) <= FStrings.Count then
begin
Inc(FCurrIndex, celt);
Result := S_OK;
end
else
begin
FCurrIndex := FStrings.Count;
Result := S_FALSE;
end;
end;
{ TACEdit }
constructor TButtonedEdit.Create(AOwner: TComponent);
begin
inherited;
FACList := TEnumString.Create;
FACEnabled := True;
FACOptions := [acAutoAppend, acAutoSuggest, acUseArrowKey];
end;
procedure TButtonedEdit.CreateWnd;
var
Dummy: IUnknown;
Strings: IEnumString;
begin
inherited;
if HandleAllocated then
begin
try
Dummy := CreateComObject(CLSID_AutoComplete);
if (Dummy <> nil) and
(Dummy.QueryInterface(IID_IAutoComplete, FAutoComplete) = S_OK) then
begin
case FACSource of
acsHistory: Strings := CreateComObject(CLSID_ACLHistory) as
IEnumString;
acsMRU: Strings := CreateComObject(CLSID_ACLMRU) as
IEnumString;
acsShell: Strings := CreateComObject(CLSID_ACListISF) as
IEnumString;
else
Strings := FACList as IEnumString;
end;
if S_OK = FAutoComplete.Init(Handle, Strings, nil, nil) then
begin
SetACEnabled(FACEnabled);
SetACOptions(FACOptions);
end;
end;
except
//CLSID_IAutoComplete is not available
end;
end;
end;
destructor TButtonedEdit.Destroy;
begin
FACList := nil;
inherited;
end;
procedure TButtonedEdit.DestroyWnd;
begin
if (FAutoComplete <> nil) then
begin
FAutoComplete.Enable(False);
FAutoComplete := nil;
end;
inherited;
end;
function TButtonedEdit.GetACStrings: TStringList;
begin
Result := FACList.FStrings;
end;
procedure TButtonedEdit.SetACEnabled(const Value: Boolean);
begin
if (FAutoComplete <> nil) then
begin
FAutoComplete.Enable(FACEnabled);
end;
FACEnabled := Value;
end;
procedure TButtonedEdit.SetACOptions(const Value: TACOptions);
const
Options : array[TACOption] of integer = (ACO_AUTOAPPEND,
ACO_AUTOSUGGEST,
ACO_UPDOWNKEYDROPSLIST);
var
Option:TACOption;
Opt: DWORD;
AC2: IAutoComplete2;
begin
if (FAutoComplete <> nil) then
begin
if S_OK = FAutoComplete.QueryInterface(IID_IAutoComplete2, AC2) then
begin
Opt := ACO_NONE;
for Option := Low(Options) to High(Options) do
begin
if (Option in FACOptions) then
Opt := Opt or DWORD(Options[Option]);
end;
AC2.SetOptions(Opt);
end;
end;
FACOptions := Value;
end;
procedure TButtonedEdit.SetACSource(const Value: TACSource);
begin
if FACSource <> Value then
begin
FACSource := Value;
RecreateWnd;
end;
end;
procedure TButtonedEdit.SetACStrings(const Value: TStringList);
begin
if Value <> FACList.FStrings then
FACList.FStrings.Assign(Value);
end;
end.
这是结果基于这个答案(Auto append/complete from text file to an edit box delphi)。
+0
哇。一段惊人的代码。第一次工作。谢谢! – 2012-07-24 09:28:34
1
我不知道在XE2(我在2007年我自己),其具体的控制,但是,假设弹出菜单属性是一个在你的控制,为什么不把窗体上创建一个弹出式菜单,并将其分配给那财产?那么作为你通过你的控制项目,你可以让他们被添加到弹出式菜单中具有共同的onclick处理程序,需要重新加载之前的项目。
相关问题
- 1. 添加一个DIV下拉列表
- 2. 如何添加一个下拉列表
- 3. 添加 - 从列表中选择一个下拉列表
- 4. 是否可以同时将列添加到多个表中?
- 5. Jquery添加到下拉列表
- 6. 添加项到下拉列表
- 7. 只有项目添加到一个下拉列表,一旦
- 8. 添加一个新的属性到ASP.NET下拉列表
- 9. 一个下拉列表,以及一周的开始列表vb6
- 10. 将文件夹列表添加到下拉列表中
- 11. 从下拉列表框中添加数据到列表框
- 12. 如何将一个空选项添加到下拉列表中
- 13. 添加到一个下拉列表中MVC3
- 14. 添加动态值到表头下的下拉列表
- 15. 基于选择另一个下拉列表的下拉列表
- 16. 我可以在列表中添加一个列表吗?
- 17. F#:是否可以使用可变.Net列表添加元素到同一个列表
- 18. 将结果添加到可见的AutoCompleteTextArea下拉列表中
- 19. 查找下拉列表值是否动态添加
- 20. 如何添加一个下拉列表将项添加到我的ckeditor中
- 21. 您可以向Android Widget添加下拉列表吗?
- 22. 我们可以在编译后在下拉列表中添加一个项目
- 23. MVC下拉列表填充另一个下拉列表
- 24. 从另一个下拉列表填充下拉列表
- 25. 是否可以使用jQuery.validVal来验证选择下拉列表?
- 26. 是否可以通过键盘扩展GWT下拉列表(ListBox)?
- 27. 是否可以在下拉列表中包含HTML元素?
- 28. 是否可以使用jQuery(.jeditable)自动下拉选择列表?
- 29. 将一个列表添加到一个单独的列表中
- 30. 依赖下拉列表不会加载选择第一个下拉列表
这是不是其实http://stackoverflow.com/questions/5465590/auto-append-complete-from-text-file-to-an-edit-box-delphi的副本Ken的答案是你应该使用的 – 2012-07-23 18:48:17