虚拟模式下的ListView(CLR 2.0)和鼠标悬停= OwnerDraw的问题 [英] ListView (CLR 2.0) in virtual mode and OwnerDraw with mouse over = problems

查看:67
本文介绍了虚拟模式下的ListView(CLR 2.0)和鼠标悬停= OwnerDraw的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我使用新版本的ListView(.net framework 2.0)。我决定将其设置为虚拟(由于项目数量很大)并将ownerdraw属性设置为true(以对项目绘图进行完全控制)。

正确显示项目(和子项目)只要我不将鼠标移动到项目/子项目上。由于我开始将鼠标移到项目/子项目上,子项目(字符串)的内容消失了。这就像控件在内部没有正确完成Invalidate。我在绘制子项的函数中设置了一个断点,当我将鼠标移动时,我从不闯入它(protected override void OnDrawSubItem)。此外,项目的选择没有正确绘制。

我加入了从ListView控件派生的类。

Hi,

I use the new version of the ListView (.net framework 2.0). I decided to make it virtual (due to the large amount of items) and set the ownerdraw property to true (to have the full control on the items drawing).

The items (and sub items) are correctly displayed as long as i do not move the mouse over the items/subitems. Since i start to move the mouse over the items/subitems, the content of the subitems (a string) disapears. It is like the Invalidate was not correctly done internally by the control. I put a break point in the function that draws the subitems and i never break into it (protected override void OnDrawSubItem) when i move the mouse over. Also, the selection of the items is not painted correctly.

I joined the class derived from the ListView control.

如果有人可以照亮我的蜡烛,我真的很感激那个问题。

谢谢!问候,弗雷德。

I would really appreciate if someone could light my candle on that issue.

Thank you!
Regards,
Fred.

使用System;
使用System.Drawing;
使用System.Collections;
使用System.Windows.Forms;

using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;


命名空间VirtualListView
{
类MyVirtualListView:ListView
{
private ListViewItem [] _itemCache;


namespace VirtualListView
{
    class MyVirtualListView : ListView
    {
        private ListViewItem[] _itemCache;

public MyVirtualListView():base()
{
SetStyle(ControlStyles.DoubleBuffer,true);
//SetStyle(ControlStyles.UserMouse ,true);

        public MyVirtualListView() : base()
        {
            SetStyle(ControlStyles.DoubleBuffer, true);
            //SetStyle(ControlStyles.UserMouse, true);

this.OwnerDraw = true;
this.VirtualMode = true;
this.DoubleBuffered = true;
this.View = View.Details;
this.FullRowSelect = true;
this.VirtualListSize = 1000000;

this._itemCache = new ListViewItem [this.VirtualListSize];

            this.OwnerDraw = true;
            this.VirtualMode = true;
            this.DoubleBuffered = true;
            this.View = View.Details;
            this.FullRowSelect = true;
            this.VirtualListSize = 1000000;
           
            this._itemCache = new ListViewItem[this.VirtualListSize];

th is.Columns.Add(" item",120,Horizo​​ntalAlignment.Left);
this.Columns.Add(" subitem1",120,Horizo​​ntalAlignment.Left);
this.Columns.Add(" subitem2",120,Horizo​​ntalAlignment.Left);
}受保护的覆盖void OnDrawItem(DrawListViewItemEventArgs e)
{
e.DrawBackground();
e.DrawFocusRectangle();

            this.Columns.Add("item", 120, HorizontalAlignment.Left);
            this.Columns.Add("subitem1", 120, HorizontalAlignment.Left);
            this.Columns.Add("subitem2", 120, HorizontalAlignment.Left);
        }
       
        protected override void OnDrawItem(DrawListViewItemEventArgs e)
        {
            e.DrawBackground();
            e.DrawFocusRectangle();

if((e.State& ListViewItemStates.Selected)!= 0)
{
//绘制所选项目的背景和焦点矩形。
e.Graphics.FillRectangle(Brushes.Blue,e.Bounds);
e .DrawFocusRectangle();
}





绘制未选项的背景。
e.Graphics.FillRectangle(Brushes.White,e.Bounds);
}

            if ((e.State & ListViewItemStates.Selected) != 0)
            {
                // Draw the background and focus rectangle for a selected item.
                e.Graphics.FillRectangle(Brushes.Blue, e.Bounds);
                e.DrawFocusRectangle();
            }
            else
            {
                // Draw the background for an unselected item.
                e.Graphics.FillRectangle(Brushes.White, e.Bounds);
            }

//为"详细信息"视图以外的视图绘制项目文本。
if(this.View!= View.Details)
{
e。 DrawText();
}
}

            // Draw the item text for views other than the Details view.
            if (this.View != View.Details)
            {
                e.DrawText();
            }
        }

//绘制子项文本并应用基于内容的格式。
protected override void OnDrawSubI tem(DrawListViewSubItemEventArgs e)
{
TextFormatFlags flags = TextFormatFlags.Left;
using(StringFormat sf = new StringFormat())
{
//存储列文本对齐,让它默认
//如果尚未设置为中心或右侧,则为左。
switch(e.Header.TextAlign)
{
case Horizo​​ntalAlignment.Center:
sf.Alignment = StringAlignment。中心;
flags = TextFormatFlags.Horizo​​ntalCenter;
break;
case Horizo​​ntalAlignment.Right:
sf.Alignment = StringAlignment.Far;
flags = TextFormatFlags.Right;
break;
}

Rectangle bounds = e.Bounds;
e.Graphics.DrawString(e.SubItem.Text,this.Font,Brushes.Black,e。 Bounds,sf);
}

受保护的覆盖void OnDrawColumnHeader(DrawListViewColumnHeaderEventArgs e)
{
using(StringFormat sf = new StringFormat())
{
//存储列文本对齐方式,如果尚未设置为"中心"或"右",则将其默认为// @左。
switch(e.Header.TextAlign)
{
case Horizo​​ntalAlignment.Center:
sf.Alignme nt = StringAlignment.Center;
break;
case Horizo​​ntalAlignment.Right:
sf.Alignment = StringAlignment.Far;
break;
}

        // Draws subitem text and applies content-based formatting.
        protected override void OnDrawSubItem(DrawListViewSubItemEventArgs e)
        {
            TextFormatFlags flags = TextFormatFlags.Left;
            using (StringFormat sf = new StringFormat())
            {
                // Store the column text alignment, letting it default
                // to Left if it has not been set to Center or Right.
                switch (e.Header.TextAlign)
                {
                    case HorizontalAlignment.Center:
                        sf.Alignment = StringAlignment.Center;
                        flags = TextFormatFlags.HorizontalCenter;
                        break;
                    case HorizontalAlignment.Right:
                        sf.Alignment = StringAlignment.Far;
                        flags = TextFormatFlags.Right;
                        break;
                }
                Rectangle bounds = e.Bounds;
                e.Graphics.DrawString(e.SubItem.Text, this.Font, Brushes.Black, e.Bounds, sf);
            }
        }
        protected override void OnDrawColumnHeader(DrawListViewColumnHeaderEventArgs e)
        {
            using (StringFormat sf = new StringFormat())
            {
                // Store the column text alignment, letting it default
                // to Left if it has not been set to Center or Right.
                switch (e.Header.TextAlign)
                {
                    case HorizontalAlignment.Center:
                        sf.Alignment = StringAlignment.Center;
                        break;
                    case HorizontalAlignment.Right:
                        sf.Alignment = StringAlignment.Far;
                        break;
                }

//绘制标准标题背景。
e.DrawBackground();

                // Draw the standard header background.
                e.DrawBackground();

//绘制标题文本。使用(Font headerFont = new Font(" Verdana" ,8,FontStyle.Regular))
{
e.Graphics.DrawString(e.Header.Text,headerFont,Brushes.Black,e.Bounds,sf);
}
}
return;
}
protected override void OnRetrieveVirtualItem(Retrie veVirtualItemEventArgs e)
{
ListViewItem itm = _itemCache [e.ItemIndex];

if(itm == null)
{
itm = new ListViewItem(" Item_" + e.ItemIndex.ToString());
itm.SubItems.Add(" subitem1_" + e.ItemIndex.ToString());
itm.SubItems.Add(" subitem2_" + e.ItemIndex .ToString());
_itemCache [e.ItemIndex] = itm;
}

e.Item = itm;

}

                // Draw the header text.
                using (Font headerFont = new Font("Verdana", 8, FontStyle.Regular))
                {
                    e.Graphics.DrawString(e.Header.Text, headerFont,Brushes.Black, e.Bounds, sf);
                }
            }
            return;
        }
        protected override void OnRetrieveVirtualItem(RetrieveVirtualItemEventArgs e)
        {
            ListViewItem itm = _itemCache[e.ItemIndex];
           
            if (itm == null)
            {
                itm = new ListViewItem("Item_" + e.ItemIndex.ToString());
                itm.SubItems.Add("subitem1_" + e.ItemIndex.ToString());
                itm.SubItems.Add("subitem2_" + e.ItemIndex.ToString());
                _itemCache[e.ItemIndex] = itm;
            }
            e.Item = itm;
          
        }

/ / protected override void OnMouseMove(MouseEventArgs e)
// {
// //this.Invalidate();
// ListViewItem Item = this.GetItemAt(5,eY);
// if(Item!= null)
// {
// Item.ListView.Invalidate(Item.GetBounds(ItemBoundsPortion.Entire));
// Item.Focused = true;
// }
//}


}

        //protected override void OnMouseMove(MouseEventArgs e)
        //{
        //    //this.Invalidate();
        //    ListViewItem Item = this.GetItemAt(5, e.Y);
        //    if (Item != null)
        //    {
        //        Item.ListView.Invalidate(Item.GetBounds(ItemBoundsPortion.Entire));
        //        Item.Focused = true;
        //    }
        //}
    }
}

推荐答案

我正在经历确切地说是同样的问题。当我同时使用OwnerDraw和VirtualMode时,我发现当鼠标进入控件并悬停时,我的代码会不断收到DrawSubItem和DrawItem通知,状态设置为零,项目索引设置为零。

I'm experiencing precisely the same problem. When I use OwnerDraw and VirtualMode at the same time, I find that when the mouse enters the control and hovers, my code continually receives DrawSubItem and DrawItem notifications with the state set to zero and the item index set to zero.

据我所知,这只是控件中的一个坏错误。由于框架是如此孤立,我还没有找到解决问题的方法。

As far as I can tell, this is simply a bad bug in the control. Because the frameworks are so insular, I haven't yet found a way to work around the problem.


这篇关于虚拟模式下的ListView(CLR 2.0)和鼠标悬停= OwnerDraw的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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