Jun
15
Written by:
Angelo
6/15/2008 1:39 PM
I've developed this control some times ago, to replace the "standard" control, in order to have the same background color, or forecolor, that changes with the Krypton palette...
It's very far to be a good control, but I gave to my app an update without rewriting the UI.
hope it helps, this is the code:
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;
using ComponentFactory.Krypton.Toolkit;
namespace AC.ExtendedRenderer.Toolkit
{
[System.Drawing.ToolboxBitmapAttribute(typeof(System.Windows.Forms.GroupBox))]
public class KryptonGroupBox : GroupBox
{
private IPalette _palette;
private PaletteRedirect _paletteRedirect;
#region ... Properties...
Color _bgColor = Color.Gray;
Boolean _darkerColors = false;
[Browsable(true), Category("Appearance-Extended")]
[DefaultValue("false")]
public Boolean DarkerColors
{
get { return _darkerColors; }
set { _darkerColors = value; Invalidate(); }
}
#endregion
#region ... Constructor ...
public KryptonGroupBox()
{
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
UpdateStyles();
// add Palette Handler
if (_palette != null)
_palette.PalettePaint += new EventHandler(OnPalettePaint);
KryptonManager.GlobalPaletteChanged += new EventHandler(OnGlobalPaletteChanged);
_palette = KryptonManager.CurrentGlobalPalette;
_paletteRedirect = new PaletteRedirect(_palette);
InitColors();
}
#endregion
private void InitColors()
{
//set BackGroudColor
_bgColor = _palette.ColorTable.ToolStripGradientMiddle;
//if darker ..., reset background
if (_darkerColors) _bgColor = _palette.ColorTable.MenuStripGradientEnd;
//set ForeColor
this.ForeColor = _palette.ColorTable.StatusStripText;
//reset FlatStyle
if (this.FlatStyle == FlatStyle.System) this.FlatStyle = FlatStyle.Standard;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
}
protected override void OnPaintBackground(PaintEventArgs e)
{
//paint deafult
base.OnPaintBackground(e);
//call to reset colors
InitColors();
//paint BackGround
e.Graphics.FillRectangle(new SolidBrush(_bgColor), e.ClipRectangle);
}
#region ... Krypton ...
//Kripton Palette Events
private void OnGlobalPaletteChanged(object sender, EventArgs e)
{
if (_palette != null)
_palette.PalettePaint -= new EventHandler(OnPalettePaint);
_palette = KryptonManager.CurrentGlobalPalette;
_paletteRedirect.Target = _palette;
if (_palette != null)
{
_palette.PalettePaint += new EventHandler(OnPalettePaint);
//repaint with new values
InitColors();
}
Invalidate();
}
//Kripton Palette Events
private void OnPalettePaint(object sender, PaletteLayoutEventArgs e)
{
Invalidate();
}
#endregion
}
}
(This is the code and a test app)
Angel
Tags:
1 comments so far...
Re: Custom GroupBox
Updated the code. When I wrote the control I've choosen to set the ForeColor in order to have the best effect with "Panel-Client" setting in Appearance Back Style. Now, for solving your issue, I've added a property: UseAlternateForeColor (boolean). With this property you can switch the fore colors, using the "second text style" (it should be ok if you set the Back Style to Control-Client).
By Angelo on
8/10/2008 10:44 AM
|