基于C#實(shí)現(xiàn)屏幕取色器
更新時(shí)間:2022年12月14日 17:00:18 作者:芝麻粒兒
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)簡(jiǎn)易的屏幕取色器,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以了解一下
實(shí)踐過(guò)程
效果

代碼
public partial class FrmGetColor : Form
{
public FrmGetColor()
{
InitializeComponent();
}
#region 定義快捷鍵
//如果函數(shù)執(zhí)行成功,返回值不為0。
//如果函數(shù)執(zhí)行失敗,返回值為0。要得到擴(kuò)展錯(cuò)誤信息,調(diào)用GetLastError。
[DllImport("user32.dll", SetLastError = true)]
public static extern bool RegisterHotKey(
IntPtr hWnd, //要定義熱鍵的窗口的句柄
int id, //定義熱鍵ID(不能與其它ID重復(fù))
KeyModifiers fsModifiers, //標(biāo)識(shí)熱鍵是否在按Alt、Ctrl、Shift、Windows等鍵時(shí)才會(huì)生效
Keys vk //定義熱鍵的內(nèi)容
);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool UnregisterHotKey(
IntPtr hWnd, //要取消熱鍵的窗口的句柄
int id //要取消熱鍵的ID
);
//定義了輔助鍵的名稱(將數(shù)字轉(zhuǎn)變?yōu)樽址员阌谟洃洠部扇コ嗣杜e而直接使用數(shù)值)
[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)
{
//注銷(xiāo)Id號(hào)為81的熱鍵設(shè)定
UnregisterHotKey(Handle, 81);
}
}
partial class FrmGetColor
{
/// <summary>
/// 必需的設(shè)計(jì)器變量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
/// <param name="disposing">如果應(yīng)釋放托管資源,為 true;否則為 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗體設(shè)計(jì)器生成的代碼
/// <summary>
/// 設(shè)計(jì)器支持所需的方法 - 不要
/// 使用代碼編輯器修改此方法的內(nèi)容。
/// </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 = "鼠標(biāo)位置:";
//
// 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 = "網(wǎng)頁(yè)顏色:";
//
// 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 = "關(guān)于";
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鍵復(fù)制網(wǎng)頁(yè)顏色";
//
// 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 = "復(fù)制顏色值:";
//
// 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;
}
到此這篇關(guān)于基于C#實(shí)現(xiàn)屏幕取色器的文章就介紹到這了,更多相關(guān)C#屏幕取色器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解如何在C#中循環(huán)訪問(wèn)目錄樹(shù)
在C#中,訪問(wèn)文件系統(tǒng)是常見(jiàn)的需求之一,有時(shí)我們需要遍歷目錄樹(shù)以執(zhí)行某些操作,例如搜索文件、計(jì)算目錄大小或執(zhí)行批量處理,本文將詳細(xì)介紹如何在C#中循環(huán)訪問(wèn)目錄樹(shù),并提供一個(gè)完整的示例,需要的朋友可以參考下2024-08-08
C#導(dǎo)入導(dǎo)出EXCEL文件的代碼實(shí)例
這篇文章主要介紹了C#導(dǎo)入導(dǎo)出EXCEL文件代碼實(shí)例,代碼的流程和方法都很詳細(xì),需要的朋友可以參考下2014-04-04
C#遞歸實(shí)現(xiàn)將一整數(shù)逆序后放入一數(shù)組中
這篇文章主要介紹了C#遞歸實(shí)現(xiàn)將一整數(shù)逆序后放入一數(shù)組中,是遞歸算法的一個(gè)簡(jiǎn)單應(yīng)用,需要的朋友可以參考下2014-10-10
C# DateTime與時(shí)間戳轉(zhuǎn)換實(shí)例
本篇文章主要介紹了C# DateTime與時(shí)間戳轉(zhuǎn)換實(shí)例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06
如何利用C#通過(guò)sql語(yǔ)句操作Sqlserver數(shù)據(jù)庫(kù)教程
ado.net提供了豐富的數(shù)據(jù)庫(kù)操作,下面這篇文章主要給大家介紹了關(guān)于如何利用C#通過(guò)sql語(yǔ)句操作Sqlserver數(shù)據(jù)庫(kù)教程的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-10-10

