RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TScreen.Cursor Property

Controls the mouse cursor image on a global level.

Pascal
property Cursor: TCursor;
C++
__property TCursor Cursor;

Set Cursor to override the cursor image associated with individual control objects. When Cursor is crDefault, the individual objects determine the cursor image. Assigning any other value sets the mouse cursor image for all windows belonging to the application. The global mouse cursor image remains in effect until the screen's Cursor property is changed back to crDefault

Cursor can be set to any of the cursor values available in the Cursors property. This can be one of the built-in cursor values, or a custom cursor that has been added to the Cursors property.  

C++ Examples: 

 

/*
This example shows how to add custom cursors to an
application.  The following code makes this cursor available
to the application via the constant crMyCursor, and sets it
as the global cursor to the application.
*/

#include <memory>       //for STL auto_ptr class

const crMyCursor = 5;

  void __fastcall TForm1::Button2Click(TObject *Sender)
{
  std::auto_ptr<Graphics::TBitmap> bmpMask(new Graphics::TBitmap);
  std::auto_ptr<Graphics::TBitmap> bmpColor(new Graphics::TBitmap);
  std::auto_ptr<TIconInfo> iconInfo(new TIconInfo);

  bmpMask->LoadFromFile("../SquareMask.bmp");
  bmpColor->LoadFromFile("../Square.bmp");

  iconInfo->fIcon = false;
  iconInfo->xHotspot = 15;
  iconInfo->yHotspot = 15;
  iconInfo->hbmMask = bmpMask->Handle;
  iconInfo->hbmColor = bmpColor->Handle;

  Screen->Cursors[crMyCursor] = CreateIconIndirect(iconInfo.get());

  Screen->Cursor = crMyCursor;
}
/*
Assignments to the Screen object's cursor property are
typically guarded by a try...finally statement to ensure
that normal cursor behavior is restored. Note that the
original cursor is saved and restored.  This allows cursor
changes to be nested without accidentally restoring the
cursor to a hard-coded value prematurely.
*/
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TCursor Save_Cursor = Screen->Cursor;
Screen->Cursor = crHourGlass;    // Show hourglass cursor 
try
{
  // Do some lengthy operation 
  int I = 0;
  int J = 0;
  int X, Y;
  const magicnumber = 150000000;
  while (I < magicnumber)
  {
    Randomize;
    while (J < magicnumber)
    {
      Y = Random(J);
      J++;
    }
    X = Random(I);
    I++;
  }
}
__finally
{
  Screen->Cursor = Save_Cursor; // always restore the cursor
}
}

 

Delphi Examples: 

{
This example shows how to add custom cursors to an
application.  The following code makes this cursor available
to the application via the constant crMyCursor, and sets it
as the global cursor to the application.
} 
var
  bmpMask : TBitmap;
  bmpColor : TBitmap;
  iconInfo : TIconInfo;
const
  crMyCursor = 5;

procedure TForm1.Button2Click(Sender: TObject);
begin
  bmpMask := TBitmap.Create;
  bmpColor := TBitmap.Create;

  bmpMask.LoadFromFile('SquareMask.bmp');
  bmpColor.LoadFromFile('Square.bmp');

  with iconInfo do
  begin
    fIcon := false;
    xHotspot := 15;
    yHotspot := 15;
    hbmMask := bmpMask.Handle;
    hbmColor := bmpColor.Handle;
  end;

  Screen.Cursors[crMyCursor] := CreateIconIndirect(iconInfo);
  
  Screen.Cursor := crMyCursor;

  bmpMask.Free;
  bmpColor.Free;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  DestroyIcon(Screen.Cursors[crMyCursor]);
end;
{
Assignments to the Screen object's cursor property are
typically guarded by a try...finally statement to ensure
that normal cursor behavior is restored. Note that the
original cursor is saved and restored.  This allows cursor
changes to be nested without accidentally restoring the
cursor to a hard-coded value prematurely.
}
procedure TForm1.Button1Click(Sender: TObject);
var 
  Save_Cursor : TCursor;
  I, J: Integer;
  X, Y: Word;
const
  magicnumber = 3000000;
begin
  Save_Cursor := Screen.Cursor;
  Screen.Cursor := crHourGlass;    { Show hourglass cursor }
  try
    { Do some lengthy operation }
  I := 0;
  J := 0;
  while I < magicnumber do
  begin
    Randomize;
    while J < magicnumber do
    begin
      Y := Random(J);
      Inc(J);
    end;
    X := Random(I);
    Inc(I);
  end;
  finally
    Screen.Cursor := Save_Cursor;  { Always restore to normal }
  end;
end;

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!