zoukankan      html  css  js  c++  java
  • ListBox重绘

     

    .NET Framework 类库  ListBox.ItemHeight 属性

    DrawMode 属性设置为 DrawMode.OwnerDrawFixed 时,所有项具有相同的高度。当 DrawMode 属性设置为 DrawMode.OwnerDrawVariable 时,ItemHeight 属性指定添加到 ListBox 中的每个项的高度。因为所有者描述的列表中的每个项可具有不同的高度,所以可使用 GetItemHeight 方法获取 ListBox 中特定项的高度。如果对具有可变高度的项的 ListBox 使用 ItemHeight 属性,则此属性返回控件中第一个项的高度。

    ListBox 项的最大高度是 255 像素。

    listbox每行的文字名称重绘

    http://files.cnblogs.com/xe2011/CSharpListBoxDrawItem.rar

    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 WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                //添加字体
                foreach (FontFamily fam in FontFamily.Families)
                {
                    listBox1.Items.Add(fam.Name);
                }
                
                //OwnerDrawVariable
                listBox1.DrawMode = DrawMode.OwnerDrawVariable;
            }
    
            private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
            {
                e.DrawBackground();
                //e.DrawFocusRectangle();
                e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), new Font(listBox1.Items[e.Index].ToString(), 12), Brushes.Black, e.Bounds);
            }
    
            private void listBox1_MeasureItem(object sender, MeasureItemEventArgs e)
            {
                e.ItemHeight =22;
            }
        }
    }
    View Code

    ListBoxbox1的每行的行高

    http://files.cnblogs.com/xe2011/CSharpListBoxDrawItemHeight.rar

    private void Form1_Load(object sender, EventArgs e)
    {
        //OwnerDrawVariable
        listBox1.DrawMode = DrawMode.OwnerDrawVariable;
    }
    
    private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
    {
        e.DrawBackground();
        //e.DrawFocusRectangle();
    
        string s= listBox1.Items[e.Index].ToString();            
        int fontSize = Convert.ToInt32( listBox1.Items[e.Index].ToString() );
        Font font =new Font("Times New Roman", fontSize, FontStyle.Bold);
    
        e.Graphics.DrawString(s, font, Brushes.Black, e.Bounds);
    }
    
    private void listBox1_MeasureItem(object sender, MeasureItemEventArgs e)
    {
        e.ItemHeight =  Convert.ToInt32(listBox1.Items[e.Index].ToString())+12;
    }
    View Code

    Combobox的和ListBox的写法一样

    Combobox 文字名称http://files.cnblogs.com/xe2011/CSharpComboboxDrawItem.rar

            private void Form1_Load(object sender, EventArgs e)
            {
                ////添加字体
                foreach (FontFamily f in FontFamily.Families)
                {
                    comboBox1.Items.Add(f.Name);
                }
                
                //OwnerDrawVariable
                comboBox1.DrawMode = DrawMode.OwnerDrawVariable;
                comboBox1.MaxDropDownItems = 15;
                comboBox1.DropDownWidth = 200;
            }
    
            private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
            {
                e.DrawBackground();
                //e.DrawFocusRectangle();
                string s = comboBox1.Items[e.Index].ToString();
                string fontName = comboBox1.Items[e.Index].ToString();
                Font font = new Font(fontName, 12);
                e.Graphics.DrawString(s, font, Brushes.Black, e.Bounds);
            }
    
            private void comboBox1_MeasureItem(object sender, MeasureItemEventArgs e)
            {
                e.ItemHeight = 20;
            }
    View Code

    Combobox文字行高

     
    附件http://files.cnblogs.com/xe2011/CSharpComboboxDrawItemHeight.rar

    private void Form1_Load(object sender, EventArgs e)
    {
        this.comboBox1.Items.AddRange(new string[] {
        "8",
        "9",
        "10",
        "11",
        "12",
        "14",
        "16",
        "18",
        "20",
        "22",
        "24",
        "26",
        "28",
        "36",
        "48",
        "72"});
    
        //OwnerDrawVariable
        comboBox1.DrawMode = DrawMode.OwnerDrawVariable;
        comboBox1.MaxDropDownItems = 15;
        comboBox1.DropDownWidth = 200;
    
    }
    
    private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
    {
    
        e.DrawBackground();
        //e.DrawFocusRectangle();
    
        string s = comboBox1.Items[e.Index].ToString();
        int fontSize = Convert.ToInt32(comboBox1.Items[e.Index].ToString());
        Font font = new Font("Times New Roman", fontSize, FontStyle.Bold);
    
        e.Graphics.DrawString(s, font, Brushes.Black, e.Bounds);
    }
    
    private void comboBox1_MeasureItem(object sender, MeasureItemEventArgs e)
    {
        e.ItemHeight = Convert.ToInt32(comboBox1.Items[e.Index].ToString()) + 12;
    }
    View Code
  • 相关阅读:
    jedisPool.returnBrokenResource 弃用
    错误,各种错误
    Spring加载resource时classpath*:与classpath:的区别
    Embedded database support
    IDEA 中 显示 *.properties 为中文
    Key promoter IDEA 快捷键 提示 插件
    Win7系统怎么删除今日热点【系统天地】
    win10深度清理c盘垃圾的方法【系统天地】
    win7系统如何清理注册表残留
    win10系统文件无法复制到u盘怎么办【系统天地】
  • 原文地址:https://www.cnblogs.com/xe2011/p/3466079.html
Copyright © 2011-2022 走看看