The screen color picker produced by C #

Source: Internet
Author: User

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
Namespace Getscrcolor
12 {
public partial class Form1:form
14 {
Public Form1 ()
16 {
InitializeComponent ();
18}
19
private bool flag = FALSE;
21st
public struct POINTAPI
23 {
public UINT X;
public uint Y;
26}
27
public class WinInfo
29 {
[DllImport ("User32.dll")]
public static extern UINT Windowfrompoint
32 (
X_point UINT,
Y_point UINT
35);
36
Panax Notoginseng [DllImport ("User32.dll")]
GetCursorPos public static extern bool
39 (
Ref POINTAPI P
41);
42
[DllImport ("User32.dll")]
ScreenToClient public static extern uint
45 (
A. UINT hwnd,
Ref POINTAPI P
48);
49
[DllImport ("User32.dll")]
Wuyi public static extern uint GetDC
52 (
(+) UINT HWND
54);
55
[DllImport ("Gdi32.dll")]
GetPixel public static extern uint
58 (
A-I-uint HDC,
A uint X,
All-in-a-uint y
62);
63
[DllImport ("User32.dll")]
ReleaseDC public static extern uint
66 (
An HWND uint,
-----UINT HDC
69);
70}
71
Form1_Load private void (object sender, EventArgs e)
73 {
Initpos (0,0,false);
The screen s = screen.primaryscreen;
Rectangle r = s.bounds;
iwidth int = r.width;
iheight int = r.height;
79//Create a bitmap as large as the screen
Image img = new Bitmap (iwidth,iheight);
81//Create a Graphics object from an object that inherits from the image class
The graphics.fromimage Graphics g = (IMG);
83//Crawl Full screen
G.copyfromscreen (new point (0, 0), new Point (0, 0), New Size (iwidth, iheight));
this. WindowState = formwindowstate.maximized;
this. BackgroundImage = img;
87}
88
Form1_mousemove private void (object sender, MouseEventArgs e)
90 {
Pointapi Papi = new Pointapi ();
Wininfo.getcursorpos (ref PAPI);
V_hwnd uint = Wininfo.windowfrompoint (papi.x, PAPI.Y);
94 UINT V_DC = WININFO.GETDC (V_hwnd);
Wininfo.screentoclient (V_hwnd, ref PAPI);
V_color uint = Wininfo.getpixel (V_DC, papi.x, PAPI.Y);
97
98 UINT v_red, V_green, V_blue;
v_red = V_color & 0xFF;
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 ");
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;
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 {
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

The screen color picker produced by C #

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.