首页 > 代码库 > C#制作的屏幕取色器
C#制作的屏幕取色器
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9 using System.Runtime.InteropServices;
10
11 namespace GetScrColor
12 {
13 public partial class Form1 : Form
14 {
15 public Form1()
16 {
17 InitializeComponent();
18 }
19
20 private bool flag = false;
21
22 public struct POINTAPI
23 {
24 public uint x;
25 public uint y;
26 }
27
28 public class WinInfo
29 {
30 [DllImport("user32.dll")]
31 public static extern uint WindowFromPoint
32 (
33 uint x_point,
34 uint y_point
35 );
36
37 [DllImport("user32.dll")]
38 public static extern bool GetCursorPos
39 (
40 ref POINTAPI p
41 );
42
43 [DllImport("user32.dll")]
44 public static extern uint ScreenToClient
45 (
46 uint hwnd,
47 ref POINTAPI p
48 );
49
50 [DllImport("user32.dll")]
51 public static extern uint GetDC
52 (
53 uint hwnd
54 );
55
56 [DllImport("gdi32.dll")]
57 public static extern uint GetPixel
58 (
59 uint hDC,
60 uint x,
61 uint y
62 );
63
64 [DllImport("user32.dll")]
65 public static extern uint ReleaseDC
66 (
67 uint hwnd,
68 uint hdc
69 );
70 }
71
72 private void Form1_Load(object sender, EventArgs e)
73 {
74 InitPos(0,0,false);
75 Screen s = Screen.PrimaryScreen;
76 Rectangle r = s.Bounds;
77 int iWidth = r.Width;
78 int iHeight = r.Height;
79 //创建一个和屏幕一样大的bitmap
80 Image img = new Bitmap(iWidth,iHeight);
81 //从一个继承自image类的对象中创建Graphics对象
82 Graphics g = Graphics.FromImage(img);
83 //抓取全屏幕
84 g.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(iWidth, iHeight));
85 this.WindowState = FormWindowState.Maximized;
86 this.BackgroundImage = img;
87 }
88
89 private void Form1_MouseMove(object sender, MouseEventArgs e)
90 {
91 POINTAPI Papi = new POINTAPI();
92 WinInfo.GetCursorPos(ref Papi);
93 uint v_hwnd = WinInfo.WindowFromPoint(Papi.x, Papi.y);
94 uint v_DC = WinInfo.GetDC(v_hwnd);
95 WinInfo.ScreenToClient(v_hwnd, ref Papi);
96 uint v_Color = WinInfo.GetPixel(v_DC, Papi.x, Papi.y);
97
98 uint v_Red, v_Green, v_Blue;
99 v_Red = v_Color & 0xff;
100 v_Green = (v_Color & 0xff00) / 256;
101 v_Blue = (v_Color & 0xff0000) / 65536;
102
103 this.txtRGB.Text ="#"+v_Red.ToString("x").PadLeft(2, ‘0‘) + v_Green.ToString("x").PadLeft(2, ‘0‘) + v_Blue.ToString("x").PadLeft(2, ‘0‘);
104 this.txtRGBDesc.Text = v_Red.ToString("d") + " " + v_Green.ToString("d") + " " + v_Blue.ToString("d");
105 this.txtShow.BackColor = Color.FromArgb((int)v_Red, (int)v_Green, (int)v_Blue);
106 WinInfo.ReleaseDC(v_hwnd, v_DC);
107 }
108
109 private void InitPos(int x,int y,bool bFlag)
110 {
111 panel1.Left = x;
112 panel1.Top = y;
113 flag = bFlag;
114 }
115
116
117 private void panel1_MouseEnter(object sender, EventArgs e)
118 {
119 if (!flag)
120 {
121 InitPos(this.Width - panel1.Width, 0, true);
122 }
123 else
124 {
125 InitPos(0, 0, false);
126 }
127 }
128
129 private void Form1_KeyDown(object sender, KeyEventArgs e)
130 {
131 if (e.KeyCode == Keys.Escape)
132 {
133 this.Close();
134 }
135 }
136
137 }
138 }
139
C#制作的屏幕取色器