Archived

This topic is now archived and is closed to further replies.

Redoc

BCB Mouse Click Event ?

Recommended Posts

Greetings! This should be an easy question so hopefully someone can help out. I have tried searching but can not pin point this issue. In Borland C++ Builder I have made a new form and added a few controls such as an image box and a few text boxes etc. I would like to know how to detect the mouse click no matter what the mouse is over when its on the form. Using the FORM1->onMouseDown function will work but only when the mouse is in the area of the FORM, not when its on the image box or the textbox etc! ... I know I can add the same code to all the controls->onmousedown event but surely there is a way to configure it to work over the whole form globally? Any ideas appreciated!

Share this post


Link to post
Share on other sites
You could write a recursive function to set all the OnMouseDown events to point to the same function, using the ComponentCount and Components properties of TComponent (and thus TForm). If you need to differentiate between clicks on different components, you can use the Sender parameter of the event.

Here is some equivalent Delphi code, it shouldn''t be too much bother to convert this to C++ Builder:

procedure SetMouseDownEvents(component: TComponent; event: MouseEvent);
var
i: Integer;
begin
if (component is TControl) then begin
TControl(component).OnMouseDown := event);
end;
for i := 0 to (component.ComponentCount - 1) do begin
SetMouseDownEvents(component.Components[i, event);
end;
end;

...
// In form create
SetMouseDownEvents(Form1, Form1.MouseDown);


Hope this helps.


"The ability to speak does not make you intelligent" - Qui-Gon Jinn
[ DGDev - The Delphi Games Development Community ] [ Help GameDev.net fight cancer ] [ Shareaza - The Ultimate P2P Client ]

Share this post


Link to post
Share on other sites
An alternative solution is to create a single MouseDown event handler, and then select that method from the drop down list in the object inspector for each of the objects you wish to use that method. The disadvantage of this way is that you will need to do this for every control you add to your form.


"The ability to speak does not make you intelligent" - Qui-Gon Jinn
[ DGDev - The Delphi Games Development Community ] [ Help GameDev.net fight cancer ] [ Shareaza - The Ultimate P2P Client ]

Share this post


Link to post
Share on other sites
you know, I never thought of that. I will try it soon as I get home from work

Thanks for the idea!

I take it thats the best way to go about it, I was thinking it would be a setting somewhere. ;D

Share this post


Link to post
Share on other sites
I didnt see your 2nd reply sorry,

perhaps I could use that instead....

thank you very much for your quick responses!!

Share this post


Link to post
Share on other sites