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
  • 相关阅读:
    校园WebGIS开发与实践(论文部分)
    初来博客园,请多多关照
    MIFtoTAB and TABtoMIF(MIF和TAB互转小工具)
    MIFtoSHP通用转换工具
    activity 对home 按钮事件的处理
    在XML布局文件里,会遇到的一些单位
    自己写的Adapter 无法被鼠标点中问题
    linux 下查看文件修改时间 等
    Android中使用GridView分页显示系统所安装的应用,支持拖动与手势滑动
    getLaunchIntentForPackage 获取到的为null原因
  • 原文地址:https://www.cnblogs.com/xe2011/p/3466079.html
Copyright © 2011-2022 走看看