Graphics.EndContainer(GraphicsContainer) 方法  
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
关闭当前图形容器,并将此 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 -> unitPublic 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 方法一起使用,以创建嵌套图形容器。 图形容器保留图形状态,例如转换、剪辑区域和呈现属性。
调用 Graphics的 BeginContainer 方法时,会将保存 Graphics 状态的信息块放在堆栈上。 BeginContainer 方法返回一个标识该信息块的 GraphicsContainer。 将标识对象传递给 EndContainer 方法时,将从堆栈中删除信息块,并用于将 Graphics 还原到 BeginContainer 方法调用时的状态。
容器可以嵌套;也就是说,在调用 EndContainer 方法之前,可以多次调用 BeginContainer 方法。 每次调用 BeginContainer 方法时,都会将信息块放在堆栈上,并且你会收到信息块的 GraphicsContainer。 将其中一个对象传递给 EndContainer 方法时,Graphics 将返回到返回该特定 GraphicsContainerBeginContainer 方法调用时的状态。 通过该 BeginContainer 方法调用放置在堆栈上的信息块将从堆栈中删除,在该堆栈上放置的所有信息块之后,也会删除 BeginContainer 方法调用。
对 Save 方法的调用会将信息块放置在与调用 BeginContainer 方法相同的堆栈上。 就像 EndContainer 方法调用与 BeginContainer 方法调用配对一样,Restore 方法调用与 Save 方法调用配对。
调用 EndContainer 方法时,从堆栈中删除对 BeginContainer 方法的相应调用后(由 Save 方法或 BeginContainer 方法)上放置的所有信息块。 同样,在调用 Restore 方法时,在从堆栈中删除对 Save 方法的相应调用后(由 Save 方法或 BeginContainer 方法)上放置的所有信息块。