ArcEngine+C# ——下拉框(ComboBox)顏色控制項

來源:互聯網
上載者:User

       這一篇介紹如何寫一個單色和漸層色的顏色選擇下拉框(ComboBox)控制項。有些功能中,需要使用者自訂選顏色時,一般只需調用ColorDialog顏色選擇控制項就可以了,但如果為了更貼近ArcGIS的操作風格,且需要使用漸層顏色時,ColorDialog可能就不能滿足需求了。

熟悉ArcGIS操作的朋友可能記得,在圖層、要素等的顏色設定時用到顏色下拉框選擇顏色,其中就有選漸層顏色的下拉框,如。在某些二次開發項目中可能會遇到類似的顏色選擇需求,希望這裡的介紹對需要的朋友能起到一點協助作用。

 

TIN模型按高程選擇漸層顏色

 

1.單色顏色下拉框

其思路就是往ComboBox中添加記錄顏色值的項(Item),再根據該顏色值重繪ComboBox對應項的矩形地區,達到直接顯示的效果。

具體步驟如下:

先在項目下添加一個新的UserControl控制項,再拖一個ComboBox控制項到UserControl上,將ComboBox的Dock屬性設為Fill,再調整UserControl的大小,使其與剛好完全位於ComboBox下,最後關鍵的一步就是加入代碼。下面貼上代碼,具體的解釋見代碼注釋,效果如:

單一色效果

using System.Windows.Forms;

using System.Drawing;

using System;

namespace WindowsFormsApplication1

{

    /// <summary>

    /// 單一顏色選擇控制項類

    /// </summary>

    public partial class PureColorComboBox : UserControl

    {

        private Color _SelectedColor;

 

        /// <summary>

        /// 已選擇顏色,封裝欄位

        /// </summary>

        public Color SelectedColor

        {

            get { return _SelectedColor; }

            set { _SelectedColor = value; }

        }

 

        /// <summary>

        /// Combobox顏色選擇發生變化時激發的事件

        /// </summary>

        public event EventHandler SelectColorChanged;

 

        //自訂的初始顏色值,有必要時可以指定一些特定的顏色

        //private static string[] colorList =

        //{

        //    "AliceBlue","AntiqueWhite","Aqua","Aquamarine","Azure","Beige",

        //    "Bisque","Black","BlanchedAlmond","Blue","BlueViolet","Brown",

        //    "BurlyWood","CadetBlue","Chartreuse","Chocolate","Coral",

        //    "CornflowerBlue","Cornsilk","Crimson","Cyan","DarkBlue","DarkCyan",

        //    "DarkGoldenrod","DarkGray","DarkGreen","DarkKhaki","DarkMagenta",

        //    "DarkOliveGreen","DarkOrange","DarkOrchid"

        //};

 

        /// <summary>

        /// 建構函式

        /// </summary>

        public PureColorComboBox()

        {

            InitializeComponent();

            AddComponent();

        }

 

        /// <summary>

        /// 載入各顏色項(Items)

        /// </summary>

        private void AddComponent()

        {

            this.comboBox1.DrawMode = DrawMode.OwnerDrawFixed;

            this.comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;

            //設定ComboBox的高度

            this.comboBox1.ItemHeight = 18;

 

            this.comboBox1.BeginUpdate();

            this.comboBox1.Items.Clear();

 

            //如要使用自訂色彩初始值時則使用這段代碼

            //foreach (string oneColor in colorList)

            //{

            //    this.comboBox1.Items.Add(oneColor);

            //}

 

            //載入系統所有的顏色,如果使用自訂色彩初始值時則登出下面的代碼

            Array colors = System.Enum.GetValues(typeof(KnownColor));

            for (int i = colors.GetLength(0) - 1; i >= 0; i--)

            {

                this.comboBox1.Items.Add(colors.GetValue(i).ToString());

            }

 

            this.comboBox1.EndUpdate();

        }

 

        //在ComboBox的DrawItem事件(繪製事件)中繪製顏色矩形框,每次添加Item時都會觸發該事件

        //該函數由在ComboBox的屬性標籤的事件欄下雙擊DrawItem自動產生

        //當然,也可以自己在前面添加

        //this.comboBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.comboBox1_DrawItem);

        private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)

        {

            if (e.Index < 0)

                return;

            Rectangle rect = e.Bounds;  //擷取Item矩形框

 

            //擷取對應項記錄的顏色值

            string colorName = comboBox1.Items[e.Index].ToString();

            //建立單一色刷子,顏色為對應項記錄的值

            SolidBrush brush = new SolidBrush(Color.FromName(colorName));

 

            _SelectedColor = brush.Color;

            //為美觀,可縮小選定項地區1個像素

            rect.Inflate(-1, -1);

            // 填充顏色

            e.Graphics.FillRectangle(brush, rect);

            // 用黑色繪製顏色邊框

            e.Graphics.DrawRectangle(Pens.Black, rect);

        }

 

        /// <summary>

        /// 在ComboBox選擇項改變觸發事件中啟用顏色傳遞事件

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)

        {

            //如果使用該控制項的表單註冊了SelectColorChanged事件,則啟用

            //相當於用SelectColorChanged事件替換了ConboBox的SelectedIndexChanged事件

            if (SelectColorChanged != null)

            {

                SelectColorChanged(this, e);

            }

        }

    }

}

最後,將該顏色控制項像使用其他控制項一樣拖入需要的表單中,在該控制項的SelectColorChanged事件下,通過控制項的SelectedColor屬性獲得選擇的顏色值:

        //下拉框所選顏色值發生改變時觸發的事件

        private void colorComboBox1_SelectColorChanged(object sender, EventArgs e)

        {

            //通過ComboBox的SelectedColor公用欄位擷取當前選中的值

            MessageBox.Show("當前選中的顏色為: "+this.colorComboBox1.SelectedColor.ToString());

        }

2.漸層色的顏色下拉框

漸層的顏色下拉框的與單色下拉框的思路是一樣的,只是著色方案和初始顏色設定稍微不同,這裡只把的代碼貼出來,注釋較略,其他則可參照上傳的具體執行個體,效果如下:

漸層色顏色下拉框

using System.Windows.Forms;

using System.Drawing;

using System;

using System.Drawing.Drawing2D;

namespace WindowsFormsApplication1

{

    public partial class GradientColorComboBox : UserControl

    {

        private Color _FromColor;

        private Color _ToColor;

 

        public Color FromColor

        {

            get { return _FromColor; }

            set { _FromColor = value; }

        }

 

        public Color ToColor

        {

            get { return _ToColor; }

            set { _ToColor = value; }

        }

 

        public event EventHandler SelectColorChanged;

 

        //預定義的漸層色

        private static string[] colorList =

        {

            "AliceBlue|AntiqueWhite","Aqua|Aquamarine","Azure|Beige",

            "Bisque|Black","BlanchedAlmond|Blue","BlueViolet|Brown",

            "BurlyWood|CadetBlue","Chartreuse|Chocolate",

            "CornflowerBlue|Cornsilk","Crimson|Cyan","DarkBlue|DarkCyan",

            "DarkGoldenrod|DarkGray","DarkGreen|DarkKhaki",

            "DarkMagenta|DarkOliveGreen","DarkOrange|DarkOrchid"

        };

 

        public GradientColorComboBox()

        {

            InitializeComponent();

            PersonalizeComponent();

        }

 

        private void PersonalizeComponent()

        {

            this.comboBox1.DrawMode = DrawMode.OwnerDrawFixed;

            this.comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;

            this.comboBox1.ItemHeight = 18;

            this.comboBox1.BeginUpdate();

            this.comboBox1.Items.Clear();

 

            foreach (string oneColor in colorList)

            {

                this.comboBox1.Items.Add(oneColor);

            }

            this.comboBox1.EndUpdate();

        }

 

        private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)

        {

            if (e.Index < 0)

                return;

 

            Rectangle rect = e.Bounds;

            //讀取起始、終止顏色值

            string fColorName = comboBox1.Items[e.Index].ToString().Split('|')[0];

            string tColorName = comboBox1.Items[e.Index].ToString().Split('|')[1];

            _FromColor = Color.FromName(fColorName);

            _ToColor = Color.FromName(tColorName);

            //選擇線性漸層刷子

            LinearGradientBrush brush = new LinearGradientBrush(rect, _FromColor, _ToColor, 0, false);

 

            rect.Inflate(-1, -1);

            // 填充顏色

            e.Graphics.FillRectangle(brush, rect);

            // 繪製邊框

            e.Graphics.DrawRectangle(Pens.Black, rect);

        }

 

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)

        {

            if (SelectColorChanged != null)

            {

                SelectColorChanged(this, e);

            }

        }

    }

}

同樣,將該漸層色控制項拖入需要使用表單中,在該控制項的SelectColorChanged事件下,通過控制項的FromColor和ToColor屬性獲得選擇的漸層色值:

        private void gradientColorComboBox1_SelectColorChanged(object sender, EventArgs e)

        {

            MessageBox.Show("當前選中的顏色為: " + this.gradientColorComboBox1.FromColor.ToString() + this.gradientColorComboBox1.ToColor.ToString());

        } 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.