简单的屏幕取色器

运行效果截图:


后台代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Runtime.InteropServices;

namespace ColorPicker
{
    public partial class ColorPickerSimple : Form
    {
        #region 引入外部方法
        //GetDC,获取DC(设备环境)
        [DllImport("user32", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
        private static extern int GetDC(int hwnd);
        //GetPixel,获取像素点
        [DllImport("gdi32", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
        private static extern int GetPixel(int hdc, int X, int y);
        //ReleaseDC,释放DC
        [DllImport("user32", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)] //确定坐标
        private static extern int ReleaseDC(int hwnd, int hdc);
        #endregion

        #region 字段
        //R、G、B
        int blue;
        int green;
        int red;
        //指定设备环境(句柄为0则为桌面)
        int hD;
        //颜色信息
        int c;
        //鼠标所在处坐标点
        int a;
        int b;
        #endregion

        public ColorPickerSimple()
        {
            InitializeComponent();
        }

        #region 窗体事件
        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ColorPickerSimple_Load(object sender, EventArgs e)
        {
            txtR.ReadOnly = true;
            txtG.ReadOnly = true;
            txtB.ReadOnly = true;
            txtColor.ReadOnly = true;
            timer1.Enabled = true;
        }
        #endregion

        #region 获取颜色
        /// <summary>
        /// Timer事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            //获取鼠标所在处的坐标
            a = System.Windows.Forms.Control.MousePosition.X;
            b = System.Windows.Forms.Control.MousePosition.Y;
            //获取DC,设备环境(桌面)
            hD = GetDC(0);
            //获取颜色值
            c = GetPixel(hD, a, b);
            //释放DC
            ReleaseDC(0, hD);
            //由颜色值计算R、G、B值
            red = c % 256;
            green = (c / 256) % 256;
            blue = (c / 256 / 256) % 256;
            //显示颜色分值
            txtR.Text = Convert.ToString(red);
            txtG.Text = Convert.ToString(green);
            txtB.Text = Convert.ToString(blue);
            //显示拾取的颜色
            Color color = Color.FromArgb(red, green, blue);
            txtColor.BackColor = color;
        }
        #endregion

    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值