Dynamically gets the color of the cursor in the current screen

Source: Internet
Author: User
Tags bool
Dynamic//////////////////////////////////////////////////////////////////////////

Program: Screen Color

Function: Dynamically get the color of the cursor position in the current screen

Author: Tripoli

Network Name: upto (a ball)

Email: itfun@163.com

Date: March 31, 2004

//////////////////////////////////////////////////////////////////////////


Using System;
Using System.Drawing;
Using System.Collections;
Using System.ComponentModel;
Using System.Windows.Forms;
Using System.Data;
Using System.Drawing.Imaging;
Using System.Runtime.InteropServices;

Namespace Libo.colorpicker
{
<summary>
Summary description of the Form1.
</summary>
public class Form1:System.Windows.Forms.Form
{
Size of the desktop workspace
Size Workingarea;
The initial position of the Form and the position in the lower-left corner, the lower-right corner
Point Formloc, Ptleftbottom, Ptrightbottom;

Private System.Windows.Forms.Label Lblcolor;
Private System.Windows.Forms.TextBox Txtargb;
Private System.Windows.Forms.Timer TMR;
Private System.Windows.Forms.ToolTip tip;
Private System.ComponentModel.IContainer components;

Public Form1 ()
{
InitializeComponent ();

This. FormBorderStyle = Formborderstyle.fixedtoolwindow;
This. StartPosition = Formstartposition.centerscreen;

Rectangle rect = Systeminformation.workingarea;
Workingarea = new Size (rect. Width, Rect. Height);
Ptleftbottom = new Point (0, Workingarea.height-this. Height);
Ptrightbottom = new Point (workingarea.width-this. Width,
Workingarea.height-this. Height);

String tipmsg = "Double-click the left mouse button in the form blank to start taking the color, press ESC to determine the color";
Tip. SetToolTip (this, tipmsg);
Tip. SetToolTip (Lblcolor, tipmsg);
Tip. SetToolTip (Txtargb, tipmsg);
}

<summary>
Clean up all resources that are in use.
</summary>
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (Components!= null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing);
}

#region Windows Form Designer generated code
<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</summary>
private void InitializeComponent ()
{
this.components = new System.ComponentModel.Container ();
This.lblcolor = new System.Windows.Forms.Label ();
THIS.TMR = new System.Windows.Forms.Timer (this.components);
This.txtargb = new System.Windows.Forms.TextBox ();
This.tip = new System.Windows.Forms.ToolTip (this.components);
This. SuspendLayout ();
//
Lblcolor
//
This.lblColor.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
This.lblColor.Location = new System.Drawing.Point (8, 8);
This.lblColor.Name = "Lblcolor";
This.lblColor.TabIndex = 0;
//
Tmr
//
This.tmr.Tick + = new System.EventHandler (This.tmr_tick);
//
Txtargb
//
This.txtArgb.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
This.txtArgb.Font = new System.Drawing.Font ("Arial", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte) (0));
This.txtArgb.Location = new System.Drawing.Point (8, 40);
This.txtArgb.Name = "Txtargb";
This.txtArgb.TabIndex = 1;
This.txtArgb.Text = "";
This.txtArgb.KeyPress + = new System.Windows.Forms.KeyPressEventHandler (this.txtargb_keypress);
//
Form1
//
This. AutoScaleBaseSize = new System.Drawing.Size (6, 14);
This. ClientSize = new System.Drawing.Size (115, 70);
This. Controls.Add (THIS.TXTARGB);
This. Controls.Add (This.lblcolor);
This. Name = "Form1";
This. Text = "screen take color (upto)";
This. DoubleClick + = new System.EventHandler (this. Form1_doubleclick);
This. MouseEnter + = new System.EventHandler (this. Form1_mouseenter);
This. ResumeLayout (FALSE);

}
#endregion

<summary>
The main entry point for the application.
</summary>
[STAThread]
static void Main ()
{
Application.Run (New Form1 ());
}

[DllImport ("Gdi32.dll")]
private static extern bool BitBlt (
IntPtr hdcdest,//handle of target device
int nxdest,//x coordinate of the upper-left corner of the target object
int nydest,//x coordinate of the upper-left corner of the target object
int nwidth,//The width of the rectangle of the target object
int nheight,//The length of the rectangle of the target object
IntPtr hdcsrc,//source device handle
int NXSRC,//x coordinate of the upper-left corner of the source object
int NYSRC,//x coordinate of the upper-left corner of the source object
int Dwrop//Raster operation value
);

[DllImport ("Gdi32.dll")]
private static extern IntPtr CreateDC (
String lpszdriver,//driver name
String lpszdevice,//device name
String lpszoutput,//useless, can be set to locate "NULL"
INTPTR Lpinitdata//Any printer data
);

private void Form1_doubleclick (object sender, EventArgs e)
{
Formloc = this. Location;
This. Location = Ptrightbottom;
This. topmost = true;
Tmr. Enabled = true;
}

private void Tmr_tick (object sender, EventArgs e)

{

To create a DC for the monitor

IntPtr Hdldisplay = CreateDC ("DISPLAY", NULL, NULL, IntPtr.Zero);

Creates a new Graphics object from the handle of the specified device

Graphics Gfxdisplay = GRAPHICS.FROMHDC (Hdldisplay);

Create a Bitmap object of only one pixel size

Bitmap bmp = New Bitmap (1, 1, gfxdisplay);

Creates a new Graphics object from the specified Image object

Graphics gfxbmp = graphics.fromimage (BMP);

Get the handle to the screen

IntPtr Hdlscreen = Gfxdisplay.gethdc ();

Get a handle to a bitmap

IntPtr hdlbmp = Gfxbmp.gethdc ();

Copy a pixel in the current screen where the mouse pointer is located in the bitmap

BitBlt (hdlbmp, 0, 0, 1, 1, Hdlscreen, mouseposition.x, MOUSEPOSITION.Y, 13369376);

Release screen handle

GFXDISPLAY.RELEASEHDC (Hdlscreen);

Releasing a bitmap handle

GFXBMP.RELEASEHDC (hdlbmp);

Lblcolor.backcolor = BMP. GetPixel (0, 0); Get the color of a pixel

Txtargb.text = "0x" + LblColor.BackColor.ToArgb (). ToString ("X"). ToUpper ();

Gfxdisplay.dispose ();
Gfxbmp.dispose ();

Bmp. Dispose (); Resources used to release BMP

}



private void Txtargb_keypress (object sender, KeyPressEventArgs e)
{
Determines the color ARGB value when the ESC key is pressed
Note: Valid only if the form is active
if (E.keychar = = (char) keys.escape)
{
Tmr. Enabled = false;
This. Location = Formloc;
This. topmost = false;
Txtargb.selectall ();
}
}

private void Form1_mouseenter (object sender, EventArgs e)
{
if (this. Location = = ptleftbottom)//form in the lower left corner
{
This. Location = Ptrightbottom;
}
else if (this. Location = = ptrightbottom)//form in the lower right corner
{
This. Location = Ptleftbottom;
}
}
}
}

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.