C#制作的屏幕取色器

摘要:
6使用System.Linq;7使用System.Text;9使用System.Runtime.InteropServices;76矩形r=s.边界;

  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#制作的屏幕取色器》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Linux学习4-阿里云服务器(CentOS)下使用 Tomcat安装 Jenkins函数的调用过程与出入栈下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

mysql 创建表 create table详解

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 4...

SpringSecurity中的授权

一.定义   所谓的授权,就是用户如果要访问某一个资源,我们要去检查用户是否具备这样的权限,如果具备就允许访问,如果不具备,则不允许访问。 二.准备测试用户(由于没有连接数据库)   1.基于内存配置测试用户      @Override protected void configure(AuthenticationManagerBuilder auth)...

gitlab忘记密码找回

在gitlab登录窗口,如果密码忘记了登录不进入,可以先尝试点击登录框下方的Forgot your password来通过邮箱的方式找回,如果邮箱不可以使用了,则看下面的强制找回方法 如图所示,需要输入当初设置的邮箱帐号,这一步前提是邮箱可以正常使用的情况下才可以使用的方法 通过xshell连接到gitlab安装的服务器上,然后输入su -...

MapReduce应用案例--单表关联

1. 实例描述   单表关联这个实例要求从给出的数据中寻找出所关心的数据,它是对原始数据所包含信息的挖掘。   实例中给出child-parent 表, 求出grandchild-grandparent表。   输入数据 file01: child parent Tom Lucy Tom Jack Jone...

初始化mysql数据库提示缺少Data:dumper模块解决方法

初始化默认数据库运行此命令:/usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql/      出现错误:      FATAL ERROR: please install the following Perl modules before exec...

第三课:部署Coredns服务

12. 部署DNS(master01) 12.1 部署coredns mkdir -p /root/dns && cd /root/dns kubectl create -f coredns.yaml [root@master01 dns]# kubectl create -f coredns.yaml serviceaccount/co...