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

发布于 2024-08-28 11:28:21 字数 531 浏览 5 评论 0原文

我想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里寻她 2024-09-04 11:28:22

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

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

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

//Draw the rotated string here

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

以下是完整的代码清单供参考:

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);
    }
}

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.

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

//Draw the rotated string here

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

Here is the full code listing for reference:

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文