基于C#实现屏幕取色器的示例详解
作者:芝麻粒儿 时间:2021-06-26 08:58:05
实践过程
效果
代码
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[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 Form1_Load(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
Point pt = new Point(Control.MousePosition.X, Control.MousePosition.Y);
Color cl = GetColor(pt);
panel1.BackColor = cl;
}
}
partial class Form1
{
/// <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.timer1 = new System.Windows.Forms.Timer(this.components);
this.panel1 = new System.Windows.Forms.Panel();
this.SuspendLayout();
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// panel1
//
this.panel1.Location = new System.Drawing.Point(12, 12);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(200, 100);
this.panel1.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(535, 298);
this.Controls.Add(this.panel1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.Panel panel1;
}
来源:https://zhima.blog.csdn.net/article/details/128123604
标签:C#,屏幕,取色器
0
投稿
猜你喜欢
Java队列篇之实现数组模拟队列及可复用环形队列详解
2021-09-12 17:49:42
Spring Bean实例的创建及构造器的挑选
2021-08-02 09:35:57
Java 如何调用long的最大值和最小值
2021-07-24 02:27:18
C# 弹出窗口show()和showdialog()的两种方式
2022-05-08 17:12:36
Android DialogUtils弹出窗工具类详解
2022-05-13 17:34:56
WPF中使用CallerMemberName简化InotifyPropertyChanged的实现
2023-05-08 16:27:07
Mybatis返回int或者Integer类型报错的解决办法
2023-08-09 02:41:14
c#委托与事件(详解)
2022-12-06 04:50:47
java 实现随机数组输出及求和实例详解
2021-10-27 13:12:50
并行Stream与Spring事务相遇会发生什么?
2022-08-28 15:40:12
ProtoBuf动态拆分Gradle Module解析
2022-01-06 17:45:21
Flutter验证码输入框的2种方法实现
2023-07-17 16:22:07
初识Spring Boot框架之Spring Boot的自动配置
2022-08-25 10:27:57
c++回调之利用函数指针示例
2022-07-26 06:59:32
如何基于SpringBoot实现人脸识别功能
2022-04-19 13:23:21
MapStruct处理Java中实体与模型间不匹配属性转换的方法
2023-08-02 22:31:28
详解AndroidStudio JNI +Gradle3.0以上JNI爬坑之旅
2021-09-01 05:45:04
C#十五子游戏编写代码
2023-06-13 07:33:22
MyBatis逆向工程的创建和使用
2022-07-18 20:07:55
基于java构造方法Vector遍历元素源码分析
2023-11-25 05:21:40