本文整理汇总了C#中System.Windows.Forms.DataGridViewCellPaintingEventArgs.PaintBackground方法的典型用法代码示例。如果您正苦于以下问题:C# DataGridViewCellPaintingEventArgs.PaintBackground方法的具体用法?C# DataGridViewCellPaintingEventArgs.PaintBackground怎么用?C# DataGridViewCellPaintingEventArgs.PaintBackground使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Windows.Forms.DataGridViewCellPaintingEventArgs
的用法示例。
在下文中一共展示了DataGridViewCellPaintingEventArgs.PaintBackground方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: OnCellPainting
protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
{
base.OnCellPainting(e);
if (e.RowIndex >= 0 && Rows[e.RowIndex].Tag is Host)
{
PoolHostDataGridViewOneCheckboxRow row = (PoolHostDataGridViewOneCheckboxRow)Rows[e.RowIndex];
//Paintout checkbox - note: it's still there
if (!row.CheckBoxVisible && e.ColumnIndex == 1)
{
e.PaintBackground(e.ClipBounds, true);
e.Handled = true;
}
else if (row.HasPool && (e.ColumnIndex == 0 || e.ColumnIndex == 1))
{
e.PaintBackground(e.ClipBounds, true);
e.Handled = true;
}
else if (!row.HasPool && e.ColumnIndex == 0)
{
e.PaintBackground(e.ClipBounds, true);
e.Handled = true;
}
}
}
示例2: dataGridView_CellPainting
private void dataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
// check that we are in a header cell!
if (e.RowIndex == -1 && e.ColumnIndex > 0)
{
e.PaintBackground(e.ClipBounds, true);
Rectangle rect = MatrixDataGridView.GetColumnDisplayRectangle(e.ColumnIndex, true);
Size titleSize = TextRenderer.MeasureText(e.Value.ToString(), e.CellStyle.Font);
int offset = 4;
if (MatrixDataGridView.ColumnHeadersHeight < titleSize.Width + 2 * offset)
{
MatrixDataGridView.ColumnHeadersHeight = titleSize.Width + 2 * offset;
}
e.Graphics.TranslateTransform(1, titleSize.Width);
e.Graphics.RotateTransform(-90.0f);
e.Graphics.DrawString(e.Value.ToString(), MatrixDataGridView.Font, Brushes.Black,
new PointF(rect.Y - (MatrixDataGridView.ColumnHeadersHeight - titleSize.Width - offset), rect.X));
e.Graphics.RotateTransform(90.0f);
e.Graphics.TranslateTransform(-1, -titleSize.Width);
e.Handled = true;
}
if (e.RowIndex != -1 && e.ColumnIndex > 0)
{
var state = (ProjectDependency)e.Value;
/*if (state == ProjectDependency.Invalid)
e.CellStyle.BackColor = Color.Silver;*/
e.PaintBackground(e.ClipBounds, true);
Rectangle rect = MatrixDataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);
var CheckBoxRegion = new Rectangle(rect.X + box_offset, rect.Y + box_offset, box_size, box_size);
switch (state)
{
case ProjectDependency.None:
ControlPaint.DrawCheckBox(e.Graphics, CheckBoxRegion, ButtonState.Normal);
break;
case ProjectDependency.Direct:
ControlPaint.DrawCheckBox(e.Graphics, CheckBoxRegion, ButtonState.Checked);
break;
case ProjectDependency.Indirect:
ControlPaint.DrawCheckBox(e.Graphics, CheckBoxRegion, ButtonState.Checked | ButtonState.Inactive);
break;
case ProjectDependency.Invalid:
ControlPaint.DrawCheckBox(e.Graphics, CheckBoxRegion, ButtonState.Inactive);
break;
}
e.Handled = true;
}
}
示例3: upgradeEquipmentGrid_CellPainting
private void upgradeEquipmentGrid_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
// This just hides numeric fields that aren't supported, they return -1
if ((e.Value is int && (int)e.Value == -1) ||
(e.Value is double && Math.Abs((double)e.Value + 1) < Double.Epsilon) ||
(e.Value is EquippableSlotFlags && (EquippableSlotFlags)e.Value == EquippableSlotFlags.None) ||
(e.Value is CoverageFlags && (CoverageFlags)e.Value == CoverageFlags.None))
{
e.PaintBackground(e.ClipBounds, true);
e.Handled = true;
}
}
示例4: DataGridView_CellPaintingRotate
private void DataGridView_CellPaintingRotate(object sender, DataGridViewCellPaintingEventArgs e)
{
// Vertical text from column 0, or adjust below, if first column(s) to be skipped
if (e.RowIndex == -1 && e.ColumnIndex > 1)
{
e.PaintBackground(e.CellBounds, true);
e.Graphics.TranslateTransform(e.CellBounds.Left, e.CellBounds.Bottom);
e.Graphics.RotateTransform(270);
e.Graphics.DrawString(e.FormattedValue.ToString(), e.CellStyle.Font, Brushes.Black, 5, 5);
e.Graphics.ResetTransform();
e.Handled = true;
}
}
示例5: PaintImageCell
/// <summary>
/// Repaint an image cell with an image in an image
/// list. Run this method in the CellPainting event
/// of the DataGridView.
///
/// This method ignores invalid parameters. Provide
/// it with empty or invalid indices to cause it to
/// abort.
/// </summary>
public static void PaintImageCell(this DataGridView dataGridView, DataGridViewCellPaintingEventArgs e, ImageList imageList, String imageKey)
{
//Abort if any parameter is null or if the image does not exist
if (e == null || imageList == null || imageKey.IsNullOrEmpty())
return;
if (!imageList.Images.ContainsKey(imageKey))
return;
//Paint the image
e.PaintBackground(e.ClipBounds, false);
var pt = e.CellBounds.Location;
pt.X += (e.CellBounds.Width - imageList.ImageSize.Width) / 2;
pt.Y += 4;
imageList.Draw(e.Graphics, pt, imageList.Images.IndexOfKey(imageKey));
e.Handled = true;
}
示例6: MyGrid_CellPainting
void MyGrid_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex == -1)
{
e.PaintBackground(e.CellBounds, true);
e.Graphics.TranslateTransform(e.CellBounds.Left, e.CellBounds.Bottom);
e.Graphics.RotateTransform(270);
e.Graphics.DrawString(e.FormattedValue.ToString(), e.CellStyle.Font, Brushes.Black, 5, 5);
e.Graphics.ResetTransform();
e.Handled = true;
DataGridViewColumn col = Columns[e.ColumnIndex];
SizeF size = e.Graphics.MeasureString("Column1", RowHeadersDefaultCellStyle.Font, 1000, new StringFormat(StringFormatFlags.DirectionVertical));
//col.Width = (int)size.Width + 10;
//Columns[e.ColumnIndex].Width =
if (ColumnHeadersHeight < (int)size.Height + 10)
ColumnHeadersHeight = (int)size.Height + 10;
}
}
示例7: grdMappingsCellPainting
private void grdMappingsCellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
// Don't display Delete button for cleared rows (as it has no effect)
// Unfortunately, delete looks CRAP on Windows 2000
// as it takes on the background colour ..??. TODO.
if (e.ColumnIndex == 3 && e.RowIndex >= 0)
{
if (_clearedKeys.Contains(e.RowIndex))
{
e.PaintBackground(e.CellBounds, true);
e.Handled = true;
}
}
}
示例8: OnCellPainting
/// ------------------------------------------------------------------------------------
/// <summary>
/// Handles the CellPainting event of the dataGridView1 control.
/// </summary>
/// ------------------------------------------------------------------------------------
protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
{
base.OnCellPainting(e);
if (e.Handled || e.RowIndex < 0 || e.ColumnIndex < 0 || m_list == null ||
m_list.Count == 0 || m_list[e.RowIndex] == null)
{
return;
}
e.Handled = true;
// Paint everything but the focus rectangle, foreground and background.
// I'm not sure what's left, but just in case...
DataGridViewPaintParts parts = e.PaintParts;
parts &= ~DataGridViewPaintParts.Focus;
parts &= ~DataGridViewPaintParts.Background;
parts &= ~DataGridViewPaintParts.ContentForeground;
e.Paint(e.CellBounds, parts);
e.PaintBackground(e.CellBounds, false);
IInspectorObject io = m_list[e.RowIndex];
Rectangle rcText = e.CellBounds;
Rectangle rcHotSpot = Rectangle.Empty;
bool isSelected = ((e.State & DataGridViewElementStates.Selected) > 0);
bool isInBlock = (!isSelected && e.RowIndex >= m_firstRowInShadind && e.RowIndex <= m_lastRowInShading);
bool isIndentedCell = (e.ColumnIndex == 0);
if (isIndentedCell)
{
// Calculate the location and size of the rectangle into which text will be drawn.
// Adjust the text rectangle to account for the +/- image and the proper indent level.
rcHotSpot = GetExpandCollapseRect(e.CellBounds, io.Level);
int dx = ((rcHotSpot.Right - rcText.X) + 5);
rcText.X += dx;
rcText.Width -= dx;
}
// Draw the background color for the cell.
using (SolidBrush br = new SolidBrush(DefaultCellStyle.BackColor))
{
if (isSelected)
br.Color = DefaultCellStyle.SelectionBackColor;
else if (isInBlock && m_clrShading != Color.Empty)
br.Color = m_clrShading;
e.Graphics.FillRectangle(br, rcText);
}
Color clrFore = (isSelected ? e.CellStyle.SelectionForeColor : e.CellStyle.ForeColor);
TextFormatFlags flags = TextFormatFlags.Left | TextFormatFlags.VerticalCenter |
TextFormatFlags.EndEllipsis | TextFormatFlags.SingleLine;
TextRenderer.DrawText(e.Graphics, e.FormattedValue as string,
e.CellStyle.Font, rcText, clrFore, flags);
DrawBorders(e, isIndentedCell,
(isInBlock && m_clrShading != Color.Empty ? m_clrShading : GridColor),
(isInBlock ? rcText.X: rcHotSpot.X));
if (!isIndentedCell)
return;
DrawTreeLines(e, rcHotSpot, io.Level);
if (io.HasChildren)
{
// Draw the expand or collapse (+/-) image.
e.Graphics.DrawImage(m_list.IsExpanded(e.RowIndex) ?
Properties.Resources.kimidCollapse : Properties.Resources.kimidExpand, rcHotSpot);
}
}
示例9: dataGridView1_CellPainting
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
string phonemeStr;
Bitmap phonemeBitmap;
if ((e.RowIndex == -1) && (e.ColumnIndex >= 0))
{
e.PaintBackground(e.CellBounds, true);
e.Graphics.TranslateTransform(e.CellBounds.Left, e.CellBounds.Bottom);
e.Graphics.RotateTransform(270);
phonemeStr = e.FormattedValue.ToString();
if (_phonemeBitmaps.TryGetValue(phonemeStr, out phonemeBitmap))
{
e.Graphics.DrawImage(new Bitmap(_phonemeBitmaps[phonemeStr], 48, 48), 5, 5);
e.Graphics.DrawString(phonemeStr, e.CellStyle.Font, Brushes.Black, 55, 5);
}
else
{
e.Graphics.DrawString(phonemeStr, e.CellStyle.Font, Brushes.Black, 5, 5);
}
e.Graphics.ResetTransform();
e.Handled = true;
}
if (e.RowIndex > -1)
{
if (e.ColumnIndex >= currentDataTable.Columns.Count - 1)
{
using (SolidBrush paintBrush = new SolidBrush((Color)e.Value))
{
e.Graphics.FillRectangle(paintBrush,
e.CellBounds.X + 2,
e.CellBounds.Y + 2,
e.CellBounds.Width - 3,
e.CellBounds.Height - 3);
e.Graphics.DrawRectangle(new Pen(Color.Gray, 2), e.CellBounds);
e.CellStyle.ForeColor = (Color)e.Value;
e.CellStyle.SelectionForeColor = (Color)e.Value;
e.CellStyle.SelectionBackColor = Color.Black;
e.CellStyle.BackColor = (Color)e.Value;
}
}
else
{
e.CellStyle.BackColor =
((e.RowIndex % 2) == 0) ? Color.White : Color.LightGreen;
}
}
}
示例10: DGVlist_CellPainting
private void DGVlist_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex == 0 && e.RowIndex == -1 && !isPaintedChecbox)
{
e.PaintBackground(e.ClipBounds, false);
Point pt = e.CellBounds.Location; // where you want the bitmap in the cell
int nChkBoxWidth = 15;
int nChkBoxHeight = 15;
int offsetx = (e.CellBounds.Width - nChkBoxWidth) / 2;
int offsety = (e.CellBounds.Height - nChkBoxHeight) / 2;
pt.X += offsetx;
pt.Y += offsety;
CheckBox cb = new CheckBox();
cb.Size = new Size(nChkBoxWidth, nChkBoxHeight);
cb.Location = pt;
cb.CheckedChanged += new EventHandler(gvSheetListCheckBox_CheckedChanged);
((DataGridView)sender).Controls.Add(cb);
e.Handled = true;
isPaintedChecbox = true;
}
}
示例11: realGrid_CellPainting
private void realGrid_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
Rectangle r = e.CellBounds;
Graphics g = e.Graphics;
int row = e.RowIndex;
int column = e.ColumnIndex;
DataGridViewCellStyle cellStyle = e.CellStyle;
DataGridViewElementStates cellState = e.State;
if (row >= 0 && column >= 0)
{
CellInfo cell = DoGetCellInfo(row, column);
StyleInfo style = DoGetStyleInfo(row, column);
if (style == null)
{
style = DoGenerateDefaultStyle(column, cellStyle, cellState);
}
int ident = DoGetIdent(row, column);
r = DoDrawBackground(r, g, style);
r.X += 2;
r.Y += 1;
r.Width -= 4;
r.Height -= 2;
if (DoDrawExpansion(column))
{
Rectangle r3 = new Rectangle(r.X + ident, r.Y, expansionImageWidth, r.Height);
ident += expansionImageWidth;
bool? isExpanded = DoGetExpanded(row, column);
if (isExpanded != null)
{
if ((Application.VisualStyleState & VisualStyleState.ClientAreaEnabled) ==
VisualStyleState.ClientAreaEnabled && VisualStyleRenderer.IsSupported)
{
VisualStyleRenderer rdr = new VisualStyleRenderer(
(((bool)isExpanded) ?
VisualStyleElement.TreeView.Glyph.Opened :
VisualStyleElement.TreeView.Glyph.Closed));
rdr.DrawBackground(g, r3);
}
else
{
Image img = (((bool)isExpanded) ? Resources.Collapse : Resources.Expand);
if (img.Height < r3.Height)
{
int chg = r3.Height - img.Height;
r3.Y += chg / 2;
r3.Height = img.Height;
}
g.DrawImage(img, r3);
}
}
}
r.X += ident;
r.Width -= ident;
DrawCellContents(g, cell, style, r);
e.Handled = true;
}
else if (row == -1 && column >= 0)
{
CellInfo cell = new CellInfo(Columns[column].HeadingText,
Columns[column].Image);
StyleInfo style = new StyleInfo(Color.Transparent, e.CellStyle.ForeColor,
e.CellStyle.Font, Columns[column].HeadingAlignment);
e.PaintBackground(e.ClipBounds, false);
r.X += 2;
r.Y += 1;
r.Width -= 4;
r.Height -= 2;
DrawCellContents(g, cell, style, r);
e.Handled = true;
}
}
示例12: datasourcesGridView_CellPainting
private void datasourcesGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if ((e.ColumnIndex == ColorColumnIndex) && (e.RowIndex > -1))
{
Rectangle rect = dataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);
e.PaintBackground(rect, true);
rect.X += 3;
rect.Y += 5;
rect.Width = ColorSquareSize;
rect.Height = ColorSquareSize;
using (var brush = new SolidBrush((Color)e.Value))
e.Graphics.FillRectangle(brush, rect);
e.Graphics.DrawRectangle(SystemPens.Window, rect);
using (var pen = new Pen(SystemColors.ControlDark, 1.5f))
e.Graphics.DrawRectangle(pen, Rectangle.Inflate(rect, 1, 1));
e.Handled = true;
}
}
示例13: dataGridViewTasks_CellPainting
private void dataGridViewTasks_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (((e.ColumnIndex == 3) && (e.RowIndex >= 0)) && ((e.RowIndex < this.dataGridViewTasks.Rows.Count) && (e.Value != null)))
{
string s = e.Value.ToString();
Rectangle cellBounds = e.CellBounds;
float single1 = 96f / (72f / e.CellStyle.Font.Size);
float num = 96f / (72f / e.CellStyle.Font.Size);
cellBounds.Y += (e.CellBounds.Height - ((int) num)) / 2;
e.PaintBackground(e.ClipBounds, false);
if (e.State == (DataGridViewElementStates.Visible | DataGridViewElementStates.Selected | DataGridViewElementStates.Displayed))
{
e.PaintBackground(e.ClipBounds, true);
}
if (s.Equals("运行中"))
{
Brush brush = new SolidBrush(Color.Green);
e.Graphics.DrawString(s, this.Font, brush, cellBounds, StringFormat.GenericDefault);
}
else
{
Brush brush2 = new SolidBrush(Color.Red);
e.Graphics.DrawString(s, this.Font, brush2, cellBounds, StringFormat.GenericDefault);
}
e.Handled = true;
}
}
示例14: dataGridViewCharStats_CellPainting
/// <summary>
/// Called when painting the dataGridViewCharStats table.
/// Does a special handling only for column headers containing skill names, and paint the skill name vertically, adjusting the header height if needed.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dataGridViewCharStats_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex == -1 && e.ColumnIndex >= _statColStart)
{
// Special handling of the header row (displaying column headers): print header names vertically
e.PaintBackground(e.ClipBounds, true);
Rectangle rect = this.dataGridViewCharStats.GetColumnDisplayRectangle(e.ColumnIndex, true);
Size titleSize = TextRenderer.MeasureText(e.Value.ToString(), dataGridViewCharSkillsHeaderFont);
if (this.dataGridViewCharStats.ColumnHeadersHeight < titleSize.Width)
{
this.dataGridViewCharStats.ColumnHeadersHeight = titleSize.Width;
}
e.Graphics.TranslateTransform(0, titleSize.Width);
e.Graphics.RotateTransform(-90.0F);
// This is the key line for bottom alignment - we adjust the PointF based on the
// ColumnHeadersHeight minus the current text width. ColumnHeadersHeight is the
// maximum of all the columns since we paint cells twice - though this fact
// may not be true in all usages!
e.Graphics.DrawString(e.Value.ToString(), dataGridViewCharSkillsHeaderFont, Brushes.Black, new PointF(rect.Y - (dataGridViewCharStats.ColumnHeadersHeight - titleSize.Width), rect.X));
// The old line for comparison
//e.Graphics.DrawString(e.Value.ToString(), this.Font, Brushes.Black, new PointF(rect.Y, rect.X));
e.Graphics.RotateTransform(90.0F);
e.Graphics.TranslateTransform(0, -titleSize.Width);
e.Handled = true;
}
}
示例15: _dgvVisitPlan_CellPainting
private void _dgvVisitPlan_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if ((e.RowIndex < 0) || (e.ColumnIndex < 0))
return;
if (_dgvVisitPlan.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == string.Empty)
return;
int day = Convert.ToInt32(_dgvVisitPlan.Rows[e.RowIndex].Cells[e.ColumnIndex].Value);
e.PaintBackground(e.ClipBounds, true); // фон отрисовывается стандартный
Font font = _dgvVisitPlan.DefaultCellStyle.Font; // стандартный шрифт ячейки, используется для первой половины текста
Font font2 = new Font("Microsoft Sans Serif", 7, FontStyle.Italic);
using (var sf = new StringFormat())
using (var firstBrush = new SolidBrush(e.CellStyle.ForeColor)) // кисть для первой половины текста
using (var RedBrush = new SolidBrush(Color.Red)) // кисть для последней половины текста
using (var GreenBrush = new SolidBrush(Color.Green)) // кисть для последней половины текста
{
sf.FormatFlags = StringFormatFlags.NoWrap; // чтобы при сжатии колонки текст не переносился на вторую строку
sf.Trimming = StringTrimming.EllipsisWord; // будет отрисовываться многоточие, если текст не влезает в колонку
string text = (string)e.FormattedValue; // полное (форматированное) значение ячейки
string firstHalf = day.ToString() + "\n"; // первая часть текста
e.Graphics.DrawString(firstHalf, font, firstBrush, e.CellBounds, sf); // первую половину текста рисуем стандартным шрифтом и цветом
e.Graphics.DrawString("\n", font, firstBrush, e.CellBounds, sf); // первую половину текста рисуем стандартным шрифтом и цветом
SizeF size = e.Graphics.MeasureString(firstHalf, font, e.CellBounds.Location, StringFormat.GenericTypographic); // размер первой половины текста
float y = e.CellBounds.Y;
StringBuilder sbCellToolTipText = new StringBuilder();
var list = from visit in visitList.visitList
where visit.DateVisit.Day == day
select visit;
foreach (Visit visit in list)
{
sbCellToolTipText.AppendLine(visit.GetPlanAndTime());
string lastHalf = visit.GetPlanAndTime();
var rect = new RectangleF(e.CellBounds.X, y + size.Height, e.CellBounds.Width, e.CellBounds.Height - size.Height); // ограничивающий прямоугольник для оставшейся части текста
Brush currentBrush = firstBrush;
if (visit.Status == Visit.VisitStatus.Complited)
currentBrush = GreenBrush;
if (visit.Status == Visit.VisitStatus.Uncomplited)
currentBrush = RedBrush;
e.Graphics.DrawString(lastHalf, font, currentBrush, rect, sf); // выводим вторую часть текста жирным шрифтом и другим цветом
if (visit.CommRD != string.Empty)
{
sbCellToolTipText.AppendLine(visit.CommRD);
size = e.Graphics.MeasureString(firstHalf, font, e.CellBounds.Location, StringFormat.GenericTypographic); // размер первой половины текста
y += size.Height;
lastHalf = visit.CommRD; // оставшаяся часть текста
rect = new RectangleF(e.CellBounds.X, y + size.Height, e.CellBounds.Width, e.CellBounds.Height - size.Height); // ограничивающий прямоугольник для оставшейся части текста
e.Graphics.DrawString(lastHalf, font2, firstBrush, rect, sf); // первую половину текста рисуем стандартным шрифтом и цветом
}
e.CellStyle.WrapMode = DataGridViewTriState.True;
firstHalf = lastHalf;
size = e.Graphics.MeasureString(firstHalf, font, e.CellBounds.Location, StringFormat.GenericTypographic); // размер первой половины текста
y += size.Height;
}
_dgvVisitPlan.Rows[e.RowIndex].Cells[e.ColumnIndex].ToolTipText = sbCellToolTipText.ToString();
}
e.Handled = true; // сигнализируем, что закончили обработку
}