Cursor.Draw(Graphics, Rectangle) Method

Definition

Draws the cursor on the specified surface, within the specified bounds.

public void Draw (System.Drawing.Graphics g, System.Drawing.Rectangle targetRect);

Parameters

g
Graphics

The Graphics surface on which to draw the Cursor.

targetRect
Rectangle

The Rectangle that represents the bounds of the Cursor.

Examples

The following code example draws the specified cursor on the form in its normal size, and in stretched mode, twice its size. This example requires that you have a Form and a Cursor object to pass into the method when it is called.

private void DrawCursorsOnForm(Cursor cursor)
{
   // If the form's cursor is not the Hand cursor and the 
   // Current cursor is the Default, Draw the specified 
   // cursor on the form in normal size and twice normal size.
   if(this.Cursor != Cursors.Hand & 
     Cursor.Current == Cursors.Default)
   {
      // Draw the cursor stretched.
      Graphics graphics = this.CreateGraphics();
      Rectangle rectangle = new Rectangle(
        new Point(10,10), new Size(cursor.Size.Width * 2, 
        cursor.Size.Height * 2));
      cursor.DrawStretched(graphics, rectangle);
        
      // Draw the cursor in normal size.
      rectangle.Location = new Point(
      rectangle.Width + rectangle.Location.X, 
        rectangle.Height + rectangle.Location.Y);
      rectangle.Size = cursor.Size;
      cursor.Draw(graphics, rectangle);

      // Dispose of the cursor.
      cursor.Dispose();
   }
}

Remarks

The drawing command originates on the graphics surface represented by the g parameter, but a Graphics does not contain information about how to render a given image, so it passes the call to the Cursor. The Draw method crops the image to the given dimensions and allows you to specify a Rectangle within which to draw the Cursor. This method is typically used if you want to draw the cursor on a Graphics surface. For example, you might have a dialog that allows the user to select cursors from a ListBox control, or a group of RadioButton controls.

Applies to

מוצר גירסאות
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

See also