Display images of the cursors available in C#


example

Using a cursor in C# is easy. Simply set the Cursor property of a control (including a form) to the cursor you want to use as in the following code.

this.Cursor = Cursors.WaitCursor;

Making images of cursors is a little harder because they don’t show up on normal screen captures.

This example displays images of the available cursors. Each is shown in a PictureBox that uses the appropriate cursor so you can compare the images to the real thing. Some of the results don’t match perfectly because the real cursor uses XOR mode drawing, animation, and other advanced graphics techniques that look nice in the cursor but that the cursor cannot draw perfectly. (For example, see the Wait cursor.)

The program uses the following ShowCursor method to display the cursors.

// Display a cursor.
private void ShowCursor(string cursor_name, Cursor the_cursor)
{
    // Make a Panel to hold the Label and PictureBox.
    Panel pan = new Panel();
    pan.Size = new Size(Wid, Hgt);
    pan.Cursor = the_cursor;
    flpSamples.Controls.Add(pan);

    // Display the cursor's name in a Label.
    Label lbl = new Label();
    lbl.AutoSize = false;
    lbl.Text = cursor_name;
    lbl.Size = new Size(Wid, 13);
    lbl.TextAlign = ContentAlignment.MiddleCenter;
    lbl.Location = new Point(0, 0);
    pan.Controls.Add(lbl);

    // Draw the cursor onto a Bitmap.
    Bitmap bm = new Bitmap(BmWid, BmWid);
    using (Graphics gr = Graphics.FromImage(bm))
    {
        the_cursor.Draw(gr, new Rectangle(0, 0, BmWid, BmWid));
    }

    // Display the Bitmap in a PictureBox.
    PictureBox pic = new PictureBox();
    pic.Location = new Point((Wid - BmWid) / 2, 15);
    pic.BorderStyle = BorderStyle.Fixed3D;
    pic.ClientSize = new Size(BmWid, BmWid);
    pic.Image = bm;
    pan.Controls.Add(pic);
}

This code makes a Panel containing a Label to show the cursor’s name and a PictureBox to show the cursor’s image.

The most interesting part of the code is where it calls the cursor’s Draw method to make the cursor draw itself onto a Graphics object. That’s the secret to displaying an image of the cursor.


Download Example   Follow me on Twitter   RSS feed




This entry was posted in graphics and tagged , , , , , , , , , . Bookmark the permalink.

One Response to Display images of the cursors available in C#

  1. Pingback: Enumerate HatchStyle values and display samples of them in C# -

Leave a Reply

Your email address will not be published. Required fields are marked *