中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

基于C#如何實現屏幕取色器

發布時間:2022-12-15 09:58:00 來源:億速云 閱讀:144 作者:iii 欄目:開發技術

本篇內容主要講解“基于C#如何實現屏幕取色器”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“基于C#如何實現屏幕取色器”吧!

實踐過程

效果

基于C#如何實現屏幕取色器

代碼

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;
}

到此,相信大家對“基于C#如何實現屏幕取色器”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

崇左市| 怀柔区| 鸡泽县| 霍州市| 陵川县| 绥滨县| 贵德县| 随州市| 浪卡子县| 老河口市| 西贡区| 平泉县| 河曲县| 公主岭市| 佛山市| 射洪县| 新平| 内丘县| 蛟河市| 上栗县| 临泉县| 陆川县| 兰溪市| 金山区| 沧州市| 隆化县| 巩留县| 贵阳市| 万宁市| 图们市| 玛纳斯县| 辰溪县| 乳源| 新兴县| 城步| 凤台县| 渝北区| 柘荣县| 西安市| 虹口区| 云梦县|