Link to home
Start Free TrialLog in
Avatar of admfotad
admfotadFlag for Poland

asked on

Component handle while mouse is over

Hi

I would like to ask you how can I get handle of any component while mouse is over and for example move this component while mouse is over or change the caption (get parent ).


Regards
Avatar of _Katka_
_Katka_
Flag of Czechia image

I think that this is what you need:
You can check on every component that you want to react on mouse over int the 2 procedures onEnter and onExit
The "parent" that you mentioned above is the Sender from the two procedures.

...
  private
    FFocusControl: TControl;
    procedure ApplicationIdle(Sender: TObject; var Done: Boolean);
    { Private declarations }
  public
    procedure OnEnter(Sender: TObject);
    procedure OnExit(Sender: TObject);
    { Public declarations }


procedure TForm1.ApplicationIdle(Sender: TObject; var Done: Boolean);
var
  CurControl: TControl;
  P: TPoint;
begin
  GetCursorPos(P);
  CurControl := FindDragTarget(P, True);
  if FFocusControl <> CurControl then
  begin
    if FFocusControl <> nil then
      OnExit(FFocusControl);
    FFocusControl := CurControl;
    if FFocusControl <> nil then
      OnEnter(FFocusControl);
  end;
end;

procedure TForm1.OnEnter(Sender: TObject);
begin
  //OnEnter code
  if (sender = Image2)or(sender=Label14) then
  begin
  Image2.Left:=Image2.Left+1;
  Image2.Picture.LoadFromFile('C:\save1.bmp');
  Label14.Font.Style:=[fsBold];
  end;
end;

procedure TForm1.OnExit(Sender: TObject);
begin
  //OnExit code
  if (sender = Image2)or(sender=Label14) then
  begin
  Image2.Left:=Image2.Left-1;
  Image2.Picture.LoadFromFile('C:\save2.bmp');
  Label14.Font.Style:=[];
  end;
end;
Avatar of admfotad

ASKER

calinutz I was trying to say that i'd like to get handle any component and not only this component's placed on the form

regards
Kate I think that it's not what I'm looking for :(((

I have to get handle of any component and in examples in this link it's not working for me ....moreover i had a listning what i'm lookin for but now i can not find it :)
Another words for instance I'd like to get handle of "My computer" when mouse is over this component and so on

Regards
Hi again, did you tried:

unit Mouse;

interface

uses Controls;

const
  MouseCallback: procedure(AControl: TControl; X, Y: Integer) = nil;

unit Main;

type
  TMainForm = class(TForm)
  end;

var
  MainForm: TMainForm;

implementation

uses Mouse;
 
procedure MyMouse(AControl: TControl; X, Y: Integer);
begin
  if MainForm <> nil then
    if AControl <> nil then MainForm.Caption := AControl.Name
      else MainForm.Caption := '';
end;

initialization
  MouseCallback := MyMouse;
end.

End of Example.
You can encapsuled this code in a Component.
Sorry for my english.}

implementation

uses Windows, Messages;

const
  FMouseHook: hHook = 0;
  FInProc: Integer = 0;
  FMouseControl: TControl = nil;

function MouseProc(Code: Integer; wParam: WParam; lParam: LParam): LResult; stdcall; export;
var
  C: TControl;
  P: TPoint;
begin
  if (Code = hc_Action) and (FInProc = 0) then
  try
    Inc(FInProc);  {Disables Recursion's}
    if (wParam = wm_MouseMove) or (wParam = wm_NCMouseMove) then
    begin
      GetCursorPos(P);
      C := FindDragTarget(P, True);
      if C <> FMouseControl then
      begin
        FMouseControl := C; // here C.Handle is your handle to control the mouse is actually over
        if Assigned(MouseCallback) then MouseCallback(FMouseControl, P.X, P.Y);
      end;
    end;
  finally
    Dec(FInProc);
  end;
  Result := CallNextHookEx(FMouseHook, Code, wParam, lParam);
end;

initialization
  FMouseHook := SetWindowsHookEx(WH_MOUSE, MouseProc, 0, GetCurrentThreadID);
finalization
  if FMouseHook <> 0 then UnHookWindowsHookEx(FMouseHook);
  FMouseHook := 0;
end.

**********

in FMouseControl.Handle is your desired handle moreover it works not
just for your application but for whole desktop if you'll encapsulate it
in DLL.

regards,
Kate
kate i know but don't want to encapsulate it in dll .... :)

regards
I have to keep all application is single file

regards
Then I'll be glad to see a different working
approach. IMHO you can't catch the global
messages differently then thru a global hook.
Which is at the best to be encapsulated in
a DLL file. Maybe you'll have to do some
compromises if none will shows us otherwise.

best regards,
Kate

ASKER CERTIFIED SOLUTION
Avatar of ZhaawZ
ZhaawZ
Flag of Latvia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ZhaawZ... you're right

Thanks for help.
Points are going to Zhaawz

Regards
ZhaawZ.. if you could tell me how can I get the caption of icons on the desktop while mouse is over i would be very appreciate

Regards
Actually icons on desktop aren't separate controls. Desktop ir a ListView control, and icons are items of it.

There was a similar question some time ago (about getting info about icons on desktop):
https://www.experts-exchange.com/questions/21335971/Moving-icons-on-Desktop.html
Thanks for help

Regards
Maybe i'm boring but there has to be any possibility to check the caption of icon link on the desktop while somebody is trying to click it with mouse .... if somebody knows any idea then I will post another question here and I will grant him 250 points.

Regards

P.S> let me know