(以Windows形式)我的自定义DataGridViewCell的Paint非常慢。帮助:)
此单元格将在贴纸应用程序上使用。确定每个单元格的颜色是用户是否没有该标签的任何一种颜色(NumCromo = 0),或者是否只有一种颜色(Sticked,并且没有重复-> NumCromos = 1),或者是否大于1,然后重复另一种颜色。 还显示os重复出现在右下角的数字。 它的运行就像我想要的一样,但是非常慢。我知道自定义DataGridViews应该慢一些,但是我想知道是否可以做些什么来使其更快。也许静态一些,如果它的配置,不,我不喜欢静态。有任何想法吗? :)
这是代码:
public class DataGridViewCromoCell : DataGridViewButtonCell
{
public int NumCromos;
public bool EmptyCell = false;
public bool PaginaCell = false;
public DataGridViewCromoCell() : base() { }
public DataGridViewCromoCell(string s, int i) : base()
{
Value = s;
NumCromos = i;
}
public void IncCromo() { NumCromos++; }
public void DecCromo() { NumCromos = (NumCromos == 0) ? 0 : NumCromos--; }
protected override void Paint(
Graphics graphics,
Rectangle clipBounds,
Rectangle cellBounds,
int rowIndex,
DataGridViewElementStates elementState,
object value,
object formattedValue,
string errorText,
DataGridViewCellStyle cellStyle,
DataGridViewAdvancedBorderStyle advancedBorderStyle,
DataGridViewPaintParts paintParts)
{
TextFormatFlags flagsCanto = TextFormatFlags.Bottom | TextFormatFlags.Right | TextFormatFlags.RightToLeft;
TextFormatFlags flagsCentro = TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter;
Font FontValue = new Font(cellStyle.Font.FontFamily, 9, cellStyle.Font.Style, GraphicsUnit.Pixel);
Font FontNum = new Font(cellStyle.Font.FontFamily, 7, cellStyle.Font.Style, GraphicsUnit.Pixel);
this.UseColumnTextForButtonValue = true;
SetColors(out SolidBrush background, out Color corNumCromos, out Color corVal);
if (PaginaCell)
{
base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
return;
}
if (EmptyCell)
{
graphics.FillRectangle(new SolidBrush(Color.FromArgb(171, 171, 171)), new Rectangle(cellBounds.X, cellBounds.Y, cellBounds.Width, cellBounds.Height));
return;
}
Rectangle r = new Rectangle(cellBounds.X, cellBounds.Y, cellBounds.Width - 2, cellBounds.Height - 2);
graphics.DrawRectangle(new Pen(Color.FromArgb(171, 171, 171)), new Rectangle(cellBounds.X, cellBounds.Y, cellBounds.Width - 1, cellBounds.Height - 1));
graphics.FillRectangle(background, r);
graphics.DrawRectangle(new Pen(Color.Black), r);
TextRenderer.DrawText(graphics, Value.ToString(), FontValue, new Rectangle(cellBounds.X, cellBounds.Y, cellBounds.Width, cellBounds.Height), corVal, flagsCentro);
if (NumCromos > 1)
TextRenderer.DrawText(graphics, (NumCromos - 1).ToString(), FontNum, new Rectangle(cellBounds.X, cellBounds.Y, cellBounds.Width - 1, cellBounds.Height - 3), corNumCromos, flagsCanto);
}
private void SetColors(out SolidBrush background, out Color corNumCromos, out Color corVal)
{
switch (NumCromos)
{
case 0:
background = new SolidBrush(Color.Tomato);
corNumCromos = Color.Black;
corVal = Color.Black;
break;
case 1:
background = new SolidBrush(Color.LightGreen);
corNumCromos = Color.Black;
corVal = Color.Black;
break;
default:
background = new SolidBrush(Color.Green);
corNumCromos = Color.White;
corVal = Color.White;
break;
}
}
}
我是C#的业余爱好者和新手,请不要侮辱太多,以防万一,ty :) 感谢您提供有关此代码的任何提示。