基于C#实现屏幕取色器

.NET
249
0
0
2023-07-04
标签   C#
目录
  • 实践过程
  • 效果
  • 代码

实践过程

效果

代码

public partial class FrmGetColor : Form
{
    public FrmGetColor()
    {
        InitializeComponent();
    }

    #region 定义快捷键

    //如果函数执行成功,返回值不为。       
    //如果函数执行失败,返回值为。要得到扩展错误信息,调用GetLastError。        
    [DllImport("user.dll", SetLastError = true)]
    public static extern bool RegisterHotKey(
        IntPtr hWnd, //要定义热键的窗口的句柄            
        int id, //定义热键ID(不能与其它ID重复)                       
        KeyModifiers fsModifiers, //标识热键是否在按Alt、Ctrl、Shift、Windows等键时才会生效         
        Keys vk //定义热键的内容            
    );

    [DllImport("user.dll", SetLastError = true)]
    public static extern bool UnregisterHotKey(
        IntPtr hWnd, //要取消热键的窗口的句柄            
        int id //要取消热键的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: //按下的是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号为的热键设定    
        UnregisterHotKey(Handle,);
    }
}
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.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();
        // 
        // checkBox
        // 
        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);
        // 
        // txtPoint
        // 
        this.txtPoint.Location = new System.Drawing.Point(, 26);
        this.txtPoint.Name = "txtPoint";
        this.txtPoint.Size = new System.Drawing.Size(, 21);
        this.txtPoint.TabIndex =;
        // 
        // timer
        // 
        this.timer.Enabled = true;
        this.timer.Tick += new System.EventHandler(this.timer1_Tick);
        // 
        // txtRGB
        // 
        this.txtRGB.Location = new System.Drawing.Point(, 51);
        this.txtRGB.Name = "txtRGB";
        this.txtRGB.Size = new System.Drawing.Size(, 21);
        this.txtRGB.TabIndex =;
        // 
        // panel
        // 
        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;
        // 
        // label
        // 
        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 = "鼠标位置:";
        // 
        // label
        // 
        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 值:";
        // 
        // panel
        // 
        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;
        // 
        // txtColor
        // 
        this.txtColor.Location = new System.Drawing.Point(, 76);
        this.txtColor.Name = "txtColor";
        this.txtColor.Size = new System.Drawing.Size(, 21);
        this.txtColor.TabIndex =;
        // 
        // label
        // 
        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 = "网页颜色:";
        // 
        // button
        // 
        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);
        // 
        // button
        // 
        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);
        // 
        // panel
        // 
        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;
        // 
        // label
        // 
        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键复制网页颜色";
        // 
        // label
        // 
        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 = "复制颜色值:";
        // 
        // label
        // 
        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);
        // 
        // FrmGetColor
        // 
        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;
}