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
  • 相关阅读:
    关于MQ的对比
    关于RabbitMQ(二)
    关于QPS、TPS、并发用户数、吞吐量的关系
    关于使用Ecplise构建gradle项目
    关于记录一次线上真实环境多线程引发的问题
    关于MySQL——find_in_set()函数的使用
    关于数据库的表连接
    关于Java线程池
    IntelliJ搭建Scala及Spark工程
    idea编写wordcount程序及spark-submit运行
  • 原文地址:https://www.cnblogs.com/nocoding/p/3369628.html
Copyright © 2011-2022 走看看