Search code examples
freepascallazarus

Lazarus Custom Component - how to return Mouse X,Y to label


I'm writing a custom component that will eventually be part of a menu panel. I'm currently trying to use TCustomPanel.MouseMove to capture the X,Y co-ordinates of the pointer as a TPoint so I can compare it to TRect Values.

Currently I'm trying to write the X,Y co-ordinates to a label to check it's working but I can't get it writing anything dynamic to the variable. The label is created at the correct point in the Canvas but it doesn't contain the string I'm trying to pass and nothing updates.

I have a custom paint routine, Do I need to update the label in there?

Where am I going wrong?

Interface:

uses
  SysUtils, Classes, StdCtrls, Controls, ExtCtrls, Graphics, Forms, Dialogs;

type
  TOC_MenuPanel = class(TCustomPanel)
  TOC_MenuStrings : TStringList;
private
  { Private declarations }
  fLinesText : TStrings;
  MenuRects : Array of TRect;
  MenuFontHeight : integer;
  MousePosX, MousePosY : TPoint;
  lbl : TLabel;

  procedure SetLinesText(const Value : TStrings);
  procedure SetFontHeight(const aNum : integer);

protected
  { Protected declarations }
  procedure Paint; override;
  procedure MouseMove(Shift:TShiftState; X,Y:Integer); override;

public
  { Public declarations }
  procedure SetInitialBounds(aLeft, aTop, aWidth, aHeight : integer); override;
  constructor create(AOwner: TComponent); override;
  // destructor Destroy; override;

published
  { Published declarations }
  property Items  : TStrings read fLinesText write SetLinesText;
  property FontSize  : integer read MenuFontHeight write SetFontHeight;

Implementation:

constructor TOC_MenuPanel.create(AOwner: TComponent);
  begin
    inherited Create(AOwner);
    fLinesText := TStringList.Create;

    lbl := TLabel.Create(AOwner);
    lbl.Parent := Self;
    lbl.Top := 100;
    lbl.Left := Width div 2 - (lbl.Width div 2);
  end;

// User actions
procedure TOC_MenuPanel.MouseMove(Shift:TShiftState; X,Y:Integer);
begin
  if (X >= 0) and (Y >= 0) and (X < Width) and (Y < Height) then
  begin
    lbl.Caption := 'X:' + IntToStr(X) + ',' + 'Y:' + IntToStr(Y);
  end
else
  begin
    // Do "move out" stuff over here.
  end;
  inherited;
end;       

Solution

  • LOL I was being a major NOOB, it doesn't update the label in the designer but when I run the program it works perfectly.

    I might have to consider returning the 1337 Tee shirt I bought the other day!