Recent

Author Topic: Function to show hint using control name on mouse over?  (Read 832 times)

vfclists

  • Hero Member
  • *****
  • Posts: 1013
    • HowTos Considered Harmful?
Function to show hint using control name on mouse over?
« on: February 22, 2024, 11:01:08 pm »
Is there some way to  centralize the OnMouse/Enter/Over/Exit events through some kind of event bubbling which I can use to see control names?

Handling the event for every control will be rather messy, so I'd prefer to handle it centrally for all of them, or set a temporary switch to do so.
Lazarus 3.0/FPC 3.2.2

jamie

  • Hero Member
  • *****
  • Posts: 6134
Re: Function to show hint using control name on mouse over?
« Reply #1 on: February 22, 2024, 11:05:48 pm »
ControlAtPos ?

The only true wisdom is knowing you know nothing

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2102
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Function to show hint using control name on mouse over?
« Reply #2 on: February 22, 2024, 11:14:00 pm »
Uhm... give Control(s) a hint and activate that it does show?...

Dunno what you try to do better than LCL already does.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: Function to show hint using control name on mouse over?
« Reply #3 on: February 22, 2024, 11:34:37 pm »
All these events you mention have a Sender paramter, which will be the control in question.

What are actually trying to achieve?

Bart

TRon

  • Hero Member
  • *****
  • Posts: 2543
Re: Function to show hint using control name on mouse over?
« Reply #4 on: February 23, 2024, 12:08:51 am »
Besides what KodeZwerg and Bart wrote, it is all a bit redundant. You can use Application.GetControlAtMouse (that is even quicker/easier jamie  ;) ) to see what control is currently at your mousepointer. You could for example use the application idle event to read that value, retrieve the name of the returned control and put it at a nice location (statuspanel comes to mind but anything will do).
« Last Edit: February 23, 2024, 12:13:47 am by TRon »

jamie

  • Hero Member
  • *****
  • Posts: 6134
Re: Function to show hint using control name on mouse over?
« Reply #5 on: February 23, 2024, 01:26:25 am »
I believe what is sot at is a way of capturing the mouse and key inputs before they go to the controls instead of implementing code for each so that it can inspect what is under the cursor.

 A overlaid window can do this but also, there is already a "OnUserInputEvent" in the TApplication class that should, when implemented, respond to mouse moves and maybe keys.
The only true wisdom is knowing you know nothing

jamie

  • Hero Member
  • *****
  • Posts: 6134
Re: Function to show hint using control name on mouse over?
« Reply #6 on: February 23, 2024, 01:34:48 am »
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, EditBtn, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     procedure FormCreate(Sender: TObject);
  17.   private
  18.  
  19.   public
  20.      Procedure UserInput(Sender:TObject;Msg:Cardinal);
  21.   end;
  22.  
  23. var
  24.   Form1: TForm1;
  25.  
  26. implementation
  27.  
  28. {$R *.lfm}
  29.  
  30. { TForm1 }
  31. Procedure TForm1.UserInput(Sender:Tobject;Msg:Cardinal);
  32. Begin
  33.  Caption :=  Application.GetControlAtMouse.Name;
  34. end;
  35.  
  36. procedure TForm1.FormCreate(Sender: TObject);
  37. begin
  38.   Application.OnUserInput := @USerInput;
  39. end;
  40.  
  41. end.
  42.  
  43.  
The only true wisdom is knowing you know nothing

Manlio

  • Full Member
  • ***
  • Posts: 164
  • Pascal dev
Re: Function to show hint using control name on mouse over?
« Reply #7 on: February 29, 2024, 09:59:01 am »
In similar situations, when I need to centralize the handling of certain events, I use the following approach, which works even if forms and controls are created at runtime.

Code: [Select]
procedure Form1.Hook(Root: TComponent);
var i: integer;
begin
  if Root is TMemo then begin
    TMemo(Root).OnClick := @CentralClick;
    exit;
  end;
  if Root is TPanel then begin
    TMemo(Root).OnClick := @PanelsClick;
  end;
  for i := 0 to Root.ComponentCount-1 do
    Hook(Root.Components[i]);
end;

With the above, a form can call Hook(Self) and the above will go through every component and control, and there you will be able to assign different event handlers for different classes of controls, etc.

In the above example, I assign certain handlers to TMemo controls (and then I exit) and I assign other handlers to TPanel controls (and then I don't exit because panels may own children controls)

Note: Instead of Components/ComponentCount you may also use Controls/ControlCount, which follows the parent/child structure instead of the Owner structure.

Call Hook when the program starts, and if you dynamically create new controls, etc. at runtime then call again Hook(...) with the root of the changed controls.

The methods you assign, of course, will have to check who is the Sender object and operate accordingly.

I also use the above approach, for example, to change the captions and hints of different controls when the user changes language, or changes color scheme, and so on.
manlio mazzon gmail

 

TinyPortal © 2005-2018