C# 在鼠标悬停事件上突出显示列表框项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12340135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hightlight Listbox item on mouse over event
提问by user1559618
I am attempting to change a listviewitem's background colour when a mouse hovers over it
listview当鼠标悬停在项目上时,我试图更改项目的背景颜色
I have a mouse hover event, but how can I add a "highlight" effect upon a mouse hovering over the item?
我有一个鼠标悬停事件,但是如何在鼠标悬停在项目上时添加“突出显示”效果?
private void pinnedAppsListBox_MouseHover(object sender, EventArgs e)
{
}
采纳答案by user1741166
Use this:
用这个:
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);
}
回答by Chibueze Opata
If you're using a ListBox, it is quite more difficult to handle, you will need to set a MouseHover event for the ListBox and determine which item is being hovered on and then draw it manually.
如果您使用的是 ListBox,则处理起来比较困难,您需要为 ListBox 设置 MouseHover 事件并确定悬停在哪个项目上,然后手动绘制。
See this answer.
看到这个答案。
However if you're using a ListView, you can easily add an ItemMouseHoverevent like this:
但是,如果您使用的是 ListView,则可以轻松添加这样的ItemMouseHover事件:
private void pinnedAppsListView_MouseHover(object sender, EventArgs e)
{
e.Item.BackColor = Color.Lime;
}
回答by user484458
Go to the ListView's ItemMouseHoverevent and add then set the property "BackColor" of the Item.
转到 ListView 的ItemMouseHover事件并添加然后设置 Item 的属性“BackColor”。
private void listView1_ItemMouseHover(object sender, ListViewItemMouseHoverEventArgs e)
{
e.Item.BackColor = Color.Black;
}
回答by Max
Declare this Global variable
声明这个全局变量
Use this Listview Item variable to keep track of what item was hovered on
使用此 Listview Item 变量来跟踪悬停在哪个项目上
ListViewItem lvHoveredItem;
Set the following function to turn on DoubleBuffering for your control to prevent flickering:
设置以下函数为您的控件打开 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);
}
Where your control is loaded call this function
加载控件的位置调用此函数
SetDoubleBuffering(lvTaskList, true);
Then use this code in the mousemove event of your 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;
}
}
}
You can also add the MouseLeave Event
您还可以添加 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;
}
回答by Redenegue
I have seen this question many times without a good answer. There is no good answer that I know, but, I did it, using some hints elsewhere. I did this using Lazarus, but you should be able to adapt it to your language.
我已经多次看到这个问题,但没有一个很好的答案。我知道没有好的答案,但是,我做到了,在其他地方使用了一些提示。我使用 Lazarus 做到了这一点,但您应该能够使其适应您的语言。
Get the item. You may want to set variables to catch these individually. You may also want to get the state of your mouse buttons at mousedown first.
拿到物品。您可能希望设置变量以单独捕获这些变量。您可能还想首先在 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!

