Recent

Author Topic: [SOLVED] Color under mouse cursor/Click  (Read 438 times)

BubikolRamios

  • Sr. Member
  • ****
  • Posts: 305
[SOLVED] Color under mouse cursor/Click
« on: August 07, 2024, 04:07:59 pm »
Why is this working here and not (also) on PaintBox1 Click. Click always produces black.
How to make it work on click ?

Code: Pascal  [Select][+][-]
  1. procedure TForm1.PaintBox1MouseMove(Sender: TObject; Shift: TShiftState; X,
  2.   Y: Integer);
  3. var a,b:TPoint;
  4. begin
  5. a.X:=Mouse.CursorPos.X;
  6. a.Y:=Mouse.CursorPos.Y;
  7. b:=PaintBox1.ScreenToClient(a);
  8.  
  9. Label2.Font.Color:= PaintBox1.Canvas.Pixels[b.X,b.y];
  10.  
  11. end;  
  12.  
  13.  
« Last Edit: August 07, 2024, 07:33:48 pm by BubikolRamios »
lazarus 3.2-fpc-3.2.2-win32/win64

Dzandaa

  • Sr. Member
  • ****
  • Posts: 362
  • From C# to Lazarus
Re: Color under mouse cursor/Click
« Reply #1 on: August 07, 2024, 06:36:16 pm »
Hi,

How do you find the X,Y position in Onclick?

Try OnMouseUp Instead or save the X,Y position in OnMouseMove and use it in OnClick.

B->
« Last Edit: August 07, 2024, 06:43:08 pm by Dzandaa »
Regards,
Dzandaa

wp

  • Hero Member
  • *****
  • Posts: 12410
Re: Color under mouse cursor/Click
« Reply #2 on: August 07, 2024, 07:00:34 pm »
I like TPaintbox a lot, but for your kind of application it is not usable in a straightforward way because its canvas exists only during the drawing process, i.e. you cannot access the Pixels property directly in the mouse events.

What you could do is to store the position of the mouse click in a variable (FClickPos: TPoint), trigger a redraw of the Paintbox, and in its OnPaint event determine color of the pixel at FClickPos:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.PaintBox1MouseMove(Sender: TObject; Shift: TShiftState; X,
  2.   Y: Integer);
  3. begin
  4.   FClickPos := Point(X, Y);
  5.   Paintbox1.Invalidate;  // Trigger redraw of the paintbox
  6. end;
  7.  
  8. procedure TForm1.Paintbox1Draw(Sender: TObject);
  9. begin
  10.   // ... your drawing code here
  11.  
  12.   if (FClickPos.X >= 0) and (FClickPos.Y >= 0) then begin
  13.     Label2.Font.Color:= PaintBox1.Canvas.Pixels[FClickPos.X, FClickPos.y];
  14.     Label2.Show;
  15.   end else
  16.     Label2.Hide;
  17. end;

Initialize FClickPos to (-1,-1) in the OnCreate event of the form and in the OnMouseLeave event of the Paintbox so that Label2 displays the color only when the mouse is over the paintbox.
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   FClickPos := Point(-1, -1);
  4. end;
  5.  
  6. procedure TForm1.Paintbox1MouseLeave(Sender: TObject);
  7. begin
  8.   FClickPos := Point(-1, -1);
  9. end;

Disclaimer: Code untested...

Depending on what you display in the Paintbox you could also use a TImage instead of the TPaintbox which has a persistent canvas and for which you could use your code directly. But TImage is sometimes more difficult to understand.
« Last Edit: August 07, 2024, 07:02:51 pm by wp »

BubikolRamios

  • Sr. Member
  • ****
  • Posts: 305
Re: Color under mouse cursor/Click
« Reply #3 on: August 07, 2024, 07:26:14 pm »
Quote
i.e. you cannot access the Pixels property directly in the mouse events.
On mouseMove you can, tested. And as tested, onMouseUp.
« Last Edit: August 07, 2024, 07:35:38 pm by BubikolRamios »
lazarus 3.2-fpc-3.2.2-win32/win64

BubikolRamios

  • Sr. Member
  • ****
  • Posts: 305
Re: Color under mouse cursor/Click
« Reply #4 on: August 07, 2024, 07:33:20 pm »
Hi,

How do you find the X,Y position in Onclick?

Try OnMouseUp Instead or save the X,Y position in OnMouseMove and use it in OnClick.

B->

For some reason OnMouseUp really works. Thanks.
lazarus 3.2-fpc-3.2.2-win32/win64

 

TinyPortal © 2005-2018