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
  • 相关阅读:
    jQuery加载
    2.第一个flask web程序
    [转]前端懒加载以及预加载
    [转]当你在浏览器中输入 google.com 并且按下回车之后发生了什么?
    [转]如何让在FLASH不浮动在层之上
    逻辑回归基础梳理
    线性回归基础梳理
    [傻瓜式一步到位] 阿里云服务器Centos上部署一个Flask项目
    Java学习之"Hello World"
    JDK安装及Java环境变量配置
  • 原文地址:https://www.cnblogs.com/nocoding/p/3369628.html
Copyright © 2011-2022 走看看