zoukankan      html  css  js  c++  java
  • winform 重绘listbox

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication17
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
    
            /// <summary>
            /// 在这个函数之前必须修改listbox的DrawMode属性为OwnerDrawVariable
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
            {
                int index = e.Index;//获取当前要进行绘制的行的序号,从0开始。
                Graphics g = e.Graphics;//获取Graphics对象。
                Rectangle bound = e.Bounds;//获取当前要绘制的行的一个矩形范围。
                string text = listBox1.Items[index].ToString();//获取当前要绘制的行的显示文本。
                if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
                {
                    //如果当前行为选中行。
                    //绘制选中时要显示的蓝色边框。
                    g.DrawRectangle(Pens.Black, bound.Left, bound.Top, bound.Width - 1, bound.Height - 1);
                    Rectangle rect = new Rectangle(bound.Left, bound.Top, bound.Width, bound.Height);
                    //绘制选中时要显示的蓝色背景。
                    g.FillRectangle(Brushes.Red, rect);
                    
                    //绘制显示文本。
                    TextRenderer.DrawText(g, text, this.Font, rect, Color.Black,
                                          TextFormatFlags.VerticalCenter | TextFormatFlags.Left);
                    
                }
                else
                {
                 
                    Rectangle rect = new Rectangle(bound.Left, (bound.Top ), bound.Width, bound.Height);
                    if (index == 0)
                    {
                        g.FillRectangle(Brushes.Yellow, rect);
                    }
                    else if (index == 1)
                    {
                        g.FillRectangle(Brushes.YellowGreen, rect);
                    }
                    else
                    {
                        g.FillRectangle(Brushes.SkyBlue, rect);
                    }
                    TextRenderer.DrawText(g, text, this.Font, rect, Color.Black, TextFormatFlags.VerticalCenter | TextFormatFlags.Left);
                    
                                   
                }
                
            }
    
            
        }
    }
    View Code
  • 相关阅读:
    MFC开发编程规范(二)
    Mysql日期和时间函数大全(转)
    php获取客户端IP地址的几种方法
    postgres 查看数据表和索引的大小
    PHP应用memcache函数详解
    css自动截取文字 兼容IE firefox Opera
    JavaScript在IE和Firefox浏览器下的7个差异兼容写法小结
    Zend_Auth与Zend_Acl访问控制链
    去除所有js,html,css代码问题
    [转]那些相见恨晚的 JavaScript 技巧
  • 原文地址:https://www.cnblogs.com/nocoding/p/3369628.html
Copyright © 2011-2022 走看看