found that many software colors are very pleasing to the eye, but do not know the specific color value is how much, a good reference. So I want to do a screen to pick the color of things. But this thing is purely for technical purposes, with QQ can get to this color value, even the brush can, but more complex. Put the effect up first. As shown in Figure 1.
Figure 1 Color Picker (very earthy)
To do this is to solve two problems as follows:
1. Global Mouse hook: Do this thing the only one technical difficulties, although the Internet has a variety of C + + DLL implementation, but always feel for a hook, in C # project to build a C + + project feel very uncomfortable. I finally found a way to create a new project. (described below.) )
2. Gets the color value of the point where the mouse is located. For experienced people, this is not a problem. People without experience, Google, is not really a problem.
Microsoft's help document swears that "Global hooks are not supported in the. NET framework". I don't know how many people get around. NET to implement the global hook. But some people do not believe this evil, made a C # implementation of the DLL does not need the global hook (see this article). Although limited to mouse, keyboard hooks, but also enough to use.
The first problem is solved. Let's look at the second question. The screen takes color. Google a little bit to know, with the Graphics object CopyFromScreen method is OK. But graphics is something in the form and there are no graphics objects in WPF. Of course, there can be another API call, but the total feeling of direct call API is not good. So think about it, or add a reference to System.Drawing in this WPF project. and wrap it up into a class. The code is as follows.
Pointcolorpicker
1using System.Windows;
2using System.Windows.Media;
3using Bitmap = System.Drawing.Bitmap;
4using Graphics = System.Drawing.Graphics;
5using Size = System.Drawing.Size;
6
7namespace colorpicker
8{
9 internal class Pointcolorpicker
{One
private static Bitmap Cach E = new Bitmap (1, 1);
private static Graphics Tempgraphics = Graphics.fromimage (cache);
/**////<summary>///Gets the Color from the "screen" at the
given point.
///<param name= "point" ></param>//////
<returns></retu rns> public
static Color getcolorfrompoint-
Tempgraphics.copyfromscree n (int) point. X, (int) point. Y, 0, 0, New Size (1, 1));
Return to
cache. GetPixel (0, 0). Upgrade ();
}
26}
Complete code Download