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

namespace GetColor
{
    /// <summary>
    /// 获取颜色
    /// </summary>
    public partial class GetColors : Form
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="MousePosition"></param>
        /// <param name="e"></param>
        public delegate void ColorHandler(object sender,Point MousePosition, Color e);
        /// <summary>
        /// 颜色变化时激发
        /// </summary>
        [Description("颜色变化时激发")]
        public event ColorHandler OnColorChanged;
        [DllImport("gdi32")]
        private static extern IntPtr CreateDC(
        string lpszDriver, // 驱动名称
        string lpszDevice, // 设备名称
        string lpszOutput, // 无用，可以设定位"NULL"
        IntPtr lpInitData // 任意的打印机数据
        );
        [DllImport("gdi32.dll")]
        private static extern bool BitBlt(
        IntPtr hdcDest, // 目标设备的句柄
        int nXDest, // 目标对象的左上角的X坐标
        int nYDest, // 目标对象的左上角的X坐标
        int nWidth, // 目标对象的矩形的宽度
        int nHeight, // 目标对象的矩形的长度
        IntPtr hdcSrc, // 源设备的句柄
        int nXSrc, // 源对象的左上角的X坐标
        int nYSrc, // 源对象的左上角的X坐标
        int dwRop // 光栅的操作值
        );
        Timer tm;
        /// <summary>
        /// 
        /// </summary>
        public Color start_color;
        Color now_color;
        /// <summary>
        /// 
        /// </summary>
        public GetColors()
        {
            InitializeComponent();
            tm = new Timer
            {
                Interval = 1
            };
            tm.Tick += new EventHandler(Tm_Tick);
            tm.Enabled = true;
        }
        void Tm_Tick(object sender, EventArgs e)
        {
            //Screen screen = Screen.PrimaryScreen;
            //Rectangle rc = screen.Bounds;
            //int iWidth = rc.Width;
            //int iHeight = rc.Height;
            //Image myImage = new Bitmap(iWidth, iHeight);
            ////从一个继承自Image类的对象中创建Graphics对象
            //Graphics g = Graphics.FromImage(myImage);
            //g.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(iWidth, iHeight));

            IntPtr hdlDisplay = CreateDC("display", null, null, IntPtr.Zero);

            Graphics g = Graphics.FromHdc(hdlDisplay);

            Bitmap bmp = new Bitmap(1, 1, g);

            Graphics gimg = Graphics.FromImage(bmp);

            IntPtr hdlScreen = g.GetHdc();
            IntPtr hdlBmp = gimg.GetHdc();

            BitBlt(hdlBmp, 0, 0, 1, 1, hdlScreen, MousePosition.X, MousePosition.Y, 13369376);

            g.ReleaseHdc(hdlScreen);
            gimg.ReleaseHdc(hdlBmp);
            now_color = bmp.GetPixel(0, 0);
            OnColorChanged?.Invoke(this, MousePosition, now_color);
            g.Dispose();
            gimg.Dispose();
            bmp.Dispose(); // 释放 bmp 所使用的资源
        }
        private void GetColors_Load(object sender, EventArgs e)
        {
            this.MaximizedBounds = Screen.PrimaryScreen.Bounds;
            this.WindowState = FormWindowState.Maximized;
            this.FormBorderStyle = FormBorderStyle.None;

            this.Opacity = 0.01;
        }

        private void GetColors_Click(object sender, EventArgs e)
        {
        }

        private void GetColors_MouseMove(object sender, MouseEventArgs e)
        {
            //MainForm Mymainform = (MainForm)this.Tag;
            //if (e.X > Mymainform.Left && e.X < Mymainform.Left + Mymainform.Width && e.Y > Mymainform.Top && e.Y < Mymainform.Top + Mymainform.Width)
            //{
            //    Mymainform.Location=Mymainform.Location == Mymainform.formLeft ? Mymainform.formRight:Mymainform.formLeft;
            //}
        }

        private void GetColors_FormClosed(object sender, FormClosedEventArgs e)
        {

        }

        private void GetColors_FormClosing(object sender, FormClosingEventArgs e)
        {
            tm.Stop();
            tm.Enabled = false;
        }

        private void GetColors_MouseClick(object sender, MouseEventArgs e)
        {
            if(e.Button==MouseButtons.Left)
            {
                OnColorChanged?.Invoke(this, MousePosition, now_color);
            }
            else
            {
                OnColorChanged?.Invoke(this, MousePosition, start_color);
            }
            Close();
        }
    }
}
