C#製作的螢幕取色器
阿新 • • 發佈:2019-02-05
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Linq;
7using System.Text;
8using System.Windows.Forms;
9using System.Runtime.InteropServices;
10
11namespace GetScrColor
12{
13publicpartialclass Form1 : Form
14 {
15public Form1()
16 {
17 InitializeComponent();
18 }
19
20privatebool flag =false;
21
22publicstruct POINTAPI
23 {
24publicuint x;
25publicuint y;
26 }
27
28publicclass WinInfo
29 {
30 [DllImport("user32.dll")]
31publicstaticexternuint WindowFromPoint
32 (
33uint x_point,
34uint y_point
35 );
36
37 [DllImport("user32.dll")]
38publicstaticexternbool GetCursorPos
39 (
40ref POINTAPI p
41 );
42
43 [DllImport("user32.dll")]
44publicstaticexternuint ScreenToClient
45 (
46uint hwnd,
47ref POINTAPI p
48 );
49
50 [DllImport("user32.dll")]
51publicstaticexternuint GetDC
52 (
53uint hwnd
54 );
55
56 [DllImport("gdi32.dll")]
57publicstaticexternuint GetPixel
58 (
59uint hDC,
60uint x,
61uint y
62 );
63
64 [DllImport("user32.dll")]
65publicstaticexternuint ReleaseDC
66 (
67uint hwnd,
68uint hdc
69 );
70 }
71
72privatevoid Form1_Load(object sender, EventArgs e)
73 {
74 InitPos(0,0,false);
75 Screen s = Screen.PrimaryScreen;
76 Rectangle r = s.Bounds;
77int iWidth = r.Width;
78int 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));
85this.WindowState = FormWindowState.Maximized;
86this.BackgroundImage = img;
87 }
88
89privatevoid Form1_MouseMove(object sender, MouseEventArgs e)
90 {
91 POINTAPI Papi =new POINTAPI();
92 WinInfo.GetCursorPos(ref Papi);
93uint v_hwnd = WinInfo.WindowFromPoint(Papi.x, Papi.y);
94uint v_DC = WinInfo.GetDC(v_hwnd);
95 WinInfo.ScreenToClient(v_hwnd, ref Papi);
96uint v_Color = WinInfo.GetPixel(v_DC, Papi.x, Papi.y);
97
98uint 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
103this.txtRGB.Text ="#"+v_Red.ToString("x").PadLeft(2, '0') + v_Green.ToString("x").PadLeft(2, '0') + v_Blue.ToString("x").PadLeft(2, '0');
104this.txtRGBDesc.Text = v_Red.ToString("d") +""+ v_Green.ToString("d") +""+ v_Blue.ToString("d");
105this.txtShow.BackColor = Color.FromArgb((int)v_Red, (int)v_Green, (int)v_Blue);
106 WinInfo.ReleaseDC(v_hwnd, v_DC);
107 }
108
109privatevoid InitPos(int x,int y,bool bFlag)
110 {
111 panel1.Left = x;
112 panel1.Top = y;
113 flag = bFlag;
114 }
115
116
117privatevoid panel1_MouseEnter(object sender, EventArgs e)
118 {
119if (!flag)
120 {
121 InitPos(this.Width - panel1.Width, 0, true);
122 }
123else
124 {
125 InitPos(0, 0, false);
126 }
127 }
128
129privatevoid Form1_KeyDown(object sender, KeyEventArgs e)
130 {
131if (e.KeyCode == Keys.Escape)
132 {
133this.Close();
134 }
135 }
136
137 }
138}
139
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Linq;
7using System.Text;
8using System.Windows.Forms;
9using System.Runtime.InteropServices;
10
11namespace GetScrColor
12{
13publicpartialclass Form1 : Form
14 {
15public Form1()
16 {
17 InitializeComponent();
18 }
19
20privatebool flag =false;
21
22publicstruct POINTAPI
23 {
24publicuint x;
25publicuint y;
26 }
27
28publicclass WinInfo
29 {
30 [DllImport("user32.dll")]
31publicstaticexternuint WindowFromPoint
32 (
33uint x_point,
34uint y_point
35 );
36
37 [DllImport("user32.dll")]
38publicstaticexternbool GetCursorPos
39 (
40ref POINTAPI p
41 );
42
43 [DllImport("user32.dll")]
44publicstaticexternuint ScreenToClient
45 (
46uint
47ref POINTAPI p
48 );
49
50 [DllImport("user32.dll")]
51publicstaticexternuint GetDC
52 (
53uint hwnd
54 );
55
56 [DllImport("gdi32.dll")]
57publicstaticexternuint GetPixel
58 (
59uint hDC,
60uint x,
61uint y
62 );
63
64 [DllImport("user32.dll")]
65publicstaticexternuint ReleaseDC
66 (
67uint hwnd,
68uint hdc
69 );
70 }
71
72privatevoid Form1_Load(object sender, EventArgs e)
73 {
74 InitPos(0,0,false);
75 Screen s = Screen.PrimaryScreen;
76 Rectangle r = s.Bounds;
77int iWidth = r.Width;
78int 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));
85this.WindowState = FormWindowState.Maximized;
86this.BackgroundImage = img;
87 }
88
89privatevoid Form1_MouseMove(object sender, MouseEventArgs e)
90 {
91 POINTAPI Papi =new POINTAPI();
92 WinInfo.GetCursorPos(ref Papi);
93uint v_hwnd = WinInfo.WindowFromPoint(Papi.x, Papi.y);
94uint v_DC = WinInfo.GetDC(v_hwnd);
95 WinInfo.ScreenToClient(v_hwnd, ref Papi);
96uint v_Color = WinInfo.GetPixel(v_DC, Papi.x, Papi.y);
97
98uint 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
103this.txtRGB.Text ="#"+v_Red.ToString("x").PadLeft(2, '0') + v_Green.ToString("x").PadLeft(2, '0') + v_Blue.ToString("x").PadLeft(2, '0');
104this.txtRGBDesc.Text = v_Red.ToString("d") +""+ v_Green.ToString("d") +""+ v_Blue.ToString("d");
105this.txtShow.BackColor = Color.FromArgb((int)v_Red, (int)v_Green, (int)v_Blue);
106 WinInfo.ReleaseDC(v_hwnd, v_DC);
107 }
108
109privatevoid InitPos(int x,int y,bool bFlag)
110 {
111 panel1.Left = x;
112 panel1.Top = y;
113 flag = bFlag;
114 }
115
116
117privatevoid panel1_MouseEnter(object sender, EventArgs e)
118 {
119if (!flag)
120 {
121 InitPos(this.Width - panel1.Width, 0, true);
122 }
123else
124 {
125 InitPos(0, 0, false);
126 }
127 }
128
129privatevoid Form1_KeyDown(object sender, KeyEventArgs e)
130 {
131if (e.KeyCode == Keys.Escape)
132 {
133this.Close();
134 }
135 }
136
137 }
138}
139