Click here to Skip to main content
15,886,199 members
Articles / Desktop Programming / WPF
Article

WPF Colors

Rate me:
Please Sign up or sign in to vote.
2.14/5 (5 votes)
2 Jul 2008CPOL 43.7K   738   9   1
Show WPF Colors

Introduction

This is a simple WPF app.
I used ScrollViewer and UniformGrid elements to show colors.
You can save the hexadecimal number of each color to the clipboard by clicking on it.

Using the Code

XAML Code

XML
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF Colors (Click to save to the Clipboard)" Height="300" Width="300">
    <Grid>
        <ScrollViewer Name="Scroll_Viewer">
            <UniformGrid Name="Uni_form_Grid">
                <!-- Placement of color rectangles  -->
            </UniformGrid>
        </ScrollViewer>
    </Grid>
</Window>

C# Code

C#
public Window1()
{
    InitializeComponent();
    Uni_form_Grid.Children.Clear();//Clear all items in Uni_form_Grid
    //collects Brushes' property to an array
    Type t = typeof(System.Windows.Media.Brushes);
    PropertyInfo[] colors = t.GetProperties();
    //\\
    int i = -1;
    foreach (PropertyInfo color in colors)
    {
        i++;

        BrushConverter convertor = new BrushConverter();
        Brush brush = convertor.ConvertFromString(color.Name) as Brush;

        string hex = string.Format("#{0}", brush.ToString().Substring(3));

        Rectangle rectangle = new Rectangle();
        rectangle.Fill = brush;//Rectangle background color
        rectangle.ToolTip = hex;//ToolTip of Rectangle
        //rectangle.Height = 20;
        //rectangle.Width = 20;
        rectangle.Stroke = Brushes.Black;//border of Rectangle
        //add event to each Rectangle
        rectangle.MouseDown += new MouseButtonEventHandler(rectangle_MouseDown);

        Uni_form_Grid.Children.Add(rectangle);
    }
}

/// <summary>
/// Save data to Clipboard
/// </summary>
void rectangle_MouseDown(object sender, MouseButtonEventArgs e)
{
    Rectangle rect = (Rectangle)sender;
    Clipboard.SetData("Text", rect.ToolTip.ToString());
}

History

  • 2nd July, 2008: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Iran (Islamic Republic of) Iran (Islamic Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralAslam-o-Alaikum Pin
Rai Shahid7-Aug-08 18:32
Rai Shahid7-Aug-08 18:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.