欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

c# 顏色選擇控件的實(shí)現(xiàn)代碼

 更新時(shí)間:2021年04月19日 09:32:21   作者:楚人無(wú)衣  
這篇文章主要介紹了c# 顏色選擇控件的實(shí)現(xiàn)代碼,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下

參考ColorComboBox做修改,并對(duì)顏色名做些修正,用于CR MVMixer產(chǎn)品中,聊作備忘~

效果圖:

代碼:

//顏色拾取框
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace CRMVMixer
{
    //event handler delegate
    public delegate void ColorChangedHandler(object sender, ColorChangeArgs e);

    [ToolboxBitmap(typeof(ComboBox))]
    public class ColorComboBox : ComboBox
    {
        private PopupWindow popupWnd;
        private ColorPopup colors = new ColorPopup();
        private Color selectedColor = Color.Black;
        private Timer timer = new Timer();
        public event ColorChangedHandler ColorChanged;

        //constructor...
        public ColorComboBox()
            : this(Color.Black)
        {
        }

        public ColorComboBox(Color selectedColor)
        {
            this.SuspendLayout();
            // 
            // ColorCombo
            // 
            this.AutoSize = false;
            this.Size = new Size(92, 22);
            this.Text = string.Empty;
            this.DrawMode = DrawMode.OwnerDrawFixed;
            this.DropDownStyle = ComboBoxStyle.DropDownList;
            this.ItemHeight = 16;

            timer.Tick += new EventHandler(OnCheckStatus);
            timer.Interval = 50;
            timer.Start();
            colors.SelectedColor = this.selectedColor = selectedColor;
            this.ResumeLayout(false);
        }

        [DefaultValue(typeof(Color), "Black")]
        public Color SelectedColor
        {
            get { return selectedColor; }
            set
            {
                selectedColor = value;
                colors.SelectedColor = value;
                Invalidate();
            }
        }

        protected override void WndProc(ref Message m)
        {
            //256: WM_KEYDOWN, 513: WM_LBUTTONDOWN, 515: WM_LBUTTONDBLCLK
            if (m.Msg == 256 || m.Msg == 513 || m.Msg == 515)
            {
                if (m.Msg == 513)
                    PopupColorPalette();
                return;
            }
            base.WndProc(ref   m);
        }

        private void PopupColorPalette()
        {
            //create a popup window
            popupWnd = new PopupWindow(colors);

            //calculate its position in screen coordinates
            Rectangle rect = Bounds;
            rect = this.Parent.RectangleToScreen(rect);
            Point pt = new Point(rect.Left, rect.Bottom);

            //tell it that we want the ColorChanged event
            popupWnd.ColorChanged += new ColorChangedHandler(OnColorChanged);

            //show the popup
            popupWnd.Show(pt);
            //disable the button so that the user can't click it
            //while the popup is being displayed
            this.Enabled = false;
            this.timer.Start();
        }

        //event handler for the color change event from the popup window
        //simply relay the event to the parent control
        protected void OnColorChanged(object sender, ColorChangeArgs e)
        {
            //if a someone wants the event, and the color has actually changed
            //call the event handler
            if (ColorChanged != null && e.color != this.selectedColor)
            {
                this.selectedColor = e.color;
                ColorChanged(this, e);
            }
            else //otherwise simply make note of the new color
                this.selectedColor = e.color;
        }

        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            var g = e.Graphics;
            e.DrawBackground();
            var brush = new SolidBrush(this.selectedColor);
            var rect = e.Bounds;
            rect.Width -= 1;
            rect.Height -= 1;
            g.FillRectangle(brush, rect);
            g.DrawRectangle(Pens.Black, rect);
            e.DrawFocusRectangle();
        }

        //This is the timer call back function. It checks to see 
        //if the popup went from a visible state to an close state
        //if so then it will uncheck and enable the button
        private void OnCheckStatus(object sender, EventArgs e)
        {
            if (popupWnd != null && !popupWnd.Visible)
            {
                this.timer.Stop();
                this.Enabled = true;
            }
        }

        /// <summary>
        /// a button style radio button that shows a color
        /// </summary>
        private class ColorRadioButton : RadioButton
        {
            public ColorRadioButton(Color color, Color backColor)
            {
                this.ClientSize = new Size(21, 21);
                this.Appearance = Appearance.Button;
                this.Name = "button";
                this.Visible = true;
                this.ForeColor = color;
                this.FlatAppearance.BorderColor = backColor;
                this.FlatAppearance.BorderSize = 0;
                this.FlatStyle = FlatStyle.Flat;
                this.Paint += new PaintEventHandler(OnPaintButton);
            }

            private void OnPaintButton(object sender, PaintEventArgs e)
            {
                //paint a square on the face of the button using the controls foreground color
                Rectangle colorRect = new Rectangle(ClientRectangle.Left + 5, ClientRectangle.Top + 5, ClientRectangle.Width - 9, ClientRectangle.Height - 9);
                e.Graphics.FillRectangle(new SolidBrush(this.ForeColor), colorRect);
                e.Graphics.DrawRectangle(Pens.DarkGray, colorRect);
            }
        }

        ///<summary>
        ///this is the popup window.  This window will be the parent of the 
        ///window with the color controls on it
        ///</summary>
        private class PopupWindow : ToolStripDropDown
        {
            public event ColorChangedHandler ColorChanged;
            private ToolStripControlHost host;
            private ColorPopup content;

            public Color SelectedColor
            {
                get { return content.SelectedColor; }
            }

            public PopupWindow(ColorPopup content)
            {
                if (content == null)
                    throw new ArgumentNullException("content");

                this.content = content;
                this.AutoSize = false;
                this.DoubleBuffered = true;
                this.ResizeRedraw = true;
                //create a host that will host the content
                host = new ToolStripControlHost(content);

                this.Padding = Margin = host.Padding = host.Margin = Padding.Empty;
                this.MinimumSize = content.MinimumSize;
                content.MinimumSize = content.Size;
                MaximumSize = new Size(content.Size.Width + 1, content.Size.Height + 1);
                content.MaximumSize = new Size(content.Size.Width + 1, content.Size.Height + 1);
                Size = new Size(content.Size.Width + 1, content.Size.Height + 1);

                content.Location = Point.Empty;
                //add the host to the list
                Items.Add(host);
            }

            protected override void OnClosed(ToolStripDropDownClosedEventArgs e)
            {
                //when the window close tell the parent that the color changed
                if (ColorChanged != null)
                    ColorChanged(this, new ColorChangeArgs(this.SelectedColor));
            }
        }

        ///<summary>
        ///this class represends the control that has all the color radio buttons.
        ///this control gets embedded into the PopupWindow class.
        ///</summary>
        private class ColorPopup : UserControl
        {
            //private Color[] colors = { Color.Black, Color.Gray, Color.Maroon, Color.Olive, Color.Green, Color.Teal, Color.Navy, Color.Purple, Color.White, Color.Silver, Color.Red, Color.Yellow, Color.Lime, Color.Aqua, Color.Blue, Color.Fuchsia };
            private Color[] colors = {
                Color.Black, Color.Navy, Color.DarkGreen, Color.DarkCyan, Color.DarkRed, Color.DarkMagenta, Color.Olive,
                Color.LightGray, Color.DarkGray, Color.Blue, Color.Lime, Color.Cyan, Color.Red, Color.Fuchsia, 
                Color.Yellow, Color.White, Color.RoyalBlue, Color.MediumBlue,  Color.LightGreen, Color.MediumSpringGreen, Color.Chocolate,
                Color.Pink, Color.Khaki, Color.WhiteSmoke, Color.BlueViolet, Color.DeepSkyBlue, Color.OliveDrab, Color.SteelBlue,
                Color.DarkOrange, Color.Tomato, Color.HotPink, Color.DimGray,
            };
            private string[] colorNames = {
                "黑色", "藏青", "深綠", "深青", "紅褐", "洋紅", "褐綠", 
                "淺灰", "灰色", "藍(lán)色", "綠色", "青色", "紅色", "紫紅", 
                "黃色", "白色", "藍(lán)灰", "藏藍(lán)", "淡綠", "青綠", "黃褐", 
                "粉紅", "嫩黃", "銀白", "紫色", "天藍(lán)", "灰綠", "青藍(lán)", 
                "橙黃", "桃紅", "英紅", "深灰"
            };
            private ToolTip toolTip = new ToolTip();
            private ColorRadioButton[] buttons;
            private Button moreColorsBtn;
            private Color selectedColor = Color.Black;

            ///<summary>
            ///get or set the selected color
            ///</summary>
            public Color SelectedColor
            {
                get { return selectedColor; }
                set
                {
                    selectedColor = value;
                    Color[] colors = this.colors;
                    for (int i = 0; i < colors.Length; i++)
                        buttons[i].Checked = selectedColor == colors[i];
                }
            }

            private void InitializeComponent()
            {
                this.SuspendLayout();
                this.Name = "Color Popup";
                this.Text = string.Empty;
                this.ResumeLayout(false);
            }

            public ColorPopup()
            {
                InitializeComponent();

                SetupButtons();
                this.Paint += new PaintEventHandler(OnPaintBorder);
            }

            //place the buttons on the window.
            private void SetupButtons()
            {
                Controls.Clear();

                int x = 1;
                int y = 2;
                int breakCount = 7;
                Color[] colors = this.colors;
                this.buttons = new ColorRadioButton[colors.Length];
                this.ClientSize = new Size(139, 137);
                //color buttons
                for (int i = 0; i < colors.Length; i++)
                {
                    if (i > 0 && i % breakCount == 0)
                    {
                        y += 19;
                        x = 1;
                    }
                    buttons[i] = new ColorRadioButton(colors[i], this.BackColor);
                    buttons[i].Location = new Point(x, y);
                    toolTip.SetToolTip(buttons[i], colorNames[i]);
                    Controls.Add(buttons[i]);
                    buttons[i].Click += new EventHandler(BtnClicked);
                    if (selectedColor == colors[i])
                        buttons[i].Checked = true;
                    x += 19;
                }

                //line...
                y += 24;
                var label = new Label();
                label.AutoSize = false;
                label.Text = string.Empty;
                label.Width = this.Width - 5;
                label.Height = 2;
                label.BorderStyle = BorderStyle.Fixed3D;
                label.Location = new Point(4, y);
                Controls.Add(label);

                //button
                y += 7;
                moreColorsBtn = new Button();
                moreColorsBtn.FlatStyle = FlatStyle.Popup;
                moreColorsBtn.Text = "其它顏色...";
                moreColorsBtn.Location = new Point(6, y);
                moreColorsBtn.ClientSize = new Size(127, 23);
                moreColorsBtn.Click += new EventHandler(OnMoreClicked);
                Controls.Add(moreColorsBtn);
            }

            private void OnPaintBorder(object sender, PaintEventArgs e)
            {
                var rect = this.ClientRectangle;
                rect.Width -= 1;
                rect.Height -= 1;
                e.Graphics.DrawRectangle(new Pen(SystemColors.WindowFrame), rect);
            }

            public void BtnClicked(object sender, EventArgs e)
            {
                selectedColor = ((ColorRadioButton)sender).ForeColor;
                ((ToolStripDropDown)Parent).Close();
            }

            public void OnMoreClicked(object sender, EventArgs e)
            {
                ColorDialog dlg = new ColorDialog();
                dlg.Color = SelectedColor;
                if (dlg.ShowDialog(this) == DialogResult.OK)
                    selectedColor = dlg.Color;
                ((ToolStripDropDown)Parent).Close();
            }
        }
    }

    //define the color changed event argument
    public class ColorChangeArgs : System.EventArgs
    {
        //the selected color
        public Color color;
        public ColorChangeArgs(Color color)
        {
            this.color = color;
        }
    }
}

以上就是c# 顏色選擇控件的實(shí)現(xiàn)代碼的詳細(xì)內(nèi)容,更多關(guān)于c# 顏色選擇控件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • c# 兩個(gè)數(shù)組比較,將重復(fù)部分去掉,返回不重復(fù)部分的實(shí)現(xiàn)

    c# 兩個(gè)數(shù)組比較,將重復(fù)部分去掉,返回不重復(fù)部分的實(shí)現(xiàn)

    下面小編就為大家?guī)?lái)一篇c# 兩個(gè)數(shù)組比較,將重復(fù)部分去掉,返回不重復(fù)部分的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-12-12
  • C#使用二維數(shù)組模擬斗地主

    C#使用二維數(shù)組模擬斗地主

    這篇文章主要介紹了C#使用二維數(shù)組模擬斗地主的方法,通過(guò)C#的二維數(shù)組簡(jiǎn)單實(shí)現(xiàn)撲克隨機(jī)發(fā)牌的功能,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-04-04
  • C# Windows API應(yīng)用之基于FlashWindowEx實(shí)現(xiàn)窗口閃爍的方法

    C# Windows API應(yīng)用之基于FlashWindowEx實(shí)現(xiàn)窗口閃爍的方法

    這篇文章主要介紹了C# Windows API應(yīng)用之基于FlashWindowEx實(shí)現(xiàn)窗口閃爍的方法,結(jié)合實(shí)例形式分析了Windows API函數(shù)FlashWindowEx的功能、定義及實(shí)現(xiàn)窗口閃爍的相關(guān)技巧,需要的朋友可以參考下
    2016-08-08
  • Unity實(shí)現(xiàn)主角移動(dòng)與攝像機(jī)跟隨

    Unity實(shí)現(xiàn)主角移動(dòng)與攝像機(jī)跟隨

    這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)主角移動(dòng)與攝像機(jī)跟隨,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-03-03
  • C#學(xué)習(xí)筆記整理_變量等基礎(chǔ)語(yǔ)法(必看篇)

    C#學(xué)習(xí)筆記整理_變量等基礎(chǔ)語(yǔ)法(必看篇)

    下面小編就為大家?guī)?lái)一篇C#學(xué)習(xí)筆記整理_變量等基礎(chǔ)語(yǔ)法(必看篇)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-09-09
  • C#獲取屏幕分辨率縮放比例的方法與示例

    C#獲取屏幕分辨率縮放比例的方法與示例

    在C#中,獲取屏幕分辨率縮放比例通常指的是Dpi縮放比例,它表示顯示設(shè)備的獨(dú)立像素與設(shè)備無(wú)關(guān)像素(96 DPI)的比例,這通常用于確保應(yīng)用程序在不同分辨率和縮放設(shè)置的顯示設(shè)備上都能正確顯示,本文給大家介紹了C#獲取屏幕分辨率縮放比例的方法與示例,需要的朋友可以參考下
    2024-07-07
  • C#實(shí)現(xiàn)Winform鼠標(biāo)拖動(dòng)窗口大小時(shí)設(shè)定窗口最小尺寸的方法

    C#實(shí)現(xiàn)Winform鼠標(biāo)拖動(dòng)窗口大小時(shí)設(shè)定窗口最小尺寸的方法

    這篇文章主要介紹了C#實(shí)現(xiàn)Winform鼠標(biāo)拖動(dòng)窗口大小時(shí)設(shè)定窗口最小尺寸的方法,涉及WinForm改變窗口大小時(shí)動(dòng)態(tài)判斷當(dāng)前窗口尺寸的相關(guān)技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下
    2015-11-11
  • WPF+ASP.NET SignalR實(shí)現(xiàn)后臺(tái)通知功能的示例代碼

    WPF+ASP.NET SignalR實(shí)現(xiàn)后臺(tái)通知功能的示例代碼

    本文以一個(gè)簡(jiǎn)單示例,簡(jiǎn)述如何通過(guò)WPF+ASP.NET SignalR實(shí)現(xiàn)消息后臺(tái)通知以及數(shù)據(jù)的實(shí)時(shí)刷新,僅供學(xué)習(xí)分享使用,如有不足之處,還請(qǐng)指正
    2022-09-09
  • C#集合類用法實(shí)例代碼詳解

    C#集合類用法實(shí)例代碼詳解

    本文通過(guò)實(shí)例代碼給大家介紹了C#集合類用法的相關(guān)知識(shí),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2017-10-10
  • 基于C#設(shè)計(jì)一個(gè)雙色球選號(hào)工具

    基于C#設(shè)計(jì)一個(gè)雙色球選號(hào)工具

    這篇文章主要為大家詳細(xì)介紹了如何利用C#設(shè)計(jì)實(shí)現(xiàn)一個(gè)雙色球選號(hào)工具,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04

最新評(píng)論