显示旋转的字符串 - DataGridView.RowPostPaint
我想在 DataGridView 中的某一行的背景中显示一个冗长的旋转字符串。然而,这个:
private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
if (e.RowIndex == 0)
{
...
//Draw the string
Graphics g = dataGridView1.CreateGraphics();
g.Clip = new Region(e.RowBounds);
g.RotateTransform(-45);
g.DrawString(printMe, font, brush, e.RowBounds, format);
}
}
不起作用,因为文本在旋转之前被剪切。
我还尝试首先在位图上绘制,但绘制透明位图似乎存在问题 - 文本显示为纯黑色。
有什么想法吗?
I want to display a lengthy rotated string in the background of one of my rows in a DataGridView. However, this:
private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
if (e.RowIndex == 0)
{
...
//Draw the string
Graphics g = dataGridView1.CreateGraphics();
g.Clip = new Region(e.RowBounds);
g.RotateTransform(-45);
g.DrawString(printMe, font, brush, e.RowBounds, format);
}
}
does not work because text is clipped before it's rotated.
I've also tried painting on a Bitmap
first, but there seems to be a problem painting transparent Bitmaps - the text comes out pure black.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想通了。问题是位图显然不具有透明度,即使您使用 PixelFormat.Format32bppArgb 也是如此。拉动绳子会导致它在黑色背景上绘制,这就是它如此黑暗的原因。
解决方案是将行从屏幕复制到位图上,在位图上绘制,然后将其复制回屏幕。
以下是完整的代码清单供参考:
I figured it out. The problem was that Bitmaps apparently don't have transparency, even when you use
PixelFormat.Format32bppArgb
. Drawing the string caused it to draw over a black background, which is why it was so dark.The solution was to copy the row from the screen onto a bitmap, draw onto the bitmap, and copy that back to the screen.
Here is the full code listing for reference: