0

当我需要设置背景颜色、绘制图像并为宽边框填充矩形时,我很难在 C1FlexGrid 中正确渲染单元格。我似乎无法为每个单元格正确绘制 DrawCell、DrawImage 和 FillRectangle 的正确组合。

“OwnerDrawCell”事件是我绘制内容、边框和图像的地方。

首先,我将每个单元格的单元格背景色设置为如下所示:

e.Style.BackColor = lockedBackColor;

然后对于某些单元格,我正在绘制图像和文本。

// CENTER TEXT IN CELL; IMAGE IS RIGHT JUSTIFIED, CENTERED VERTICALLY
// Must draw cell first - background color, borders, etc..
e.DrawCell(DrawCellFlags.Background | DrawCellFlags.Border);

// Draw cell text
int textWidth = (int)e.Graphics.MeasureString(e.Text, e.Style.Font).Width;
int textHeight = (int)e.Graphics.MeasureString(e.Text, e.Style.Font).Height;
float textCenterX = e.Bounds.Left + ((e.Bounds.Width - textWidth) / 2);
float textCenterY = e.Bounds.Top + ((e.Bounds.Height - textHeight) / 2);
e.Graphics.DrawString(e.Text, e.Style.Font, brushColorForString, textCenterX, textCenterY);

if (e.Row == 8 || PlantHasBins())
{
    // Draw cell image
    int cellImageX = e.Bounds.Right - _cellImage.Width;
    int cellImageY = e.Bounds.Top + ((e.Bounds.Height - _cellImage.Height) / 2);
    var cellImagePoint = new Point(cellImageX, cellImageY);
    e.Graphics.DrawImage(_cellImage, cellImagePoint);
}

e.Handled = true;

然后对于某些列,我想绘制一个沉重的右边框,以便在列组之间进行视觉分离。

e.DrawCell(DrawCellFlags.Border);

Rectangle rc;
Margins m = new Margins(0, 0, 0, 0);
m.Right = 3;
CellRange rg;

rg = PlantAlleyBinGrid.GetCellRange(e.Row, e.Col);
rc = e.Bounds;
rg.c1 = rg.c2 = 2 + 1;
rg.c1 = rg.c2 = 2;

rc.X = rc.Right - m.Right;
rc.Width = m.Right;
e.Graphics.FillRectangle(new SolidBrush(Color.Black), rc);

e.Handled = true;

编写的代码几乎就在那里。我尝试了许多替代方案和流程,但最终没有绘制单元格图像、单元格边框、单元格内容或它们的任何组合。

我需要有关如何在单元格上绘制所有内容的帮助。

4

1 回答 1

0

I resolved this by calling DrawCell and DrawString for cells that do not draw an image.

于 2015-12-30T19:52:44.093 回答