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

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

问题描述

我有一个 ListView 控件,它只有一个列的详细信息模式.它位于一个只能与键盘一起使用的表单上,主要是使用向上/向下箭头进行滚动和输入以进行选择.所以我真的不需要滚动条,只是希望它们不显示以获得更清晰的外观.但是,当我将 ListView.Scrollable 属性设置为 false 时,我仍然可以上下移动所选项目,但是一旦它移动到当前不在视图中的项目,列表就不会移动以显示该项目.我曾尝试使用确保可视化以编程方式滚动列表,但在此模式下它什么也不做.

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.

有没有办法手动上下移动列表来滚动,但没有滚动条?

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天全站免登陆