可拖拽的ListBox

时间:2022-12-20 23:35:13

  之前在写播放器的时候,遇到了一个问题,现在播放器无论是千千,KuGoo还是比较原始的MediaPlayer,它们的播放表都是可以拖拽的,直接把文件拖到播放表实现歌曲的添加那个先暂且不说,光是播放表里面的歌曲次序也可以通过拖拽来调整。但是VS提供的ListBox没能直接通过设定某个属性实现这个拖拽排序,于是俺就开始了实现这功能的探索,无意中还找到了ListBox与ListBox之间元素的拖拽,于是一并实现了,遂述此文以记之。

  其实无论是ListBox里的拖拽排序,还是ListBox间的拖动,都是通过三个事件来实现的:DragDrop,DragOver和MouseDown,对于整个拖拽的过程来说,三个事件的触发顺序是MouseDown->DragOver->DragDrop。对于拖拽排序和控件间的拖动,代码会有所差异。下面则一分为二的说说各自的处理,由于我这里是扩展控件的,直接重写ListBox类的Onxxx方法。如果直接使用ListBox控件的话,就要在这三个事件绑定的方法里面写了。

  拖拽排序  

        protected override void  OnMouseDown(MouseEventArgs e)
{
base.OnMouseDoubleClick(e); if (this.Items.Count == || e.Button != MouseButtons.Left || this.SelectedIndex == - || e.Clicks == )
return; int index = this.SelectedIndex;
object item = this.Items[index];
DragDropEffects dde = DoDragDrop(item,
DragDropEffects.All);
}

首先要判断一下当前的ListBox有没有元素,还有鼠标有没有点中元素。接着到拖拽的过程

        protected override void OnDragOver(DragEventArgs drgevent)
{
base.OnDragOver(drgevent); drgevent.Effect = DragDropEffects.Move;
}

最后到拖放结束,鼠标按键松开的时候触发

        protected override void OnDragDrop(DragEventArgs drgevent)
{
base.OnDragDrop(drgevent); object item = dragSource.SelectedItem; int index = this.IndexFromPoint(this.PointToClient(new Point(drgevent.X, drgevent.Y)));
this.Items.Remove(item);
if (index < )
this.Items.Add(item);
else
this.Items.Insert(index, item);
}

主要是获取那个被选中(后来就被拖动)的那个元素,然后把它从ListBox先移除,从鼠标当前的坐标来获取到所在元素的索引值,那个位置,那个索引就是被拖拽的元素的新位置,用Insert把它插进去。

  ListBox间的拖动

  这个就要先知道各个方法究竟是那个控件的事件触发时调用的。其余大体上跟上面的是一致的。

  举个例子,现在有两个ListBox lst1,lst2。我把lst1的一个元素拖到lst2中去,事件触发的队列是这样的

  Lst1.MouseDown=>lst1.DragOver=>lst1.DragOver=>….lst1.DragOver=>lst2.DragOver=>lst2.DragOver=>…..=>lst2.DragOver=>lst2.DragDrop

由于整个流程涉及到两个控件,最后元素的添加与元素的删除分别是两个控件的事,于是我这里就另外声明多一个静态字段来存放那个lst1。记录来源的ListBox只能在MouseDown记录了。

public static ListBox dragSource;

        protected override void  OnMouseDown(MouseEventArgs e)
{
base.OnMouseDoubleClick(e); if (this.Items.Count == || e.Button != MouseButtons.Left || this.SelectedIndex == - || e.Clicks == )
return;
dragSource = this; int index = this.SelectedIndex;
object item = this.Items[index];
DragDropEffects dde = DoDragDrop(item,
DragDropEffects.All);
}

除了dragSource = this;外,其余都与拖拽排序的一样。

        protected override void OnDragOver(DragEventArgs drgevent)
{
base.OnDragOver(drgevent);
drgevent.Effect = DragDropEffects.Move;
}

这个可以说跟拖拽排序的一模一样了。

        protected override void OnDragDrop(DragEventArgs drgevent)
{
base.OnDragDrop(drgevent); object item = dragSource.SelectedItem; if ( dragSource != this)
{
dragSource.Items.Remove(item);
this.Items.Add(item);
} }

  因为是ListBox间的拖动,所以源ListBox和目标ListBox不能一样,处理还更简单,从源ListBox把元素删掉,然后增加到当前的ListBox中来。

  如果想要既要控件间的拖动,拖动后又要按位置插入,那就把两个处理融合一下咯,本文末尾有我拓展控件的整份源码。需要的园友可以展开来看一下。

  

  交替颜色

  下面还介绍我另外一个拓展,就是单双行的交替颜色。记得以前的千千的播放表是有交替颜色的,现在的不知道,太久没用了,KuGoo现在的没有了,MediaPlayer12的也没有。

  我这里是重写OnDrawItem,直接拖控件的可以用DrawItem时间,接下来就是GDI+的内容了。

        protected override void OnDrawItem(DrawItemEventArgs e)
{
if (this.Items.Count < ) return;
if (e.Index < ) return;
bool selected = (e.State & DrawItemState.Selected) == DrawItemState.Selected;
if(selected)
e.Graphics.FillRectangle(selectRowBursh, e.Bounds);
else if (e.Index % != )
e.Graphics.FillRectangle(OddRowBursh, e.Bounds);
else
e.Graphics.FillRectangle(EvenRowBursh, e.Bounds); if(selected)
{
e.Graphics.DrawString(this.GetItemText(e.Index), e.Font,
selectFontBursh, e.Bounds); }
else
{
e.Graphics.DrawString(this.GetItemText(e.Index), e.Font,
normalFontBursh, e.Bounds);
}
e.DrawFocusRectangle();
base.OnDrawItem(e);
}

  值得一提的是这里判断选中的不是用e.Index==this.SelectedIndex,而是用(e.State & DrawItemState.Selected) == DrawItemState.Selected,当ListBox的选择模式用了多选而不是单选的时候,调用Select属性会报错,这个也是上面拖拽的局限性,当ListBox是多选的时候,上面的拖拽就会抛异常了,而且如果单纯用e.Index==this.SelectIndex的话,选择了一个元素,当在选择另一个元素的时候,之前选择过的元素的高亮状态不会消失,这个是什么原因我也没搞懂。如果有哪位园友知道的,麻烦指点一下。

  最后附上整个控件的源码

     public class DragableListBox:ListBox
{
#region 字段
private bool isDraw; //是否执行绘制
SolidBrush evenRowBursh ;
SolidBrush oddRowBursh;
Brush normalFontBursh=SystemBrushes.ControlText;
Brush selectFontBursh = SystemBrushes.HighlightText;
Brush selectRowBursh = SystemBrushes.Highlight;
private bool dragAcross;
private bool dragSort; public static ListBox dragSource;
#endregion public DragableListBox()
{
this.DoubleBuffered = true;
this.OddColor = this.BackColor;
} #region 外放成员 #region 属性 [Description("跨ListBox拖放元素"), Category("行为")]
public bool DragAcross
{
get { return (dragAcross&&AllowDrop&&SelectionMode== System.Windows.Forms.SelectionMode.One); }
set
{
dragAcross = value;
if (value) this.AllowDrop = true;
}
} [Description("元素拖动排序"),Category("行为")]
public bool DragSort
{
get { return dragSort && AllowDrop && SelectionMode == System.Windows.Forms.SelectionMode.One; }
set
{
dragSort = value;
if (value) this.AllowDrop = true;
}
} private Color oddColor;
[Description("单数行的底色"), Category("外观")]
public Color OddColor
{
get { return oddColor; }
set
{
oddColor = value;
isDraw = oddColor != this.BackColor;
if (isDraw) DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
else DrawMode = System.Windows.Forms.DrawMode.Normal;
}
} #endregion #region 事件 [Description("跨ListBox拖拽完成后触发"),Category("行为")]
public event DraggdHandler DraggedAcross; [Description("拖拽排序后触发"), Category("行为")]
public event DraggdHandler DraggedSort; #endregion #endregion #region 重写方法 #region 拖拽 protected override void OnDragDrop(DragEventArgs drgevent)
{
base.OnDragDrop(drgevent);
if (!DragAcross && !DragSort) return; object item = dragSource.SelectedItem; if (DragAcross && !DragSort && dragSource != this)
{
dragSource.Items.Remove(item);
this.Items.Add(item);
if (DraggedAcross != null)
DraggedAcross(this, new DraggedEventArgs() { DragItem=item, SourceControl=dragSource });
}
else if (DragSort &&(( dragSource == this&&! DragAcross)||DragAcross))
{
int index = this.IndexFromPoint(this.PointToClient(new Point(drgevent.X, drgevent.Y)));
dragSource.Items.Remove(item);
if (index < )
this.Items.Add(item);
else
this.Items.Insert(index, item);
if (DragAcross && DraggedAcross != null)
DraggedAcross(this, new DraggedEventArgs() { DragItem=item,SourceControl=dragSource });
if (DraggedSort != null)
DraggedSort(this, new DraggedEventArgs() { DragItem=item,SourceControl=dragSource, DestineIndex=index });
} } protected override void OnDragOver(DragEventArgs drgevent)
{
base.OnDragOver(drgevent);
if (!DragAcross&&!DragSort) return; //dragDestince=this;
drgevent.Effect = DragDropEffects.Move;
} protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDoubleClick(e);
if (!DragAcross && !DragSort ) return; if (this.Items.Count == || e.Button != MouseButtons.Left || this.SelectedIndex == - || e.Clicks == )
return;
dragSource = this; int index = this.SelectedIndex;
object item = this.Items[index];
DragDropEffects dde = DoDragDrop(item,
DragDropEffects.All);
} #endregion #region 绘制 protected override void OnDrawItem(DrawItemEventArgs e)
{
if (this.Items.Count < ) return;
if (!isDraw) return;
if (e.Index < ) return;
bool selected = (e.State & DrawItemState.Selected) == DrawItemState.Selected;
if(selected)
//if (e.Index == this.SelectedIndex)
e.Graphics.FillRectangle(selectRowBursh, e.Bounds);
else if (e.Index % != )
e.Graphics.FillRectangle(OddRowBursh, e.Bounds);
else
e.Graphics.FillRectangle(EvenRowBursh, e.Bounds); //if (e.Index == this.SelectedIndex )
if(selected)
{
e.Graphics.DrawString(this.GetItemText(e.Index), e.Font,
selectFontBursh, e.Bounds); }
else
{
e.Graphics.DrawString(this.GetItemText(e.Index), e.Font,
normalFontBursh, e.Bounds);
}
e.DrawFocusRectangle();
base.OnDrawItem(e);
} protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (oddRowBursh != null) oddRowBursh.Dispose();
if (evenRowBursh != null) evenRowBursh.Dispose();
} #endregion #endregion #region 私有方法和属性 private SolidBrush EvenRowBursh
{
get
{
if(evenRowBursh==null)
{
evenRowBursh = new SolidBrush(this.BackColor);
return evenRowBursh;
}
if ( evenRowBursh.Color == this.BackColor)return evenRowBursh;
evenRowBursh.Dispose();
evenRowBursh = new SolidBrush(this.BackColor);
return evenRowBursh;
}
set
{
if(evenRowBursh!=null) evenRowBursh.Dispose();
evenRowBursh = value;
}
} private SolidBrush OddRowBursh
{
get
{
if (oddRowBursh == null)
{
oddRowBursh = new SolidBrush(this.OddColor);
return oddRowBursh;
}
if (oddRowBursh.Color == this.OddColor) return oddRowBursh;
oddRowBursh.Dispose();
oddRowBursh = new SolidBrush(this.OddColor);
return oddRowBursh;
}
set
{
if (oddRowBursh != null) oddRowBursh.Dispose();
oddRowBursh = value;
}
} private string GetItemText(int index)
{
try
{
object item = this.Items[index];
if (string.IsNullOrEmpty(this.DisplayMember) || string.IsNullOrWhiteSpace(this.DisplayMember))
return item.ToString();
PropertyInfo proInfo = item.GetType().GetProperty(this.DisplayMember);
return proInfo.GetValue(item, null).ToString();
}
catch { return this.Name; }
} #endregion public class DraggedEventArgs:EventArgs
{
public ListBox SourceControl { get; set; } public object DragItem { get; set; } public int DestineIndex { get; set; } public DraggedEventArgs()
{
DestineIndex = -;
}
} public delegate void DraggdHandler(object sender, DraggedEventArgs e);
}

DragableListBox