Your Ads Here

Tuesday, 14 February 2012

Display the SystemColors in C#.NET


It shows how to display the SystemColors in C# .NET. It calls a subroutine to display the name of each value in SystemColors and draw a sample.
Keywords: Csharp.NET, C#.NET, System  Colors.

Here the complete code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace testc
{
    public partial class Form1 : Form
    {
       public int x = 0;
       public int y = 0;
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
         
        }

        private void DrawSample(Graphics gr,ref   int x, ref int y,
            Color clr)
        {
            gr.DrawString(clr.Name, this.Font, Brushes.Black, x, y);
            SolidBrush the_brush = new SolidBrush(clr);
            gr.FillRectangle(the_brush, x, y + 15, 100, 20);
            gr.DrawRectangle(Pens.Black, x, y + 15, 100, 20);

            y += 40;
            if (y + 40 > this.ClientRectangle.Height)
            {
                x += 150;
                y = 0;
            }
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {

            x = 0;
            y = 0;
            DrawSample(e.Graphics,ref x,ref y, SystemColors.ActiveBorder);
            DrawSample(e.Graphics,ref x,ref y, SystemColors.ActiveCaption);           
            DrawSample(e.Graphics, ref x, ref y, SystemColors.ActiveCaptionText);
            DrawSample(e.Graphics, ref x, ref y, SystemColors.AppWorkspace);
            DrawSample(e.Graphics, ref x, ref y, SystemColors.Control);
            DrawSample(e.Graphics, ref x, ref y, SystemColors.ControlDark);
            DrawSample(e.Graphics, ref x, ref y, SystemColors.ControlDarkDark);
            DrawSample(e.Graphics, ref x, ref y, SystemColors.ControlLight);
            DrawSample(e.Graphics, ref x, ref y, SystemColors.ControlLightLight);
            DrawSample(e.Graphics, ref x, ref y, SystemColors.ControlText);         
            DrawSample(e.Graphics, ref x, ref y, SystemColors.Desktop);         
            DrawSample(e.Graphics, ref x, ref y, SystemColors.GrayText);
            DrawSample(e.Graphics, ref x, ref y, SystemColors.Highlight);          
            DrawSample(e.Graphics, ref x, ref y, SystemColors.HighlightText);
            DrawSample(e.Graphics, ref x, ref y, SystemColors.HotTrack);      
            DrawSample(e.Graphics, ref x, ref y, SystemColors.InactiveBorder);          
            DrawSample(e.Graphics, ref x, ref y, SystemColors.InactiveCaption);          
            DrawSample(e.Graphics, ref x, ref y, SystemColors.InactiveCaptionText);           
            DrawSample(e.Graphics, ref x, ref y, SystemColors.Info);          
            DrawSample(e.Graphics, ref x, ref y, SystemColors.InfoText);           
            DrawSample(e.Graphics, ref x, ref y, SystemColors.Menu);         
            DrawSample(e.Graphics, ref x, ref y, SystemColors.MenuText);          
            DrawSample(e.Graphics, ref x, ref y, SystemColors.ScrollBar);        
            DrawSample(e.Graphics, ref x, ref y, SystemColors.Window);         
            DrawSample(e.Graphics, ref x, ref y, SystemColors.WindowFrame);           
            DrawSample(e.Graphics, ref x, ref y, SystemColors.WindowText);
        } 
    }
}


1 comment: