Added highlight of dropdown items on mouse move

This commit is contained in:
Patrick Simpson 2017-06-28 10:10:34 +02:00
parent 500499a67e
commit ddec4f5bfc
4 changed files with 89 additions and 12 deletions

View File

@ -241,6 +241,9 @@
<Compile Include="Controls\KHintButton.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Controls\KListBox.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Controls\KProgressBar.cs">
<SubType>Component</SubType>
</Compile>

View File

@ -215,23 +215,14 @@ namespace Acacia.Controls
// Cannot use visibility of _dropDown to keep the open state, as clicking on the button already
// hides the popup before the event handler is shown.
private bool _isDroppedDown;
private bool _clickedButton;
private void _dropDown_Closed(object sender, ToolStripDropDownClosedEventArgs e)
{
/*if (_stateButton.IsMouseOver)
{
_clickedButton = true;
}*/
_isDroppedDown = false;
}
private void Button_Clicked()
{
/*if (_clickedButton)
_clickedButton = false;
else
DroppedDown = true;*/
DroppedDown = !DroppedDown;
this._edit.Focus();
}
@ -258,7 +249,6 @@ namespace Acacia.Controls
_dropControl.Height = Util.Bound(prefSize.Height, minHeight, maxHeight);
// Show the drop down below the current control
_dropDown.Show(this.PointToScreen(new Point(0, Height)));
_dropDown.Capture = true;
}
else
{

View File

@ -14,7 +14,7 @@ namespace Acacia.Controls
{
public class KComboBox : KAbstractComboBox
{
private readonly ListBox _list;
protected readonly KListBox _list;
#region Items properties
@ -45,10 +45,16 @@ namespace Acacia.Controls
public KComboBox()
{
MaxDropDownItems = 8;
_list = new ListBox();
_list = new KListBox();
_list.IntegralHeight = true;
DropControl = _list;
_list.DisplayMember = "DisplayName"; // TODO: remove from here
_list.SelectedIndexChanged += _list_SelectedIndexChanged;
}
private void _list_SelectedIndexChanged(object sender, EventArgs e)
{
System.Diagnostics.Trace.WriteLine("SELECTED: " + _list.SelectedIndex);
}
public void BeginUpdate()

View File

@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Acacia.Controls
{
public class KListBox : ListBox
{
private int _hoverIndex = -1;
public KListBox()
{
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
DrawMode = DrawMode.OwnerDrawFixed;
}
protected override void OnMouseMove(MouseEventArgs e)
{
Point point = PointToClient(Cursor.Position);
int newIndex = IndexFromPoint(point);
if (newIndex != _hoverIndex)
{
int oldIndex = _hoverIndex;
_hoverIndex = newIndex;
InvalidateItem(oldIndex);
InvalidateItem(_hoverIndex);
}
}
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
_hoverIndex = -1;
}
protected override void OnVisibleChanged(EventArgs e)
{
base.OnVisibleChanged(e);
_hoverIndex = -1;
}
private void InvalidateItem(int index)
{
if (index < 0)
return;
Invalidate(GetItemRectangle(index));
}
protected override void OnDrawItem(DrawItemEventArgs e)
{
// Create a custom event instance to be able to set the selected state for mouse hover
DrawItemState state = e.State;
if (_hoverIndex >= 0)
{
state = _hoverIndex == e.Index ? DrawItemState.Selected : DrawItemState.None;
}
DrawItemEventArgs draw = new DrawItemEventArgs(e.Graphics, e.Font, e.Bounds, e.Index, state);
draw.DrawBackground();
string text = Items[draw.Index].ToString();
using (StringFormat format = new StringFormat())
{
format.LineAlignment = StringAlignment.Center;
using (Brush brush = new SolidBrush(draw.ForeColor))
{
draw.Graphics.DrawString(text,
draw.Font, brush,
draw.Bounds,
format);
}
}
}
}
}