Graphics.Restore(GraphicsState) 方法

定义

将此 Graphics 的状态还原到 GraphicsState 表示的状态。Restores the state of this Graphics to the state represented by a GraphicsState.

public:
 void Restore(System::Drawing::Drawing2D::GraphicsState ^ gstate);
public void Restore (System.Drawing.Drawing2D.GraphicsState gstate);
member this.Restore : System.Drawing.Drawing2D.GraphicsState -> unit
Public Sub Restore (gstate As GraphicsState)

参数

gstate
GraphicsState

GraphicsState,它表示要将此 Graphics 还原到的状态。GraphicsState that represents the state to which to restore this Graphics.

示例

下面的代码示例旨在与 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:

  • 将 Windows 窗体的世界变换转换为 (100,0) 的矢量。Translates the world transform of the Windows Form by a vector (100, 0).

  • 保存窗体的图形状态。Saves the graphics state of the form.

  • 将窗体的世界变换重置为一个标识,并使用纯红色画笔填充矩形。Resets the world transform of the form to an identity and fills a rectangle with a solid red brush.

  • 还原已转换的图形状态,并使用纯色的蓝色画笔填充第二个矩形。Restores the translated graphics state and fills a second rectangle with a solid blue brush.

结果是一个未转换的红色实心矩形和一个已转换的蓝色填充矩形。The result is an untranslated red-filled rectangle and a translated blue-filled rectangle.

public:
   void SaveRestore2( PaintEventArgs^ e )
   {
      // Translate transformation matrix.
      e->Graphics->TranslateTransform( 100, 0 );

      // Save translated graphics state.
      GraphicsState^ transState = e->Graphics->Save();

      // Reset transformation matrix to identity and fill rectangle.
      e->Graphics->ResetTransform();
      e->Graphics->FillRectangle( gcnew SolidBrush( Color::Red ), 0, 0, 100, 100 );

      // Restore graphics state to translated state and fill second
      // rectangle.
      e->Graphics->Restore( transState );
      e->Graphics->FillRectangle( gcnew SolidBrush( Color::Blue ), 0, 0, 100, 100 );
   }
private void SaveRestore2(PaintEventArgs e)
{

    // Translate transformation matrix.
    e.Graphics.TranslateTransform(100, 0);

    // Save translated graphics state.
    GraphicsState transState = e.Graphics.Save();

    // Reset transformation matrix to identity and fill rectangle.
    e.Graphics.ResetTransform();
    e.Graphics.FillRectangle(new SolidBrush(Color.Red), 0, 0, 100, 100);

    // Restore graphics state to translated state and fill second

    // rectangle.
    e.Graphics.Restore(transState);
    e.Graphics.FillRectangle(new SolidBrush(Color.Blue), 0, 0, 100, 100);
}
Private Sub SaveRestore2(ByVal e As PaintEventArgs)

    ' Translate transformation matrix.
    e.Graphics.TranslateTransform(100, 0)

    ' Save translated graphics state.
    Dim transState As GraphicsState = e.Graphics.Save()

    ' Reset transformation matrix to identity and fill rectangle.
    e.Graphics.ResetTransform()
    e.Graphics.FillRectangle(New SolidBrush(Color.Red), 0, 0, 100, 100)

    ' Restore graphics state to translated state and fill second

    ' rectangle.
    e.Graphics.Restore(transState)
    e.Graphics.FillRectangle(New SolidBrush(Color.Blue), 0, 0, _
    100, 100)
End Sub

注解

调用的方法时 Save Graphics ,保存的状态的信息块将 Graphics 放在堆栈上。When you call the Save method of a Graphics, an information block that holds the state of the Graphics is put on a stack. Save方法返回 GraphicsState 标识该信息块的。The Save method returns a GraphicsState that identifies that information block. 将标识传递 GraphicsStateRestore 方法时,将从堆栈中移除信息块,并使用将 Graphics 其还原到在方法调用时它所处的状态 SaveWhen you pass the identifying GraphicsState to the Restore 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 Save method call. 请注意, GraphicsState 给定调用方法返回的 Save 仅可向方法传递一次 RestoreNote that the GraphicsState returned by a given call to the Save method can be passed only once to the Restore method.

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

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

当你调用 Restore 方法时,将 Save BeginContainer Save 从堆栈中移除在调用方法后,由方法或方法) 放置在堆栈 (的所有信息块。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. 同样,在调用方法时 EndContainer ,通过方法) 或方法调用的堆栈上放置的所有信息块 (Save BeginContainer BeginContainer 将从堆栈中移除。Likewise, 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.

适用于