亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C#實(shí)現(xiàn)自定義ListBox背景的示例詳解

 更新時(shí)間:2022年12月16日 10:41:37   作者:芝麻粒兒  
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)自定義ListBox背景,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下

實(shí)踐過程

效果

代碼

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

        #region 變量
        private static Brush[] listBoxBrushes;//該數(shù)組用來存儲(chǔ)繪制listBox1背景的Brush對(duì)象
        private static int place = -1;//顏色位置的取值
        private static bool naught = true;//判斷是否重繪
        #endregion

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

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

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

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

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

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

        #region 事件
        /// <summary>
        /// 鼠標(biāo)移出控件的可見區(qū)域時(shí)觸發(fā)
        /// </summary>
        protected virtual void ListBox_DrawItem(object sender, DrawItemEventArgs e)
        {
            Rectangle r = new Rectangle(0, 0, this.Width, this.Height);//設(shè)置重繪的區(qū)域
            SolidBrush SolidB1 = new SolidBrush(this.Color1);//設(shè)置上一行顏色
            SolidBrush SolidB2 = new SolidBrush(this.Color2);//設(shè)置下一行顏色
            //設(shè)置上一行的漸變色
            LinearGradientBrush LinearG1 = new LinearGradientBrush(r, this.Color1, this.Color1Gradual, LinearGradientMode.BackwardDiagonal);
            //設(shè)置下一行的漸變色
            LinearGradientBrush LinearG2 = new LinearGradientBrush(r, this.Color2, this.Color2Gradual, LinearGradientMode.BackwardDiagonal);
            //將單色與漸變色存入Brush數(shù)組中
            listBoxBrushes = new Brush[] { SolidB1, LinearG1, SolidB2, LinearG2 };
            e.DrawBackground();
            if (this.Items.Count <= 0)//如果當(dāng)前控件為空
                return;
            if (e.Index == (this.Items.Count - 1))//如果繪制的是最后一個(gè)項(xiàng)
            {
                bool tem_bool = true;
                if (e.Index == 0 && tem_bool)//如果當(dāng)前繪制的是第一個(gè)或最后一個(gè)項(xiàng)
                    naught = false;//不進(jìn)行重繪
            }
            if (naught)//對(duì)控件進(jìn)行重繪
            {
                //獲取當(dāng)前繪制的顏色值
                Brush brush = listBoxBrushes[place = (GradualC) ? (((e.Index % 2) == 0) ? 1 : 3) : (((e.Index % 2) == 0) ? 0 : 2)];
                e.Graphics.FillRectangle(brush, e.Bounds);//用指定的畫刷填充列表項(xiàng)范圍所形成的矩形
                bool selected = ((e.State & DrawItemState.Selected) == DrawItemState.Selected) ? true : false;//判斷當(dāng)前項(xiàng)是否被選取中
                if (selected)//如果當(dāng)前項(xiàng)被選中
                {
                    e.Graphics.FillRectangle(new SolidBrush(ColorSelect), e.Bounds);//繪制當(dāng)前項(xiàng)
                }
                e.Graphics.DrawString(this.Items[e.Index].ToString(), this.Font, Brushes.Black, e.Bounds);//繪制當(dāng)前項(xiàng)中的文本
            }
            e.DrawFocusRectangle();//繪制聚焦框
        }

        #endregion
    }

到此這篇關(guān)于C#實(shí)現(xiàn)自定義ListBox背景的示例詳解的文章就介紹到這了,更多相關(guān)C#自定義ListBox背景內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 一個(gè)讀寫csv文件的C#類

    一個(gè)讀寫csv文件的C#類

    這篇文章主要為大家詳細(xì)介紹了一個(gè)讀寫csv文件的C#類,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • 詳解C#中的Async和Await用法

    詳解C#中的Async和Await用法

    這篇文章主要介紹了C#中的Async和Await用法,包括在C#5.0下一些新特性的影響,需要的朋友可以參考下
    2015-07-07
  • WPF實(shí)現(xiàn)自帶觸控鍵盤的文本框

    WPF實(shí)現(xiàn)自帶觸控鍵盤的文本框

    這篇文章實(shí)現(xiàn)了WPF自帶觸控鍵盤的文本框,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-10-10
  • C#使用linq語句查詢數(shù)組中以特定字符開頭元素的方法

    C#使用linq語句查詢數(shù)組中以特定字符開頭元素的方法

    這篇文章主要介紹了C#使用linq語句查詢數(shù)組中以特定字符開頭元素的方法,涉及C#使用linq進(jìn)行查詢的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-04-04
  • 如何在C#中使用指針

    如何在C#中使用指針

    這篇文章主要介紹了如何在C#中使用指針,文中代碼簡(jiǎn)單易懂,幫助大家更好的工作和學(xué)習(xí),感興趣的朋友快來了解下
    2020-06-06
  • C#中out與ref的區(qū)別實(shí)例解析

    C#中out與ref的區(qū)別實(shí)例解析

    這篇文章主要介紹了C#中out與ref的區(qū)別實(shí)例解析,對(duì)C#初學(xué)者有不錯(cuò)的學(xué)習(xí)借鑒價(jià)值,需要的朋友可以參考下
    2014-08-08
  • C#實(shí)現(xiàn)多文件打包壓縮(.Net?Core)

    C#實(shí)現(xiàn)多文件打包壓縮(.Net?Core)

    本文詳細(xì)講解了.Net?Core框架下C#實(shí)現(xiàn)多文件打包壓縮的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-12-12
  • c# yield提高代碼性能和可讀性

    c# yield提高代碼性能和可讀性

    Yield可以讓你的代碼更加高效并擁有更高的可讀性,我想已經(jīng)沒有什么借口可以阻止我們學(xué)習(xí)和使用yield
    2013-12-12
  • c#采用toml做配置文件遇到的坑

    c#采用toml做配置文件遇到的坑

    這篇文章主要介紹了c#采用toml做配置文件遇到的坑,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),通過本文介紹得出c#用toml文件讀取非整數(shù)字請(qǐng)用double,不要用float,decimal倒無所謂,反正編譯不過,切記不要用float,需要的朋友可以參考下
    2024-04-04
  • C#實(shí)現(xiàn)員工ID卡的識(shí)別功能

    C#實(shí)現(xiàn)員工ID卡的識(shí)別功能

    這篇文章主要為大家詳細(xì)介紹了C#如何實(shí)現(xiàn)識(shí)別員工ID卡的功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下
    2023-01-01

最新評(píng)論