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:
procedure TForm1.PaintBox1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
FClickPos := Point(X, Y);
Paintbox1.Invalidate; // Trigger redraw of the paintbox
end;
procedure TForm1.Paintbox1Draw(Sender: TObject);
begin
// ... your drawing code here
if (FClickPos.X >= 0) and (FClickPos.Y >= 0) then begin
Label2.Font.Color:= PaintBox1.Canvas.Pixels[FClickPos.X, FClickPos.y];
Label2.Show;
end else
Label2.Hide;
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.
procedure TForm1.FormCreate(Sender: TObject);
begin
FClickPos := Point(-1, -1);
end;
procedure TForm1.Paintbox1MouseLeave(Sender: TObject);
begin
FClickPos := Point(-1, -1);
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.