目录
实践过程
效果
代码
public partial class FrmGetColor : Form
{
public FrmGetColor()
{
InitializeComponent();
}
#region 定义快捷键
[DllImport("user.dll", SetLastError = true)]
public static extern bool RegisterHotKey(
IntPtr hWnd,
int id,
KeyModifiers fsModifiers,
Keys vk
);
[DllImport("user.dll", SetLastError = true)]
public static extern bool UnregisterHotKey(
IntPtr hWnd,
int id
);
[Flags()]
public enum KeyModifiers
{
None =,
Alt =,
Ctrl =,
Shift =,
WindowsKey =
}
#endregion
[DllImport("gdi.dll")]
static public extern uint GetPixel(IntPtr hDC, int XPos, int YPos);
[DllImport("gdi.dll")]
static public extern IntPtr CreateDC(string driverName, string deviceName, string output, IntPtr lpinitData);
[DllImport("gdi.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)) >>));
}
static public byte GetBValue(uint color)
{
return ((byte) ((color) >>));
}
static public byte GetAValue(uint color)
{
return ((byte) ((color) >>));
}
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 checkBox_CheckedChanged(object sender, EventArgs e)
{
if (checkBox.Checked == true)
{
this.TopMost = true;
}
else
{
this.TopMost = false;
}
}
private void timer_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);
panel.BackColor = cl;
txtRGB.Text = cl.R + "," + cl.G + "," + cl.B;
txtColor.Text = ColorTranslator.ToHtml(cl).ToString();
RegisterHotKey(Handle,, KeyModifiers.Ctrl, Keys.F);
}
private void button_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button_Click(object sender, EventArgs e)
{
AboutBox ab = new AboutBox1();
ab.ShowDialog();
}
private void label_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("http://www.mrbccd.com");
}
protected override void WndProc(ref Message m)
{
const int WM_HOTKEY =x0312;
switch (m.Msg)
{
case WM_HOTKEY:
switch (m.WParam.ToInt())
{
case:
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)
{
UnregisterHotKey(Handle,);
}
}
partial class FrmGetColor
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.checkBox = new System.Windows.Forms.CheckBox();
this.txtPoint = new System.Windows.Forms.TextBox();
this.timer = new System.Windows.Forms.Timer(this.components);
this.txtRGB = new System.Windows.Forms.TextBox();
this.panel = new System.Windows.Forms.Panel();
this.label = new System.Windows.Forms.Label();
this.label = new System.Windows.Forms.Label();
this.panel = new System.Windows.Forms.Panel();
this.txtColor = new System.Windows.Forms.TextBox();
this.label = new System.Windows.Forms.Label();
this.button = new System.Windows.Forms.Button();
this.button = new System.Windows.Forms.Button();
this.panel = new System.Windows.Forms.Panel();
this.label = new System.Windows.Forms.Label();
this.label = new System.Windows.Forms.Label();
this.label = new System.Windows.Forms.Label();
this.panel.SuspendLayout();
this.panel.SuspendLayout();
this.SuspendLayout();
this.checkBox.AutoSize = true;
this.checkBox.Checked = true;
this.checkBox.CheckState = System.Windows.Forms.CheckState.Checked;
this.checkBox.Location = new System.Drawing.Point(6, 7);
this.checkBox.Name = "checkBox1";
this.checkBox.Size = new System.Drawing.Size(120, 16);
this.checkBox.TabIndex = 0;
this.checkBox.Text = "是否显示在最顶层";
this.checkBox.UseVisualStyleBackColor = true;
this.checkBox.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged);
this.txtPoint.Location = new System.Drawing.Point(, 26);
this.txtPoint.Name = "txtPoint";
this.txtPoint.Size = new System.Drawing.Size(, 21);
this.txtPoint.TabIndex =;
this.timer.Enabled = true;
this.timer.Tick += new System.EventHandler(this.timer1_Tick);
this.txtRGB.Location = new System.Drawing.Point(, 51);
this.txtRGB.Name = "txtRGB";
this.txtRGB.Size = new System.Drawing.Size(, 21);
this.txtRGB.TabIndex =;
this.panel.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.panel.Location = new System.Drawing.Point(6, 33);
this.panel.Name = "panel1";
this.panel.Size = new System.Drawing.Size(60, 60);
this.panel.TabIndex = 4;
this.label.AutoSize = true;
this.label.Location = new System.Drawing.Point(79, 31);
this.label.Name = "label1";
this.label.Size = new System.Drawing.Size(65, 12);
this.label.TabIndex = 5;
this.label.Text = "鼠标位置:";
this.label.AutoSize = true;
this.label.Location = new System.Drawing.Point(80, 56);
this.label.Name = "label2";
this.label.Size = new System.Drawing.Size(65, 12);
this.label.TabIndex = 6;
this.label.Text = "R G B 值:";
this.panel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.panel.Controls.Add(this.txtColor);
this.panel.Controls.Add(this.label4);
this.panel.Controls.Add(this.txtPoint);
this.panel.Controls.Add(this.label2);
this.panel.Controls.Add(this.checkBox1);
this.panel.Controls.Add(this.label1);
this.panel.Controls.Add(this.txtRGB);
this.panel.Controls.Add(this.panel1);
this.panel.Location = new System.Drawing.Point(6, 7);
this.panel.Name = "panel2";
this.panel.Size = new System.Drawing.Size(278, 104);
this.panel.TabIndex = 7;
this.txtColor.Location = new System.Drawing.Point(, 76);
this.txtColor.Name = "txtColor";
this.txtColor.Size = new System.Drawing.Size(, 21);
this.txtColor.TabIndex =;
this.label.AutoSize = true;
this.label.Location = new System.Drawing.Point(79, 81);
this.label.Name = "label4";
this.label.Size = new System.Drawing.Size(65, 12);
this.label.TabIndex = 8;
this.label.Text = "网页颜色:";
this.button.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.button.Location = new System.Drawing.Point(220, 156);
this.button.Name = "button1";
this.button.Size = new System.Drawing.Size(53, 21);
this.button.TabIndex = 8;
this.button.Text = "退出";
this.button.UseVisualStyleBackColor = true;
this.button.Click += new System.EventHandler(this.button1_Click);
this.button.Location = new System.Drawing.Point(166, 156);
this.button.Name = "button2";
this.button.Size = new System.Drawing.Size(48, 21);
this.button.TabIndex = 9;
this.button.Text = "关于";
this.button.UseVisualStyleBackColor = true;
this.button.Click += new System.EventHandler(this.button2_Click);
this.panel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.panel.Controls.Add(this.label6);
this.panel.Controls.Add(this.label5);
this.panel.Location = new System.Drawing.Point(6, 117);
this.panel.Name = "panel3";
this.panel.Size = new System.Drawing.Size(278, 33);
this.panel.TabIndex = 10;
this.label.AutoSize = true;
this.label.Location = new System.Drawing.Point(82, 8);
this.label.Name = "label6";
this.label.Size = new System.Drawing.Size(149, 12);
this.label.TabIndex = 1;
this.label.Text = "按住Ctrl+F键复制网页颜色";
this.label.AutoSize = true;
this.label.Location = new System.Drawing.Point(5, 9);
this.label.Name = "label5";
this.label.Size = new System.Drawing.Size(77, 12);
this.label.TabIndex = 0;
this.label.Text = "复制颜色值:";
this.label.AutoSize = true;
this.label.Cursor = System.Windows.Forms.Cursors.Help;
this.label.Location = new System.Drawing.Point(7, 160);
this.label.Name = "label3";
this.label.Size = new System.Drawing.Size(131, 12);
this.label.TabIndex = 11;
this.label.Text = "http://www.mrbccd.com";
this.label.Click += new System.EventHandler(this.label3_Click);
this.AutoScaleDimensions = new System.Drawing.SizeF(F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.WhiteSmoke;
this.ClientSize = new System.Drawing.Size(, 180);
this.Controls.Add(this.label);
this.Controls.Add(this.panel);
this.Controls.Add(this.button);
this.Controls.Add(this.button);
this.Controls.Add(this.panel);
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.panel.ResumeLayout(false);
this.panel.PerformLayout();
this.panel.ResumeLayout(false);
this.panel.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.CheckBox checkBox;
private System.Windows.Forms.TextBox txtPoint;
private System.Windows.Forms.Timer timer;
private System.Windows.Forms.TextBox txtRGB;
private System.Windows.Forms.Panel panel;
private System.Windows.Forms.Label label;
private System.Windows.Forms.Label label;
private System.Windows.Forms.Panel panel;
private System.Windows.Forms.TextBox txtColor;
private System.Windows.Forms.Label label;
private System.Windows.Forms.Button button;
private System.Windows.Forms.Button button;
private System.Windows.Forms.Panel panel;
private System.Windows.Forms.Label label;
private System.Windows.Forms.Label label;
private System.Windows.Forms.Label label;
}