//////////////////////////////////////////////////////////////////////////
/// 程序:屏幕取色
/// 功能:動態獲取當前屏幕中光標所在位置的顏色
/// 作者:黎波
/// 網名: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;
}
}
}
}國內最大的酷站演示中心!