文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>C# WinForm控件美化扩展系列之ListView

C# WinForm控件美化扩展系列之ListView

时间:2011-03-21  来源:誓言的爱

昨天的文章中介绍了怎样使ListBox隔行显示不同的颜色,今天接着介绍怎样扩展ListView控件,使他也具有这样的功能,而且重绘他的Header,使其更美观。

在这篇文章中,我只对View 为 Details的时候进行了重绘,至于其他的Ttile,List,SmallIcon,LargeIcon,大家可以照着代码自己实现。

这个控件的美化大致跟ListBox的差不了多少,我们还是一步一步的来实现它吧:

1、  继承ListView,并把 OwnerDraw 设为True,如下:

       public ListViewEx()

            : base()

        {

            base.OwnerDraw = true;

}

2、  给继承的控件添加4个属性:RowBackColor1,RowBackColor2,SelectedColor,HeadColor这三个颜色分别是数据项的交替的背景色、数据项选择后的背景色和列表头的背景颜色。

3、  重绘Header,通过重写OnDrawColumnHeader函数来实现。

        protected override void OnDrawColumnHeader(

            DrawListViewColumnHeaderEventArgs e)

        {

            base.OnDrawColumnHeader(e);

 

            Graphics g = e.Graphics;

            Rectangle bounds = e.Bounds;

 

            Color baseColor = _headColor;

            Color borderColor = _headColor;

            Color innerBorderColor = Color.FromArgb(200, 255, 255);

 

            RenderBackgroundInternal(

                g,

                bounds,

                baseColor,

                borderColor,

                innerBorderColor,

                0.35f,

                true,

                LinearGradientMode.Vertical);

 

            TextFormatFlags flags = GetFormatFlags(e.Header.TextAlign);

            Rectangle textRect = new Rectangle(

                       bounds.X + 3,

                       bounds.Y,

                       bounds.Width - 6,

                       bounds.Height); ;

 

            if (e.Header.ImageList != null)

            {

                Image image = e.Header.ImageIndex == -1 ?

                    null : e.Header.ImageList.Images[e.Header.ImageIndex];

                if (image != null)

                {

                    Rectangle imageRect = new Rectangle(

                        bounds.X + 3,

                        bounds.Y + 2,

                        bounds.Height - 4,

                        bounds.Height - 4);

                    g.InterpolationMode = InterpolationMode.HighQualityBilinear;

                    g.DrawImage(image, imageRect);

 

                    textRect.X = imageRect.Right + 3;

                    textRect.Width -= imageRect.Width;

                }

            }

            TextRenderer.DrawText(

                   g,

                   e.Header.Text,

                   e.Font,

                   textRect,

                   e.ForeColor,

                   flags);

        }

4、  重绘 Item,通过重写OnDrawItem,OnDrawSubItem来实现,这里我只对Details这种显示模式进行了重写。

        protected override void OnDrawItem(DrawListViewItemEventArgs e)

        {

            base.OnDrawItem(e);

            if (View != View.Details)

            {

                e.DrawDefault = true;

            }

        }

 

        protected override void OnDrawSubItem(DrawListViewSubItemEventArgs e)

        {

            base.OnDrawSubItem(e);

            if (View != View.Details)

            {

                return;

            }

            if(e.ItemIndex == -1)

            {

                return;

            }

            Rectangle bounds = e.Bounds;

            ListViewItemStates itemState = e.ItemState;

            Graphics g = e.Graphics;

            if ((itemState & ListViewItemStates.Selected)

                == ListViewItemStates.Selected)

            {

                bounds.Height--;

                Color baseColor = _selectedColor;

                Color borderColor = _selectedColor;

                Color innerBorderColor = Color.FromArgb(200, 255, 255);

 

                RenderBackgroundInternal(

                    g,

                    bounds,

                    baseColor,

                    borderColor,

                    innerBorderColor,

                    0.35f,

                    true,

                    LinearGradientMode.Vertical);

                bounds.Height++;

            }

            else

            {

                Color backColor = e.ItemIndex % 2 == 0 ?

                _rowBackColor1 : _rowBackColor2;

 

                using (SolidBrush brush = new SolidBrush(backColor))

                {

                    g.FillRectangle(brush, bounds);

                }

            }

 

            TextFormatFlags flags = GetFormatFlags(e.Header.TextAlign);

 

            if (e.ColumnIndex == 0)

            {

                if (e.Item.ImageList == null)

                {

                    e.DrawText(flags);

                    return;

                }

                Image image = e.Item.ImageIndex == -1 ?

                    null : e.Item.ImageList.Images[e.Item.ImageIndex];

                if (image == null)

                {

                    e.DrawText(flags);

                    return;

                }

                Rectangle imageRect = new Rectangle(

                    bounds.X + 4,

                    bounds.Y + 2,

                    bounds.Height - 4,

                    bounds.Height - 4);

                g.DrawImage(image, imageRect);

 

                Rectangle textRect = new Rectangle(

                    imageRect.Right + 3,

                    bounds.Y,

                    bounds.Width - imageRect.Right - 3,

                    bounds.Height);

                TextRenderer.DrawText(

                    g,

                    e.Item.Text,

                    e.Item.Font,

                    textRect,

                    e.Item.ForeColor,

                    flags);

                return;

            }

            e.DrawText(flags);

     }

 

转载:http://www.csharpwin.net/ddwstp/net/csharp/418dr7279.shtml

 

相关阅读 更多 +
排行榜 更多 +
找茬脑洞的世界安卓版

找茬脑洞的世界安卓版

休闲益智 下载
滑板英雄跑酷2手游

滑板英雄跑酷2手游

休闲益智 下载
披萨对对看下载

披萨对对看下载

休闲益智 下载