我试图在鼠标悬停在其上时更改listview
项目的背景颜色
我有一个鼠标悬停事件,但是如何将鼠标悬停在该项目上时添加“突出显示”效果?
private void pinnedAppsListBox_MouseHover(object sender, EventArgs e)
{
}
答案 0 :(得分:10)
使用此:
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);
}
答案 1 :(得分:1)
转到ListView的 ItemMouseHover 事件并添加,然后设置项目的属性“BackColor”。
private void listView1_ItemMouseHover(object sender, ListViewItemMouseHoverEventArgs e)
{
e.Item.BackColor = Color.Black;
}
答案 2 :(得分:0)
如果您正在使用ListBox,则处理起来非常困难,您需要为ListBox设置MouseHover事件并确定正在悬停的项目,然后手动绘制。
请参阅this answer。
但是,如果您使用的是ListView,则可以轻松添加ItemMouseHover
这样的事件:
private void pinnedAppsListView_MouseHover(object sender, EventArgs e)
{
e.Item.BackColor = Color.Lime;
}
答案 3 :(得分: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!
答案 4 :(得分:-1)
声明此全局变量
使用此Listview项目变量来跟踪在
上悬停的项目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);
然后在listview的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;
}