简体   繁体   English

当重新绘制并且光标在项目上方时,ListViewItem失去背景色

[英]ListViewItem loses background color when repainted and the cursor is over the item

I have a listview control obviously with numerous listviewitems, whenever the control is repainted the item which the mouse is "over" at the time loses it's background colour (ie. it's white). 我有一个显然带有大量listviewitems的listview控件,每当重新绘制该控件时,鼠标当时在其上方的项目就会失去其背景色(即白色)。

Once the item is painted again and the cursor is not over it the correct background colour is restored. 再次绘制该项目且光标不在其上方时,将恢复正确的背景颜色。

Does anyone know why this happens and how it can be stopped? 有谁知道为什么会这样以及如何阻止它?

Any help would be appreciated 任何帮助,将不胜感激

To expand on this and hopefully give a little more context.... 对此进行扩展,并希望提供更多背景信息。

It's happening on Windows 7, .NET 3.5 (although it was also happening when the project was targetted at 4.0 too). 它在Windows 7,.NET 3.5上发生(尽管在将项目定位为4.0时也发生了)。 The theme on my dev computer is Viao Theme 5, however it also happens on another of my machines which has the default Windows 7 theme. 我的开发计算机上的主题是Viao Theme 5,但是它也发生在另一台具有默认Windows 7主题的计算机上。

The listview is instantiated like this... 列表视图是这样实例化的...

        this.PlaylistTrackList.Cursor = System.Windows.Forms.Cursors.Hand;
        this.PlaylistTrackList.Dock = System.Windows.Forms.DockStyle.Fill;
        this.PlaylistTrackList.FullRowSelect = true;
        this.PlaylistTrackList.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable;
        this.PlaylistTrackList.LabelWrap = false;
        this.PlaylistTrackList.Location = new System.Drawing.Point(4, 4);
        this.PlaylistTrackList.Margin = new System.Windows.Forms.Padding(4);
        this.PlaylistTrackList.MultiSelect = false;
        this.PlaylistTrackList.UseCompatibleStateImageBehavior = false;
        this.PlaylistTrackList.View = System.Windows.Forms.View.Details;

Let's say there are 5 list view items, A,B,C,D, and E. If the background color property of items BC and D is red, nothing is selected and nothing is going to be selected. 假设有5个列表视图项目A,B,C,D和E。如果项目BC和D的背景色属性为红色,则不会选择任何内容,也不会选择任何内容。 When the list view control is redrawn for whatever reason and the cursor happens to be over any of items BC or D then the item is drawn with a white background color not red as it should be. 如果出于任何原因重新绘制了列表视图控件,并且光标恰巧位于项目BC或D上,则该项目将以白色背景色绘制,而不应该是红色。 However once the mouse is not over the item again and its redrawn is returns to red. 但是,一旦鼠标不再位于该项目上,并且其重绘将变为红色。

How can I stop it always drawing the item that the cursor is over with a white background regardless of the "real" background color property? 我如何停止始终以白色背景绘制光标悬停的项目,而不管“真实”背景颜色属性如何?

Initialize the ListView control as follows: 初始化ListView控件,如下所示:

    listView1.BackColor = Color.Black;
    listView1.ForeColor = Color.White;
    listView1.Dock = DockStyle.Fill;
    listView1.View = View.Details;
    listView1.FullRowSelect = true;

For more details check the following link: http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.ownerdraw.aspx 有关更多详细信息,请检查以下链接: http : //msdn.microsoft.com/zh-cn/library/system.windows.forms.listview.ownerdraw.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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