3

listview当鼠标悬停在项目上时,我正在尝试更改项目的背景颜色

我有一个鼠标悬停事件,但是如何在鼠标悬停在项目上时添加“突出显示”效果?

private void pinnedAppsListBox_MouseHover(object sender, EventArgs e)
{

}
4

5 回答 5

12

用这个:

private void pinnedAppsListBox_MouseHover(object sender, EventArgs e){

   Point point = pinnedAppsListBox.PointToClient(Cursor.Position);
   int index = pinnedAppsListBox.IndexFromPoint(point);
   if (index < 0) return;
   //Do any action with the item
   pinnedAppsListBox.GetItemRectangle(index).Inflate(1,2);
}
于 2012-10-12T12:51:40.207 回答
1

转到 ListView 的ItemMouseHover事件并添加然后设置 Item 的属性“BackColor”。

private void listView1_ItemMouseHover(object sender, ListViewItemMouseHoverEventArgs e)
{
            e.Item.BackColor = Color.Black;
}
于 2012-09-09T15:23:03.943 回答
1

声明这个全局变量

使用此 Listview Item 变量来跟踪悬停的项目

ListViewItem lvHoveredItem;

设置以下函数为您的控件打开 DoubleBuffering 以防止闪烁:

public static void SetDoubleBuffering(System.Windows.Forms.Control control, bool value)
    {
        System.Reflection.PropertyInfo controlProperty = typeof(System.Windows.Forms.Control)
            .GetProperty("DoubleBuffered", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
        controlProperty.SetValue(control, value, null);
    }

加载控件的位置调用此函数

SetDoubleBuffering(lvTaskList, true);

然后在列表视图的 mousemove 事件中使用此代码

private void lvTaskList_MouseMove(object sender, MouseEventArgs e)
    {
        //Set the Color you want the list Item to be when mouse is over
        Color oItemColor = Color.Lavender;
        Color oOriginalColor = Color.blue; //Your original color


        //get the Item the Mouse is currently hover
        ListViewItem lvCurrentItem = lvTaskList.GetItemAt(e.X, e.Y);


        if ((lvCurrentItem != null) && (lvCurrentItem != lvHoveredItem))
        {
            lvCurrentItem.BackColor = oItemColor;

            if(lvHoveredItem != null)
            {
                lvHoveredItem.BackColor = oOriginalColor ;                    
            }

            lvHoveredItem = lvCurrentItem;
            return;

        }


        if (lvCurrentItem == null)
        {
            if (lvHoveredItem != null)
            {
                lvHoveredItem.BackColor = oOriginalColor; 
            }
        }

    }

您还可以添加 MouseLeave 事件

private void lvTaskList_MouseLeave(object sender, EventArgs e)
    {

        Color oOriginalColor = Color.Blue; //Your original color

        //When the mouse leave the control. If a ListViewItem was highlighted then set it's original color back
        if (lvHoveredItem != null)
        {
            lvHoveredItem.BackColor = oOriginalColor ;
        }

        lvHoveredItem = null;
    }
于 2016-11-03T21:49:49.490 回答
0

如果您使用的是 ListBox,则处理起来相当困难,您需要为 ListBox 设置 MouseHover 事件并确定悬停在哪个项目上,然后手动绘制它。

看到这个答案

但是,如果您使用的是 ListView,则可以轻松添加如下ItemMouseHover事件:

private void pinnedAppsListView_MouseHover(object sender, EventArgs e)
{
      e.Item.BackColor = Color.Lime;
}
于 2012-09-09T15:07:01.560 回答
0

我已经多次看到这个问题没有一个好的答案。我知道没有好的答案,但是,我做到了,在别处使用了一些提示。我是用 Lazarus 做的,但你应该能够适应你的语言。

获取项目。您可能需要设置变量以单独捕获这些变量。您可能还想先在 mousedown 时获取鼠标按钮的状态。

If (ListView.GetItemAt(X,Y) <> nil) then  // do this with an if else

// Next, you can get the bounding rect:
ListView.GetItemAt(X,Y).DisplayRect(drSelectBounds);  

//Option: If Button up or down then 
// you may have to catch this elsewhere, such as for a drag operation.

// Create and set a boolean variable:
HighLightOn := True;
ListView.Repaint;  // clears previous hightlights
ListView.Canvas.Brush.Color := clBtnFace; // or your color of choice     
ListView.Canvas.FillRect(Rect); 

// If you are moving around in an area where GetItem is nil, 
// then do this to stop flicker and remove the highlight:

If (ListView.GetItemAt(X,Y) = nil)  // do this with an if else
If HighLightOn then
begin
    SelectedList.Repaint;
    HighLightOn := False;
end;

// If a highlight gets left behind, 
// you may need to repeat this elsewhere, such as in a component exit.

// This is the basic gist of the issue. 
// There can be a lot of options or things to look for, 
// so you code could get more complicated. 
// I am not suggesting this is the best way to implement it, 
// but it is easy. Part of this code only works inside your app!
于 2016-12-31T06:29:12.903 回答