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

namespace ryControls
{
    /// <summary>
    /// 超级颜色选择控件
    /// </summary>
    public partial class SuperColor : UserControl
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="nVirtKey"></param>
        /// <returns></returns>
        [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "GetKeyState")]
        public static extern int GetKeyState(
           int nVirtKey // Long，欲测试的虚拟键键码。对字母、数字字符（A-Z、a-z、0-9），用它们实际的ASCII值
       );
        /// <summary>
        /// 
        /// </summary>

        public const int KEYEVENTF_KEYUP = 0x02;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="bVk"></param>
        /// <param name="bScan"></param>
        /// <param name="dwFlags"></param>
        /// <param name="dwExtraInfo"></param>
        [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "keybd_event")]
        public static extern void keybd_event(
            byte bVk,       // Byte，欲模拟的虚拟键码
            byte bScan,     // Byte，键的OEM扫描码
            int dwFlags,    // Long，零；或设为下述两个标志之一 KEYEVENTF_EXTENDEDKEY 指出是一个扩展键，而且在前面冠以0xE0代码 KEYEVENTF_KEYUP 模拟松开一个键
            int dwExtraInfo // Long，通常不用的一个值。api函数GetMessageExtraInfo可取得这个值。允许使用的值取决于特定的驱动程序
        );
        /// <summary>
        /// 获取网页颜色
        /// </summary>
        public string HtmlColor
        {
            get { return string.Format("#{0:X2}{1:X2}{2:X2}", BackColor.R, BackColor.G, BackColor.B); }
        }
        private bool GetKeyState(System.Windows.Forms.Keys keys)
        {
            return ((GetKeyState((int)keys) & 0x8000) != 0) ? true : false;
        }
        /// <summary>
        /// 超级颜色选择控件
        /// </summary>
        public SuperColor()
        {
            InitializeComponent();
            //禁止使用自定义颜色  
            colorDialog1.AllowFullOpen = false;
            //提供自己给定的颜色  
            colorDialog1.CustomColors = new int[] {ColorTranslator.ToOle(System.Drawing.ColorTranslator.FromHtml("#9bd028")),
                 ColorTranslator.ToOle(System.Drawing.ColorTranslator.FromHtml("#6a96e6")), ColorTranslator.ToOle(System.Drawing.ColorTranslator.FromHtml("#ffa519"))};
        }
        private void GetColors_OnColorChanged(object sender, Point MousePosition, Color e)
        {
            //lblX.Text = MousePosition.X.ToString();
            //lblY.Text = MousePosition.Y.ToString();
            BackColor = e;
            //txtARGB.Text = "0x" + picColor.BackColor.ToArgb().ToString("x").ToUpper();
            //txtRGB.Text = picColor.BackColor.R.ToString().ToLower() + "," + picColor.BackColor.G.ToString().ToLower() + "," + picColor.BackColor.B.ToString().ToLower();
            //txtL.Text = "#" + picColor.BackColor.R.ToString("x").PadLeft(2, '0') + picColor.BackColor.G.ToString("x").PadLeft(2, '0') + picColor.BackColor.B.ToString("x").PadLeft(2, '0');
        }

        private void SuperColor_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (GetKeyState(Keys.LControlKey) || GetKeyState(Keys.RControlKey))
                {
                    ryControls.FrmColorConvert frm = new ryControls.FrmColorConvert();
                    if(frm.ShowDialog()==DialogResult.OK)
                    {
                        this.BackColor = frm.LblColor.BackColor;
                    }
                }
                else
                {
                    colorDialog1.Color = this.BackColor;
                    if (colorDialog1.ShowDialog() == DialogResult.OK)
                    {
                        this.BackColor = colorDialog1.Color;
                    }
                }
            }
            else if (e.Button == MouseButtons.Right)
            {
                GetColors getColors = new GetColors
                {
                    start_color = this.BackColor
                };
                getColors.OnColorChanged += GetColors_OnColorChanged;
                getColors.Tag = this;
                getColors.ShowDialog();
                getColors.OnColorChanged -= GetColors_OnColorChanged;
            }
        }
    }
}
