C#制作的屏幕取色器
来源:互联网 发布:关于发动机的软件 编辑:程序博客网 时间:2024/05/19 05:30
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
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
0 0
- C#制作的屏幕取色器
- C#制作屏幕捕捉程序
- C#制作简易屏幕保护
- 用制作C#作屏幕捕获程序
- c#制作仿win7屏幕键盘之笔记
- 用c#和wpf制作屏幕录像gif工具
- .NET制作的屏幕取色工具软件
- MTk屏幕切换效果的制作
- 制作适应屏幕旋转的QML界面
- 多种方法制作自适应屏幕的iframe
- MTk屏幕切换效果的制作
- MTk屏幕切换效果的制作
- (转)MTk屏幕切换效果的制作
- C#屏幕截图的实现
- C#屏幕锁的实现
- c#下的屏幕截图
- C# 获取屏幕的大小
- C# 获取屏幕的大小
- HBase 简单数据操作
- OpenCV数据读写操作
- 软件性能测试_loadrunner之web_custom_request函数做get接口测试
- 安装android studio后的设置
- Jedis下的ShardedJedis(分布式)使用方法(二)
- C#制作的屏幕取色器
- tableViewCell高度计算,自动计算 or 手动计算
- 转让App开发者账号
- 针对上一篇指针问题详细说明(c++向函数传递指针并修改其指向的问题)
- codahale Metrics
- Linux基础学习笔记之file
- 经典排序——堆排序——C语言版
- 静态语句块,语句块,构造函数执行顺序
- 数据库水平切分的实现原理解析——分库,分表,主从,集群,负载均衡器(转)