码迷,mamicode.com
首页 > 其他好文 > 详细

拾色器

时间:2016-01-23 01:31:58      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:

本来想用线程实现的,最终选用了定时器解决

 

 public partial class ColorForm : Form
    {
        private int _redValue = 255;
        private int _greenValue = 255;
        private int _blueValue = 255;

        public ColorForm()
        {
            InitializeComponent();
            InitPanelColor();
            panelColor.MouseDown += panelCololr_MouseDown;
            panelColor.MouseUp += panelColor_MouseUp;
        }

        //   private bool flag = false;

        public struct Pointapi
        {
            public uint X;
            public uint Y;
        }

        public class WinInfo
        {
            /// <summary>
            /// 返回包含了指定点的窗口的句柄
            /// </summary>
            /// <param name="x_point"></param>
            /// <param name="y_point"></param>
            /// <returns></returns>
            [DllImport("user32.dll")]
            public static extern uint WindowFromPoint(uint x_point, uint y_point);

            /// <summary>
            /// 获取鼠标指针当前位置
            /// </summary>
            /// <param name="p"></param>
            /// <returns></returns>
            [DllImport("user32.dll")]
            public static extern bool GetCursorPos(ref Pointapi p);

            /// <summary>
            /// 判断屏幕上一个指定点的客户区坐标
            /// </summary>
            /// <param name="hwnd">一个窗口的句柄,该窗口定义了要使用的客户区坐标系统</param>
            /// <param name="p"></param>
            /// <returns></returns>
            [DllImport("user32.dll")]
            public static extern uint ScreenToClient(uint hwnd, ref Pointapi p);


            /// <summary>
            /// 获取指定窗口的设备场景
            /// </summary>
            /// <param name="hwnd">将获取其设备场景的窗口的句柄。若为0,则要获取整个屏幕的DC</param>
            /// <returns></returns>
            [DllImport("user32.dll")]
            public static extern uint GetDC(uint hwnd);

            /// <summary>
            /// 在指定的设备场景中取得一个像素的RGB值
            /// </summary>
            /// <param name="hDC">一个设备场景的句柄</param>
            /// <param name="x"></param>
            /// <param name="y"></param>
            /// <returns></returns>
            [DllImport("gdi32.dll")]
            public static extern uint GetPixel(uint hDC, uint x, uint y);

            /// <summary>
            /// 释放由调用GetDC或GetWindowDC函数获取的指定设备场景。它对类或私有设备场景无效(但这样的调用不会造成损害)
            /// </summary>
            /// <param name="hwnd">要释放的设备场景相关的窗口句柄</param>
            /// <param name="hdc">要释放的设备场景句柄</param>
            /// <returns></returns>
            [DllImport("user32.dll")]
            public static extern uint ReleaseDC(uint hwnd, uint hdc);

            /// <summary>
            /// 获取整个窗口(包括边框、滚动条、标题栏、菜单等)的设备场景
            /// </summary>
            /// <param name="hwnd">将获取其设备场景的窗口</param>
            /// <returns></returns>
            [DllImport("user32.dll")]
            public static extern uint GetWindowDC(uint hwnd);

            /// <summary>
            /// 模拟一次鼠标事件
            /// </summary>
            /// <param name="dwFlags"></param>
            /// <param name="dx"></param>
            /// <param name="dy"></param>
            [DllImport("user32.dll")]
            public static extern void mouse_event(uint dwFlags, uint dx, uint dy);

            [DllImport("user32.dll")]
            public static extern void keybd_event(byte bVk, byte bScan, long dwFlags, long dwExtraInfo);

            /// <summary>
            /// 判断指定键盘状态
            /// </summary>
            /// <param name="nVirtKey"></param>
            /// <returns></returns>
            [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
            public static extern short GetKeyState(long nVirtKey);
        }

        /// <summary>
        /// 初始化色板颜色-白色
        /// </summary>
        private void InitPanelColor()
        {
            panelColor.BackColor = Color.FromArgb(Convert.ToInt32(txtRedValue.Text), Convert.ToInt32(txtGreenValue.Text),
                Convert.ToInt32(txtBlueValue.Text));
        }

        private void txtRedValue_TextChanged(object sender, EventArgs e)
        {
            _redValue = Convert.ToInt32(txtRedValue.Text == "" ? "0" : txtRedValue.Text);
            SetPanelColor();
        }

        private void txtGreenValue_TextChanged(object sender, EventArgs e)
        {
            _greenValue = Convert.ToInt32(txtGreenValue.Text == "" ? "0" : txtGreenValue.Text);
            SetPanelColor();
        }

        private void txtBlueValue_TextChanged(object sender, EventArgs e)
        {
            _blueValue = Convert.ToInt32(txtBlueValue.Text == "" ? "0" : txtBlueValue.Text);
            SetPanelColor();
        }

        /// <summary>
        /// 设置面板颜色
        /// </summary>
        private void SetPanelColor()
        {
            panelColor.BackColor = Color.FromArgb(LimitNum(_redValue), LimitNum(_greenValue), LimitNum(_blueValue));
        }

        private int LimitNum(int num)
        {
            if (num > 255)
            {
                return 255;
            }
            else if (num < 0)
            {
                return 0;
            }
            else
            {
                return num;
            }
        }


        /// <summary>
        /// 按ESC开始停止取色
        /// </summary>
        private void GetColor()
        {
            this.Cursor = Cursors.Cross;
            uint vDc = WinInfo.GetDC(0); //取到设备场景(0就是全屏的设备场景)  
            Pointapi papi = new Pointapi();
            WinInfo.GetCursorPos(ref papi);
            uint vColor = WinInfo.GetPixel(vDc, papi.X, papi.Y);
            uint vRed = vColor & 0xff;
            uint vGreen = (vColor & 0xff00)/256;
            uint vBlue = (vColor & 0xff0000)/65536;
            panelColor.BackColor = Color.FromArgb((int) vRed, (int) vGreen, (int) vBlue);
        }

        private void txtRedValue_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtGreenValue_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtBlueValue_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("winform版取色器\r\r版本1.0.0\r\r作者 Kevin", "关于", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        }

        private void 介绍ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("在颜色表里面按ESC开取色,要结束再按一次ESC\r\r交流QQ334081981", "介绍", MessageBoxButtons.OK,
                MessageBoxIcon.Asterisk);
        }

        private Timer timer = new Timer();

        private void panelCololr_MouseDown(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Hand;
            timer.Interval = 1;
            timer.Tick += delegate { GetColor(); };
            timer.Start();
        }

        private void panelColor_MouseUp(object sender, MouseEventArgs e)
        {
            timer.Stop();
            this.Cursor = Cursors.Default;
        }
    }

  

拾色器

标签:

原文地址:http://www.cnblogs.com/kinggod/p/5152620.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!