惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - Franky
D
Docker
Jina AI
Jina AI
The GitHub Blog
The GitHub Blog
博客园 - 聂微东
B
Blog RSS Feed
大猫的无限游戏
大猫的无限游戏
M
MIT News - Artificial intelligence
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
博客园 - 叶小钗
爱范儿
爱范儿
D
DataBreaches.Net
Hugging Face - Blog
Hugging Face - Blog
IT之家
IT之家
Recent Announcements
Recent Announcements
U
Unit 42
腾讯CDC
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
宝玉的分享
宝玉的分享
量子位
Stack Overflow Blog
Stack Overflow Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Microsoft Azure Blog
Microsoft Azure Blog

博客园 - 投石问路

ASP做贝宝(paypal)支付接口(推荐)[转] - 投石问路 - 博客园 asp paypal接口处理 - 投石问路 - 博客园 Asp.Net 学习资源列表 推荐几个.NET开源图表组件 机器克隆会导致DTC进程堵死 [SQLServer2005]ADO.NET2.0中的查询通知 SQL数据缓存依赖 [SqlServer | Cache | SqlCacheDependency ] -- [转] 讲解SQL 2000的Collection排序规则 (3)[转] 讲解SQL 2000的Collection排序规则 (2)[转] 讲解SQL 2000的Collection排序规则 (1)[转] IIS出现server application error,解决方案一 Excel开发(VSTO2005):简化工作表中选定区域的操作。[轉載] .NET 2.0 CER学习笔记 [转载] 格式化类型连接地址 FLV Flash video streaming with ASP.NET 2.0, IIS and HTTP handler [转载] ASP.NET 视频截图功能的C#代码[转载] NET 2.0(C#)调用ffmpeg处理视频的方法 利用Mencoder转换视频格式[转载] FLV播放器 源码二例[转载]
C#动态获取当前屏幕中光标所在位置的颜色
投石问路 · 2008-03-20 · via 博客园 - 投石问路

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;
}
}
}
}

原文出处:

http://www.host01.com/article/Net/00020007/0561316505784534.htm