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 创建嵌套图形容器的方法。 图形容器保留图形状态,例如转换、剪辑区域和呈现属性。

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

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

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

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

适用于