listview
マウスをアイテムの上に置いたときにアイテムの背景色を変更しようとしています
マウスホバーイベントがありますが、マウスがアイテムの上にホバリングしたときに「ハイライト」効果を追加するにはどうすればよいですか?
private void pinnedAppsListBox_MouseHover(object sender, EventArgs e)
{
}
これを使って:
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);
}
ListViewのItemMouseHoverイベントに移動し、アイテムのプロパティ「BackColor」を追加して設定します。
private void listView1_ItemMouseHover(object sender, ListViewItemMouseHoverEventArgs e)
{
e.Item.BackColor = Color.Black;
}
このグローバル変数を宣言します
このListviewItem変数を使用して、ホバーされたアイテムを追跡します
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;
}
ListBoxを使用している場合、処理は非常に困難です。ListBoxにMouseHoverイベントを設定し、ホバーされているアイテムを特定してから、手動で描画する必要があります。
この回答を参照してください。
ItemMouseHover
ただし、ListViewを使用している場合は、次のようなイベントを簡単に追加できます。
private void pinnedAppsListView_MouseHover(object sender, EventArgs e)
{
e.Item.BackColor = Color.Lime;
}
私はこの質問を何度も見ましたが、良い答えはありませんでした。私が知っている良い答えはありませんが、他の場所でいくつかのヒントを使用してそれを行いました。私はLazarusを使用してこれを行いましたが、あなたはそれをあなたの言語に適応させることができるはずです。
アイテムを入手してください。これらを個別にキャッチするように変数を設定することをお勧めします。また、最初にマウスダウン時のマウスボタンの状態を取得することもできます。
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!