Graphics.EndContainer(GraphicsContainer) Graphics.EndContainer(GraphicsContainer) Graphics.EndContainer(GraphicsContainer) Graphics.EndContainer(GraphicsContainer) Method


关闭当前图形容器,并将此 Graphics 的状态还原到通过调用 BeginContainer() 方法保存的状态。Closes the current graphics container and restores the state of this Graphics to the state saved by a call to the BeginContainer() method.

 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)


GraphicsContainer GraphicsContainer GraphicsContainer GraphicsContainer

GraphicsContainer,它表示此方法还原的容器。GraphicsContainer that represents the container this method restores.


下面的代码示例旨在与 Windows 窗体一起使用,并且它需要PaintEventArgs e作为Paint事件处理程序的参数。The following code example is designed for use with Windows Forms, and it requires PaintEventArgse, which is a parameter of the Paint event handler. 该代码执行下列操作:The code performs the following actions:

  • 打开新的图形容器并保存旧容器。Opens a new graphics container and saves the old container.

  • 将世界坐标转换到容器中。Translates the world coordinates in the container.

  • 在的(已转换的)新容器中填充红色矩形。Fills a red rectangle in the (translated coordinates of the) new container.

  • 关闭新容器并还原已保存的容器。Closes the new container and restores the saved container.

  • 填充已保存容器的绿色矩形(到未翻译的坐标)。Fills a green rectangle (to the untranslated coordinates) of the saved container.

结果是一个绿色矩形,它 overlies 大小相同的红色矩形。The result is a green rectangle that overlies a red rectangle of the same size.

   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.
    // 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 = _

    ' 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.

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


将此方法与BeginContainer方法一起使用可创建嵌套图形容器。Use this method with the BeginContainer method to create nested graphics containers. 图形容器将保留图形状态,如转换、剪辑区域和呈现属性。Graphics containers retain graphics state, such as transformation, clipping region, and rendering properties.

调用BeginContainer的方法Graphics时,保存的Graphics状态的信息块将放在堆栈上。When you call the BeginContainer method of a Graphics, an information block that holds the state of the Graphics is put on a stack. BeginContainer 方法GraphicsContainer返回标识该信息块的。The BeginContainer method returns a GraphicsContainer that identifies that information block. 将标识对象传递给EndContainer方法时,信息块将从堆栈中移除,并用于将Graphics还原到在BeginContainer方法调用时它所处的状态。When you pass the identifying object to the EndContainer method, the information block is removed from the stack and is used to restore the Graphics to the state it was in at the time of the BeginContainer method call.

容器可以嵌套;也就是说,您可以在BeginContainer EndContainer调用方法之前多次调用方法。Containers can be nested; that is, you can call the BeginContainer method several times before you call the EndContainer method. 每次调用BeginContainer方法时,都会将信息块放在堆栈上,并GraphicsContainer收到信息块的。Each time you call the BeginContainer method, an information block is put on the stack, and you receive a GraphicsContainer for the information block. 将其中一个EndContainer对象传递给方法时Graphics ,将返回到它在BeginContainer方法调用时返回该特定GraphicsContainer的状态。When you pass one of those objects to the EndContainer method, the Graphics is returned to the state it was in at the time of the BeginContainer method call that returned that particular GraphicsContainer. 从堆栈中删除由该BeginContainer方法调用放置在堆栈上的信息块,并且也会删除该BeginContainer方法调用之后置于该堆栈上的所有信息块。The information block placed on the stack by that BeginContainer method call is removed from the stack, and all information blocks placed on that stack after that BeginContainer method call are also removed.

调用方法会将信息块放在与调用BeginContainer方法相同的堆栈上。 SaveCalls to the Save method place information blocks on the same stack as calls to the BeginContainer method. 正如EndContainer方法调用BeginContainer与方法调用配对一样, Restore方法调用与Save方法调用配对。Just as an EndContainer method call is paired with a BeginContainer method call, a Restore method call is paired with a Save method call.

调用EndContainer方法时,将从堆栈中移除在调用BeginContainer方法后,在堆栈Save上放置的所有BeginContainer信息块(由方法或方法调用)。When you call the EndContainer method, all information blocks placed on the stack (by the Save method or by the BeginContainer method) after the corresponding call to the BeginContainer method are removed from the stack. 同样, Restore当调用方法时,将从堆栈中移除对Save方法的相应调用后Save放置在堆栈上BeginContainer的所有信息块(由方法或方法调用)。Likewise, when you call the Restore method, all information blocks placed on the stack (by the Save method or by the BeginContainer method) after the corresponding call to the Save method are removed from the stack.