Home > Software engineering >  How to highlight mouse pointer position using Delphi
How to highlight mouse pointer position using Delphi

Time:06-11

I want to create a simple mouse pointer highlighter. A circle centered at the mouse pointer. It must be semi-transparent and visible everywhere (in and out of my application).

Any idea is appreciated.

Example:

enter image description here

CodePudding user response:

Although I believe this question is a bit too broad for Stack Overflow, I cannot resist writing this short answer, because it is not only easy -- but surprisingly easy -- to make a primitive implementation of this using almost nothing but the VCL.

The idea is to have a semi-transparent, borderless form (window) that follows the mouse cursor. An ordinary TTimer updates the form's position many times each second.

Create a new VCL application. In addition to your main form, also create another form, MouseDiscForm, with the following properties:

object MouseDiscForm: TMouseDiscForm
  AlphaBlend = True
  AlphaBlendValue = 127
  BorderStyle = bsNone
  ClientHeight = 64
  ClientWidth = 64
  Color = clWhite
  TransparentColor = True
  TransparentColorValue = clWhite
  FormStyle = fsStayOnTop
  object Shape1: TShape
    Align = alClient
    Brush.Color = clYellow
    Pen.Style = psClear
    Shape = stCircle
  end
end

Override the form's CreateParams method:

procedure TMouseDiskForm.CreateParams(var Params: TCreateParams);
begin
  inherited;
  Params.ExStyle := Params.ExStyle or WS_EX_LAYERED or WS_EX_TRANSPARENT;
end;

Then in your main form, simply add a TTimer with Interval = 50 and this OnTimer handler:

procedure TForm6.Timer1Timer(Sender: TObject);
begin
  var CP := Mouse.CursorPos;
  SetWindowPos(
    MouseDiscForm.Handle,
    HWND_TOPMOST,
    CP.X - MouseDiscForm.Width div 2,
    CP.Y - MouseDiscForm.Height div 2,
    0,
    0,
    SWP_SHOWWINDOW or SWP_NOSIZE or SWP_NOACTIVATE
  );
end;

I am sure there are a few additional details one has to consider, but generally I do find this very primitive approach to work quite well.

Screen recording

  • Related