예제 #1
0
        public static void AddNewImgCol(this DataGridView dgv, string headerText, DataGridViewImageCellLayout imgLayout = DataGridViewImageCellLayout.Zoom)
        {
            DataGridViewImageColumn img = GetDGVImgCol(headerText, imgLayout);

            dgv.Columns.Add(img);
            dgv.CellFormatting += EventUtil.DataGridView_CellFormatting;
        }
예제 #2
0
        private static DataGridViewImageColumn GetDGVImgCol(string headerText, DataGridViewImageCellLayout imgLayout, string propertyName)
        {
            DataGridViewImageColumn img = GetDGVImgCol(headerText, imgLayout);

            img.DataPropertyName = propertyName;

            return(img);
        }
예제 #3
0
        private static DataGridViewImageColumn GetDGVImgCol(string headerText, DataGridViewImageCellLayout imgLayout)
        {
            DataGridViewImageColumn img = new DataGridViewImageColumn();

            img.HeaderText  = headerText;
            img.ImageLayout = DataGridViewImageCellLayout.Stretch;

            return(img);
        }
예제 #4
0
        /// <summary>
        /// 创建DataGridView扩展的Image列
        /// </summary>
        /// <param name="_dgv">要创建列的DataGridView</param>
        /// <param name="_alignment">设置列的对齐方式</param>
        /// <param name="_columnName">列名</param>
        /// <param name="_headerText">标题名</param>
        /// <param name="_dataPropertyName">绑定数据源的字段名称</param>
        /// <param name="_toolTipText">TipText提示</param>
        /// <param name="_image">图片</param>
        /// <param name="_layout">设置图片布局方式</param>
        /// <param name="_readOnly">设置列是否只读,true 只读,false 读写</param>
        /// <param name="_visible">设置列是否可见,true 显示,false 隐藏</param>
        /// <param name="_notEmpty">设置列是否为必填列,true 必填,false 非必填</param>
        /// <param name="_backColor">设置列的背景色,当_notEmpty为true时,此项为必需值,为false,此项可以为Color.Empty</param>
        /// <param name="_columnState">装载DataGridView可写可读、只读列的数据字典</param>
        public static void InitDgvImageColumn(DataGridView _dgv,
                                              DataGridViewContentAlignment _alignment, string _columnName, string _headerText,
                                              string _dataPropertyName, string _toolTipText,
                                              Image _image, DataGridViewImageCellLayout _layout,
                                              bool _readOnly, bool _visible, bool _notEmpty, Color _backColor, ref Dictionary <string, bool> _columnState)
        {
            DataGridViewImageColumn imageCol = new DataGridViewImageColumn();

            imageCol.HeaderCell.Style.Alignment = _alignment == 0 ? DataGridViewContentAlignment.MiddleLeft : _alignment;
            imageCol.Name             = _columnName;
            imageCol.HeaderText       = _headerText;
            imageCol.DataPropertyName = _dataPropertyName;
            imageCol.ToolTipText      = _toolTipText;
            imageCol.Image            = _image;
            imageCol.ImageLayout      = _layout;
            imageCol.Visible          = _visible;
            imageCol.ReadOnly         = _readOnly;
            if (_notEmpty == true)
            {
                imageCol.DefaultCellStyle.BackColor = _backColor;
            }
            _columnState.Add(_columnName, _readOnly);
            _dgv.Columns.Add(imageCol);
        }
예제 #5
0
        // PaintPrivate is used in three places that need to duplicate the paint code:
        // 1. DataGridViewCell::Paint method
        // 2. DataGridViewCell::GetContentBounds
        // 3. DataGridViewCell::GetErrorIconBounds
        //
        // if computeContentBounds is true then PaintPrivate returns the contentBounds
        // else if computeErrorIconBounds is true then PaintPrivate returns the errorIconBounds
        // else it returns Rectangle.Empty;
        private Rectangle PaintPrivate(Graphics g,
                                       Rectangle clipBounds,
                                       Rectangle cellBounds,
                                       int rowIndex,
                                       DataGridViewElementStates elementState,
                                       object formattedValue,
                                       string errorText,
                                       DataGridViewCellStyle cellStyle,
                                       DataGridViewAdvancedBorderStyle advancedBorderStyle,
                                       DataGridViewPaintParts paintParts,
                                       bool computeContentBounds,
                                       bool computeErrorIconBounds,
                                       bool paint)
        {
            // Parameter checking.
            // One bit and one bit only should be turned on
            Debug.Assert(paint || computeContentBounds || computeErrorIconBounds);
            Debug.Assert(!paint || !computeContentBounds || !computeErrorIconBounds);
            Debug.Assert(!computeContentBounds || !computeErrorIconBounds || !paint);
            Debug.Assert(!computeErrorIconBounds || !paint || !computeContentBounds);
            Debug.Assert(cellStyle != null);

            if (paint && DataGridViewCell.PaintBorder(paintParts))
            {
                PaintBorder(g, clipBounds, cellBounds, cellStyle, advancedBorderStyle);
            }

            Rectangle resultBounds;
            Rectangle valBounds    = cellBounds;
            Rectangle borderWidths = BorderWidths(advancedBorderStyle);

            valBounds.Offset(borderWidths.X, borderWidths.Y);
            valBounds.Width  -= borderWidths.Right;
            valBounds.Height -= borderWidths.Bottom;

            if (valBounds.Width > 0 && valBounds.Height > 0 && (paint || computeContentBounds))
            {
                Rectangle imgBounds = valBounds;
                if (cellStyle.Padding != Padding.Empty)
                {
                    if (DataGridView.RightToLeftInternal)
                    {
                        imgBounds.Offset(cellStyle.Padding.Right, cellStyle.Padding.Top);
                    }
                    else
                    {
                        imgBounds.Offset(cellStyle.Padding.Left, cellStyle.Padding.Top);
                    }
                    imgBounds.Width  -= cellStyle.Padding.Horizontal;
                    imgBounds.Height -= cellStyle.Padding.Vertical;
                }

                bool       cellSelected = (elementState & DataGridViewElementStates.Selected) != 0;
                SolidBrush br           = DataGridView.GetCachedBrush((DataGridViewCell.PaintSelectionBackground(paintParts) && cellSelected) ? cellStyle.SelectionBackColor : cellStyle.BackColor);

                if (imgBounds.Width > 0 && imgBounds.Height > 0)
                {
                    Image img = formattedValue as Image;
                    Icon  ico = null;
                    if (img == null)
                    {
                        ico = formattedValue as Icon;
                    }
                    if (ico != null || img != null)
                    {
                        DataGridViewImageCellLayout imageLayout = ImageLayout;
                        if (imageLayout == DataGridViewImageCellLayout.NotSet)
                        {
                            if (OwningColumn is DataGridViewImageColumn)
                            {
                                imageLayout = ((DataGridViewImageColumn)OwningColumn).ImageLayout;
                                Debug.Assert(imageLayout != DataGridViewImageCellLayout.NotSet);
                            }
                            else
                            {
                                imageLayout = DataGridViewImageCellLayout.Normal;
                            }
                        }

                        if (imageLayout == DataGridViewImageCellLayout.Stretch)
                        {
                            if (paint)
                            {
                                if (DataGridViewCell.PaintBackground(paintParts))
                                {
                                    DataGridViewCell.PaintPadding(g, valBounds, cellStyle, br, DataGridView.RightToLeftInternal);
                                }
                                if (DataGridViewCell.PaintContentForeground(paintParts))
                                {
                                    if (img != null)
                                    {
                                        //

                                        ImageAttributes attr = new ImageAttributes();

                                        attr.SetWrapMode(WrapMode.TileFlipXY);
                                        g.DrawImage(img, imgBounds, 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, attr);
                                        attr.Dispose();
                                    }
                                    else
                                    {
                                        g.DrawIcon(ico, imgBounds);
                                    }
                                }
                            }

                            resultBounds = imgBounds;
                        }
                        else
                        {
                            Rectangle imgBounds2 = ImgBounds(imgBounds, (img == null) ? ico.Width : img.Width, (img == null) ? ico.Height : img.Height, imageLayout, cellStyle);
                            resultBounds = imgBounds2;

                            if (paint)
                            {
                                if (DataGridViewCell.PaintBackground(paintParts) && br.Color.A == 255)
                                {
                                    g.FillRectangle(br, valBounds);
                                }
                                if (DataGridViewCell.PaintContentForeground(paintParts))
                                {
                                    //paint the image
                                    Region reg = g.Clip;
                                    g.SetClip(Rectangle.Intersect(Rectangle.Intersect(imgBounds2, imgBounds), Rectangle.Truncate(g.VisibleClipBounds)));
                                    if (img != null)
                                    {
                                        g.DrawImage(img, imgBounds2);
                                    }
                                    else
                                    {
                                        g.DrawIconUnstretched(ico, imgBounds2);
                                    }
                                    g.Clip = reg;
                                }
                            }
                        }
                    }
                    else
                    {
                        if (paint && DataGridViewCell.PaintBackground(paintParts) && br.Color.A == 255)
                        {
                            g.FillRectangle(br, valBounds);
                        }
                        resultBounds = Rectangle.Empty;
                    }
                }
                else
                {
                    if (paint && DataGridViewCell.PaintBackground(paintParts) && br.Color.A == 255)
                    {
                        g.FillRectangle(br, valBounds);
                    }
                    resultBounds = Rectangle.Empty;
                }

                Point ptCurrentCell = DataGridView.CurrentCellAddress;
                if (paint &&
                    DataGridViewCell.PaintFocus(paintParts) &&
                    ptCurrentCell.X == ColumnIndex &&
                    ptCurrentCell.Y == rowIndex &&
                    DataGridView.ShowFocusCues &&
                    DataGridView.Focused)
                {
                    // Draw focus rectangle
                    ControlPaint.DrawFocusRectangle(g, valBounds, Color.Empty, br.Color);
                }

                if (DataGridView.ShowCellErrors && paint && DataGridViewCell.PaintErrorIcon(paintParts))
                {
                    PaintErrorIcon(g, cellStyle, rowIndex, cellBounds, valBounds, errorText);
                }
            }
            else if (computeErrorIconBounds)
            {
                if (!string.IsNullOrEmpty(errorText))
                {
                    resultBounds = ComputeErrorIconBounds(valBounds);
                }
                else
                {
                    resultBounds = Rectangle.Empty;
                }
            }
            else
            {
                Debug.Assert(valBounds.Height <= 0 || valBounds.Width <= 0);
                resultBounds = Rectangle.Empty;
            }

            return(resultBounds);
        }
예제 #6
0
        private Rectangle ImgBounds(Rectangle bounds, int imgWidth, int imgHeight, DataGridViewImageCellLayout imageLayout, DataGridViewCellStyle cellStyle)
        {
            // when the imageLayout == stretch there is nothing to do
            Debug.Assert(imageLayout != DataGridViewImageCellLayout.Stretch);

            Rectangle imgBounds = Rectangle.Empty;

            switch (imageLayout)
            {
            case DataGridViewImageCellLayout.Normal:
            case DataGridViewImageCellLayout.NotSet:
                imgBounds = new Rectangle(bounds.X, bounds.Y, imgWidth, imgHeight);
                break;

            case DataGridViewImageCellLayout.Zoom:
                // we have to determine which side will be fully filled: the height or the width
                if (imgWidth * bounds.Height < imgHeight * bounds.Width)
                {
                    // we fill the height
                    imgBounds = new Rectangle(bounds.X, bounds.Y, decimal.ToInt32((decimal)imgWidth * bounds.Height / imgHeight), bounds.Height);
                }
                else
                {
                    // we fill the width
                    imgBounds = new Rectangle(bounds.X, bounds.Y, bounds.Width, decimal.ToInt32((decimal)imgHeight * bounds.Width / imgWidth));
                }
                break;

            default:
                break;
            }

            // now use the alignment on the cellStyle to determine the final bounds
            if (DataGridView.RightToLeftInternal)
            {
                switch (cellStyle.Alignment)
                {
                case DataGridViewContentAlignment.TopRight:
                    imgBounds.X = bounds.X;
                    break;

                case DataGridViewContentAlignment.TopLeft:
                    imgBounds.X = bounds.Right - imgBounds.Width;
                    break;

                case DataGridViewContentAlignment.MiddleRight:
                    imgBounds.X = bounds.X;
                    break;

                case DataGridViewContentAlignment.MiddleLeft:
                    imgBounds.X = bounds.Right - imgBounds.Width;
                    break;

                case DataGridViewContentAlignment.BottomRight:
                    imgBounds.X = bounds.X;
                    break;

                case DataGridViewContentAlignment.BottomLeft:
                    imgBounds.X = bounds.Right - imgBounds.Width;
                    break;
                }
            }
            else
            {
                switch (cellStyle.Alignment)
                {
                case DataGridViewContentAlignment.TopLeft:
                    imgBounds.X = bounds.X;
                    break;

                case DataGridViewContentAlignment.TopRight:
                    imgBounds.X = bounds.Right - imgBounds.Width;
                    break;

                case DataGridViewContentAlignment.MiddleLeft:
                    imgBounds.X = bounds.X;
                    break;

                case DataGridViewContentAlignment.MiddleRight:
                    imgBounds.X = bounds.Right - imgBounds.Width;
                    break;

                case DataGridViewContentAlignment.BottomLeft:
                    imgBounds.X = bounds.X;
                    break;

                case DataGridViewContentAlignment.BottomRight:
                    imgBounds.X = bounds.Right - imgBounds.Width;
                    break;
                }
            }

            switch (cellStyle.Alignment)
            {
            case DataGridViewContentAlignment.TopCenter:
            case DataGridViewContentAlignment.MiddleCenter:
            case DataGridViewContentAlignment.BottomCenter:
                imgBounds.X = bounds.X + (bounds.Width - imgBounds.Width) / 2;
                break;
            }

            switch (cellStyle.Alignment)
            {
            case DataGridViewContentAlignment.TopLeft:
            case DataGridViewContentAlignment.TopCenter:
            case DataGridViewContentAlignment.TopRight:
                imgBounds.Y = bounds.Y;
                break;

            case DataGridViewContentAlignment.MiddleLeft:
            case DataGridViewContentAlignment.MiddleCenter:
            case DataGridViewContentAlignment.MiddleRight:
                imgBounds.Y = bounds.Y + (bounds.Height - imgBounds.Height) / 2;
                break;

            case DataGridViewContentAlignment.BottomLeft:
            case DataGridViewContentAlignment.BottomCenter:
            case DataGridViewContentAlignment.BottomRight:
                imgBounds.Y = bounds.Bottom - imgBounds.Height;
                break;

            default:
                Debug.Assert(cellStyle.Alignment == DataGridViewContentAlignment.NotSet, "this is the only alignment left");
                break;
            }
            return(imgBounds);
        }
        private Rectangle PaintPrivate(Graphics g, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates elementState, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts, bool computeContentBounds, bool computeErrorIconBounds, bool paint)
        {
            Rectangle empty;
            Point     point;

            if (paint && DataGridViewCell.PaintBorder(paintParts))
            {
                this.PaintBorder(g, clipBounds, cellBounds, cellStyle, advancedBorderStyle);
            }
            Rectangle cellValueBounds = cellBounds;
            Rectangle rectangle3      = this.BorderWidths(advancedBorderStyle);

            cellValueBounds.Offset(rectangle3.X, rectangle3.Y);
            cellValueBounds.Width  -= rectangle3.Right;
            cellValueBounds.Height -= rectangle3.Bottom;
            if (((cellValueBounds.Width <= 0) || (cellValueBounds.Height <= 0)) || (!paint && !computeContentBounds))
            {
                if (computeErrorIconBounds)
                {
                    if (!string.IsNullOrEmpty(errorText))
                    {
                        return(base.ComputeErrorIconBounds(cellValueBounds));
                    }
                    return(Rectangle.Empty);
                }
                return(Rectangle.Empty);
            }
            Rectangle destRect = cellValueBounds;

            if (cellStyle.Padding != Padding.Empty)
            {
                if (base.DataGridView.RightToLeftInternal)
                {
                    destRect.Offset(cellStyle.Padding.Right, cellStyle.Padding.Top);
                }
                else
                {
                    destRect.Offset(cellStyle.Padding.Left, cellStyle.Padding.Top);
                }
                destRect.Width  -= cellStyle.Padding.Horizontal;
                destRect.Height -= cellStyle.Padding.Vertical;
            }
            bool       flag        = (elementState & DataGridViewElementStates.Selected) != DataGridViewElementStates.None;
            SolidBrush cachedBrush = base.DataGridView.GetCachedBrush((DataGridViewCell.PaintSelectionBackground(paintParts) && flag) ? cellStyle.SelectionBackColor : cellStyle.BackColor);

            if ((destRect.Width > 0) && (destRect.Height > 0))
            {
                Image image = formattedValue as Image;
                Icon  icon  = null;
                if (image == null)
                {
                    icon = formattedValue as Icon;
                }
                if ((icon != null) || (image != null))
                {
                    DataGridViewImageCellLayout imageLayout = this.ImageLayout;
                    switch (imageLayout)
                    {
                    case DataGridViewImageCellLayout.NotSet:
                        if (base.OwningColumn is DataGridViewImageColumn)
                        {
                            imageLayout = ((DataGridViewImageColumn)base.OwningColumn).ImageLayout;
                        }
                        else
                        {
                            imageLayout = DataGridViewImageCellLayout.Normal;
                        }
                        break;

                    case DataGridViewImageCellLayout.Stretch:
                        if (paint)
                        {
                            if (DataGridViewCell.PaintBackground(paintParts))
                            {
                                DataGridViewCell.PaintPadding(g, cellValueBounds, cellStyle, cachedBrush, base.DataGridView.RightToLeftInternal);
                            }
                            if (DataGridViewCell.PaintContentForeground(paintParts))
                            {
                                if (image != null)
                                {
                                    ImageAttributes imageAttr = new ImageAttributes();
                                    imageAttr.SetWrapMode(WrapMode.TileFlipXY);
                                    g.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttr);
                                    imageAttr.Dispose();
                                }
                                else
                                {
                                    g.DrawIcon(icon, destRect);
                                }
                            }
                        }
                        empty = destRect;
                        goto Label_037E;
                    }
                    Rectangle a = this.ImgBounds(destRect, (image == null) ? icon.Width : image.Width, (image == null) ? icon.Height : image.Height, imageLayout, cellStyle);
                    empty = a;
                    if (paint)
                    {
                        if (DataGridViewCell.PaintBackground(paintParts) && (cachedBrush.Color.A == 0xff))
                        {
                            g.FillRectangle(cachedBrush, cellValueBounds);
                        }
                        if (DataGridViewCell.PaintContentForeground(paintParts))
                        {
                            Region clip = g.Clip;
                            g.SetClip(Rectangle.Intersect(Rectangle.Intersect(a, destRect), Rectangle.Truncate(g.VisibleClipBounds)));
                            if (image != null)
                            {
                                g.DrawImage(image, a);
                            }
                            else
                            {
                                g.DrawIconUnstretched(icon, a);
                            }
                            g.Clip = clip;
                        }
                    }
                }
                else
                {
                    if ((paint && DataGridViewCell.PaintBackground(paintParts)) && (cachedBrush.Color.A == 0xff))
                    {
                        g.FillRectangle(cachedBrush, cellValueBounds);
                    }
                    empty = Rectangle.Empty;
                }
            }
            else
            {
                if ((paint && DataGridViewCell.PaintBackground(paintParts)) && (cachedBrush.Color.A == 0xff))
                {
                    g.FillRectangle(cachedBrush, cellValueBounds);
                }
                empty = Rectangle.Empty;
            }
Label_037E:
            point = base.DataGridView.CurrentCellAddress;
            if (((paint && DataGridViewCell.PaintFocus(paintParts)) && ((point.X == base.ColumnIndex) && (point.Y == rowIndex))) && (base.DataGridView.ShowFocusCues && base.DataGridView.Focused))
            {
                ControlPaint.DrawFocusRectangle(g, cellValueBounds, Color.Empty, cachedBrush.Color);
            }
            if ((base.DataGridView.ShowCellErrors && paint) && DataGridViewCell.PaintErrorIcon(paintParts))
            {
                base.PaintErrorIcon(g, cellStyle, rowIndex, cellBounds, cellValueBounds, errorText);
            }
            return(empty);
        }
        private Rectangle ImgBounds(Rectangle bounds, int imgWidth, int imgHeight, DataGridViewImageCellLayout imageLayout, DataGridViewCellStyle cellStyle)
        {
            Rectangle empty = Rectangle.Empty;

            switch (imageLayout)
            {
            case DataGridViewImageCellLayout.NotSet:
            case DataGridViewImageCellLayout.Normal:
                empty = new Rectangle(bounds.X, bounds.Y, imgWidth, imgHeight);
                break;

            case DataGridViewImageCellLayout.Zoom:
                if ((imgWidth * bounds.Height) >= (imgHeight * bounds.Width))
                {
                    empty = new Rectangle(bounds.X, bounds.Y, bounds.Width, decimal.ToInt32((imgHeight * bounds.Width) / imgWidth));
                    break;
                }
                empty = new Rectangle(bounds.X, bounds.Y, decimal.ToInt32((imgWidth * bounds.Height) / imgHeight), bounds.Height);
                break;
            }
            if (base.DataGridView.RightToLeftInternal)
            {
                switch (cellStyle.Alignment)
                {
                case DataGridViewContentAlignment.MiddleRight:
                    empty.X = bounds.X;
                    break;

                case DataGridViewContentAlignment.BottomLeft:
                    empty.X = bounds.Right - empty.Width;
                    break;

                case DataGridViewContentAlignment.BottomRight:
                    empty.X = bounds.X;
                    break;

                case DataGridViewContentAlignment.TopLeft:
                    empty.X = bounds.Right - empty.Width;
                    break;

                case DataGridViewContentAlignment.TopRight:
                    empty.X = bounds.X;
                    break;

                case DataGridViewContentAlignment.MiddleLeft:
                    empty.X = bounds.Right - empty.Width;
                    break;
                }
            }
            else
            {
                switch (cellStyle.Alignment)
                {
                case DataGridViewContentAlignment.MiddleRight:
                    empty.X = bounds.Right - empty.Width;
                    break;

                case DataGridViewContentAlignment.BottomLeft:
                    empty.X = bounds.X;
                    break;

                case DataGridViewContentAlignment.BottomRight:
                    empty.X = bounds.Right - empty.Width;
                    break;

                case DataGridViewContentAlignment.TopLeft:
                    empty.X = bounds.X;
                    break;

                case DataGridViewContentAlignment.TopRight:
                    empty.X = bounds.Right - empty.Width;
                    break;

                case DataGridViewContentAlignment.MiddleLeft:
                    empty.X = bounds.X;
                    break;
                }
            }
            switch (cellStyle.Alignment)
            {
            case DataGridViewContentAlignment.TopCenter:
            case DataGridViewContentAlignment.MiddleCenter:
            case DataGridViewContentAlignment.BottomCenter:
                empty.X = bounds.X + ((bounds.Width - empty.Width) / 2);
                break;
            }
            DataGridViewContentAlignment alignment = cellStyle.Alignment;

            if (alignment <= DataGridViewContentAlignment.MiddleCenter)
            {
                switch (alignment)
                {
                case DataGridViewContentAlignment.TopLeft:
                case DataGridViewContentAlignment.TopCenter:
                case DataGridViewContentAlignment.TopRight:
                    empty.Y = bounds.Y;
                    return(empty);

                case (DataGridViewContentAlignment.TopCenter | DataGridViewContentAlignment.TopLeft):
                    return(empty);

                case DataGridViewContentAlignment.MiddleLeft:
                case DataGridViewContentAlignment.MiddleCenter:
                    goto Label_030C;
                }
                return(empty);
            }
            if (alignment <= DataGridViewContentAlignment.BottomLeft)
            {
                switch (alignment)
                {
                case DataGridViewContentAlignment.MiddleRight:
                    goto Label_030C;

                case DataGridViewContentAlignment.BottomLeft:
                    goto Label_032E;
                }
                return(empty);
            }
            switch (alignment)
            {
            case DataGridViewContentAlignment.BottomCenter:
            case DataGridViewContentAlignment.BottomRight:
                goto Label_032E;

            default:
                return(empty);
            }
Label_030C:
            empty.Y = bounds.Y + ((bounds.Height - empty.Height) / 2);
            return(empty);

Label_032E:
            empty.Y = bounds.Bottom - empty.Height;
            return(empty);
        }
예제 #9
0
        public static void AddNewImgBlobCol(this DataGridView dgv, string headerText, string propertyName, DataGridViewImageCellLayout imgLayout = DataGridViewImageCellLayout.Zoom)
        {
            DataGridViewImageColumn img = GetDGVImgCol(headerText, imgLayout, propertyName);

            dgv.Columns.Add(img);
        }
		public DataGridViewImageCell (bool valueIsIcon) {
			this.valueIsIcon = valueIsIcon;
			this.imageLayout = DataGridViewImageCellLayout.NotSet;
		}
예제 #11
0
        private Rectangle ImgBounds(Rectangle bounds, int imgWidth, int imgHeight, DataGridViewImageCellLayout imageLayout, DataGridViewCellStyle cellStyle)
        {
            // when the imageLayout == stretch there is nothing to do
            Debug.Assert(imageLayout != DataGridViewImageCellLayout.Stretch);

            Rectangle imgBounds = Rectangle.Empty;

            switch (imageLayout)
            {
                case DataGridViewImageCellLayout.Normal:
                case DataGridViewImageCellLayout.NotSet:
                    imgBounds = new Rectangle(bounds.X, bounds.Y, imgWidth, imgHeight);
                    break;
                case DataGridViewImageCellLayout.Zoom:
                    // we have to determine which side will be fully filled: the height or the width
                    if (imgWidth * bounds.Height < imgHeight * bounds.Width)
                    {
                        // we fill the height
                        imgBounds = new Rectangle(bounds.X, bounds.Y, Decimal.ToInt32((decimal)imgWidth * bounds.Height / imgHeight), bounds.Height);
                    }
                    else
                    {
                        // we fill the width
                        imgBounds = new Rectangle(bounds.X, bounds.Y, bounds.Width, Decimal.ToInt32((decimal)imgHeight * bounds.Width / imgWidth));
                    }
                    break;
                default:
                    break;
            }

            // now use the alignment on the cellStyle to determine the final bounds
            if (this.DataGridView.RightToLeftInternal)
            {
                switch (cellStyle.Alignment)
                {
                    case DataGridViewContentAlignment.TopRight:
                        imgBounds.X = bounds.X;
                        break;
                    case DataGridViewContentAlignment.TopLeft:
                        imgBounds.X = bounds.Right - imgBounds.Width;
                        break;
                    case DataGridViewContentAlignment.MiddleRight:
                        imgBounds.X = bounds.X;
                        break;
                    case DataGridViewContentAlignment.MiddleLeft:
                        imgBounds.X = bounds.Right - imgBounds.Width;
                        break;
                    case DataGridViewContentAlignment.BottomRight:
                        imgBounds.X = bounds.X;
                        break;
                    case DataGridViewContentAlignment.BottomLeft:
                        imgBounds.X = bounds.Right - imgBounds.Width;
                        break;
                }
            }
            else
            {
                switch (cellStyle.Alignment)
                {
                    case DataGridViewContentAlignment.TopLeft:
                        imgBounds.X = bounds.X;
                        break;
                    case DataGridViewContentAlignment.TopRight:
                        imgBounds.X = bounds.Right - imgBounds.Width;
                        break;
                    case DataGridViewContentAlignment.MiddleLeft:
                        imgBounds.X = bounds.X;
                        break;
                    case DataGridViewContentAlignment.MiddleRight:
                        imgBounds.X = bounds.Right - imgBounds.Width;
                        break;
                    case DataGridViewContentAlignment.BottomLeft:
                        imgBounds.X = bounds.X;
                        break;
                    case DataGridViewContentAlignment.BottomRight:
                        imgBounds.X = bounds.Right - imgBounds.Width;
                        break;
                }
            }

            switch (cellStyle.Alignment)
            {
                case DataGridViewContentAlignment.TopCenter:
                case DataGridViewContentAlignment.MiddleCenter:
                case DataGridViewContentAlignment.BottomCenter:
                    imgBounds.X = bounds.X + (bounds.Width - imgBounds.Width) / 2;
                    break;
            }

            switch (cellStyle.Alignment)
            {
                case DataGridViewContentAlignment.TopLeft:
                case DataGridViewContentAlignment.TopCenter:
                case DataGridViewContentAlignment.TopRight:
                    imgBounds.Y = bounds.Y;
                    break;

                case DataGridViewContentAlignment.MiddleLeft:
                case DataGridViewContentAlignment.MiddleCenter:
                case DataGridViewContentAlignment.MiddleRight:
                    imgBounds.Y = bounds.Y + (bounds.Height - imgBounds.Height) / 2;
                    break;

                case DataGridViewContentAlignment.BottomLeft:
                case DataGridViewContentAlignment.BottomCenter:
                case DataGridViewContentAlignment.BottomRight:
                    imgBounds.Y = bounds.Bottom - imgBounds.Height;
                    break;

                default:
                    Debug.Assert(cellStyle.Alignment == DataGridViewContentAlignment.NotSet, "this is the only alignment left");
                    break;
            }
            return imgBounds;
        }
예제 #12
0
 public DataGridViewImageCell(bool valueIsIcon)
 {
     this.valueIsIcon = valueIsIcon;
     this.imageLayout = DataGridViewImageCellLayout.NotSet;
 }
        private Rectangle ImgBounds(Rectangle bounds, int imgWidth, int imgHeight, DataGridViewImageCellLayout imageLayout, DataGridViewCellStyle cellStyle)
        {
            Rectangle empty = Rectangle.Empty;
            switch (imageLayout)
            {
                case DataGridViewImageCellLayout.NotSet:
                case DataGridViewImageCellLayout.Normal:
                    empty = new Rectangle(bounds.X, bounds.Y, imgWidth, imgHeight);
                    break;

                case DataGridViewImageCellLayout.Zoom:
                    if ((imgWidth * bounds.Height) >= (imgHeight * bounds.Width))
                    {
                        empty = new Rectangle(bounds.X, bounds.Y, bounds.Width, decimal.ToInt32((imgHeight * bounds.Width) / imgWidth));
                        break;
                    }
                    empty = new Rectangle(bounds.X, bounds.Y, decimal.ToInt32((imgWidth * bounds.Height) / imgHeight), bounds.Height);
                    break;
            }
            if (base.DataGridView.RightToLeftInternal)
            {
                switch (cellStyle.Alignment)
                {
                    case DataGridViewContentAlignment.MiddleRight:
                        empty.X = bounds.X;
                        break;

                    case DataGridViewContentAlignment.BottomLeft:
                        empty.X = bounds.Right - empty.Width;
                        break;

                    case DataGridViewContentAlignment.BottomRight:
                        empty.X = bounds.X;
                        break;

                    case DataGridViewContentAlignment.TopLeft:
                        empty.X = bounds.Right - empty.Width;
                        break;

                    case DataGridViewContentAlignment.TopRight:
                        empty.X = bounds.X;
                        break;

                    case DataGridViewContentAlignment.MiddleLeft:
                        empty.X = bounds.Right - empty.Width;
                        break;
                }
            }
            else
            {
                switch (cellStyle.Alignment)
                {
                    case DataGridViewContentAlignment.MiddleRight:
                        empty.X = bounds.Right - empty.Width;
                        break;

                    case DataGridViewContentAlignment.BottomLeft:
                        empty.X = bounds.X;
                        break;

                    case DataGridViewContentAlignment.BottomRight:
                        empty.X = bounds.Right - empty.Width;
                        break;

                    case DataGridViewContentAlignment.TopLeft:
                        empty.X = bounds.X;
                        break;

                    case DataGridViewContentAlignment.TopRight:
                        empty.X = bounds.Right - empty.Width;
                        break;

                    case DataGridViewContentAlignment.MiddleLeft:
                        empty.X = bounds.X;
                        break;
                }
            }
            switch (cellStyle.Alignment)
            {
                case DataGridViewContentAlignment.TopCenter:
                case DataGridViewContentAlignment.MiddleCenter:
                case DataGridViewContentAlignment.BottomCenter:
                    empty.X = bounds.X + ((bounds.Width - empty.Width) / 2);
                    break;
            }
            DataGridViewContentAlignment alignment = cellStyle.Alignment;
            if (alignment <= DataGridViewContentAlignment.MiddleCenter)
            {
                switch (alignment)
                {
                    case DataGridViewContentAlignment.TopLeft:
                    case DataGridViewContentAlignment.TopCenter:
                    case DataGridViewContentAlignment.TopRight:
                        empty.Y = bounds.Y;
                        return empty;

                    case (DataGridViewContentAlignment.TopCenter | DataGridViewContentAlignment.TopLeft):
                        return empty;

                    case DataGridViewContentAlignment.MiddleLeft:
                    case DataGridViewContentAlignment.MiddleCenter:
                        goto Label_030C;
                }
                return empty;
            }
            if (alignment <= DataGridViewContentAlignment.BottomLeft)
            {
                switch (alignment)
                {
                    case DataGridViewContentAlignment.MiddleRight:
                        goto Label_030C;

                    case DataGridViewContentAlignment.BottomLeft:
                        goto Label_032E;
                }
                return empty;
            }
            switch (alignment)
            {
                case DataGridViewContentAlignment.BottomCenter:
                case DataGridViewContentAlignment.BottomRight:
                    goto Label_032E;

                default:
                    return empty;
            }
        Label_030C:
            empty.Y = bounds.Y + ((bounds.Height - empty.Height) / 2);
            return empty;
        Label_032E:
            empty.Y = bounds.Bottom - empty.Height;
            return empty;
        }