基于C#实现屏幕取色器
芝麻粒儿 人气:0实践过程
效果
代码
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; }
加载全部内容