網站首頁 編程語言 正文
實踐過程
效果
代碼
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
相關推薦
- 2022-04-16 Python實現杰卡德距離以及環比算法講解_python
- 2023-06-05 最新python?字符串數組互轉問題_python
- 2023-07-10 Python使用MongoDB數據庫
- 2022-05-11 使用Redission實現分布式鎖
- 2022-07-08 Pytest如何使用mark的方法_python
- 2022-07-19 Linux 性能監測命令
- 2023-12-07 com.fasterxml.jackson.databind.ObjectMapper
- 2024-03-24 SpringBoot工具庫:解決SpringBoot2.*版本跨域問題
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支