Display the predefined system icons in C#

[system icons]

This example displays the available system icons. The following DrawIconSample method displays an icon and its name.

private const int column_width = 150;
private const int row_height = 50;

// Draw a sample and its name.
private void DrawIconSample(Graphics gr, ref int x, int y,
    Icon ico, string ico_name)
{
    gr.DrawIconUnstretched(ico,
        new Rectangle(x, y, ico.Width, ico.Height));
    int text_y = y + (int)(ico.Height -
        gr.MeasureString(ico_name, this.Font).Height) / 2;
    gr.DrawString(ico_name, this.Font, Brushes.Black,
        x + ico.Width + 5, text_y);
    x += column_width;
}

The code calls the Graphics object’s DrawIconUnstretched method to display the icon and then draws the icon’s name. It finishes by increasing the value of the parameter x (which is passed into the method by reference) so the next icon will appear to the right of this one.

The SystemIcons class provides static properties that return predefined standard icons. The example’s Paint event handler uses the SystemIcons properties and the DrawIconSample method to display the icons.

// Draw samples.
private void Form1_Paint(object sender, PaintEventArgs e)
{
    int x = 10;
    int y = 10;
    DrawIconSample(e.Graphics, ref x, y,
        SystemIcons.Application, "Application");
    DrawIconSample(e.Graphics, ref x, y,
        SystemIcons.Asterisk, "Asterisk");
    DrawIconSample(e.Graphics, ref x, y,
        SystemIcons.Error, "Error");
    x = 10;
    y += 50;
    DrawIconSample(e.Graphics, ref x, y,
        SystemIcons.Exclamation, "Exclamation");
    DrawIconSample(e.Graphics, ref x, y,
        SystemIcons.Hand, "Hand");
    DrawIconSample(e.Graphics, ref x, y,
        SystemIcons.Information, "Information");
    x = 10;
    y += row_height;
    DrawIconSample(e.Graphics, ref x, y,
        SystemIcons.Question, "Question");
    DrawIconSample(e.Graphics, ref x, y,
        SystemIcons.Shield, "Shield");
    DrawIconSample(e.Graphics, ref x, y,
        SystemIcons.Warning, "Warning");
    x = 10;
    y += 50;
    DrawIconSample(e.Graphics, ref x, y,
        SystemIcons.WinLogo, "WinLogo");

    this.ClientSize = new Size(3 * column_width, 4 * row_height);
}

The event handler uses DrawIconSample to draw the samples, increasing t and resetting x as necessary. After it finishes drawing the samples, the code sets the form’s ClientSize so the form is big enough to show all of the icons.


Download Example   Follow me on Twitter   RSS feed   Donate




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

2 Responses to Display the predefined system icons in C#

  1. Ali says:

    nice blog,you need like buttons google plus , facebook etc for posts.

Leave a Reply

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