【问题标题】:GDI Overall Clipping BoundsGDI 整体剪裁范围
【发布时间】:2015-03-26 08:38:51
【问题描述】:

GDI 是否有任何设置整体剪切区域的方法,就像 GDI+ 图形对象一样?

具体来说:

  • Graphics.DrawString 使用 GDI+,其中包括剪辑边界系统。
  • TextRenderer.DrawText 使用 GDI 而不是。

我的新滚动条系统会根据需要自动调整Graphics 绘制区域的大小。在整理使用它的控件时,我将一些内联字符串渲染调用切换到使用TextRenderer.DrawText 的文本布局类。我不太记得为什么我使用它而不是 Graphics.DrawString,但在我重构之前我想检查是否有某种方法可以解决问题。

public class GraphicsClipExample : Form
{
    public GraphicsClipExample()
    {
        this.ClientSize = new Size(this.ClientSize.Width, 100);
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        // update the graphics clip area
        e.Graphics.Clear(Color.Cyan);
        e.Graphics.SetClip(new Rectangle(0, 0, e.ClipRectangle.Width, 50));
        e.Graphics.Clear(Color.Magenta);

        Font font = new Font("Calibri", 24);
        e.Graphics.DrawString("Testing", font, Brushes.Black, 10, 28);

        TextRenderer.DrawText(e.Graphics, "Testing", font, new Point(150, 28), Color.Black);
    }
}

这会产生以下输出:

有没有办法为整体GDI 或专门为TextRenderer 提供一个简单的剪切区域?

非常感谢

【问题讨论】:

    标签: c# winforms graphics gdi+ gdi


    【解决方案1】:

    尝试使用 PreserveGraphicsClipping 格式标志:

      TextRenderer.DrawText(e.Graphics, "Testing", font, new Point(150, 28),
                            Color.Black, Color.Empty,
                            TextFormatFlags.PreserveGraphicsClipping);
    

    【讨论】:

      猜你喜欢
      • 2014-07-16
      • 2010-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-10
      • 2012-09-28
      • 2016-01-24
      相关资源
      最近更新 更多