Magnifying glass for the keyboard cursor

Asked

Viewed 343 times

2

I would like to create a magnifying glass to enlarge the region around where the keyboard cursor is positioned and also the focus on a form. I wonder if anyone has any suggestions on the subject.

I found a source but it’s about the mouse position.

procedure TForm1.Timer1Timer(Sender: TObject);
var
  Srect,Drect,PosForme:TRect;
  iWidth,iHeight,DmX,DmY:Integer;
  iTmpX,iTmpY:Real;
  C:TCanvas;
  hDesktop: Hwnd;
  Kursor:TPoint;
begin
  If not IsIconic(Application.Handle) then begin
  hDesktop:= GetDesktopWindow;
  GetCursorPos(Kursor);
  PosForme:=Rect(Form1.Left,Form1.Top,Form1.Left+Form1.Width,Form1.Top+Form1.Height);
  If not PtInRect(PosForme,Kursor) then begin

If Panel1.Visible=True then Panel1.Visible:=False;
If Image1.Visible=False then Image1.Visible:=True;

 iWidth:=Image1.Width;
     iHeight:=Image1.Height;
Drect:=Rect(0,0,iWidth,iHeight);

iTmpX:=iWidth / (Slider.Position * 4);
iTmpY:=iHeight / (Slider.Position * 4);

Srect:=Rect(Kursor.x,Kursor.y,Kursor.x,Kursor.y);
InflateRect(Srect,Round(iTmpX),Round(iTmpY));
// move Srect if outside visible area of the screen
If Srect.Left<0 then OffsetRect(Srect,-Srect.Left,0);
If Srect.Top<0 then OffsetRect(Srect,0,-Srect.Top);
If Srect.Right>Screen.Width then OffsetRect(Srect,-(Srect.Right-Screen.Width),0);
If Srect.Bottom>Screen.Height then OffsetRect(Srect,0,-(Srect.Bottom-Screen.Height));

C:=TCanvas.Create;
try
  C.Handle:=GetDC(GetDesktopWindow);
  Image1.Canvas.CopyRect(Drect,C,Srect);
 finally
   ReleaseDC(hDesktop, C.Handle);
   C.Free;
    end;
    If cbSrediste.Checked=True then begin // show crosshair
        with Image1.Canvas do begin
        DmX:=Slider.Position * 2 * (Kursor.X-Srect.Left);
        DmY:=Slider.Position * 2 * (Kursor.Y-Srect.Top);  
         MoveTo(DmX - (iWidth div 4),DmY); // -
         LineTo(DmX + (iWidth div 4),DmY); // -
             MoveTo(DmX,DmY - (iHeight div 4)); // |
         LineTo(DmX,DmY + (iHeight div 4)); // |
      end; // with image1.Canvas
    end; // show crosshair
    Application.ProcessMessages;
    end // Cursor not inside form
   else begin  // cursor inside form
    If Panel1.Visible=False then Panel1.Visible:=True;
    If Image1.Visible=True then Image1.Visible:=False;
  end;
 end; // IsIconic
end;

end.

1 answer

2

To capture the Rect wrapped around the object with focus use the function:

GetWindowRect(GetFocus, Srect);

If you replace in your code the part:

Srect:=Rect(Kursor.x,Kursor.y,Kursor.x,Kursor.y);

by the above function, will already produce an effect close to the desired one, being necessary to apply only a basic mathematics, changing the value of the top, left, right and bottom of Srect so that a greater coverage is applied to zoom.

For this, subtract an Offset from Left and Top, and increment an Offset in Right and Bottom

Browser other questions tagged

You are not signed in. Login or sign up in order to post.