//
// Program: Screen takes color
/// Function: Dynamically get the color where the cursor is located in the current screen
/// Author: Tripoli
/// Name: Upto (Aba)
/// mailbox: 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.image;
Using system.Runtime.InteropServices;
Namespace libo.colorpicker
{
///
/// Form1 summary description.
/// summary>
Public Class Form1: System.Windows.Forms.form
{
// Desktop workspace size
Size Workingarea;
// Form's initial location and location in the lower left corner, 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 ()
{
InitializationComponent ();
This.FormBorderstyle = FormBorderstyle.fixedToolWindow;
THIS.StartPosition = formstartPosition.centerscreen
Rectangle Rect = systemInformation.workingarea;
Workingarea = new size (replact.width, rest.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 blank to start colors, press the ESC key to determine the color";
Tip.SetTooltip (this, tipmsg);
Tip.SetTooltip (LBLCOLOR, TIPMSG);
Tip.SetTooltip (TXTARGB, TIPMSG);
}
///
/// Clean all the resources being used.
/// summary>
Protected Override Void Dispose (Bool Disposing)
{
IF (Disposing)
{
IF (Components! = NULL)
{
Components.dispose ();
}
}
Base.dispose (Disposing);
}
#Region Windows Form Designer Generated Code
///
/// Designer supports the required method - do not use the code editor to modify
/// This method is content.
/// summary>
Private vidinitiRizeComponent ()
{
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_tic);
//
// txtargb
//
This.txTargb.Borderstyle = system.windows.Forms.Borderstyle.fixedsingle;
THISTXTARGB.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 takes color (UPTO)";
This.doubleClick = new system.eventhandler (this.form1_doubleclick);
THIS.MOUSEENTER = New System.EventHandler (this.form1_mouseenter);
THIS.ResumeLayout (false);
#ndregion
///
/// 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, / / Target device handle
INT NXDEST, / / X coordinate in the upper left corner of the target object
INT NYDEST, / / X coordinate in 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 Equipment Handle
INT NXSRC, // / 坐 坐 坐 in the upper left corner of the source
INT NYSRC, // 坐 坐 坐 坐 坐 坐
INT DWROP / / Raster operation value
);
[DLLIMPORT ("gdi32.dll")]]]]]]
Private static extern INTPTR CREATEDC (
String lpszdriver, // Drive name
String lpszdevice, // device name
String lpszoutput, // is useless, you can set "NULL"
INTPTR LPINITDATA / / Arbitrary Printer Data
);
Private Void Form1_DoubleClick (Object Sender, Eventargs E)
{
Formloc = THISLOCATION;
THIS.LOCATION = PtrightBottom;
THIS.TOPMOST = True;
TMR.enabled = true;
}
Private void TMR_Tick (Object Sender, Eventargs E)
{
// Create a display of the DC
INTPTR HDLDISPLAY = CREATEDC ("Display", NULL, NULL, INTPTR.ZERO);
// Create a new Graphics object from the handle of the specified device
Graphics gfxdisplay = graphics.fromhdc (hdldisplay);
// Create a Bitmap object with only one pixel size
Bitmap bmp = New Bitmap (1, 1, gfxdisplay);
/ / Create a new Graphics object from the specified image object
Graphics gfxbmp = graphics.fromimage (bmp);
// Get the handle of the screen
INTPTR HDLSCREEN = gfxdisplay.getHDC ();
// Get the handle of a bitmap
INTPTR HDLBMP = gfxbmp.getHDC ();
/ / Copy a pixel copy in the position of the mouse pointer in the current screen into the bit map
Bitblt (HDLBMP, 0, 0, 1, 1, HDLSCreen, MousePosition.x, MousePosition.y, 13369376);
// Release the screen handle
gfxdisplay.releaseHDC (HDLScreen);
// Release bitmap
GFXBMP.RELEASEHDC (HDLBMP);
lblcolor.backcolor = bmp.getpixel (0, 0); // Get the color of the pixel
TXTARGB.TEXT = "0x" lblcolor.backcolor.toargb (). TOSTRING ("x"). TouPper (); gfxdisplay.dispose (); gfxbmp.dispose ();
bmp.dispose (); // Release the resources used by BMP
}
Private void txtargb_keypress (Object Sender, KeyPressEventArgs E)
{
/ / When pressed the ESC button, determine the color argb value taken.
// Note: It is only valid when 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;
}
}
}
}