基于C#实现屏幕取色器
作者:芝麻粒儿 发布时间:2023-02-16 04:17:53
标签:C#,屏幕,取色器
实践过程
效果
代码
public partial class FrmGetColor : Form
{
public FrmGetColor()
{
InitializeComponent();
}
#region 定义快捷键
//如果函数执行成功,返回值不为0。
//如果函数执行失败,返回值为0。要得到扩展错误信息,调用GetLastError。
[DllImport("user32.dll", SetLastError = true)]
public static extern bool RegisterHotKey(
IntPtr hWnd, //要定义热键的窗口的句柄
int id, //定义热键ID(不能与其它ID重复)
KeyModifiers fsModifiers, //标识热键是否在按Alt、Ctrl、Shift、Windows等键时才会生效
Keys vk //定义热键的内容
);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool UnregisterHotKey(
IntPtr hWnd, //要取消热键的窗口的句柄
int id //要取消热键的ID
);
//定义了辅助键的名称(将数字转变为字符以便于记忆,也可去除此枚举而直接使用数值)
[Flags()]
public enum KeyModifiers
{
None = 0,
Alt = 1,
Ctrl = 2,
Shift = 4,
WindowsKey = 8
}
#endregion
[DllImport("gdi32.dll")]
static public extern uint GetPixel(IntPtr hDC, int XPos, int YPos);
[DllImport("gdi32.dll")]
static public extern IntPtr CreateDC(string driverName, string deviceName, string output, IntPtr lpinitData);
[DllImport("gdi32.dll")]
static public extern bool DeleteDC(IntPtr DC);
static public byte GetRValue(uint color)
{
return (byte) color;
}
static public byte GetGValue(uint color)
{
return ((byte) (((short) (color)) >> 8));
}
static public byte GetBValue(uint color)
{
return ((byte) ((color) >> 16));
}
static public byte GetAValue(uint color)
{
return ((byte) ((color) >> 24));
}
public Color GetColor(Point screenPoint)
{
IntPtr displayDC = CreateDC("DISPLAY", null, null, IntPtr.Zero);
uint colorref = GetPixel(displayDC, screenPoint.X, screenPoint.Y);
DeleteDC(displayDC);
byte Red = GetRValue(colorref);
byte Green = GetGValue(colorref);
byte Blue = GetBValue(colorref);
return Color.FromArgb(Red, Green, Blue);
}
private void FrmGetColor_Load(object sender, EventArgs e)
{
this.TopMost = true;
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked == true)
{
this.TopMost = true;
}
else
{
this.TopMost = false;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
txtPoint.Text = Control.MousePosition.X.ToString() + "," + Control.MousePosition.Y.ToString();
Point pt = new Point(Control.MousePosition.X, Control.MousePosition.Y);
Color cl = GetColor(pt);
panel1.BackColor = cl;
txtRGB.Text = cl.R + "," + cl.G + "," + cl.B;
txtColor.Text = ColorTranslator.ToHtml(cl).ToString();
RegisterHotKey(Handle, 81, KeyModifiers.Ctrl, Keys.F);
}
private void button1_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button2_Click(object sender, EventArgs e)
{
AboutBox1 ab = new AboutBox1();
ab.ShowDialog();
}
private void label3_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("http://www.mrbccd.com");
}
protected override void WndProc(ref Message m)
{
const int WM_HOTKEY = 0x0312;
//按快捷键
switch (m.Msg)
{
case WM_HOTKEY:
switch (m.WParam.ToInt32())
{
case 81: //按下的是CTRL+F
Clipboard.SetText(txtColor.Text.Trim());
break;
}
break;
}
base.WndProc(ref m);
}
private void FrmGetColor_Leave(object sender, EventArgs e)
{
}
private void FrmGetColor_FormClosed(object sender, FormClosedEventArgs e)
{
//注销Id号为81的热键设定
UnregisterHotKey(Handle, 81);
}
}
partial class FrmGetColor
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.txtPoint = new System.Windows.Forms.TextBox();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.txtRGB = new System.Windows.Forms.TextBox();
this.panel1 = new System.Windows.Forms.Panel();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.panel2 = new System.Windows.Forms.Panel();
this.txtColor = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.panel3 = new System.Windows.Forms.Panel();
this.label6 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.panel2.SuspendLayout();
this.panel3.SuspendLayout();
this.SuspendLayout();
//
// checkBox1
//
this.checkBox1.AutoSize = true;
this.checkBox1.Checked = true;
this.checkBox1.CheckState = System.Windows.Forms.CheckState.Checked;
this.checkBox1.Location = new System.Drawing.Point(6, 7);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(120, 16);
this.checkBox1.TabIndex = 0;
this.checkBox1.Text = "是否显示在最顶层";
this.checkBox1.UseVisualStyleBackColor = true;
this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged);
//
// txtPoint
//
this.txtPoint.Location = new System.Drawing.Point(146, 26);
this.txtPoint.Name = "txtPoint";
this.txtPoint.Size = new System.Drawing.Size(111, 21);
this.txtPoint.TabIndex = 1;
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// txtRGB
//
this.txtRGB.Location = new System.Drawing.Point(146, 51);
this.txtRGB.Name = "txtRGB";
this.txtRGB.Size = new System.Drawing.Size(111, 21);
this.txtRGB.TabIndex = 2;
//
// panel1
//
this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.panel1.Location = new System.Drawing.Point(6, 33);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(60, 60);
this.panel1.TabIndex = 4;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(79, 31);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(65, 12);
this.label1.TabIndex = 5;
this.label1.Text = "鼠标位置:";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(80, 56);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(65, 12);
this.label2.TabIndex = 6;
this.label2.Text = "R G B 值:";
//
// panel2
//
this.panel2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.panel2.Controls.Add(this.txtColor);
this.panel2.Controls.Add(this.label4);
this.panel2.Controls.Add(this.txtPoint);
this.panel2.Controls.Add(this.label2);
this.panel2.Controls.Add(this.checkBox1);
this.panel2.Controls.Add(this.label1);
this.panel2.Controls.Add(this.txtRGB);
this.panel2.Controls.Add(this.panel1);
this.panel2.Location = new System.Drawing.Point(6, 7);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(278, 104);
this.panel2.TabIndex = 7;
//
// txtColor
//
this.txtColor.Location = new System.Drawing.Point(146, 76);
this.txtColor.Name = "txtColor";
this.txtColor.Size = new System.Drawing.Size(111, 21);
this.txtColor.TabIndex = 9;
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(79, 81);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(65, 12);
this.label4.TabIndex = 8;
this.label4.Text = "网页颜色:";
//
// button1
//
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.button1.Location = new System.Drawing.Point(220, 156);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(53, 21);
this.button1.TabIndex = 8;
this.button1.Text = "退出";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(166, 156);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(48, 21);
this.button2.TabIndex = 9;
this.button2.Text = "关于";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// panel3
//
this.panel3.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.panel3.Controls.Add(this.label6);
this.panel3.Controls.Add(this.label5);
this.panel3.Location = new System.Drawing.Point(6, 117);
this.panel3.Name = "panel3";
this.panel3.Size = new System.Drawing.Size(278, 33);
this.panel3.TabIndex = 10;
//
// label6
//
this.label6.AutoSize = true;
this.label6.Location = new System.Drawing.Point(82, 8);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(149, 12);
this.label6.TabIndex = 1;
this.label6.Text = "按住Ctrl+F键复制网页颜色";
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(5, 9);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(77, 12);
this.label5.TabIndex = 0;
this.label5.Text = "复制颜色值:";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Cursor = System.Windows.Forms.Cursors.Help;
this.label3.Location = new System.Drawing.Point(7, 160);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(131, 12);
this.label3.TabIndex = 11;
this.label3.Text = "http://www.mrbccd.com";
this.label3.Click += new System.EventHandler(this.label3_Click);
//
// FrmGetColor
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.WhiteSmoke;
this.ClientSize = new System.Drawing.Size(288, 180);
this.Controls.Add(this.label3);
this.Controls.Add(this.panel3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.panel2);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "FrmGetColor";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "颜色拾取器";
this.Load += new System.EventHandler(this.FrmGetColor_Load);
this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.FrmGetColor_FormClosed);
this.Leave += new System.EventHandler(this.FrmGetColor_Leave);
this.panel2.ResumeLayout(false);
this.panel2.PerformLayout();
this.panel3.ResumeLayout(false);
this.panel3.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.CheckBox checkBox1;
private System.Windows.Forms.TextBox txtPoint;
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.TextBox txtRGB;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.TextBox txtColor;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Panel panel3;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label6;
}
来源:https://zhima.blog.csdn.net/article/details/128256673
0
投稿
猜你喜欢
- 目录前言整合swagger api自定义配置信息简单使用Swagger常用注解Api标记ApiOperation标记ApiParam标记Ap
- 什么是slf4jSLF4J,即简单日志门面(Simple Logging Facade for Java),不是具体的日志解决方案,它只服务
- 有些情况下,在开发一些C#小项目的时候,交付给别人用的时候就是单独EXE文件,但是若涉及什么EXCEL,图片什么的时候,比较麻烦,这时候可以
- 一.模拟问题最近在公司遇到一个问题,挂号系统是做的集群,比如启动了两个相同的服务,病人挂号的时候可能会出现同号的情况,比如两个病人挂出来的号
- 本文实例为大家分享了java实现砸金蛋抽奖的具体代码,供大家参考,具体内容如下代码如下需求:用户每一次砸金蛋,抽中一等奖的概率为2% 二等奖
- 目录一.什么是负载均衡二.负载均衡的简单分类三.为什么需要做负载均衡四.springCloud如何开启负载均衡五.IRule1.Random
- 经典的Java基础面试题集锦,欢迎收藏和分享。问题:如果main方法被声明为private会怎样?答案:能正常编译,但运行的时候会提示”ma
- JPA连接到数据库,调用存储过程,这样的需求很常见。本文就针对这一点,讲述如何使用spring Data JPA调用存储过程的方法。1、存储
- 手机升级到安卓O后,突然发现创建快捷方式的功能失效了,查询一番后发现:安卓O要使用ShortcutManager来创建快捷方式。安卓N及以下
- 这段C#代码主要是验证身份证的开头和身份证的格式和长度是否正确,没有按照身份证的编码规则进行严格验证/// <summary>
- Date类概述java.util.Date类 表示特定的瞬间,精确到毫秒。 继续查阅Date类的描述,发现Date拥有多个构造函数,只是部分
- 本文实例为大家分享了C#绘制饼状图和柱状图的具体代码,供大家参考,具体内容如下#代码如下:using System;using System
- 场景我们团队现在面临着多端数据接口对接的问题,为了解决这个问题我们定义了接口对接的规范,前端(安卓,Ios,web前端)和后端进行了数据的格
- 用java压缩/解压文件: import java.io.*; import java.awt.*; import java.aw
- AtomicInteger 类底层存储一个int值,并提供方法对该int值进行原子操作。AtomicInteger 作为java.util.
- 谷歌的AI击败了一位围棋大师,是一种衡量人工智能突然的快速发展的方式,也揭示了这些技术如何发展而来和将来可以如何发展。人工智能是一种未来性的
- 一.并行LINQSystem.Linq名称空间中包含的类ParallelEnumerable可以分解查询的工作,使其分布在多个线程上。尽管E
- Json的简介JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。它基于ECMAScript的一个
- 多态概述多态概念:所谓多态就是指程序中定义的引用变量所指向的具体类型和通过该引用变量发出的方法调用在编程时并不确定,而是在程序运行期间才确定
- 本文实例讲述了C#利用原图和水印图的重叠简单实现水印的方法。分享给大家供大家参考,具体如下:图片操作类/// <summary>