日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C#實現ComboBox變色的示例代碼_C#教程

作者:芝麻粒兒 ? 更新時間: 2023-02-14 編程語言

實踐過程

效果

代碼

   public partial class B_ComboBox : ComboBox
    {
        public B_ComboBox()
        {
            InitializeComponent();
            this.DrawMode = DrawMode.OwnerDrawFixed;
            //this.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.ListBox_DrawItem);
        }

        #region 變量

        private static Brush[] listBoxBrushes; //該數組用來存儲繪制listBox1背景的Brush對象
        private static int place = -1; //顏色位置的取值
        private static bool naught = true; //判斷是否重繪

        #endregion

        #region 屬性

        private bool TGradualC = false;

        [Browsable(true), Category("控件的重繪設置"), Description("判斷是否進行漸變色的設置")] //在“屬性”窗口中顯示DataStyle屬性
        public bool GradualC
        {
            get { return TGradualC; }
            set
            {
                TGradualC = value;
                this.Invalidate();
            }
        }

        private Color TColorSelect = Color.Gainsboro;

        [Browsable(true), Category("控件的重繪設置"), Description("項被選中后的高亮度顏色")] //在“屬性”窗口中顯示DataStyle屬性
        public Color ColorSelect
        {
            get { return TColorSelect; }
            set
            {
                TColorSelect = value;
                this.Invalidate();
            }
        }

        private Color TColor1 = Color.CornflowerBlue;

        [Browsable(true), Category("控件的重繪設置"), Description("第一個顏色的設置")] //在“屬性”窗口中顯示DataStyle屬性
        public Color Color1
        {
            get { return TColor1; }
            set
            {
                TColor1 = value;
                this.Invalidate();
            }
        }

        private Color TColor1Gradual = Color.Thistle;

        [Browsable(true), Category("控件的重繪設置"), Description("第一個顏色的漸變色設置")] //在“屬性”窗口中顯示DataStyle屬性
        public Color Color1Gradual
        {
            get { return TColor1Gradual; }
            set
            {
                TColor1Gradual = value;
                this.Invalidate();
            }
        }

        private Color TColor2 = Color.PaleGreen;

        [Browsable(true), Category("控件的重繪設置"), Description("第二個顏色的設置")] //在“屬性”窗口中顯示DataStyle屬性
        public Color Color2
        {
            get { return TColor2; }
            set
            {
                TColor2 = value;
                this.Invalidate();
            }
        }

        private Color TColor2Gradual = Color.DarkKhaki;

        [Browsable(true), Category("控件的重繪設置"), Description("第二個顏色的漸變色設置")] //在“屬性”窗口中顯示DataStyle屬性
        public Color Color2Gradual
        {
            get { return TColor2Gradual; }
            set
            {
                TColor2Gradual = value;
                this.Invalidate();
            }
        }

        #endregion

        private void B_ComboBox_DrawItem(object sender, DrawItemEventArgs e)
        {
            Rectangle r = new Rectangle(0, 0, this.Width, this.Height); //設置重繪的區域
            SolidBrush SolidB1 = new SolidBrush(this.Color1); //設置上一行顏色
            SolidBrush SolidB2 = new SolidBrush(this.Color2); //設置下一行顏色
            //設置上一行的漸變色
            LinearGradientBrush LinearG1 = new LinearGradientBrush(r, this.Color1, this.Color1Gradual,
                LinearGradientMode.BackwardDiagonal);
            //設置下一行的漸變色
            LinearGradientBrush LinearG2 = new LinearGradientBrush(r, this.Color2, this.Color2Gradual,
                LinearGradientMode.BackwardDiagonal);
            //將單色與漸變色存入Brush數組中
            listBoxBrushes = new Brush[] {SolidB1, LinearG1, SolidB2, LinearG2};
            if (this.Items.Count <= 0) //如果當前控件為空
                return;
            if (e.Index == (this.Items.Count - 1)) //如果繪制的是最后一個項
            {
                bool tem_bool = true;
                if (e.Index == 0 && tem_bool) //如果當前繪制的是第一個或最后一個項
                    naught = false; //不進行重繪
            }

            if (naught) //對控件進行重繪
            {
                //獲取當前繪制的顏色值
                Brush brush =
                    listBoxBrushes[
                        place = (GradualC) ? (((e.Index % 2) == 0) ? 1 : 3) : (((e.Index % 2) == 0) ? 0 : 2)];
                //用指定的畫刷填充列表項范圍所形成的矩形
                e.Graphics.FillRectangle(brush, e.Bounds);
                //判斷當前項是否被選取中
                bool selected = ((e.State & DrawItemState.Selected) == DrawItemState.Selected) ? true : false;
                if (selected) //如果當前項被選中
                {
                    e.Graphics.FillRectangle(new SolidBrush(ColorSelect), e.Bounds); //繪制當前項
                }

                //繪制當前項中的文本
                e.Graphics.DrawString(this.Items[e.Index].ToString(), this.Font, Brushes.Black, e.Bounds);
            }

            e.DrawFocusRectangle(); //繪制聚焦框
        }
    }

原文鏈接:https://zhima.blog.csdn.net/article/details/128101726

欄目分類
最近更新