当前位置: 首页 > news >正文

动态获取当前屏幕中光标所在位置的颜色

//

/// 程序:屏幕取色

/// 功能:动态获取当前屏幕中光标所在位置的颜色

///作者:黎波

///网名:upto(阿球)

/// 邮箱:itfun@163.com

/// 日期:<chsdate year="2004" month="3" day="31" islunardate="False" isrocdate="False" w:st="on"></chsdate>2004331

//

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

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • python使用技巧(二十五):*args, **kwargs生成可变列表与字典
  • 爱因斯坦在普朗克生日会上的演讲:探索的动机
  • python使用技巧(二十六):批量复制图片或文件到另一文件目录
  • 完美解决ubuntu系统的中文输入法无法写入VS Code的问题
  • 谁比谁活得更长 杜丽
  • AI模型设计:yolov1+darknet+yolov2,3,4,5,X全系列资料汇总[源码仓库]标星收藏
  • C++使用技巧(八):输入输出读写文件
  • C++使用技巧(九):ubuntu环境下Eigen线性代数库的简单使用(C++版本的numpy库))
  • C++使用技巧(十):C++编译生成与调用自定义静态库/动态库
  • 张越:智慧要包括一种向善之心
  • C++使用技巧(十一):函数返回一个数组
  • 张越:每张脸背后都有故事
  • C++使用技巧(十三):Google-GTest测试框架的安装与使用(demo源码实现)
  • C++使用技巧(十四):ubuntu16.04--C++ (Cpp) PCM示例与pcm及音频算法的参考资料
  • 熊节:谁震撼了世界——第14届Jolt奖点评
  • “寒冬”下的金三银四跳槽季来了,帮你客观分析一下局面
  • 【技术性】Search知识
  • 【译】React性能工程(下) -- 深入研究React性能调试
  • avalon2.2的VM生成过程
  • CentOS 7 防火墙操作
  • Consul Config 使用Git做版本控制的实现
  • hadoop入门学习教程--DKHadoop完整安装步骤
  • JS基础篇--通过JS生成由字母与数字组合的随机字符串
  • JWT究竟是什么呢?
  • Next.js之基础概念(二)
  • PHP 7 修改了什么呢 -- 2
  • text-decoration与color属性
  • 代理模式
  • 关于Android中设置闹钟的相对比较完善的解决方案
  • 简析gRPC client 连接管理
  • 京东美团研发面经
  • 使用 QuickBI 搭建酷炫可视化分析
  • 它承受着该等级不该有的简单, leetcode 564 寻找最近的回文数
  • 一份游戏开发学习路线
  • 一个JAVA程序员成长之路分享
  • 译米田引理
  • 长三角G60科创走廊智能驾驶产业联盟揭牌成立,近80家企业助力智能驾驶行业发展 ...
  • # 透过事物看本质的能力怎么培养?
  • (12)Linux 常见的三种进程状态
  • (31)对象的克隆
  • (4) openssl rsa/pkey(查看私钥、从私钥中提取公钥、查看公钥)
  • (分布式缓存)Redis分片集群
  • (附源码)ssm基于jsp的在线点餐系统 毕业设计 111016
  • (附源码)ssm经济信息门户网站 毕业设计 141634
  • (六)Hibernate的二级缓存
  • (算法)N皇后问题
  • (原)本想说脏话,奈何已放下
  • (转)拼包函数及网络封包的异常处理(含代码)
  • .java 指数平滑_转载:二次指数平滑法求预测值的Java代码
  • .NET 6 在已知拓扑路径的情况下使用 Dijkstra,A*算法搜索最短路径
  • .Net 6.0--通用帮助类--FileHelper
  • .Net Core 中间件验签
  • .NET core 自定义过滤器 Filter 实现webapi RestFul 统一接口数据返回格式
  • .Net MVC + EF搭建学生管理系统
  • .NET 给NuGet包添加Readme