大于列宽时,如何自动滚动列表视图列中的文本?

时间:2019-06-16 14:01:17

标签: winforms listview scroll auto

我有一个详细的列表视图。 提取的数据(文本)有时比列宽还宽。 选择后如何使文本自动水平滚动?

这样,列宽可以保持不变,并且如果总的文本显示太多,自动滚动最终将显示该列内的所有文本。 滚动条也不需要显示(请不要!)。

在没有更好的示例的情况下,应近似表示为: https://www.youtube.com/watch?v=HMUmw-cq5w4

尽管我现在在我的电脑上找不到一个台式机应用程序,但我已经看到这种行为。

到目前为止,我已经搜索了几个小时,但仍然无法定义正确的术语,导致所有结果显示出与我想要达到的结果有所不同的东西。

            int counter = 0;
            bool addItems = false;
            foreach (var listbox in info)
            {
                // Create items 
                if (string.IsNullOrEmpty(listbox.nbAlbumArtist) == false)
                {
                    ListViewItem item = new ListViewItem(listbox.name, counter);
                    //item.SubItems.Add(listbox.name);

                    item.SubItems.Add(listbox.nbAlbumArtist + " albums");

                    items[counter] = item;
                    counter++;

                    addItems = true;
                }
                else
                {
                    lb_result.Items.Add(listbox.name);
                }```

protected void listView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
        {
            e.DrawDefault = true;
        }

        private TextFormatFlags GetTextAlignment(ListView lstView, int colIndex)
        {
            TextFormatFlags flags = (lstView.View == View.Tile)
                ? (colIndex == 0) ? TextFormatFlags.Default : TextFormatFlags.Bottom
                : TextFormatFlags.VerticalCenter;

            flags |= TextFormatFlags.LeftAndRightPadding | TextFormatFlags.NoPrefix;
            switch (lstView.Columns[colIndex].TextAlign)
            {
                case HorizontalAlignment.Left:
                    flags |= TextFormatFlags.Left;
                    break;
                case HorizontalAlignment.Right:
                    flags |= TextFormatFlags.Right;
                    break;
                case HorizontalAlignment.Center:
                    flags |= TextFormatFlags.HorizontalCenter;
                    break;
            }
            return flags;
        }

protected void listView1_DrawItem(object sender, DrawListViewItemEventArgs e)
        {
            ListView lView = sender as ListView;
            if (lView.View == View.Details) return;
            TextFormatFlags flags = GetTextAlignment(lView, 0);
            Color itemColor = e.Item.ForeColor;
            if (e.Item.Selected)
            {
                using (SolidBrush bkgrBrush = new SolidBrush(listViewSelectionColor))
                {
                    e.Graphics.FillRectangle(bkgrBrush, e.Bounds);
                }
                itemColor = e.Item.ForeColor;
            }
            else
            {
                e.DrawBackground();
            }
            TextRenderer.DrawText(e.Graphics, e.Item.Text, e.Item.Font, e.Bounds, itemColor, flags);

            if (lView.View == View.Tile && e.Item.SubItems.Count > 1)
            {
                var subItem = e.Item.SubItems[1];
                flags = GetTextAlignment(lView, 1);
                TextRenderer.DrawText(e.Graphics, subItem.Text, subItem.Font, e.Bounds, SystemColors.GrayText, flags);
            }
        }

        private void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
        {
            ListView lView = sender as ListView;
            TextFormatFlags flags = GetTextAlignment(lView, e.ColumnIndex);
            Color itemColor = e.Item.ForeColor;
            if (e.Item.Selected)
            {
                if (e.ColumnIndex == 0 || lView.FullRowSelect)
                {
                    using (SolidBrush bkgrBrush = new SolidBrush(listViewSelectionColor))
                    {
                        e.Graphics.FillRectangle(bkgrBrush, e.Bounds);
                    }
                    itemColor = e.Item.ForeColor;
                }
            }
            else
            {
                e.DrawBackground();
            }
            TextRenderer.DrawText(e.Graphics, e.SubItem.Text, e.SubItem.Font, e.Bounds, itemColor, flags);
        }

0 个答案:

没有答案
相关问题