//////////////////////////////////////////////////////////////////////////
/// 程序:屏幕取色
/// 功能:动态获取当前屏幕中光标所在位置的颜色
/// 作者:黎波
/// 网名:upto(阿球)
/// 邮箱:[email protected]
/// 日期:2004年3月31日
//////////////////////////////////////////////////////////////////////////
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>
/// form1 的摘要说明。
/// </summary>
public class form1 : system.windows.forms.form
{
// 桌面工作区的尺寸
size workingarea;
// form 的初始位置和在左下角,右下角的位置
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 = "在窗体空白处双击鼠标左键开始取色,按esc键确定颜色";
tip.settooltip(this, tipmsg);
tip.settooltip(lblcolor, tipmsg);
tip.settooltip(txtargb, tipmsg);
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.dispose();
}
}
base.dispose( disposing );
}
#region windows form designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </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 = "屏幕取色(upto)";
this.doubleclick += new system.eventhandler(this.form1_doubleclick);
this.mouseenter += new system.eventhandler(this.form1_mouseenter);
this.resumelayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[stathread]
static void main()
{
application.run(new form1());
}
[ dllimport ( "gdi32.dll" ) ]
private static extern bool bitblt (
intptr hdcdest, // 目标设备的句柄
int nxdest, // 目标对象的左上角的x坐标
int nydest, // 目标对象的左上角的x坐标
int nwidth, // 目标对象的矩形的宽度
int nheight, // 目标对象的矩形的长度
intptr hdcsrc, // 源设备的句柄
int nxsrc, // 源对象的左上角的x坐标
int nysrc, // 源对象的左上角的x坐标
int dwrop // 光栅的操作值
);
[ dllimport ( "gdi32.dll" ) ]
private static extern intptr createdc (
string lpszdriver, // 驱动名称
string lpszdevice, // 设备名称
string lpszoutput, // 无用,可以设定位"null"
intptr lpinitdata // 任意的打印机数据
);
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)
{
// 创建显示器的dc
intptr hdldisplay = createdc("display", null, null, intptr.zero);
// 从指定设备的句柄创建新的 graphics 对象
graphics gfxdisplay = graphics.fromhdc(hdldisplay);
// 创建只有一个象素大小的 bitmap 对象
bitmap bmp = new bitmap(1, 1, gfxdisplay);
// 从指定 image 对象创建新的 graphics 对象
graphics gfxbmp = graphics.fromimage(bmp);
// 获得屏幕的句柄
intptr hdlscreen = gfxdisplay.gethdc();
// 获得位图的句柄
intptr hdlbmp = gfxbmp.gethdc();
// 把当前屏幕中鼠标指针所在位置的一个象素拷贝到位图中
bitblt(hdlbmp, 0, 0, 1, 1, hdlscreen, mouseposition.x, mouseposition.y, 13369376);
// 释放屏幕句柄
gfxdisplay.releasehdc(hdlscreen);
// 释放位图句柄
gfxbmp.releasehdc(hdlbmp);
lblcolor.backcolor = bmp.getpixel(0, 0); // 获取像素的颜色
txtargb.text = "0x" + lblcolor.backcolor.toargb().tostring("x").toupper();
gfxdisplay.dispose();
gfxbmp.dispose();
bmp.dispose(); // 释放 bmp 所使用的资源
}
private void txtargb_keypress(object sender, keypresseventargs e)
{
// 当按下esc键时,确定所取的颜色argb值
// 注意:只有当窗体处于激活状态时才有效
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) //窗体在左下角
{
this.location = ptrightbottom;
}
else if (this.location == ptrightbottom) // 窗体在右下角
{
this.location = ptleftbottom;
}
}
}
}国内最大的酷站演示中心!