C1FlexGrid中单元格上的图形DrawImage,FillRectangle和DrawCell

时间:2015-12-30 18:28:05

标签: c# cell system.drawing c1flexgrid

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

" OwnerDrawCell" event是我绘制内容,边框和图像的地方。

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

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;

编写的代码几乎就在那里。我尝试了很多替代品和流程,但最终没有绘制细胞图像,细胞边界,细胞内容或其任何组合。

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

1 个答案:

答案 0 :(得分:0)

我通过为不绘制图像的单元调用DrawCell和DrawString来解决这个问题。