AD
Administrator
Syncfusion Team
September 23, 2004 02:27 PM UTC
Hi Charles,
By default the grid uses ControlPaint.DrawButton which does not support changing the color. You can however handle the following event:
(From ms-help://Syncfusion.EssentialSuiteClassRef/Syncfusion.EssentialSuiteClassRef/Syncfusion.Grid~Syncfusion.Windows.Forms.Grid.GridDrawCellButtonBackgroundEventArgs.html)
The DrawCellButtonBackground event allows custom drawing of a cell button elements background. Set the Cancel property true if you have drawn the cell button elements background and do not want the grid to proceed with default drawing of the cell button elements background.
The event will automatically be called for every button inside a cell.
You should handle this event if you want to change for example the background color of pushbuttons and do not want the grid to proceed with its default drawing routine calling ControlPaint.DrawButton. This event supplies you also with the current button state that should be drawn. After the background has been drawn any foreground text will be drawn by the grid.
Be sure to set e.Cancel to true if you did your own drawing.
The followin sample demonstrates drawing a custom cell button element background
[C#]
public static void Draw3dFrame(Graphics g, int x0, int y0, int x1, int y1, int w, Color rgbTopLeft, Color rgbBottomRight)
{
Rectangle rc;
for (int i = 0; i != w; i++)
{
// Top
Brush brTL = new SolidBrush(rgbTopLeft);
rc = Rectangle.FromLTRB(x0, y0, x1, y0+1);
g.FillRectangle(brTL, rc);
// Left
rc = Rectangle.FromLTRB(x0, y0, x0+1, y1);
g.FillRectangle(brTL, rc);
brTL.Dispose();
Brush brBR = new SolidBrush(rgbBottomRight);
// Bottom
rc = Rectangle.FromLTRB(x0, y1, x1+1, y1+1);
g.FillRectangle(brBR, rc);
// Right
rc = Rectangle.FromLTRB(x1, y0, x1+1, y1);
g.FillRectangle(brBR, rc);
brBR.Dispose();
if (i != w-1)
{
x0++;
y0++;
x1--;
y1--;
}
}
}
protected override void OnDrawCellButtonBackground(GridDrawCellButtonBackgroundEventArgs e)
{
bool drawPressed = (e.ButtonState & ButtonState.Pushed) != 0;
Rectangle rect = e.Bounds;
Graphics g = e.Graphics;
Color hilight = SystemColors.ControlLightLight;
Color shadow = SystemColors.ControlDarkDark;
if (!drawPressed)
{
Draw3dFrame(e.Graphics, rect.Left, rect.Top, rect.Right-1, rect.Bottom-1, 1,
hilight, shadow);
}
else
{
Brush br = new SolidBrush(shadow);
g.FillRectangle(br, Rectangle.FromLTRB(rect.Left, rect.Bottom-1, rect.Right-1, rect.Bottom));
g.FillRectangle(br, Rectangle.FromLTRB(rect.Right-1, rect.Top, rect.Right, rect.Bottom));
br.Dispose();
}
e.Cancel = true;
}
Stefan