[C# Helper]
Index Books FAQ Contact About Rod
[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[Beginning Software Engineering]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Display the predefined system icons in C#

[Display the predefined system icons in C#]

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 the example to experiment with it and to see additional details.

© 2009-2022 Rocky Mountain Computer Consulting, Inc. All rights reserved.