Combining Windows Form Components with Krypton Components

Topics related to the Krypton Navigator.

Moderators: Phil Wright, Chris Porter

Combining Windows Form Components with Krypton Components

Postby nbb007 » Mon May 21, 2007 3:53 pm

I have purchased the toolkit, ribbon, and navigator + source, and I love the way my apps look now. You can build a pretty robust app using only Krypton components and Windows.Fom components that support ManagedRenderMode, but inevitably you have to add a "regular" component, which of course does not look the same as everything else.

Most notably, I use the DataGridView component pretty often to easily display data. When I dropped a DataGridView into my newly built Krypton GUI, it looked terrible however, since it looks nothing like the rest of the controls. Has anybody created a DataGridView component that conforms to the rest of the Krypton components, or might a Krypton DataGridView component be added to the toolkit one day?

In trying to match my GUI's DataGridView to the rest of my GUI, I've come up with the following setting and thought I'd share them in hopes that others will contribute. It doesn't look as good as the other Krypton components, but at least some of the coloring looks the same. The column headers are the biggest issue because the have a flat color background - not the nice color gradient like the other Krypton stuff. Does anybody know how to add this gradient to the Column Headers?

//Get the Global color palette
IPalette _palette = KryptonManager.CurrentGlobalPalette;

//Get Globel Separator Color
this._separatorColor = _palette.GetBorderColor1(PaletteBorderStyle.SeparatorHighProfile, PaletteState.Normal);

//Get Globel Border Color
this._clientControlColor = _palette.GetBorderColor1(PaletteBorderStyle.ControlClient, PaletteState.Normal);

//Get Panel Back Color
this._panelBackColor = _palette.GetBackColor1(PaletteBackStyle.PanelClient, PaletteState.Normal);

//Get Header Color for Column Header Background
this._cellBackColor = _palette.GetBackColor1(PaletteBackStyle.HeaderPrimary, PaletteState.Normal);

//Get Label Text Color for Column Header Text Color
this._cellForeColor = _palette.GetContentLongTextColor1(PaletteContentStyle.LabelTitleControl, PaletteState.Normal);

//Get Label Font for Column Header Text/Font
this._labelFont = _palette.GetContentLongTextFont(PaletteContentStyle.LabelTitleControl, PaletteState.Normal);

//Make sure we don't inherit colors so we can customize...
this.dataGridView_ModelStats.EnableHeadersVisualStyles = false;

//Set Grid Font
this.dataGridView_ModelStats.Font = this._labelFont;

//Set Border Style
this.dataGridView_ModelStats.BorderStyle = BorderStyle.Fixed3D;

//Set DataGridVeiw Control Back Color
this.dataGridView_ModelStats.BackgroundColor = this._panelBackColor;

//Set Header Font
this.dataGridView_ModelStats.ColumnHeadersDefaultCellStyle.Font = this._labelFont;

//Set Header Fore Color and Back Color
this.dataGridView_ModelStats.ColumnHeadersDefaultCellStyle.BackColor = this._cellBackColor;
this.dataGridView_ModelStats.ColumnHeadersDefaultCellStyle.ForeColor = this._cellForeColor;

//Set Header Styling
this.dataGridView_ModelStats.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Raised;

this.dataGridView_ModelStats.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;

//Set Grid Colors
this.dataGridView_ModelStats.GridColor = this._separatorColor;
this.dataGridView_ModelStats.ForeColor = this._clientControlColor;
nbb007
 
Posts: 10
Joined: Sun May 20, 2007 6:15 am

Postby Phil Wright » Mon May 21, 2007 4:17 pm

You will be pleased to hear that the next major release will have a DataGridView that is custom drawn to have a Krypton look and feel. The bad news is you need to wait until the next release. Before you ask, I am not sure how long that will be, probably around the start of August.
Phil Wright
Site Admin
 
Posts: 2720
Joined: Thu Apr 13, 2006 2:55 pm
Location: Melbourne, Australia

Postby nbb007 » Tue May 22, 2007 12:15 am

Excellent! That is really great news - are you going to add any other windows form components with a krypton look and feel?
nbb007
 
Posts: 10
Joined: Sun May 20, 2007 6:15 am

Postby cornelha » Tue May 22, 2007 2:18 am

Until Phil gets the grid out this might help you

Code: Select all
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;
namespace RibbonStyle.Controls
{
    public class KryptonGrid : DataGridView
    {
        private KryptonManager manager;
        private IPalette _palette;
        private PaletteRedirect _paletteRedirect;
        private PaletteBackInheritRedirect _paletteBack;
        private PaletteBorderInheritRedirect _paletteBorder;
        private PaletteContentInheritRedirect _paletteContent;
        private IDisposable _mementoBack;
        public KryptonGrid()
        {
            SetStyle(
      ControlStyles.AllPaintingInWmPaint |
      ControlStyles.OptimizedDoubleBuffer |
      ControlStyles.ResizeRedraw, true);
           // manager = Rendering.Manager;
            _palette = KryptonManager.CurrentGlobalPalette;//manager.GlobalPalette;
            _paletteRedirect = new PaletteRedirect(_palette);
            _paletteBack = new PaletteBackInheritRedirect(_paletteRedirect);
            _paletteBorder = new PaletteBorderInheritRedirect(_paletteRedirect);
            _paletteContent = new PaletteContentInheritRedirect(_paletteRedirect);


        }

        protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
        {
            if (e.RowIndex == -1 && e.ColumnIndex != -1)
            {
                //if (_paletteBack.GetBackDraw(PaletteState.Normal) == InheritBool.True)
                //{
                using (GraphicsPath path = new GraphicsPath())
                {
                    IRenderer renderer = _palette.GetRenderer();
                    path.AddRectangle(e.ClipBounds);

                    using (RenderContext context = new RenderContext(this, e.Graphics, e.ClipBounds, renderer))
                    {
                        if (e.State == DataGridViewElementStates.Selected)
                        {
                            _paletteBack.Style = PaletteBackStyle.ButtonAlternate;
                            _paletteBorder.Style = PaletteBorderStyle.HeaderSecondary;
                        }
                        else
                        {
                            _paletteBack.Style = PaletteBackStyle.HeaderPrimary;
                            _paletteBorder.Style = PaletteBorderStyle.HeaderSecondary;
                        }
                        //_paletteContent.Style = PaletteContentStyle.HeaderPrimary;
                        _mementoBack = renderer.RenderStandardBack.DrawBack(context, e.CellBounds, path, _paletteBack, VisualOrientation.Top, PaletteState.Normal, _mementoBack);
                        renderer.RenderStandardBorder.DrawBorder(context,
                                                       e.CellBounds,
                                                       _paletteBorder,
                                                       VisualOrientation.Top,
                                                       PaletteState.Normal);
                    }


                }

                e.Paint(e.ClipBounds, (DataGridViewPaintParts.All & ~DataGridViewPaintParts.Background & ~DataGridViewPaintParts.ContentBackground));

                e.Handled = true;
                //}
            }
            else if (e.RowIndex >= 0 && e.State == DataGridViewElementStates.Selected)
            {
                using (GraphicsPath path = new GraphicsPath())
                {
                    IRenderer renderer = _palette.GetRenderer();
                    path.AddRectangle(e.ClipBounds);

                    using (RenderContext context = new RenderContext(this, e.Graphics, e.ClipBounds, renderer))
                    {

                        _paletteBack.Style = PaletteBackStyle.ButtonAlternate;
                        _paletteBorder.Style = PaletteBorderStyle.HeaderSecondary;
                        //_paletteContent.Style = PaletteContentStyle.HeaderPrimary;
                        _mementoBack = renderer.RenderStandardBack.DrawBack(context, e.CellBounds, path, _paletteBack, VisualOrientation.Top, PaletteState.Tracking, _mementoBack);
                        renderer.RenderStandardBorder.DrawBorder(context,
                                                       e.CellBounds,
                                                       _paletteBorder,
                                                       VisualOrientation.Top,
                                                       PaletteState.Normal);
                    }


                }

                e.Paint(e.ClipBounds, (DataGridViewPaintParts.All & ~DataGridViewPaintParts.Background & ~DataGridViewPaintParts.ContentBackground));

                e.Handled = true;
            }
            else
                base.OnCellPainting(e);
        }
    }
}
[/code]
cornelha
 
Posts: 48
Joined: Tue Apr 24, 2007 11:38 pm
Location: South Africa

Postby Miketrix » Tue May 22, 2007 4:36 am

Nice!

I will try this code snippet asap.

Cornelha, maybe you could post a simple demo solution (or just the class file) on the new Code Snippet forum ?


Thank you !

Mike
Miketrix
 
Posts: 82
Joined: Wed Dec 13, 2006 5:49 pm
Location: New York

Postby nbb007 » Tue May 22, 2007 4:41 am

Fantastic! Thanks allot for the code - I really appriciate it.
nbb007
 
Posts: 10
Joined: Sun May 20, 2007 6:15 am

Postby cornelha » Tue May 22, 2007 4:06 pm

Miketrix yeah i think i will. I just dont comment my code all that well :P
cornelha
 
Posts: 48
Joined: Tue Apr 24, 2007 11:38 pm
Location: South Africa

Postby carpenter » Wed May 23, 2007 4:27 am

Nice job you forget to render the row header so I just put it on, theres two issues the sort glyph and the focused header this looks more than work for Phil because this is what he focused to do
Code: Select all
 protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
        {
            if (e.RowIndex == -1 && e.ColumnIndex != -1)
            {
                //if (_paletteBack.GetBackDraw(PaletteState.Normal) == InheritBool.True)
                //{
                using (GraphicsPath path = new GraphicsPath())
                {
                    IRenderer renderer = _palette.GetRenderer();
                    path.AddRectangle(e.ClipBounds);

                    using (RenderContext context = new RenderContext(this, e.Graphics, e.ClipBounds, renderer))
                    {
                        if (e.State == DataGridViewElementStates.Selected)
                        {
                            _paletteBack.Style = PaletteBackStyle.ButtonAlternate;
                            _paletteBorder.Style = PaletteBorderStyle.HeaderSecondary;
                        }
                        else
                        {
                            _paletteBack.Style = PaletteBackStyle.HeaderPrimary;
                            _paletteBorder.Style = PaletteBorderStyle.HeaderSecondary;
                        }
                        //_paletteContent.Style = PaletteContentStyle.HeaderPrimary;
                        _mementoBack = renderer.RenderStandardBack.DrawBack(context, e.CellBounds, path, _paletteBack, VisualOrientation.Top, PaletteState.Normal, _mementoBack);
                        renderer.RenderStandardBorder.DrawBorder(context,
                                                       e.CellBounds,
                                                       _paletteBorder,
                                                       VisualOrientation.Top,
                                                       PaletteState.Normal);
                    }


                }

                e.Paint(e.ClipBounds, (DataGridViewPaintParts.All & ~DataGridViewPaintParts.Background & ~DataGridViewPaintParts.ContentBackground));

                e.Handled = true;
                //}
            }
            else if (e.RowIndex >= 0 && e.State == DataGridViewElementStates.Selected)
            {
                using (GraphicsPath path = new GraphicsPath())
                {
                    IRenderer renderer = _palette.GetRenderer();
                    path.AddRectangle(e.ClipBounds);

                    using (RenderContext context = new RenderContext(this, e.Graphics, e.ClipBounds, renderer))
                    {

                        _paletteBack.Style = PaletteBackStyle.ButtonAlternate;
                        _paletteBorder.Style = PaletteBorderStyle.HeaderSecondary;
                        //_paletteContent.Style = PaletteContentStyle.HeaderPrimary;
                        _mementoBack = renderer.RenderStandardBack.DrawBack(context, e.CellBounds, path, _paletteBack, VisualOrientation.Top, PaletteState.Tracking, _mementoBack);
                        renderer.RenderStandardBorder.DrawBorder(context,
                                                       e.CellBounds,
                                                       _paletteBorder,
                                                       VisualOrientation.Top,
                                                       PaletteState.Normal);
                    }


                }
               
                e.Paint(e.ClipBounds, (DataGridViewPaintParts.All & ~DataGridViewPaintParts.Background & ~DataGridViewPaintParts.ContentBackground ));

                e.Handled = true;
            }
            else if(e.ColumnIndex == -1)
            {
                using (GraphicsPath path = new GraphicsPath())
                {
                    IRenderer renderer = _palette.GetRenderer();
                    path.AddRectangle(e.ClipBounds);

                    using (RenderContext context = new RenderContext(this, e.Graphics, e.ClipBounds, renderer))
                    {
                        if (e.State == DataGridViewElementStates.Selected)
                        {
                            _paletteBack.Style = PaletteBackStyle.ButtonAlternate;
                            _paletteBorder.Style = PaletteBorderStyle.HeaderSecondary;
                        }
                        else
                        {
                            _paletteBack.Style = PaletteBackStyle.HeaderPrimary;
                            _paletteBorder.Style = PaletteBorderStyle.HeaderSecondary;
                        }
                        //_paletteContent.Style = PaletteContentStyle.HeaderPrimary;
                        _mementoBack = renderer.RenderStandardBack.DrawBack(context, e.CellBounds, path, _paletteBack, VisualOrientation.Top, PaletteState.Normal, _mementoBack);
                        renderer.RenderStandardBorder.DrawBorder(context,
                                                       e.CellBounds,
                                                       _paletteBorder,
                                                       VisualOrientation.Top,
                                                       PaletteState.Normal);
                    }


                }

                e.Paint(e.ClipBounds, (DataGridViewPaintParts.All & ~DataGridViewPaintParts.Background & ~DataGridViewPaintParts.ContentBackground));

                e.Handled = true;
                //}
            }
            else
                base.OnCellPainting(e);
        }
carpenter
 
Posts: 56
Joined: Sun Feb 11, 2007 7:25 am
Location: Greece, Thessalonika

Postby carpenter » Wed May 23, 2007 5:08 am

This is an easy way to do it and also you can have the DataGridView glyphs but the issue of focus event is not covered
Code: Select all
protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
        {
            if (e.ColumnIndex >= 0 && e.RowIndex == -1)
            {
                PaintColumnHeader(e);
            }

            if (e.ColumnIndex < 0)
            {
                PaintRowHeader(e);
            }

            base.OnCellPainting(e);
        }

        private void PaintRowHeader(DataGridViewCellPaintingEventArgs e)
        {
            if (_palette != null)
            {
               
                PaletteState state = PaletteState.Normal;

               
                Color backColor1 = _palette.GetBackColor1(PaletteBackStyle.ButtonStandalone, Enabled ? PaletteState.Normal : PaletteState.Disabled);
                Color backColor2 = _palette.GetBackColor2(PaletteBackStyle.ButtonStandalone, Enabled ? PaletteState.Normal : PaletteState.Disabled);

                Color fillColor1 = _palette.GetBackColor1(PaletteBackStyle.ButtonStandalone, state);
                Color fillColor2 = _palette.GetBackColor2(PaletteBackStyle.ButtonStandalone, state);
                Color borderColor = _palette.GetBorderColor1(PaletteBorderStyle.ButtonStandalone, state);

                using (Brush backBrush = new LinearGradientBrush(e.CellBounds, backColor1, backColor2, LinearGradientMode.Vertical))
                    e.Graphics.FillRectangle(backBrush, e.CellBounds);

                GraphicsPath path = new GraphicsPath();
                path.AddRectangle(e.CellBounds);
               
                using (Brush fillBrush = new LinearGradientBrush(e.CellBounds, fillColor1, fillColor2, LinearGradientMode.Vertical))
                    e.Graphics.FillPath(fillBrush, path);

               
                using (Pen borderPen = new Pen(borderColor))
                    e.Graphics.DrawPath(borderPen, path);

                DataGridViewPaintParts parts = DataGridViewPaintParts.ContentBackground | DataGridViewPaintParts.ContentForeground;

               
                e.Paint(e.ClipBounds, parts);

                e.Handled = true;

            }
        }

       

        private void PaintColumnHeader(DataGridViewCellPaintingEventArgs e)
        {
            if (_palette != null)
            {
               
                PaletteState state = PaletteState.Normal;
               
               
                Color backColor1 = _palette.GetBackColor1(PaletteBackStyle.ButtonStandalone, Enabled ? PaletteState.Normal : PaletteState.Disabled);
                Color backColor2 = _palette.GetBackColor2(PaletteBackStyle.ButtonStandalone, Enabled ? PaletteState.Normal : PaletteState.Disabled);

                Color fillColor1 = _palette.GetBackColor1(PaletteBackStyle.ButtonStandalone, state);
                Color fillColor2 = _palette.GetBackColor2(PaletteBackStyle.ButtonStandalone, state);
                Color borderColor = _palette.GetBorderColor1(PaletteBorderStyle.ButtonStandalone, state);

                using (Brush backBrush = new LinearGradientBrush(e.CellBounds, backColor1, backColor2,LinearGradientMode.Vertical))
                    e.Graphics.FillRectangle(backBrush, e.CellBounds);

                GraphicsPath path = new GraphicsPath();
                path.AddRectangle(e.CellBounds);
               
                using (Brush fillBrush = new LinearGradientBrush(e.CellBounds, fillColor1, fillColor2, LinearGradientMode.Vertical))
                    e.Graphics.FillPath(fillBrush, path);

               
                using (Pen borderPen = new Pen(borderColor))
                    e.Graphics.DrawPath(borderPen, path);

                DataGridViewPaintParts parts = DataGridViewPaintParts.ContentBackground | DataGridViewPaintParts.ContentForeground;
   
       
        e.Paint(e.ClipBounds, parts);

        e.Handled = true;

            }
        }
carpenter
 
Posts: 56
Joined: Sun Feb 11, 2007 7:25 am
Location: Greece, Thessalonika

Postby cornelha » Wed May 23, 2007 4:23 pm

I am glad to see this generated so much interest and feedback. Phil this type of feedback is a testiment to a great job with Krypton. Thanks again. Phil posted a test sample of the the grid on the Snippets section.
cornelha
 
Posts: 48
Joined: Tue Apr 24, 2007 11:38 pm
Location: South Africa

Postby nbb007 » Fri May 25, 2007 5:46 pm

I think that this was great too - I'm glad that everybody is so helpful. I'm really lovin' Krypton and have re-written several GUIs for some apps my company uses.

Another question: Does anybody have any ideas on how to "Kryptonize" the vScrollBar & hScrollBar windows forms components? I'd love to have them look more like the rest of the GUI, but I'm all thumbs when it comes to creating GUI elements, painting, etc... Thanks!
nbb007
 
Posts: 10
Joined: Sun May 20, 2007 6:15 am


Return to Krypton Navigator

Who is online

Users browsing this forum: No registered users and 1 guest

cron