使用ctrl+mousewheel更改ListView的fontSize

本文关键字:fontSize ListView 更改 ctrl+mousewheel 使用 | 更新日期: 2023-09-27 18:00:39

我有一个ListView(auftraegeView)。在这个ListView中,我通过Ctrl+MouseWheel(又名)来更改其项目的字体大小。在excel或浏览器中进行简单的缩放。

在表单的ctor中,我将方法订阅到事件

        this.MouseWheel += scrollZoom;

我的EventHandler计算新的FontHeight并应用它,如果它没有超过界限的话。RowHeight总是保持稍大,最后我调整了列的大小,这样缩放也可以在水平比例上进行。

private void scrollZoom(object sender, MouseEventArgs e)
    {
        if(Control.ModifierKeys != Keys.Control)
            return;
        int currFontHeight = ListViewFontHeight;
        int delta = (e.Delta)/120;
        int newFontHeight = currFontHeight + delta;
        if(newFontHeight < 1 || newFontHeight > 150)
            return;
        ListViewFontHeight = newFontHeight;
        ListViewRowHeight = ListViewFontHeight + 4;
        auftraegeView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
    }

ListViewFontHeight获取第一个项的Font.Height。(所有项目的价值都是相同的,所以第一个项目和任何项目一样好。)

集合似乎是问题所在(见下文)。我的想法是,我只需浏览每个项目并更改字体。

    private int ListViewFontHeight
    {
        get { return auftraegeView.Items[0].Font.Height; }
        set
        {
            foreach (ListViewItem line in auftraegeView.Items)
            {
                line.Font = new Font(line.Font.FontFamily, value);
            }
        }
    }

问题
无论我滚动的方向如何,FontSize只有才会增加,直到它碰到天花板。其余部分工作正常(设置ListViewRowHeight、检测事件…)。
是什么原因造成的?

使用ctrl+mousewheel更改ListView的fontSize

试试这个:

delta = (e.Delta > 0? 1 : -1);

为了安全起见,可以使用不同的鼠标设置。

这对我有效:

 float delta = (e.Delta > 0 ? 2f : -2f);
 listView1.Font = new Font (listView1.Font.FontFamily, listView1.Font.Size + delta);

自己找到的:

在ListViewFontHeight-属性中,get访问器使用Item.Font.Height而不是Item.Font.Size

private int ListViewFontHeight
    {
        get { return (int)auftraegeView.Items[0].Font.Size; } //works now