简体   繁体   中英

Change fontSize of ListView with ctrl + mousewheel

I have a ListView ( auftraegeView ). Of this ListView I whish to change the FontSize of its Items through Ctrl + MouseWheel aka. a simple zoom like in excel or a browser.

In the form's ctor I subscribed my method to the event

        this.MouseWheel += scrollZoom;

My EventHandler calculates the new FontHeight and applies it, if it doesn't exceed the bounds. The RowHeight is always kept a little bigger, finally I resize the columns so the zoom also works on the horizontal scale.

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 gets the Font.Height of the first Item. (Value is identical across all Items, so the first is as good as any.)

The set is where the issue seems to be (see below). My idea is that I just go through each Item and change the Font.

    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);
            }
        }
    }

ISSUE / QUESTION
Regardless of the direction I scroll in, the FontSize only increases till it hits the ceiling. The rest works fine (setting ListViewRowHeight, detecting the event at all,...).
What might be causing this?

Try this:

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

to be on the safe side for different mouse settings.

This works for me:

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

Found it myself:

In the ListViewFontHeight - property the get accessor used Item.Font.Height instead of Item.Font.Size

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM