Отображение предопределенных значков системы в C#
В этом примере отображаются доступные системные значки. Следующий способ DrawIconSample отображает значок и его название.
private const int column_width = 150;
private const int row_height = 50;
// Нарисуем образец и его имя.
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;
}
Код вызывает метод Graphics объекта DrawIconUnstretched для отображения значка, а затем рисует имя значка. Он заканчивается, увеличивая значение параметра x (который передается в метод по ссылке), поэтому следующий значок появится справа от него.
Класс SystemIcons предоставляет статические свойства, возвращающие стандартные стандартные значки. В обработчике событий Paint примера используются свойства SystemIcons и метод DrawIconSample для отображения значков.
// Рисуем образцы.
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);
}
