Tuesday, February 10, 2009

WPF Simple ColorPicker Control

Introduction

This is a simple ColorPicker control. It allows you to pick a color from a specified range. This ColorPicker includes some event wrappers for CurrentColorChanged and LastColorChanged. So when the value changes, the respective event will be thrown.

Background

The ColorPickerUnit is the left square, the ColorSelectorUnit describes the centered square in the image you can see, the ColorBarUnit is at the bottom, and shows the Current and Last selected color. Last but not least is the ColorInfoUnit which shows you the RGB and HEX value of the color. The main idea of this project was to clip the mouse to the ColorPicker or ColorSelectorUnit in the way which one has been clicked. So what I mean is, the mouse gets "jailed", you can't move out of this location. My second idea was to set the mouse to the last position wherever you clicked one of these units.

Using the Code

My project includes the following classes:

  • _ColorPicker.xaml with codefile: This one is the root element. It includes the basic events and the units mentioned above. So I can say this class controls the main user interaction.
  • CopyBox.cs: This class inherits from border, and the border includes a textbox, and one property called value, which sets or gets the current value of the CopyBoxes. That's all.
  • MouseClipping.cs : This is one tricky class. It allows to clip the mouse to any UIElement. This is possible by using an API-Method called ClipCursor which is stored in the user32.dll.
  • MouseControling.cs : This one also includes some API calls -> SetCursorPos, GetCursorPos, GetPixel, GetWindowDC and ReleaseDC.
  • ValueBox.cs : This class has some properties -> Maximum and Value, and some events. When you set the value property, the class calculates the length of a border. So you can see the value stored in a textbox and the position in graphic mode.

Article From Codeproject

No comments: