Recent

Author Topic: Separating event calls depending on where the mouse is.  (Read 700 times)

OC DelGuy

  • Full Member
  • ***
  • Posts: 121
Separating event calls depending on where the mouse is.
« on: March 19, 2023, 08:02:18 am »
I created a form and put a TMemo and a TEdit on it.  I then clicked on the "OnMouseWheelDown" event and Lazarus created the procedure FormMouseWheelDown.  In the procedure I wrote "Edit1.Text:='Mouse wheel down.';".  I did the same with the "OnMouseWheelUp" event, replacing "down" with the word "up".
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormMouseWheelDown(Sender: TObject; Shift: TShiftState;
  2.   MousePos: TPoint; var Handled: Boolean);
  3. begin
  4.   Edit1.Text:='Mouse wheel down.';
  5. end;
  6.  
  7. procedure TForm1.FormMouseWheelUp(Sender: TObject; Shift: TShiftState;
  8.   MousePos: TPoint; var Handled: Boolean);
  9. begin
  10.   Edit1.Text:='Mouse wheel up.';
  11. end;
When I run the application, and I roll the mouse wheel outside the Memo box, but inside the form, this code executes (as it should).
How can I prevent this code from running when I roll the mouse wheel up and down inside the memo?  I realize that while the mouse pointer is in the memo box, it is ALSO in the form.  But I don't want it to run this code while "wheeling" in the memo box.  Just in the remainder of the form.
I tried making events in the Memo, "OnMouseWheelUp" and "OnMouseWheelDown", and putting in different strings.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Memo1MouseWheelDown(Sender: TObject; Shift: TShiftState;
  2.   MousePos: TPoint; var Handled: Boolean);
  3. begin
  4.   Edit1.Text:='From Memo1, down.';
  5. end;
  6.  
  7. procedure TForm1.Memo1MouseWheelUp(Sender: TObject; Shift: TShiftState;
  8.   MousePos: TPoint; var Handled: Boolean);
  9. begin
  10.   Edit1.Text:='From Memo1, up.';
  11. end;
But these don't execute when the mouse pointer is in the memo box.

I'm writing this as I'm coding, so this is quite literally all I have coded.
Thanks in advance for any help Y'all can give me.
Free Pascal Lazarus Version #: 2.2.4
Date: 24 SEP 2022
FPC Version: 3.2.2
Revision: Lazarus_2_2_4
x86_64-win64-win32/win64

Josh

  • Hero Member
  • *****
  • Posts: 1271
Re: Separating event calls depending on where the mouse is.
« Reply #1 on: March 19, 2023, 09:29:39 am »
You could try using FindDragTarget

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Memo1MouseWheelDown(Sender: TObject; Shift: TShiftState;
  2.   MousePos: TPoint; var Handled: Boolean);
  3. var cntrl:tcontrol;
  4. begin
  5.   cntrl:=tcontrol(FindDragTarget(Mouse.CursorPos,true));
  6.   if cntrl is tmemo then
  7.   begin
  8.     if cntrl.Name='Memo1' then
  9.     begin
  10.       memolab.caption:='Memo1 MouseWheelDown MousePos X:= '+inttostr(mousepos.X);
  11.     end;
  12.   end;
  13. end;        
  14.  

example proj attahed
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

Thaddy

  • Hero Member
  • *****
  • Posts: 14214
  • Probably until I exterminate Putin.
Specialize a type, not a var.

jamie

  • Hero Member
  • *****
  • Posts: 6091
Re: Separating event calls depending on where the mouse is.
« Reply #3 on: March 19, 2023, 03:01:13 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormMouseWheel(Sender: TObject; Shift: TShiftState;
  2.   WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
  3. begin
  4.   Handled := (Self.ControlAtPos(ScreenToClient(Mouse.CurSorPos),true,True) <> nil);
  5. end;                                                                                
  6.  

There appears to be to be an issue with the "ControlAtPos"? whereas if you only use the simplest version to allow disabled controls, it sets the Window controls to false? Does that really make any sense to you?

 Basically, what that means is, it only finds the disabled and enabled Non-Window Controls.

 Maybe it's my old version I used but that is the case here.

 Using the two Boolean overloaded version allows to indicate to also include the windows controls.

 I also noticed that the MousePos always reports the child control client position if over a child, which is why I had to not use that. Seems strange to be that way since there is no way with input parameters to know that. Sender is always the form.

The only true wisdom is knowing you know nothing

jamie

  • Hero Member
  • *****
  • Posts: 6091
Re: Separating event calls depending on where the mouse is.
« Reply #4 on: March 19, 2023, 03:35:28 pm »
Hmm.
  Checking the trunk, it appears that problem with MousePos within those events over child controls verses Parent (From) has been fixed.
The only true wisdom is knowing you know nothing

OC DelGuy

  • Full Member
  • ***
  • Posts: 121
Re: Separating event calls depending on where the mouse is.
« Reply #5 on: March 29, 2023, 04:02:08 am »
You could try using FindDragTarget

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Memo1MouseWheelDown(Sender: TObject; Shift: TShiftState;
  2.   MousePos: TPoint; var Handled: Boolean);
  3. var cntrl:tcontrol;
  4. begin
  5.   cntrl:=tcontrol(FindDragTarget(Mouse.CursorPos,true));
  6.   if cntrl is tmemo then
  7.   begin
  8.     if cntrl.Name='Memo1' then
  9.     begin
  10.       memolab.caption:='Memo1 MouseWheelDown MousePos X:= '+inttostr(mousepos.X);
  11.     end;
  12.   end;
  13. end;        
  14.  

example proj attahed


Thanks Josh.  This worked...     Kind of.

Yes, it worked while the mouse was in the memo control.  But while outside the control but inside the form, then it does nothing.  The edit box stays empty.  That's pretty much the opposite of what I want.  Don't get me wrong, you made it so it separates what happens depending on the mouse location.  And I'm looking to do that, But it needs to behave as normal while in the Memo and run my code while outside the Memo.
Here's what I'm looking for:  Since the Memo box automatically scrolls up and down when the text is more than the Memo can hold, I want it to keep doing that.  But when you roll the mouse wheel outside the Memo box, I want it to do something (I'll write that code later) while outside the Memo box.

Kind of like the forum here.  As I typed my original post, I can scroll up and down inside the white box.  But the box stays in the same position (on the screen).  So while the box is 11 lines tall, I can scroll through 30, 50, or however many lines the text has without moving the box.  But while the mouse is outside, then the Memo box no longer scrolls, but the entire webpage does.  And thus, I can see all the other comments.

Let me also give you what my goal is to code.  It's like an industry specific glossary of terms.  Or a list of chemical constants.  Or astronomical definitions.  In the Edit box will appear the term or constant they're looking for and in the Memo will appear the definition or value of the constant.  So since some definitions can be kind of lengthy, when they show up in the Memo box, I can scroll up and down to read the entire definition.  But if I'm wheeling the mouse outside the Memo box, then I want it to scroll through the terms and constants.  So that's why I was looking to separate them.

Since the terms are going to be in a record, I imagine the record to be:

Code: Pascal  [Select][+][-]
  1. type
  2.   TLog = Record
  3.     Term : String;
  4.     Definition  : Array [1..10] of String;
  5.   end;
  6.  
So, inside the Memo box, it scrolls up and down the definition and outside the Memo box it goes up and down the Terms in the Edit box (and therefore the definitions in the Memo box).
Free Pascal Lazarus Version #: 2.2.4
Date: 24 SEP 2022
FPC Version: 3.2.2
Revision: Lazarus_2_2_4
x86_64-win64-win32/win64

 

TinyPortal © 2005-2018