回复 1楼leadcom的帖子
请参考附件Demo:
效果图:
关键代码:
- private void imageList_MouseDown(object sender, MouseEventArgs e)
- {
- try
- {
- if (imageList.HitTest(e.X, e.Y) != null)
- {
- bMouseDown = true;
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.ToString());
- }
- base.OnMouseDown(e);
- }
- private void imageList_MouseUp(object sender, MouseEventArgs e)
- {
- if (bDragging)
- {
- try
- {
- RasterImageListItem HitTestListItem = imageList.HitTest(e.X, e.Y);
- if (HitTestListItem != null)
- {
- Rectangle itemRect = imageList.GetItemRectangle(HitTestListItem);
- int insertIndex = imageList.Items.IndexOf(HitTestListItem);
- if (e.X > (itemRect.Left + itemRect.Width / 2))
- {
- insertIndex++;
- }
- // 禁掉一些事件
- imageList.BeginUpdate();
- // 创建三个Item用来保存拖拽前后的项目
- RasterImageListItemCollection NonSelectedItemsBefore = new RasterImageListItemCollection(null);
- RasterImageListItemCollection SelectedItems = new RasterImageListItemCollection(null);
- RasterImageListItemCollection NonSelectedItemsAfter = new RasterImageListItemCollection(null);
- int counter = 0;
- RasterImageListItem[] tempItems = new RasterImageListItem[imageList.Items.Count];
- imageList.AutoDisposeImages = false;
- while (imageList.Items.Count > 0)
- {
- if (imageList.Items[0].Selected)
- {
- SelectedItems.Add(imageList.Items[0]);
- }
- else
- {
- if (counter < insertIndex)
- NonSelectedItemsBefore.Add(imageList.Items[0]);
- else
- NonSelectedItemsAfter.Add(imageList.Items[0]);
- }
- imageList.Items.RemoveAt(0);
- counter++;
- }
- // 重新添加
- imageList.Items.AddRange(NonSelectedItemsBefore);
- imageList.Items.AddRange(SelectedItems);
- imageList.Items.AddRange(NonSelectedItemsAfter);
- // 绘制
- imageList.AutoDisposeImages = true;
- imageList.EndUpdate();
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.ToString());
- }
- }
- bDragging = false;
- bMouseDown = false;
- base.OnMouseUp(e);
- }
- private void imageList_MouseMove(object sender, MouseEventArgs e)
- {
- if (bMouseDown)
- {
- bDragging = true;
- }
- base.OnMouseMove(e);
- }
复制代码 |