Krypton-Suite / Extended-Toolkit

A companion toolkit for the standard toolkit.
MIT License
190 stars 32 forks source link

[Feature Request]: Alphablend / Hide Selection and Rownumbers in KryptonDataGridView #345

Closed mikel3361 closed 2 years ago

mikel3361 commented 2 years ago

I inherited KryptonDataGridView and include my helper functionality insde the events. Would be great if this will be included in the future. Have translated my inherited class to c# so you can look deeper into it. References should have cleanup - increased during convert.

image

New properties: bool HideSelection bool AlphaBlendSelection double AlphaBlendFactor (any decimal for 0 to 1 - 1 equals 100% alpha and no transparency, default is 0,25) bool ShowRowHeaderNumbers

Edit: Have to Override OnCellPainting - some Column Types have it own Focus Highlight on CellEnter - 2022-08-10

Code: `using System; using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using System.IO; using System.Linq; using System.Reflection; using System.Runtime.CompilerServices; using System.Security; using System.Text; using System.Threading.Tasks; using Microsoft.VisualBasic; using System.Drawing; using System.Windows.Forms; using System.ComponentModel; using Krypton.Toolkit;

public partial class KryptonDataGridViewEx : KryptonDataGridView { private bool _showRowHeaderNumbers = false; private bool _hideSelection = false; private bool _alphaBlendSelection = false; private double _alphaBlendFactor = "0,25"; private int _RowHeadersWidth = RowHeadersWidth;

[Category("Extended attributes")]
[Description("Show rownumber in rowheader")]
public bool ShowRowHeaderNumbers
{
    get
    {
        return _showRowHeaderNumbers;
    }
    set
    {
        if (_showRowHeaderNumbers != value)
            Invalidate();
        _showRowHeaderNumbers = value;
    }
}

[Category("Extended attributes")]
[Description("Hide Selection")]
public bool HideSelection
{
    get
    {
        return _hideSelection;
    }
    set
    {
        if (_hideSelection != value)
            Invalidate();
        _hideSelection = value;
    }
}

[Category("Extended attributes")]
[Description("Enable alphablend selection")]
public bool AlphaBleandSelection
{
    get
    {
        return _alphaBlendSelection;
    }
    set
    {
        if (_alphaBlendSelection != value)
            Invalidate();
        _alphaBlendSelection = value;
    }
}

[Category("Extended attributes")]
[Description("Factor to alphablend (any decimal between 0 and 1)")]
public double AlphaBlendFactor
{
    get
    {
        return _alphaBlendFactor;
    }
    set
    {
        if (value > 1)
            value = 1;
        if (value < 0)
            value = 0;
        if (_alphaBlendFactor != value)
            Invalidate();
        _alphaBlendFactor = value;
    }
}

protected override void OnCellFormatting(DataGridViewCellFormattingEventArgs e)
{
    base.OnCellFormatting(e);

    // Hide Selection or AlphaBelnding ?
    if (_hideSelection | _alphaBlendSelection & e.RowIndex >= 0 & e.ColumnIndex >= 0)
    {
        // Change cell selection colors to cell colors            
        e.CellStyle.SelectionBackColor = Rows(e.RowIndex).Cells(e.ColumnIndex).InheritedStyle.BackColor;
        e.CellStyle.SelectionForeColor = Rows(e.RowIndex).Cells(e.ColumnIndex).InheritedStyle.ForeColor;
    }
    else
    {
        // Remove cell selection colors
        e.CellStyle.SelectionBackColor = Color.Empty;
        e.CellStyle.SelectionForeColor = Color.Empty;
    }
}

protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
{
    base.OnCellPainting(e);

    // Hide Cell Highlight for special Column Type?
    if (_hideSelection | _alphaBlendSelection & e.RowIndex >= 0 & e.ColumnIndex >= 0)
    {
        // Draw Cell Backgroound
        e.Graphics.FillRectangle(new SolidBrush(e.CellStyle.BackColor), new Rectangle(e.CellBounds.X + 1, e.CellBounds.Y + 1, e.CellBounds.Width - 2, e.CellBounds.Height - 2));
        // Draw Cell Right Gridline
        e.Graphics.DrawLine(new Pen(GridColor), e.CellBounds.X + e.CellBounds.Width - 1, e.CellBounds.Y, e.CellBounds.X + e.CellBounds.Width - 1, e.CellBounds.Y + e.CellBounds.Height - 1);
        // Draw Cell Bottom Gridline
        e.Graphics.DrawLine(new Pen(GridColor), e.CellBounds.X, e.CellBounds.Y + e.CellBounds.Height - 1, e.CellBounds.X + e.CellBounds.Width - 1, e.CellBounds.Y + e.CellBounds.Height - 1);
        e.PaintContent(e.CellBounds);
        e.Paint(e.CellBounds, e.PaintParts & !DataGridViewPaintParts.SelectionBackground);
    }
}

protected override void OnRowPostPaint(DataGridViewRowPostPaintEventArgs e)
{
    base.OnRowPostPaint(e);

    // Display Row Number
    if (ShowRowHeaderNumbers & RowHeadersVisible)
    {
        string title = (e.RowIndex + 1).ToString();
        Brush brush = Brushes.Black;
        var centerFormat = new StringFormat();
        centerFormat.Alignment = StringAlignment.Far;
        centerFormat.LineAlignment = StringAlignment.Near;
        Rectangle headerBounds = new Rectangle(e.RowBounds.Left + 3, e.RowBounds.Top + 3, RowHeadersWidth - 6, e.RowBounds.Height - 6);
        e.Graphics.DrawString(title, DefaultCellStyle.Font, brush, headerBounds, centerFormat);
    }

    // Alphablend selection
    if (_alphaBlendSelection & !_hideSelection)
    {
        foreach (DataGridViewCell cell in Rows(e.RowIndex).Cells)
        {
            SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(System.Convert.ToByte(255 * _alphaBlendFactor), cell.InheritedStyle.SelectionBackColor.R, cell.InheritedStyle.SelectionBackColor.G, cell.InheritedStyle.SelectionBackColor.B));
            if (cell.Selected)
            {
                Rectangle rct = GetCellDisplayRectangle(cell.ColumnIndex, e.RowIndex, true);
                e.Graphics.FillRectangle(semiTransBrush, rct);
            }
        }
    }
}

} `

mikel3361 commented 2 years ago

Instead of changing backcolor as in SemiTransparent Selection i changed to draw a transparent brush over cells...

mikel3361 commented 2 years ago

Why "cell.InheritedStyle.SelectionBackColor" points to my default Palette Office365Blue instead of the current selected Palette. PaletteMode in Forms set to Global, Palette declared in Module with events. Changes are shown in Form, but property returns Palette Color from default. My transparent Box has wrong color, Header is in Style from current Palette. Any help how to get the right propery from inherited Class are welcome.

mikel3361 commented 2 years ago

As Workaround i use now RowHeader.SelecteBackColor and it use the color i want. Added a additional option property for a differnt color if i want change it.