c#winform datagridview :: cellpainting()调用drawString()有时会导致文本比应该是淡淡的文本
我有几个datagridViews在cellpainting()事件处理程序中使用graphics.drawString()在特定列中显示自定义值。通常,这些值显示良好,文本看起来正常(请参阅图像1)。但是我也有一个datavisualization.charting.chart Control(在完全不同的Winform上),我用来显示一个简单的线图。问题:如果我在查看任何自定义绘制的datagridview单元格之前查看图表,则通过drawString()绘制的文本明显比应(
令人惊讶的是,如果我在查看图表之前查看任何自定义的网格,则所有自定义绘制的单元格继续看起来正常。
我在两个测试用例中检查了图形和字体对象,它们看起来相同。我尝试创建一个静态字体对象,以在呼叫drawString()的调用中使用,但没有效果。我尝试将FontStyle从常规变为粗体,在错误测试案例中,文本看起来像常规而不是粗体。
似乎确实确实在某种程度上影响了图表控件在随后呼叫drawString()的后续呼叫中使用的字体使用的输入。
我的应用程序代码太大了,无法发布,但也许我可以创建一个相当简单的2个窗口应用程序,如果有人认为这会有所帮助,可以证明问题。
我徒劳地搜寻了其他任何人都报告问题甚至与此相似的人,但什么也没发现。任何帮助/思想/意见/建议都值得赞赏!
编辑:这是来自DataGridView :: CellPainting()事件处理程序的代码:
private void GridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex == 3 && e.RowIndex >= 0)
{
Rectangle cellRect = new Rectangle(e.CellBounds.X + 1,
e.CellBounds.Y + 1,
e.CellBounds.Width - 2,
e.CellBounds.Height - 2);
Color rowBackColor = dataGridView.Rows[e.RowIndex].DefaultCellStyle.BackColor;
Color rowForeColor = SystemColors.WindowText;
if (dataGridView.Rows[e.RowIndex].Selected)
{
rowBackColor = SystemColors.Highlight;
rowForeColor = SystemColors.HighlightText;
}
using (Brush gridBrush = new SolidBrush(dataGridView.GridColor),
foreColorBrush = new SolidBrush(rowForeColor),
backColorBrush = new SolidBrush(rowBackColor))
{
using (Pen gridLinePen = new Pen(gridBrush))
{
// Erase the cell
e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
// Draw the grid lines (only the right and bottom lines;
// DataGridView takes care of the others).
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,
e.CellBounds.Right - 1,
e.CellBounds.Bottom - 1);
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
e.CellBounds.Top,
e.CellBounds.Right - 1,
e.CellBounds.Bottom);
// Draw the text content of the cell
if (e.Value != null)
{
int measureId = Convert.ToInt32(e.Value.ToString());
string displayValue = ConvertToMeasure(measureId);
StringFormat stringFormat = new StringFormat(StringFormat.GenericDefault)
{
Alignment = StringAlignment.Near
};
e.Graphics.DrawString(displayValue, e.CellStyle.Font, foreColorBrush, cellRect, stringFormat);
}
e.Handled = true;
}
}
}
}
I have several DataGridViews that use Graphics.DrawString() in the CellPainting() event handler to display custom values in specific columns. Normally, these values display just fine and the text looks normal (see image 1). But I also have a DataVisualization.Charting.Chart control (on a completely different WinForm) that I use to display a simple line graph. The problem: If I view the chart before viewing any of the custom-drawn DataGridView cells, the text drawn by DrawString() is noticeably fainter than it should be (see image 2).
Amazingly, if I view any of the custom-drawn grids before viewing the chart, all of the custom-drawn cells continue to look normal.
I checked the Graphics and Font objects in both test cases and they look identical. I tried creating a static Font object to use in the calls to DrawString() but that had no effect. I tried changing the FontStyle from Regular to Bold, and in the error test case the text looked like Regular rather than Bold.
It really does seem as if somehow the chart control is effecting the FontWeight used for the Font in subsequent calls to DrawString().
My application code is much too large to post but perhaps I can create a fairly simple 2 window app that demonstrates the problem if anyone thinks that would help.
I have searched in vain for anyone else reporting a problem even remotely similar to this but have found nothing. Any help/thoughts/opinions/suggestions appreciated!
EDIT: Here is the code from one of the DataGridView::CellPainting() event handlers:
private void GridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex == 3 && e.RowIndex >= 0)
{
Rectangle cellRect = new Rectangle(e.CellBounds.X + 1,
e.CellBounds.Y + 1,
e.CellBounds.Width - 2,
e.CellBounds.Height - 2);
Color rowBackColor = dataGridView.Rows[e.RowIndex].DefaultCellStyle.BackColor;
Color rowForeColor = SystemColors.WindowText;
if (dataGridView.Rows[e.RowIndex].Selected)
{
rowBackColor = SystemColors.Highlight;
rowForeColor = SystemColors.HighlightText;
}
using (Brush gridBrush = new SolidBrush(dataGridView.GridColor),
foreColorBrush = new SolidBrush(rowForeColor),
backColorBrush = new SolidBrush(rowBackColor))
{
using (Pen gridLinePen = new Pen(gridBrush))
{
// Erase the cell
e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
// Draw the grid lines (only the right and bottom lines;
// DataGridView takes care of the others).
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,
e.CellBounds.Right - 1,
e.CellBounds.Bottom - 1);
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
e.CellBounds.Top,
e.CellBounds.Right - 1,
e.CellBounds.Bottom);
// Draw the text content of the cell
if (e.Value != null)
{
int measureId = Convert.ToInt32(e.Value.ToString());
string displayValue = ConvertToMeasure(measureId);
StringFormat stringFormat = new StringFormat(StringFormat.GenericDefault)
{
Alignment = StringAlignment.Near
};
e.Graphics.DrawString(displayValue, e.CellStyle.Font, foreColorBrush, cellRect, stringFormat);
}
e.Handled = true;
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论