Beispiel #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="G"></param>
        /// <param name="Item"></param>
        /// <param name="Selected"></param>
        private void DrawImage(Graphics G, ListViewItem Item, bool Selected)
        {
            ImageList imageList = Item.ImageList;

            Size imageSize = imageList.ImageSize;

            Rectangle imageRect = Item.GetBounds(ItemBoundsPortion.Icon);

            if (imageRect.Width > imageSize.Width)
            {
                imageRect.X    += (imageRect.Width - imageSize.Width) / 2;
                imageRect.Width = imageSize.Width;
            }

            if (imageRect.Height > imageSize.Height)
            {
                imageRect.Y     += (imageRect.Height - imageSize.Height) / 2;
                imageRect.Height = imageSize.Height;
            }

            int imageIndex =
                Item.ImageIndex != -1 ?
                Item.ImageIndex :
                imageList.Images.IndexOfKey(Item.ImageKey);

            if (Selected)
            {
                IntPtr hIcon = ListViewNativeMethods.ImageList_GetIcon(
                    imageList.Handle,
                    imageIndex,
                    (int)ListViewNativeMethods.ImageListDrawFlags.ILD_SELECTED);

                G.DrawIcon(Icon.FromHandle(hIcon), imageRect);

                ListViewNativeMethods.DestroyIcon(hIcon);
            }
            else
            {
                Image image = imageList.Images[imageIndex];

                G.DrawImage(
                    image,
                    imageRect,
                    0,
                    0,
                    image.Width,
                    image.Height,
                    GraphicsUnit.Pixel);
            }
        }
Beispiel #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="Column"></param>
        /// <returns></returns>
        private int ColumnAtIndex(int Column)
        {
            ListViewNativeMethods.HDITEM hd = new ListViewNativeMethods.HDITEM();

            hd.mask = HDI_ORDER;

            for (int i = 0; i < ColumnCount; i++)
            {
                if (ListViewNativeMethods.SendMessage(HeaderWnd, HDM_GETITEMA, Column, ref hd) != IntPtr.Zero)
                {
                    return(hd.iOrder);
                }
            }
            return(0);
        }
Beispiel #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="Msg"></param>
        private void WmNcPaint(ref Message Msg)
        {
            base.WndProc(ref Msg);

            if (base.BorderStyle == BorderStyle.None)
            {
                return;
            }

            IntPtr hDC = ListViewNativeMethods.GetWindowDC(Msg.HWnd);

            if (hDC == IntPtr.Zero)
            {
                throw new Win32Exception();
            }

            try
            {
                Color backColor = BackColor;

                Color borderColor = this._BorderColor;

                Rectangle bounds = new Rectangle(0, 0, Width, Height);

                using (Graphics g = Graphics.FromHdc(hDC))
                {
                    using (Region region = new Region(bounds))
                    {
                        region.Exclude(AbsoluteClientRectangle);
                        using (Brush brush = new SolidBrush(backColor))
                        {
                            g.FillRegion(brush, region);
                        }
                    }
                    ControlPaint.DrawBorder(
                        g,
                        bounds,
                        borderColor,
                        ButtonBorderStyle.Solid);
                }
            }
            finally
            {
                ListViewNativeMethods.ReleaseDC(Msg.HWnd, hDC);
            }
            Msg.Result = IntPtr.Zero;
        }
Beispiel #4
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        private Rectangle HeaderEndRect()
        {
            ListViewNativeMethods.RECT rect = new ListViewNativeMethods.RECT();

            IntPtr headerWnd = HeaderWnd;

            ListViewNativeMethods.SendMessage(headerWnd, HDM_GETITEMRECT, ColumnAtIndex(ColumnCount - 1), ref rect);

            int left = rect.Right;

            ListViewNativeMethods.GetWindowRect(headerWnd, ref rect);

            ListViewNativeMethods.OffsetRect(ref rect, -rect.Left, -rect.Top);

            rect.Left = left;

            return(Rectangle.FromLTRB(rect.Left, rect.Top, rect.Right, rect.Bottom));
        }
Beispiel #5
0
            /// <summary>
            ///
            /// </summary>
            /// <param name="Msg"></param>
            protected override void WndProc(ref Message Msg)
            {
                base.WndProc(ref Msg);

                if (Msg.Msg == 0xF || Msg.Msg == 0x47)
                {
                    IntPtr hdc = ListViewNativeMethods.GetDC(Msg.HWnd);
                    try
                    {
                        using (Graphics g = Graphics.FromHdc(hdc))
                        {
                            Rectangle bounds = this._Owner.HeaderEndRect();

                            Color baseColor = this._Owner.HeadColor;

                            Color borderColor = this._Owner.HeadColor;

                            Color innerBorderColor = Color.FromArgb(150, 255, 255, 255);

                            if (this._Owner.ColumnCount > 0)
                            {
                                bounds.X--;
                                bounds.Width++;
                            }
                            this._Owner.RenderBackgroundInternal(
                                g,
                                bounds,
                                baseColor,
                                borderColor,
                                innerBorderColor,
                                0.45f,
                                true,
                                LinearGradientMode.Vertical);
                        }
                    }
                    finally
                    {
                        ListViewNativeMethods.ReleaseDC(Msg.HWnd, hdc);
                    }
                }
            }
Beispiel #6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="e"></param>
        private void DrawSubItemDetails(DrawListViewSubItemEventArgs e)
        {
            Rectangle          bounds    = e.Bounds;
            ListViewItemStates itemState = e.ItemState;
            Graphics           g         = e.Graphics;
            ListViewItem       item      = e.Item;
            bool bSelected =
                (itemState & ListViewItemStates.Selected) != 0;

            bool bDrawImage = false;
            bool bFistItem  = false;
            int  imageIndex = -1;

            if (e.ColumnIndex == 0)
            {
                bFistItem = true;
                if (item.ImageList != null)
                {
                    if (item.ImageIndex != -1)
                    {
                        imageIndex = item.ImageIndex;
                    }
                    else if (!string.IsNullOrEmpty(item.ImageKey))
                    {
                        imageIndex =
                            item.ImageList.Images.IndexOfKey(item.ImageKey);
                    }

                    if (imageIndex != -1)
                    {
                        bDrawImage = true;
                    }
                }
            }

            Rectangle backRect  = bounds;
            Rectangle imageRect = Rectangle.Empty;

            if (bDrawImage)
            {
                imageRect       = item.GetBounds(ItemBoundsPortion.Icon);
                backRect        = item.GetBounds(ItemBoundsPortion.Label);
                backRect.X     += 2;
                backRect.Width -= 2;
            }

            if (bSelected &&
                (base.FullRowSelect ||
                 (!base.FullRowSelect && bFistItem)))
            {
                backRect.Height--;
                Color baseColor        = this._SelectedColor;
                Color borderColor      = this._SelectedColor;
                Color innerBorderColor = Color.FromArgb(150, 255, 255, 255);

                RenderBackgroundInternal(
                    g,
                    backRect,
                    baseColor,
                    borderColor,
                    innerBorderColor,
                    0.45f,
                    false,
                    LinearGradientMode.Vertical);

                if (!base.FullRowSelect && bFistItem)
                {
                    backRect.Width--;
                    using (Pen pen = new Pen(borderColor))
                    {
                        g.DrawRectangle(pen, backRect);
                    }
                    backRect.Width++;
                }
                else
                {
                    Point[] points = new Point[4];
                    points[0] = new Point(backRect.X, backRect.Y);
                    points[1] = new Point(backRect.Right, backRect.Y);
                    points[2] = new Point(backRect.Right, backRect.Bottom);
                    points[3] = new Point(backRect.X, backRect.Bottom);
                    using (Pen pen = new Pen(borderColor))
                    {
                        if (bFistItem)
                        {
                            g.DrawLine(pen, points[0], points[1]);
                            g.DrawLine(pen, points[0], points[3]);
                            g.DrawLine(pen, points[2], points[3]);
                        }
                        if (e.ColumnIndex == ColumnCount - 1)
                        {
                            points[1].X--;
                            points[2].X--;
                            g.DrawLine(pen, points[0], points[1]);
                            g.DrawLine(pen, points[1], points[2]);
                            g.DrawLine(pen, points[2], points[3]);
                        }
                        else
                        {
                            g.DrawLine(pen, points[0], points[1]);
                            g.DrawLine(pen, points[2], points[3]);
                        }
                    }
                }
            }
            else
            {
                Color backColor = e.ItemIndex % 2 == 0 ?
                                  this._RowBackColor1 : this._RowBackColor2;

                using (SolidBrush brush = new SolidBrush(backColor))
                {
                    g.FillRectangle(brush, backRect);
                }
            }

            TextFormatFlags flags = GetFormatFlags(e.Header.TextAlign);

            if (bDrawImage)
            {
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                if (bSelected)
                {
                    IntPtr hIcon = ListViewNativeMethods.ImageList_GetIcon(
                        item.ImageList.Handle,
                        imageIndex,
                        (int)ListViewNativeMethods.ImageListDrawFlags.ILD_SELECTED);
                    g.DrawIcon(Icon.FromHandle(hIcon), imageRect);
                    ListViewNativeMethods.DestroyIcon(hIcon);
                }
                else
                {
                    Image image = item.ImageList.Images[imageIndex];
                    g.DrawImage(
                        image,
                        imageRect,
                        0,
                        0,
                        image.Width,
                        image.Height,
                        GraphicsUnit.Pixel);
                }

                Rectangle textRect = new Rectangle(
                    imageRect.Right + 3,
                    bounds.Y,
                    bounds.Width - imageRect.Right - 3,
                    bounds.Height);

                TextRenderer.DrawText(
                    g,
                    item.Text,
                    item.Font,
                    textRect,
                    item.ForeColor,
                    flags);
            }
            else
            {
                bounds.X += 3;
                TextRenderer.DrawText(
                    g,
                    e.SubItem.Text,
                    e.SubItem.Font,
                    bounds,
                    e.SubItem.ForeColor,
                    flags);
            }
        }