internal void RenderItem(ShengImageListViewItem item)
 {
     if (Suspend == false)
     {
         _layoutManager.RenderItem(this.CreateGraphics(), item);
     }
 }
 /// <summary>
 /// 双击项
 /// </summary>
 /// <param name="item"></param>
 public void OnItemDoubleClick(ShengImageListViewItem item)
 {
     if (ItemDoubleClick != null)
     {
         ItemDoubleClick(this, new ShengImageListViewItemDoubleClickEventArgs(item));
     }
 }
        /// <summary>
        /// 判断指定的项是否处于可见状态
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public ShengImageListViewItemVisibility IsItemVisible(ShengImageListViewItem item)
        {
            int itemIndex = _imageListView.Items.IndexOf(item);

            if (_imageListView.Items.Count == 0)
            {
                return(ShengImageListViewItemVisibility.NotVisible);
            }

            if (itemIndex < 0 || itemIndex > _imageListView.Items.Count - 1)
            {
                return(ShengImageListViewItemVisibility.NotVisible);
            }

            if (itemIndex < _firstPartiallyVisible || itemIndex > _lastPartiallyVisible)
            {
                return(ShengImageListViewItemVisibility.NotVisible);
            }
            else if (itemIndex >= _firstVisible && itemIndex <= _lastVisible)
            {
                return(ShengImageListViewItemVisibility.Visible);
            }
            else
            {
                return(ShengImageListViewItemVisibility.PartiallyVisible);
            }
        }
Example #4
0
        /// <summary>
        /// 绘制项的内容
        /// </summary>
        /// <param name="g"></param>
        /// <param name="bounds"></param>
        /// <param name="item"></param>
        internal virtual void DrawItemContent(Graphics g, Rectangle bounds, ShengImageListViewItem item)
        {
            //显示debug信息
            string debugInfo = item.Index + Environment.NewLine +
                               bounds.ToString() + Environment.NewLine +
                               item.State.ToString();

            g.DrawString(debugInfo, SystemFonts.DefaultFont, Brushes.Black, bounds);
        }
Example #5
0
        public bool Container(ShengImageListViewItem item)
        {
            Debug.Assert(item != null, "ImageListViewItem 为 null");

            if (item == null)
            {
                return(false);
            }

            return(_thumbnails.Keys.Contains(item));
        }
Example #6
0
        /// <summary>
        /// 绘制项
        /// </summary>
        /// <param name="g"></param>
        /// <param name="item"></param>
        /// <param name="state"></param>
        /// <param name="bounds"></param>
        internal virtual void DrawItem(Graphics g, ShengImageListViewItem item)
        {
            Rectangle bounds = LayoutManager.GetItemBounds(item);

            g.SetClip(bounds);

            DrawItemBackground(g, bounds);

            Rectangle backgroundRect = bounds;

            backgroundRect.Width  -= 1;
            backgroundRect.Height -= 1;

            // Paint background Selected
            if ((LayoutManager.Focused && ((item.State & ShengImageListViewItemState.Selected) == ShengImageListViewItemState.Selected)) ||
                (LayoutManager.Focused == false && ((item.State & ShengImageListViewItemState.Selected) == ShengImageListViewItemState.Selected) && ((item.State & ShengImageListViewItemState.Hovered) == ShengImageListViewItemState.Hovered)))
            {
                using (Brush bSelected = new LinearGradientBrush(backgroundRect, Theme.SelectedColorStart, Theme.SelectedColorEnd, LinearGradientMode.Vertical))
                {
                    //   ImageListViewUtility.FillRoundedRectangle(g, bSelected, bounds, 4);
                    g.FillPath(bSelected, DrawingTool.RoundedRect(backgroundRect, _radius));
                }
            }
            // Paint background unfocused
            else if (LayoutManager.Focused == false && ((item.State & ShengImageListViewItemState.Selected) == ShengImageListViewItemState.Selected))
            {
                using (Brush bGray64 = new LinearGradientBrush(backgroundRect, Theme.UnFocusedColorStart, Theme.UnFocusedColorEnd, LinearGradientMode.Vertical))
                {
                    // ImageListViewUtility.FillRoundedRectangle(g, bGray64, bounds, 4);
                    g.FillPath(bGray64, DrawingTool.RoundedRect(backgroundRect, _radius));
                }
            }

            // Paint background Hovered
            //如果正处于框选状态,不绘制Hover状态,减小闪烁
            if (LayoutManager.MouseSelecting == false && (item.State & ShengImageListViewItemState.Hovered) == ShengImageListViewItemState.Hovered)
            {
                using (Brush bHovered = new LinearGradientBrush(backgroundRect, Theme.HoverColorStart, Theme.HoverColorEnd, LinearGradientMode.Vertical))
                {
                    //  ImageListViewUtility.FillRoundedRectangle(g, bHovered, bounds, 4);
                    g.FillPath(bHovered, DrawingTool.RoundedRect(backgroundRect, _radius));
                }
            }

            DrawItemBorder(g, bounds);

            // Focus rectangle
            if (LayoutManager.Focused && ((item.State & ShengImageListViewItemState.Focused) == ShengImageListViewItemState.Focused))
            {
                ControlPaint.DrawFocusRectangle(g, bounds);
            }

            DrawItemContent(g, bounds, item);
        }
        protected override void OnMouseDoubleClick(MouseEventArgs e)
        {
            if (ItemDoubleClick != null)
            {
                ShengImageListViewHitInfo hitInfo = _layoutManager.HitTest(e.Location);
                if (hitInfo.ItemHit)
                {
                    ShengImageListViewItem item = Items[hitInfo.ItemIndex];
                    OnItemDoubleClick(item);
                }
            }

            base.OnMouseDoubleClick(e);
        }
Example #8
0
        public void RenderItem(Graphics g, ShengImageListViewItem item)
        {
            if (LayoutManager.Suspend)
            {
                return;
            }

            if (LayoutManager.IsItemVisible(item) == ShengImageListViewItemVisibility.NotVisible)
            {
                return;
            }

            DrawItem(g, item);
        }
Example #9
0
        public Image GetThumbnail(ShengImageListViewItem item)
        {
            Debug.Assert(item != null, "ImageListViewItem 为 null");

            if (item == null)
            {
                throw new ArgumentNullException();
            }

            if (Container(item) == false)
            {
                throw new ArgumentOutOfRangeException();
            }

            return(_thumbnails[item]);
        }
Example #10
0
        public void AddThumbnail(ShengImageListViewItem item, Image thumbnail)
        {
            Debug.Assert(item != null, "ImageListViewItem 为 null");
            Debug.Assert(thumbnail != null, "thumbnail 为 null");

            if (item == null || thumbnail == null)
            {
                return;
            }

            if (Container(item))
            {
                Debug.Assert(false, "已经缓存过了指定 ImageListViewItem 的缩略图");
                return;
            }

            _thumbnails.Add(item, thumbnail);
        }
Example #11
0
        /// <summary>
        /// 移除指定项的缓存缩略图,并dispose
        /// </summary>
        /// <param name="item"></param>
        public void RemoveThumbnail(ShengImageListViewItem item)
        {
            Debug.Assert(item != null, "ImageListViewItem 为 null");

            if (item == null)
            {
                return;
            }

            if (Container(item) == false)
            {
                Debug.Assert(false, "不存在指定 ImageListViewItem 的缓存缩略图");
                return;
            }

            _thumbnails[item].Dispose();
            _thumbnails.Remove(item);
        }
        internal override void DrawItemContent(Graphics g, Rectangle bounds, ShengImageListViewItem item)
        {
            if (_thumbnailSizeInited == false)
            {
                SizeF headerSize = g.MeasureString(item.Header, Theme.ItemHeaderFont);
                _headerHeight = (int)Math.Ceiling(headerSize.Height);

                int width = LayoutManager.ItemSize.Width - _itemPadding.Width * 2;
                //_itemPadding.Height * 3 是因为去掉缩略图顶部,底部,和与文本区域之间的Padding
                int height = LayoutManager.ItemSize.Height - _itemPadding.Height * 3 - _headerHeight;
                _thumbnailSize = new Size(width, height);

                _thumbnailSizeInited = true;
            }

            #region  绘制缩略图

            Image img = null;
            if (_thumbnailsCache.Container(item))
            {
                img = _thumbnailsCache.GetThumbnail(item);
            }
            else
            {
                img = DrawingTool.GetScaleImage(item.Image, _thumbnailSize);
                _thumbnailsCache.AddThumbnail(item, img);
            }

            if (img != null)
            {
                Rectangle pos = DrawingTool.GetSizedImageBounds(img, new Rectangle(bounds.Location + _itemPadding, _thumbnailSize));
                g.DrawImage(img, pos);
                // Draw image border
                if (Math.Min(pos.Width, pos.Height) > 32)
                {
                    using (Pen pOuterBorder = new Pen(Theme.ImageOuterBorderColor))
                    {
                        g.DrawRectangle(pOuterBorder, pos);
                    }
                    if (System.Math.Min(_thumbnailSize.Width, _thumbnailSize.Height) > 32)
                    {
                        using (Pen pInnerBorder = new Pen(Theme.ImageInnerBorderColor))
                        {
                            //Rectangle.Inflate(pos, -1, -1) 用于取内框
                            g.DrawRectangle(pInnerBorder, Rectangle.Inflate(pos, -1, -1));
                        }
                    }
                }
            }

            #endregion

            #region 绘制文本

            _headerBounds        = new Rectangle();
            _headerBounds.X      = _itemPadding.Width;
            _headerBounds.Y      = LayoutManager.ItemSize.Height - _headerHeight - _itemPadding.Height;
            _headerBounds.Width  = _thumbnailSize.Width;
            _headerBounds.Height = _headerHeight;
            _headerBounds.Offset(bounds.Location);

            //g.DrawRectangle(Pens.Gray, _headerBounds);
            using (SolidBrush brush = new SolidBrush(Theme.ItemHeaderColor))
            {
                g.DrawString(item.Header, Theme.ItemHeaderFont, brush, _headerBounds, _itemHeaderStringFormat);
            }

            #endregion
        }
        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;
            }

            ShengImageListViewItem 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 <ShengImageListViewItem> 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();
        }
        public void MouseDown(MouseEventArgs e)
        {
            /*
             * 如果按下的是鼠标右键
             * 如果按在已选定的项上,只切换焦点,不改变选择
             * 如果按在未选定项上,则切换为点的项为选中项
             * 不考虑键盘按键
             */

            _mouseItemsAreaOffset = _itemsAreaOffset;

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

            ShengImageListViewHitInfo hitInfo = HitTest(e.Location);

            if (hitInfo.ItemHit)
            {
                ShengImageListViewItem        item               = _imageListView.Items[hitInfo.ItemIndex];
                List <ShengImageListViewItem> allItems           = _imageListView.Items.ToList();
                ShengImageListViewItem        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++)
                                {
                                    ShengImageListViewItem 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 <ShengImageListViewItem> newSelectedItems = _imageListView.GetSelectedItems();
            if (oldSelectedItems.SequenceEqual(newSelectedItems) == false)
            {
                _imageListView.NeedPaint();
                _imageListView.OnSelectedItemChanged();
            }
        }
        /// <summary>
        /// 获取项的呈现区域
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public Rectangle GetItemBounds(ShengImageListViewItem item)
        {
            int index = _imageListView.Items.IndexOf(item);

            return(GetItemBounds(index));
        }
 public ShengImageListViewItemDoubleClickEventArgs(ShengImageListViewItem item)
 {
     Item = item;
 }
        public void RenderItem(Graphics graphics, ShengImageListViewItem item)
        {
            Debug.Assert(MouseSelecting == false, "MouseSelecting 为 " + MouseSelecting.ToString());

            _renderer.RenderItem(graphics, item);
        }