How to show the hint of a TEdit when it receives focus, without the mouse being on top of it? I did not put code because I have not the slightest idea.
How to show the hint of a TEdit when it receives focus, without the mouse being on top of it? I did not put code because I have not the slightest idea.
Work on the following code in the event OnEnter
of the TEdit
(s) you want:
Create the required variables:
var
Origin : TPoint; // Receberá a origem do Form na tela
HintWindow : THintWindow; // Objeto Hint do delphi
Hint : String; // Receberá a Hint do Edit
EditL, EditT, EditR, EditB : Integer; // Parâmetros do retângulo da Hint
Set the values of the variables:
Origin := ClientToScreen(Point(0,0)); //Origem do Form na tela
Hint := (Sender as TControl).Hint; //Atribua a Hint do Edit
Calculate the positions for the Hint rectangle:
EditL := Origin.X + (Sender as TControl).Left + 20;
EditT := Origin.Y + (Sender as TControl).Top - 12;
EditR := EditL + Length(Hint) * 9;
EditB := EditT + 18;
Create a new Hint object; show it in the rectangle calculated on the previous line:
HintWindow := THintWindow.Create(nil);
HintWindow.ActivateHint( Rect(EditL, EditT, EditR, EditB), Hint );
At the moment you want to release Hint just use HintWindow.Free
.
I suggest that you release it in the event OnExit
of Edit or using a Timer of 5 to 8 seconds for a traditional Windows effect.
Another way to do this, which also works on events, is to create your own suggestion window with THintWindow
as suggested this article .
Define the following variables:
FActive: boolean;
FHint: THintWindow;
Update : Below the Uses
clause, set the constant:
Uses
//....
Const
UM_EXITPROC = WM_USER + 42;
Use the method below to disable the suggestion window:
procedure DesativarSugestao;
begin
FActive := false;
if Assigned(FHint) then
begin
FHint.ReleaseHandle;
FHint.Free;
FHint := nil;
end;
end;
Use the method below to receive right-click and left-mouse click messages:
procedure AppMessage(var AMessage: TMsg; var Handled: Boolean);
begin
if (AMessage.Message = WM_LBUTTONDOWN) or (AMessage.Message = WM_RBUTTONDOWN) then
if Assigned(FHint) and FHint.Visible then
DesativarSugestao;
end;
Method responsible for displaying the suggestion box:
procedure mostrarSugestao(Sender: TObject);
Var
P: TPoint;
R: TRect;
Sugestao: string;
begin
if Assigned(FHint) and FActive then
Exit;
P.X := (Sender As TEdit).Left;
P.Y := (Sender As TEdit).Top - 24;
with R do
begin
topLeft := ClientToScreen(P);
Right := Left + 150;
Bottom := Top + 18;
end;
Sugestao := 'A sugestão é...'; // Ou (Sender As TEdit).Text
FHint := THintWindow.Create(Self);
FHint.ActivateHint(R, Sugestao);
FActive := True;
PostMessage(Handle, UM_EXITPROC, 0, 0);
end;
In event OnEnter
of Edit
do:
procedure Edit1Enter(Sender: TObject);
begin
if not Assigned(FHint) and FActive = False then
mostrarSugestao(Sender);
end;
In event OnExit
of Edit
do:
procedure Edit1Exit(Sender: TObject);
begin
if Assigned(FHint) and FActive then
DesativarSugestao;
end;
In event OnKeyPress
of Edit
do:
procedure Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if Assigned(FHint) and FActive then
DesativarSugestao;
end;
In event OnMouseEnter
do:
procedure Edit1MouseEnter(Sender: TObject);
begin
if not Assigned(FHint) and FActive = False then
mostrarSugestao(Sender);
end;
In event OnMouseLeave
do:
procedure Edit1MouseLeave(Sender: TObject);
begin
if Assigned(FHint) and FActive then
DesativarSugestao;
end;
In event OnClick
of Edit
do:
procedure TForm1.Edit1Click(Sender: TObject);
begin
if Assigned(FHint) and FActive then
DesativarSugestao;
end;
In event OnCreate
of the form do:
procedure FormCreate(Sender: TObject);
begin
Application.OnMessage := AppMessage;
end;
The result should look something like this: