Exemple #1
0
 public SEListViewItemDoubleClickEventArgs(SEListViewItem item)
 {
     Item = item;
 }
        public void OnKeyDown(KeyEventArgs e)
        {
            // If the shift key or the control key is pressed and there is no focused item
            // set the first item as the focused item.
            if ((ShiftKey || ControlKey) && _imageListView.Items.Count != 0 &&
                _imageListView.FocusedItem == null)
            {
                _imageListView.Items[0].Focused = true;
            }

            SEListViewItem currentFocusedItem = _imageListView.FocusedItem;

            if (_imageListView.Items.Count != 0)
            {
                int index = 0;
                if (currentFocusedItem != null)
                {
                    index = currentFocusedItem.Index;
                }

                int newindex = ApplyNavKey(index, e.KeyCode);
                if (index != newindex)
                {
                    #region 根据新index做选择

                    if (ControlKey)
                    {
                        // Just move the focus
                    }
                    else if (_imageListView.AllowMultiSelection && ShiftKey)
                    {
                        int startIndex = 0;
                        int endIndex   = 0;
                        List <SEListViewItem> selectedItems = _imageListView.GetSelectedItems();
                        if (selectedItems.Count != 0)
                        {
                            startIndex = selectedItems[0].Index;
                            endIndex   = selectedItems[selectedItems.Count - 1].Index;
                            _imageListView.ClearSelect();
                        }
                        if (index == startIndex)
                        {
                            startIndex = newindex;
                        }
                        else if (index == endIndex)
                        {
                            endIndex = newindex;
                        }
                        for (int i = Math.Min(startIndex, endIndex); i <= Math.Max(startIndex, endIndex); i++)
                        {
                            _imageListView.Items[i].Selected = true;
                        }
                    }
                    else
                    {
                        _imageListView.ClearSelect();
                        _imageListView.Items[newindex].Selected = true;
                    }

                    currentFocusedItem.Focused             = false;
                    _imageListView.Items[newindex].Focused = true;

                    EnsureVisible(newindex);

                    #endregion

                    //触发事件
                    _imageListView.OnSelectedItemChanged();
                }
            }

            _imageListView.NeedPaint();
        }
        /// <summary>
        /// 获取项的呈现区域
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public Rectangle GetItemBounds(SEListViewItem item)
        {
            int index = _imageListView.Items.IndexOf(item);

            return(GetItemBounds(index));
        }
        public void MouseDown(MouseEventArgs e)
        {
            /*
             * 如果按下的是鼠标右键
             * 如果按在已选定的项上,只切换焦点,不改变选择
             * 如果按在未选定项上,则切换为点的项为选中项
             * 不考虑键盘按键
             */

            _mouseItemsAreaOffset = _itemsAreaOffset;

            List <SEListViewItem> oldSelectedItems = _imageListView.GetSelectedItems();

            SEListViewHitInfo hitInfo = HitTest(e.Location);

            if (hitInfo.ItemHit)
            {
                SEListViewItem        item               = _imageListView.Items[hitInfo.ItemIndex];
                List <SEListViewItem> allItems           = _imageListView.Items.ToList();
                SEListViewItem        currentFocusedItem = _imageListView.FocusedItem;

                if (_imageListView.LeftButton)
                {
                    #region 如果不允许多选
                    if (_imageListView.AllowMultiSelection == false)
                    {
                        //如果点击的项就是当前选择的项
                        if (oldSelectedItems.Count > 0 && oldSelectedItems.Contains(item))
                        {
                            //判断Control键是否按下,如果按下了Control键,取消当前项的选择状态
                            if (_imageListView.ControlKey)
                            {
                                item.Selected = false;
                            }
                        }
                        else
                        {
                            //如果点击的项不是当前选择的项
                            //清除原选定项的选定状态
                            _imageListView.ClearSelect();
                            //设置新项为选定项
                            item.Selected = true;
                        }
                    }
                    #endregion
                    #region 如果允许多选
                    //在同时按下 Control 和 Shift 的情况下,优先考虑 Shift
                    else
                    {
                        #region 如果按下 Shift
                        //判断是否按下了 Shift ,如果按下 Shift,不考虑 Control 的状态
                        //也不用考虑是否点击的项是否是现有选定项之一
                        if (_imageListView.ShiftKey)
                        {
                            //如果当前存在具有输入焦点的项
                            if (currentFocusedItem != null && currentFocusedItem != item)
                            {
                                //连续选中从当前具有焦点的项至点击的项之间的所有项
                                //并将不在此范围内的项取消选中状态
                                int startIndex = Math.Min(currentFocusedItem.Index, hitInfo.ItemIndex);
                                int endIndex   = Math.Max(currentFocusedItem.Index, hitInfo.ItemIndex);
                                foreach (var i in from c in oldSelectedItems where c.Index <startIndex || c.Index> endIndex select c)
                                {
                                    i.Selected = false;
                                }
                                for (int i = startIndex; i <= endIndex; i++)
                                {
                                    SEListViewItem eachItem = allItems[i];
                                    if (eachItem.Selected == false)
                                    {
                                        eachItem.Selected = true;
                                    }
                                }
                            }
                            //如果当前不存在具有输入焦点的项
                            else
                            {
                                //清除原选定项的选定状态
                                _imageListView.ClearSelect();
                                item.Selected = true;
                            }
                        }
                        #endregion
                        #region 如果 Shift键没有处于按下状态
                        else
                        {
                            #region 如果点击的项 是 当前选择的项之一
                            if (oldSelectedItems.Count > 0 && oldSelectedItems.Contains(item))
                            {
                                //判断是否按下了 Control,且没有按下 Shift
                                if (_imageListView.ControlKey && _imageListView.ShiftKey == false)
                                {
                                    item.Selected = false;
                                }

                                //判断是否什么键都没有按下
                                if (_imageListView.ControlKey == false && _imageListView.ShiftKey == false)
                                {
                                    //清除原选定项的选定状态
                                    _imageListView.ClearSelect();
                                    item.Selected = true;
                                }
                            }
                            #endregion
                            #region 如果点击的项 不是 当前选择的项之一
                            else
                            {
                                //判断Control键是否按下,如果按下了Control键,则保持原有选择的情况把新项也设置为选中
                                //否则清除当前选择
                                if (_imageListView.ControlKey == false)
                                {
                                    //清除原选定项的选定状态
                                    _imageListView.ClearSelect();
                                }
                                item.Selected = true;
                            }
                            #endregion
                        }
                        #endregion
                    }
                    #endregion
                }
                else
                {
                    //如果点在未选中的项上
                    if (oldSelectedItems.Contains(item) == false)
                    {
                        _imageListView.ClearSelect();
                        //设置新项为选定项
                        item.Selected = true;
                    }
                }

                #region 为项设置输入焦点

                //设置新的输入焦点要放在后面处理,因为在使用Shift连续选择时,需要用到原具有焦点的项
                if (currentFocusedItem == null || (currentFocusedItem != null && currentFocusedItem != item))
                {
                    if (currentFocusedItem != null)
                    {
                        currentFocusedItem.Focused = false;
                    }
                    item.Focused = true;
                }
                #endregion
            }
            else
            {
                _imageListView.ClearSelect();
            }

            List <SEListViewItem> newSelectedItems = _imageListView.GetSelectedItems();
            if (oldSelectedItems.SequenceEqual(newSelectedItems) == false)
            {
                _imageListView.NeedPaint();
                _imageListView.OnSelectedItemChanged();
            }
        }
        public void RenderItem(Graphics graphics, SEListViewItem item)
        {
            Debug.Assert(MouseSelecting == false, "MouseSelecting 为 " + MouseSelecting.ToString());

            _renderer.RenderItem(graphics, item);
        }