Graphics.EndContainer(GraphicsContainer) 方法

定义

关闭当前图形容器,并将此 Graphics 的状态还原到通过调用 BeginContainer() 方法保存的状态。

public:
 void EndContainer(System::Drawing::Drawing2D::GraphicsContainer ^ container);
public void EndContainer (System.Drawing.Drawing2D.GraphicsContainer container);
member this.EndContainer : System.Drawing.Drawing2D.GraphicsContainer -> unit
Public Sub EndContainer (container As GraphicsContainer)

参数

container
GraphicsContainer

GraphicsContainer,它表示此方法还原的容器。

示例

下面的代码示例旨在与 Windows 窗体 一起使用,它需要 PaintEventArgse,这是事件处理程序的参数Paint。 此代码执行以下操作:

  • 打开新的图形容器并保存旧容器。

  • 转换容器中的世界坐标。

  • 在) 新容器的 (转换坐标中填充红色矩形。

  • 关闭新容器并还原保存的容器。

  • 将绿色矩形 (填充到已保存容器的未翻译坐标) 。

结果是一个绿色矩形,该矩形覆盖相同大小的红色矩形。

public:
   void EndContainerState( PaintEventArgs^ e )
   {
      // Begin graphics container.
      GraphicsContainer^ containerState = e->Graphics->BeginContainer();

      // Translate world transformation.
      e->Graphics->TranslateTransform( 100.0F, 100.0F );

      // Fill translated rectangle in container with red.
      e->Graphics->FillRectangle( gcnew SolidBrush( Color::Red ), 0, 0, 200, 200 );

      // End graphics container.
      e->Graphics->EndContainer( containerState );

      // Fill untransformed rectangle with green.
      e->Graphics->FillRectangle( gcnew SolidBrush( Color::Green ), 0, 0, 200, 200 );
   }
public void EndContainerState(PaintEventArgs e)
{
             
    // Begin graphics container.
    GraphicsContainer containerState = e.Graphics.BeginContainer();
             
    // Translate world transformation.
    e.Graphics.TranslateTransform(100.0F, 100.0F);
             
    // Fill translated rectangle in container with red.
    e.Graphics.FillRectangle(new SolidBrush(Color.Red), 0, 0, 200, 200);
             
    // End graphics container.
    e.Graphics.EndContainer(containerState);
             
    // Fill untransformed rectangle with green.
    e.Graphics.FillRectangle(new SolidBrush(Color.Green), 0, 0, 200, 200);
}
Public Sub EndContainerState(ByVal e As PaintEventArgs)

    ' Begin graphics container.
    Dim containerState As GraphicsContainer = _
    e.Graphics.BeginContainer()

    ' Translate world transformation.
    e.Graphics.TranslateTransform(100.0F, 100.0F)

    ' Fill translated rectangle in container with red.
    e.Graphics.FillRectangle(New SolidBrush(Color.Red), 0, 0, _
    200, 200)

    ' End graphics container.
    e.Graphics.EndContainer(containerState)

    ' Fill untransformed rectangle with green.
    e.Graphics.FillRectangle(New SolidBrush(Color.Green), 0, 0, _
    200, 200)
End Sub

注解

将此方法与 方法结合使用 BeginContainer ,以创建嵌套图形容器。 图形容器保留图形状态,例如转换、剪辑区域和呈现属性。

调用 BeginContainerGraphics方法时,将把包含 状态 Graphics 的信息块放在堆栈上。 方法 BeginContainer 返回一个 GraphicsContainer 标识该信息块的 。 将标识对象传递给 EndContainer 方法时,信息块将从堆栈中删除,并用于将 还原 Graphics 到调用方法时 BeginContainer 的状态。

容器可以嵌套;也就是说,在调用 BeginContainer 方法之前,可以多次调用 EndContainer 方法。 每次调用 BeginContainer 方法时,都会在堆栈上放置一个信息块,并且你会收到 GraphicsContainer 信息块的 。 将其中一个对象传递给 EndContainer 方法时,Graphics将返回到返回该特定 GraphicsContainer的方法调用时BeginContainer它处于的状态。 该方法 BeginContainer 调用放置在堆栈上的信息块将从堆栈中删除,并且该方法调用后 BeginContainer 放置在该堆栈上的所有信息块也会被删除。

对 方法的调用将 Save 信息块与对 方法的调用放在同一堆栈上 BeginContainer 。 就像方法调用与方法调用配对一BeginContainerRestoreEndContainer,方法调用与Save方法调用配对。

调用 EndContainer 方法时,在相应调用BeginContainer方法后, (方法或BeginContainer方法) 放置在堆栈Save上的所有信息块都将从堆栈中删除。 同样,调用 Restore 方法时,在相应调用 Save 方法后, (方法或BeginContainer方法) 放置在堆栈Save上的所有信息块都会从堆栈中删除。

适用于