.Net Framework Standard/Component

Devexpress GridContro/ TreeList Indicator Header에 버튼 추가

달빛에취하다 2024. 7. 3. 17:09

Devexpress의 Winform 의 GridControl (GridView, BandedGridView) 및 TreeList 의 Indicator Header에 버튼을 추가 하는 코드 

/*
// 사용법
// 이미지 및 ToolTipContainer 를 인스턴스의 속성으로 노출

IndicatorHeaderButton indicatorHeaderButton = new IndicatorHeaderButton(this.gc_Input);
indicatorHeaderButton.IndicatorButtonClick += (ss, ee) =>
{
    //  버튼 클릭
};

*/

public sealed class IndicatorHeaderButton
{

    private static readonly string AddButtonImage = @"iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAADUElEQVR4Xm2TbWiVZRjHf/f9POec7bx0tnM82zk7W29u0YQKRmgmOZDm3Gzm5japwLURhdqQYIFILyvLvhRaUdQHoaSZZEHGJmmrhGhKoChmSvswX7apbZ7pNj07O89zX50d6FsX/Ph/+l3/68ul2t5K8t+ICJk7ZontUS8CtQgPiREcR845GTl+85qz98TB1FkgU99dImIEFha8158AUBteL+tpfzue7v6sSnYPrJXvTm7Ns7u/UTZ/UinrtscytR3Rd4EwYNVtjpG/YCbl+IJF9qGiiK5fs6KJROQ+/m/GUyP8MPg9o8OZ46f6p9tuXc9eU4BqeaPsw1hcdbeu6sJnF4KCQ0e/4cr4ZUSE8vjdNDdsBIH0/Az7B/YyfNL5euhAaotavyPxiD/IqTW1K3V5rBoApeDTfR+xq3OEYMDD1j1Juru2ASACY5MXOPDtoPl7KL3OVhYvBMKOjobLybpzKKVQAMZgcmgl+XTNPCICAtFwkkVx0dfjnk4bUfV+v4NrMhw5dpjxa1cRATGCm2Milca4hj2ff4wxQjJRRlNjHeFF4CvUy21E7vEVKiZnL+XlXV2X8o3GFdyFdFx6Nw3jGpcH7o+ycUeU1OxlCgMWynIithiUt8DL2NTZvBgKeFAq34zrGCrKQiitsHLoHK7rMnbzT3xeG8TRtuvKmC2+e41kCIUtXno/BijEQG/HcF5+9rUoxoDJycUlXoR5lFtANpOe0k7G/JKe9mBZmuqaELUNJTQ2V5F1HFzX5JuzTpbW56tZ+1ySZasWYXsspic06RnntFVaWTCJrTsXVwUVGDy2Bsvh/OkbbGnbidaavsPvsCS3XGkA8Hvv4o9jUzJy5s5O/XvfjVP/XJnrmxi1KQ5GsL02lmVRmgiw/tUgTa/4SVT4sWyNx/JQHIhw5YLh4vnbg6Pn5n62gfkzR271CLI4PRt7vGZpHNEZVj/tR3BQCmy8aO1FOT6Gfr3Kbz+N/zU8dLsHmKBhWymABUSWtRZ/sGF7xXzv/hr54viT8uPFZjl6sUW+PFEnb371qDz1ctKpXhncB1QCvsfaiyD3lqzeGgPQQKhqeaBuaUvRwRXPRC7VdkTkiU0RWdZaNPpwfWig7MGCdqAUsBfkBf4F0YF4pAYVQAAAAAAASUVORK5CYII=";

    private int _Size = 24;

    public int Size
    {
        get { return _Size; }
        set { _Size = value; }
    }

    Rectangle _ButtonRectangle = default(Rectangle);

    DevExpress.XtraEditors.SimpleButton Button { get; set; }

    Rectangle ButtonRectangle
    {
        get { return _ButtonRectangle; }
        set
        {
            _ButtonRectangle = value;

            if (_ButtonRectangle != null && Button != null)
            {
                Button.Location = new Point(_ButtonRectangle.X, _ButtonRectangle.Y);
                Button.Size = new Size(_ButtonRectangle.Width, _ButtonRectangle.Height);
                Button.Visible = true;
                Button.BringToFront();
                //Button.Update();
            }
            else
            {
                Button.Visible = false;
                Button.BringToFront();
            }
        }
    }

    private IndicatorHeaderButton()
    {
        this.Button = CreateButton();
    }

    public IndicatorHeaderButton(DevExpress.XtraGrid.GridControl gridControl) : this()
    {
        bool isBandedGridView = false;
        if (gridControl.MainView is DevExpress.XtraGrid.Views.BandedGrid.BandedGridView bgv)
        {
            isBandedGridView = bgv.OptionsView.ShowColumnHeaders == false;
        }

        if (gridControl.MainView is DevExpress.XtraGrid.Views.Grid.GridView gv)
        {

            DevExpress.XtraGrid.Skins.GridSkinElementsPainter _Painter = new DevExpress.XtraGrid.Skins.GridSkinElementsPainter(gv);
            this.Button.Appearance.BackColor = _Painter.Column.DefaultAppearance.BackColor;
            this.Button.Appearance.ForeColor = _Painter.Column.DefaultAppearance.ForeColor;

            gridControl.Controls.Add(this.Button);

            gv.CustomDrawRowIndicator += (ss, ee) =>
            {
                if (gv.IndicatorWidth < (this.Size + 2))
                {
                    gv.IndicatorWidth = (this.Size + 2);
                }

                if (ee.RowHandle >= 0)
                    return;

                if (gv.IsFilterRow(ee.RowHandle))
                    return;

                if (isBandedGridView || ee.Info.Kind == DevExpress.Utils.Drawing.IndicatorKind.Header)
                {
                    var rec = new Rectangle(ee.Bounds.Left + (ee.Bounds.Width - Size) / 2, ee.Bounds.Top + (ee.Bounds.Height - Size) / 2, Size, Size);

                    if (ButtonRectangle != rec)
                    {
                        ButtonRectangle = rec;
                    }

                    return;
                }
            };
        }
    }

    public IndicatorHeaderButton(DevExpress.XtraTreeList.TreeList treeList) : this()
    {
        treeList.Controls.Add(this.Button);

        this.Button.Appearance.BackColor = treeList.Appearance.HeaderPanel.BackColor;
        this.Button.Appearance.ForeColor = treeList.Appearance.HeaderPanel.ForeColor;

        if (treeList.OptionsView.ShowColumns)
        {
            treeList.CustomDrawColumnHeader += (ss, ee) =>
            { 

                if (ee.Info.Column == null && ee.Info.HeaderPosition != DevExpress.Utils.Drawing.HeaderPositionKind.Right)
                {
                    var rec = new Rectangle(ee.Bounds.Left + (ee.Bounds.Width - Size) / 2, ee.Bounds.Top + (ee.Bounds.Height - Size) / 2, Size, Size);
                    if (ButtonRectangle != rec)
                    {
                        ButtonRectangle = rec;
                    }
                }
            };
        }
        else
        {
            treeList.CustomDrawBandHeader += (ss, ee) =>
            {
                if (ee.Info.Band == null && ee.Info.HeaderPosition != DevExpress.Utils.Drawing.HeaderPositionKind.Right)
                {
                    var rec = new Rectangle(ee.Bounds.Left + (ee.Bounds.Width - Size) / 2, ee.Bounds.Top + (ee.Bounds.Height - Size) / 2, Size, Size);
                    if (ButtonRectangle != rec)
                    {
                        ButtonRectangle = rec;
                    }
                }
            };
        }

        return;
        treeList.CustomDrawNodeIndicator += (ss, ee) =>
        {
            if (treeList.IndicatorWidth < (this.Size + 2))
            {
                treeList.IndicatorWidth = (this.Size + 2);
            }


            if ((ee.Node?.Id ?? 0) > -1)
                return;

            if (ee.Info.Kind == DevExpress.Utils.Drawing.IndicatorKind.Header)
            {
                var rec = new Rectangle(ee.Bounds.Left + (ee.Bounds.Width - Size) / 2, ee.Bounds.Top + (ee.Bounds.Height - Size) / 2, Size, Size);
                if (ButtonRectangle != rec)
                {
                    ButtonRectangle = rec;
                }

                return;
            }
        };
    }

    private DevExpress.XtraEditors.SimpleButton CreateButton()
    {
        DevExpress.XtraEditors.SimpleButton simpleButton = new DevExpress.XtraEditors.SimpleButton();
        simpleButton.Text = "";

        simpleButton.Size = new Size(120, 40);
        simpleButton.PaintStyle = DevExpress.XtraEditors.Controls.PaintStyles.Default;
        simpleButton.ImageOptions.Image = GetDefaultAddImage();
        //simpleButton.LookAndFeel.SetDefaultStyle();
        simpleButton.Visible = false;
        simpleButton.LookAndFeel.UseDefaultLookAndFeel = false;
        simpleButton.LookAndFeel.Style = DevExpress.LookAndFeel.LookAndFeelStyle.Flat;
        simpleButton.TabStop = false;

        return simpleButton;
    }

    private Bitmap GetDefaultAddImage()
    {
        byte[] fileBytes = Convert.FromBase64String(AddButtonImage);

        using (var ms = new System.IO.MemoryStream(fileBytes))
        {
            return new Bitmap(ms);
        }
    }

    public DevExpress.XtraEditors.SimpleButtonImageOptions ImageOptions
    {
        get
        {
            return this.Button.ImageOptions;
        }
    }


    public event EventHandler IndicatorButtonClick
    {
        add
        {
            this.Button.Click += value;
        }
        remove
        {
            this.Button.Click -= value;
        }
    }

    public DevExpress.Utils.ToolTipController ToolTip
    {
        get
        {
            return this.Button.ToolTipController;
        }
        set
        {
            this.Button.ToolTipController = value;
            this.Button.ShowToolTips = true;
        }
    }
}