看一下下面的代码:其实只要override TEdit的Create,Change和KeyDown方法,
生成自己的 FHintBox,在 Change 和 KeyDown 中动态查找符合条件的 Item,
添加到 FHintBox 中,根据条件 Show/Hide 你的 HintBox。
具体的实现各人的环境不同,这里只给出框架而已,不好意思献丑了。
From: BaKuBaKu.
THintEdit = class(TEdit)
private
FHintBox: TListBox; // 提示框
procedure WMKILLFOCUS(var Message: TWMKillFocus); message
procedure ShowHintBox;
procedure HideHintBox;
WM_KILLFOCUS;
protected
procedure Change; override;
procedure KeyDown(var Key: Word; Shift: TShiftState); override;
end;
...
implementation
procedure THintEdit.Change;
begin
if (Text = '') then begin HideHintBox; inherited; Exit; end;
ShowHintBox;
inherited;
end;
procedure THintEdit.KeyDown(var Key: Word; Shift: TShiftState);
var
S: string;
begin
if (FHintBox = nil) then begin inherited; Exit; end;
case Key of
VK_RETURN: // 取得选中的条目
...
VK_ESCAPE: // 关闭提示框
if FHintBox.Visible then HideHintBox;
VK_UP, VK_DOWN, VK_PRIOR, VK_NEXT:
if FHintBox.Visible then
SendMessage(FHintBox.Handle, WM_KEYDOWN, Key, 0);
VK_LEFT:
if FHintBox.Visible then
SendMessage(FHintBox.Handle, WM_HSCROLL, 0, 0);
VK_RIGHT:
if FHintBox.Visible then
SendMessage(FHintBox.Handle, WM_HSCROLL, 1, 0);
end;
inherited;
end;
procedure THintEdit.WMKILLFOCUS(var Message: TWMKillFocus);
begin
inherited;
if (FHintBox <> nil) and
(Message.FocusedWnd <> FHintBox.Handle) and
(Message.FocusedWnd <> Self.Handle)
then HideHintBox;
end;