gpt4 book ai didi

c# - 显示旋转的字符串 - DataGridView.RowPostPaint

转载 作者:太空宇宙 更新时间:2023-11-03 16:54:40 24 4
gpt4 key购买 nike

我想在 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);
}
}

不起作用,因为文本在旋转之前被剪裁了。

我也尝试过先在 Bitmap 上绘画,但似乎在绘制透明位图时出现问题 - 文本显示为纯黑色。

有什么想法吗?

最佳答案

我想通了。问题是位图显然没有透明度,即使您使用 PixelFormat.Format32bppArgb 也是如此。绘制字符串导致它绘制在黑色背景上,这就是它如此黑暗的原因。

解决方案是将屏幕上的行复制到位图上,绘制到位图上,然后将其复制回屏幕。

g.CopyFromScreen(absolutePosition, Point.Empty, args.RowBounds.Size);

//Draw the rotated string here

args.Graphics.DrawImageUnscaledAndClipped(buffer, args.RowBounds);

这里是完整的代码 list 以供引用:

private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs args)
{
if(args.RowIndex == 0)
{
Font font = new Font("Verdana", 11);
Brush brush = new SolidBrush(Color.FromArgb(70, Color.DarkGreen));
StringFormat format = new StringFormat
{
FormatFlags = StringFormatFlags.NoWrap | StringFormatFlags.NoClip,
Trimming = StringTrimming.None,
};

//Setup the string to be printed
string printMe = String.Join(" ", Enumerable.Repeat("RUNNING", 10).ToArray());
printMe = String.Join(Environment.NewLine, Enumerable.Repeat(printMe, 50).ToArray());

//Draw string onto a bitmap
Bitmap buffer = new Bitmap(args.RowBounds.Width, args.RowBounds.Height);
Graphics g = Graphics.FromImage(buffer);
Point absolutePosition = dataGridView1.PointToScreen(args.RowBounds.Location);
g.CopyFromScreen(absolutePosition, Point.Empty, args.RowBounds.Size);
g.RotateTransform(-45, MatrixOrder.Append);
g.TranslateTransform(-50, 0, MatrixOrder.Append); //So we don't see the corner of the rotated rectangle
g.DrawString(printMe, font, brush, args.RowBounds, format);

//Draw the bitmap onto the table
args.Graphics.DrawImageUnscaledAndClipped(buffer, args.RowBounds);
}
}

关于c# - 显示旋转的字符串 - DataGridView.RowPostPaint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2610018/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com