如何隐藏在详细模式下的.NET ListView控件垂直滚动条 [英] How to hide the vertical scroll bar in a .NET ListView Control in Details mode

查看:144
本文介绍了如何隐藏在详细模式下的.NET ListView控件垂直滚动条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个单一列详细模式ListView控件。这,是为了只与键盘使用的形式,大多与上/下箭头滚动并输入选择。所以,我并不真的需要有滚动条,并会和他们一样不显示简洁的外观。然而,当我设置ListView.Scrollable属性设置为false,我仍然可以移动所选的项目上下,但只要它移动到某个项目目前尚未考虑,列表不会移动,以显示该项目。我已经使用EnsureVisible以编程方式滚动列表试过,但不起任何作用时,在此模式下。

I've got a ListView control in Details mode with a single column. It's on a form that is meant to only be used with the keyboard, mostly with the up/down arrows for scrolling and enter to select. So I don't really need to have the scroll bars and would just like them to not show for a cleaner look. However, when I set the ListView.Scrollable property to false, I can still move the selected item up and down, but as soon as it moves to an item not currently in view, the list won't move to show that item. I've tried using EnsureVisible to programmatically scroll the list, but it does nothing when in this mode.

有什么办法手动移动列表中上下滚动,但无需滚动present?

Is there any way to manually move the list up and down to scroll, but without having the scrollbar present?

推荐答案

这并不容易,但它可以做到的。如果你试图隐藏通过ShowScrollBar滚动条,在ListView会简单地把它回来了。所以,你必须做一些更狡猾。

It's not easy but it can be done. If you try to hide the scroll bar through ShowScrollBar, the ListView will simply put it back again. So you have to do something more devious.

您将有拦截WM_NCCALCSIZE消息,并在那里,关闭垂直滚动的风格。每当列表视图试图再次打开它,您将在此处理程序再次将其关闭。

You will have to intercept the WM_NCCALCSIZE message, and in there, turn off the vertical scroll style. Whenever the listview tries to turn it on again, you will turn it off again in this handler.

public class ListViewWithoutScrollBar : ListView
{
    protected override void WndProc(ref Message m) {
        switch (m.Msg) {
            case 0x83: // WM_NCCALCSIZE
                int style = (int)GetWindowLong(this.Handle, GWL_STYLE);
                if ((style & WS_VSCROLL) == WS_VSCROLL)
                    SetWindowLong(this.Handle, GWL_STYLE, style & ~WS_VSCROLL);
                base.WndProc(ref m);
                break;
            default:
                base.WndProc(ref m);
                break;
        }
    }
    const int GWL_STYLE = -16;
    const int WS_VSCROLL = 0x00200000;

    public static int GetWindowLong(IntPtr hWnd, int nIndex) {
        if (IntPtr.Size == 4)
            return (int)GetWindowLong32(hWnd, nIndex);
        else
            return (int)(long)GetWindowLongPtr64(hWnd, nIndex);
    }

    public static int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong) {
        if (IntPtr.Size == 4)
            return (int)SetWindowLongPtr32(hWnd, nIndex, dwNewLong);
        else
            return (int)(long)SetWindowLongPtr64(hWnd, nIndex, dwNewLong);
    }

    [DllImport("user32.dll", EntryPoint = "GetWindowLong", CharSet = CharSet.Auto)]
    public static extern IntPtr GetWindowLong32(IntPtr hWnd, int nIndex);

    [DllImport("user32.dll", EntryPoint = "GetWindowLongPtr", CharSet = CharSet.Auto)]
    public static extern IntPtr GetWindowLongPtr64(IntPtr hWnd, int nIndex);

    [DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)]
    public static extern IntPtr SetWindowLongPtr32(IntPtr hWnd, int nIndex, int dwNewLong);

    [DllImport("user32.dll", EntryPoint = "SetWindowLongPtr", CharSet = CharSet.Auto)]
    public static extern IntPtr SetWindowLongPtr64(IntPtr hWnd, int nIndex, int dwNewLong);
}

这会给你没有滚动条一个ListView当您使用箭头键更改选择仍然滚动。

This will give you a ListView without scroll bars that still scrolls when you use the arrow keys to change selection.

这篇关于如何隐藏在详细模式下的.NET ListView控件垂直滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆