C#学习笔记:Windows窗体——自行绘制列表控件的项

参考书目:C#6.0学习笔记——从第一行C#代码到第一个项目设计(作者周家安)P328

学习内容:自行绘制列表控件的项,在listbox控件中显示系统字体

第一步:建立界面如下

仅包含一个listBox框,将其Dock属性设置为Fill,DrawMode属性设置为OwnerDrawVariable。

第二步,为列表框添加MeasureItem和DrawItem事件。

第三步,在Form的构造函数中加入读入系统字体的代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Example11_14
{
    public partial class Form1 : Form
    {
        /// <summary>
        /// 要绘制的文本的字体大小
        /// </summary>
        const float FONT_SIZE = 16f;
        public Form1()
        {
            InitializeComponent();
            this.Load += (s, e) =>
            {
                // 向ListBox添加字体的名字
                foreach (FontFamily family in FontFamily.Families)
                {
                    listBox1.Items.Add(family.Name);
                }
            };
        }

        private void listBox1_MeasureItem(object sender, MeasureItemEventArgs e)
        {
            //取出项中的文本
            if(e.Index <0 )
            {
                return;
            }
            string itemText = (sender as ListBox).Items[e.Index] as string;

            //创建相应的字体
            using (Font font = new Font(itemText, FONT_SIZE))
            {
                //计算待绘制文本的大小(宽度和高度)
                SizeF size = e.Graphics.MeasureString(itemText, font);
                //设置项的高度
                e.ItemHeight = Convert.ToInt32(size.Height);
            }

        }

        private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
        {
            //取出文本
            if (e.Index <0)
            {
                return;
            }
            string itemText = (sender as ListBox).Items[e.Index] as string;
            //创建用于绘制文本的字体对象
            using (Font font = new Font(itemText, FONT_SIZE))
            {
                //创建用于设置文本格式的对象
                StringFormat sf = new StringFormat();
                //文本在水平方向上对齐
                sf.Alignment = StringAlignment.Near;
                //文本在垂直方向上对齐
                sf.Alignment = StringAlignment.Center;
                //绘制默认背景
                e.DrawBackground();
                //绘制文本
                if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
                {    //&表示与运算,检测某个经组合后的枚举值是否包含某个值
                    //当前项被选中
                    e.Graphics.DrawString(itemText, font, SystemBrushes.HighlightText,
                        e.Bounds, sf);
                }
                else
                {
                    //当前项未选中
                    e.Graphics.DrawString(itemText, font, SystemBrushes.ControlText,
                        e.Bounds, sf);
                }
                //释放资源
                sf.Dispose();
                
            }

        }
    }
}

运行结果如下:

问题:感觉读入的字体数量不足,为什么呢?

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值