user6365505
user6365505

Reputation:

How change the mouse cursor inside a control?

I have a Remote Access similar to Team View software and I want change the mouse cursor ( on "Controler" part, server side in other words ) according with icon of mouse of "Controled" part, like was done in Team View software.

My software is using a TPaintBox because I need make others thing that is necessary a TPaintBox for work fine.

TPaintBox has crDefault as default cursor. How can I change this ( on "Controler" part ) only while mouse is inside the TPaintBox?

Here is code used for capture icon of mouse in "Controled" part ( client side ).

And here is my code until now trying change icon of mouse in "Controler" part ( server side ):

//pbRec is name of TPaintBox used

procedure TForm2.pbRecMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
  if Form2.lblPoint.Caption = 'OCR_NORMAL' then
    pbRec.Cursor := crDefault
  else if Form2.lblPoint.Caption = 'OCR_HAND' then
    pbRec.Cursor := crHandPoint
  else if Form2.lblPoint.Caption = 'OCR_IBEAM' then
    pbRec.Cursor := crIBeam;
end;

All suggestions will welcome.

Upvotes: 2

Views: 1565

Answers (1)

Johan
Johan

Reputation: 76753

If you want to change the cursor in code the following will work.

//Context: Timer.Interval = 50; :-)

procedure TForm57.Timer1Timer(Sender: TObject);
var
  p: TPoint;
begin
  if Ord(PaintBox1.Cursor) < Ord(crSizeAll) then PaintBox1.Cursor:= crArrow
  else PaintBox1.Cursor:= Pred(PaintBox1.Cursor);
  //Force Windows to change the cursor by sending a WM_SETCURSOR message.
  PaintBox1.Parent.Perform(WM_SETCURSOR, PaintBox1.Parent.Handle, MakeLParam(HTCLIENT, WM_MOUSEMOVE)); 
  (** //if you're viewing using a slow remote connection you make need to do this: 
  //Wiggle the mouse to force cursor change.
  GetCursorPos(p);
  SetCursorPos(p.x-1, p.y);
  Sleep(100); //needed on slow remote connection.
  SetCursorPos(p.x, p.y);  (**)
end;

If you're going to change the cursor based on some context every time the mouse enters the paintbox it's rather wasteful to do this in the MouseMove event.
Change it in the OnMouseEnter event instead.

procedure TForm57.PaintBox1MouseEnter(Sender: TObject);
begin
  if .... then PaintBox1.Cursor:= crIBeam
  else if .....
  PaintBox1.Parent.Perform(WM_SETCURSOR, PaintBox1.Parent.Handle, MakeLParam(HTCLIENT, WM_MOUSEMOVE));
end;

If you're on a remote connection then the client remote-side may cache the cursor. In that case you may need to wiggle it one side, Sleep(100) and wiggle it back for the client software to detect a mouse move and force a cursor refresh.

If you just want the cursor inside the PaintBox to be static, but different from the rest of the app then this works just fine:

enter image description here

Upvotes: 5

Related Questions