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
  • 相关阅读:
    关于java异常处理的自我学习
    html学习
    java第七周动手动脑
    作业
    动手动脑
    我要建立自己的java代码仓库
    第三周作业
    day0319 模块
    day0318装饰器和内置函数
    day0315 迭代器
  • 原文地址:https://www.cnblogs.com/nocoding/p/3369628.html
Copyright © 2011-2022 走看看