Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Component handle while mouse is over

Posted on 2005-03-13
15
Medium Priority
?
187 Views
Last Modified: 2010-04-05
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
0
Comment
Question by:admfotad
  • 9
  • 3
  • 2
  • +1
15 Comments
 
LVL 10

Expert Comment

by:_Katka_
ID: 13528283
0
 
LVL 11

Expert Comment

by:calinutz
ID: 13528389
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;
0
 

Author Comment

by:admfotad
ID: 13528574
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
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:admfotad
ID: 13528677
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 :)
0
 

Author Comment

by:admfotad
ID: 13528688
Another words for instance I'd like to get handle of "My computer" when mouse is over this component and so on

Regards
0
 
LVL 10

Expert Comment

by:_Katka_
ID: 13528870
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
0
 

Author Comment

by:admfotad
ID: 13529008
kate i know but don't want to encapsulate it in dll .... :)

regards
0
 

Author Comment

by:admfotad
ID: 13529023
I have to keep all application is single file

regards
0
 
LVL 10

Expert Comment

by:_Katka_
ID: 13529930
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

0
 
LVL 11

Accepted Solution

by:
ZhaawZ earned 1000 total points
ID: 13530284
WindowFromPoint(Mouse.CursorPos) will return a handle of control/window that is beneath a cursor.
SetWindowText(hwnd, 'new caption') will change a caption/text of a window/control with handle 'hwnd' (cardinal).
SetWindowPos(hwnd, 0, 10, 100, 0, 0, swp_nozorder or swp_nosize) will move window/control with handle 'hwnd' (cardinal) to position (x=10;y=100).
SetWindowPos(hwnd, 0, 0, 0, 150, 100, swp_nozorder or swp_nomove) will resize window/control with handle 'hwnd' (cardinal) to size (w=150;h=100).
SetWindowPos(hwnd, hwnd_topmost, 0, 0, 0, 0, swp_nomove or swp_nosize) will set window/control with handle 'hwnd' (cardinal) as always-on-top

0
 

Author Comment

by:admfotad
ID: 13530351
ZhaawZ... you're right

Thanks for help.
Points are going to Zhaawz

Regards
0
 

Author Comment

by:admfotad
ID: 13530580
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
0
 
LVL 11

Expert Comment

by:ZhaawZ
ID: 13530615
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):
http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_21335971.html
0
 

Author Comment

by:admfotad
ID: 13530637
Thanks for help

Regards
0
 

Author Comment

by:admfotad
ID: 13530689
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
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question