protected override void OnRenderItemCheck(ToolStripItemImageRenderEventArgs e)
{
// Staring size of the checkbox is the image rectangle
Rectangle checkBox = e.ImageRectangle;
// Make the border of the check box 1 pixel bigger on all sides, as a minimum
checkBox.Inflate(1, 1);
// Can we extend upwards?
if (checkBox.Top > _checkInset)
{
int diff = checkBox.Top - _checkInset;
checkBox.Y -= diff;
checkBox.Height += diff;
}
// Can we extend downwards?
if (checkBox.Height <= (e.Item.Bounds.Height - (_checkInset * 2)))
{
int diff = e.Item.Bounds.Height - (_checkInset * 2) - checkBox.Height;
checkBox.Height += diff;
}
// Drawing with anti aliasing to create smoother appearance
using (AntiAlias aa = new AntiAlias(e.Graphics))
{
// Create border path for the check box
using (GraphicsPath borderPath = CreateBorderPath(checkBox, _cutMenuItemBack))
{
Color colorFill = KCT.CheckBackground;
Color colorBorder = CommonHelper.BlackenColor(KCT.CheckBackground, 0.89f, 0.88f, 0.98f);
if (!e.Item.Enabled)
{
colorFill = CommonHelper.ColorToBlackAndWhite(colorFill);
colorBorder = CommonHelper.ColorToBlackAndWhite(colorBorder);
}
// Fill the background in a solid color
using (SolidBrush fillBrush = new SolidBrush(colorFill))
e.Graphics.FillPath(fillBrush, borderPath);
// Draw the border around the check box
using (Pen borderPen = new Pen(colorBorder))
e.Graphics.DrawPath(borderPen, borderPath);
// If there is not an image, then we can draw the tick, square etc...
if (e.Item.Image == null)
{
CheckState checkState = CheckState.Unchecked;
// Extract the check state from the item
if (e.Item is ToolStripMenuItem)
{
ToolStripMenuItem item = (ToolStripMenuItem)e.Item;
checkState = item.CheckState;
}
// Decide what graphic to draw
Image drawImage = null;
switch (checkState)
{
case CheckState.Checked:
drawImage = _contextMenuChecked;
break;
case CheckState.Indeterminate:
drawImage = _contextMenuIndeterminate;
break;
}
if (drawImage != null)
{
// Draw the image centered in the available space
int xOffset = e.ImageRectangle.Width - drawImage.Width;
int yOffset = e.ImageRectangle.Height - drawImage.Height;
Rectangle drawRect = new Rectangle(e.ImageRectangle.X + xOffset,
e.ImageRectangle.Y + yOffset,
drawImage.Width,
drawImage.Height);
// Do we need to draw disabled?
if (e.Item.Enabled)
e.Graphics.DrawImage(drawImage, e.ImageRectangle);
else
{
using (ImageAttributes attribs = new ImageAttributes())
{
attribs.SetColorMatrix(CommonHelper.MatrixDisabled);
// Draw using the disabled matrix to make it look disabled
e.Graphics.DrawImage(drawImage, e.ImageRectangle,
0, 0, drawImage.Width, drawImage.Height,
GraphicsUnit.Pixel, attribs);
}
}
}
}
}
}
}