Share via


Graphics::Restore 方法 (gdiplusgraphics.h)

Graphics::Restore方法會將此Graphics物件的狀態設定為此Graphics物件先前呼叫Graphics::Save方法所儲存的狀態。

語法

Status Restore(
  [in] GraphicsState gstate
);

參數

[in] gstate

類型: GraphicsState

32 位值 (先前呼叫此Graphics物件的Graphics::Save方法所傳回,) 識別已儲存狀態欄塊。

傳回值

類型: 狀態

如果方法成功,它會傳回 Ok,這是 Status 列舉的元素。

如果方法失敗,它會傳回 Status 列舉的其中一個其他元素。

備註

當您呼叫Graphics物件的Graphics::Save方法時,保存Graphics物件狀態的資訊區塊會放在堆疊上。 Graphics::Save方法會傳回識別該資訊區塊的值。 當您將識別值傳遞至 Restore 方法時,資訊區塊會從堆疊中移除,並用來將 Graphics 物件還原到 Graphics::Save 呼叫時的狀態。 請注意,指定呼叫 Graphics::Save 方法所傳回的識別碼只能傳遞一次至 Graphics::Restore 方法。

Graphics::Save方法的呼叫可以是巢狀的;也就是說,您可以在呼叫Graphics::Restore方法之前多次呼叫Graphics::Save方法。 每次呼叫 Graphics::Save 方法時,都會將資訊區塊放在堆疊上,而且您會收到資訊區塊的識別碼。 當您將其中一個識別碼傳遞至 Graphics::Restore 方法時, Graphics 物件會傳回至圖形: :Save 呼叫傳回該特定識別碼時的狀態。 該 Graphics::Save呼叫放置在堆疊上的資訊區塊會從堆疊中移除,而且該堆疊上放置於該堆疊上的所有資訊區塊也會移除Graphics::Save呼叫。

呼叫 BeginContainer 方法時,會將資訊區塊放在與 Graphics::Save 方法呼叫相同的堆疊上。 就像 Graphics::Restore 呼叫與 Graphics::Save 呼叫配對時, EndContainer 呼叫會與 BeginContainer 呼叫配對。

注意 當您呼叫 Graphics::Restore時, 圖形::SaveBeginContainer 會在對應呼叫 Graphics::Save 之後 (堆疊上放置的所有資訊區塊) 。 同樣地,當您呼叫EndContainer時,在對BeginContainer的對應呼叫從 (堆疊中移除之後,圖形::SaveBeginContainer) 放置於堆疊上的所有資訊區塊。
 

範例

下列範例示範使用 Graphics::Restore 方法的兩種方式。 第一個範例示範如何還原巢狀儲存狀態,第二個範例示範如何只還原兩個巢狀儲存狀態的第一個。

還原巢狀儲存狀態

下列範例會將 Graphics 物件的世界轉換設定為旋轉,然後儲存 Graphics 物件的狀態。 接下來,程式碼會呼叫 TranslateTransform,然後再次儲存狀態。 然後程式碼會呼叫 ScaleTransform。 此時, Graphics 物件的世界轉換是複合轉換:先旋轉、轉譯、縮放。 程式碼會使用紅色畫筆來繪製該複合轉換所轉換的省略號。

程式碼會將第二次呼叫Save傳回的 state2傳遞給Graphics::Restore方法,然後使用綠色畫筆再次繪製橢圓形。 綠色橢圓形會旋轉並轉譯,但不會縮放。 最後,程式碼會將第一次呼叫 Save傳回的 state1 傳遞給 Graphics::Restore 方法,然後使用藍色畫筆再次繪製省略號。 藍色橢圓形會旋轉,但不會轉譯或縮放。

VOID Example_Save1(HDC hdc)
{
   Graphics graphics(hdc);
   GraphicsState state1, state2;

   graphics.RotateTransform(30.0f);
   state1 = graphics.Save();
   graphics.TranslateTransform(100.0f, 0.0f, MatrixOrderAppend);
   state2 = graphics.Save();
   graphics.ScaleTransform(1.0f, 3.0f, MatrixOrderAppend);
   
   // Draw an ellipse. 
   // Three transformations apply: rotate, then translate, then scale.
   Pen redPen(Color(255, 255, 0, 0));
   graphics.DrawEllipse(&redPen, 0, 0, 100, 20);
 
   // Restore to state2 and draw the ellipse again. 
   // Two transformations apply: rotate then translate.
   graphics.Restore(state2);
   Pen greenPen(Color(255, 0, 255, 0));
   graphics.DrawEllipse(&greenPen, 0, 0, 100, 20);

   // Restore to state1 and draw the ellipse again. 
   // Only the rotation transformation applies.
   graphics.Restore(state1);
   Pen bluePen(Color(255, 0, 0, 255));
   graphics.DrawEllipse(&bluePen, 0, 0, 100, 20);
}

只還原兩個巢狀儲存狀態的第一個

下列範例會將 Graphics 物件的世界轉換設定為旋轉,然後儲存 Graphics 物件的狀態。 接下來,程式碼會呼叫 TranslateTransform,然後再次儲存狀態。 然後程式碼會呼叫 ScaleTransform。 此時, Graphics 物件的世界轉換是複合轉換:先旋轉、轉譯、縮放。 程式碼會使用紅色畫筆來繪製該複合轉換所轉換的省略號。

程式碼會將第一次呼叫Save傳回的 state1傳遞給Graphics::Restore方法,然後使用綠色畫筆再次繪製橢圓形。 綠色橢圓形會旋轉,但不會轉譯或縮放。

接下來,程式碼會嘗試還原 state2所識別的狀態。 嘗試失敗,因為呼叫 Restore (state1) 從堆疊移除 state1state2 所識別的資訊區塊。

VOID Example_Save2(HDC hdc)
{
   Graphics graphics(hdc);
   GraphicsState state1, state2;

   graphics.RotateTransform(30.0f);
   state1 = graphics.Save();
   graphics.TranslateTransform(100.0f, 0.0f, MatrixOrderAppend);
   state2 = graphics.Save();
   graphics.ScaleTransform(1.0f, 3.0f, MatrixOrderAppend);
   
   // Draw an ellipse. 
   // Three transformations apply: rotate, then translate, then scale.
   Pen redPen(Color(255, 255, 0, 0));
   graphics.DrawEllipse(&redPen, 0, 0, 100, 20);
 
   // Restore to state1 and draw the ellipse again. 
   // Only the rotation transformation applies.
   graphics.Restore(state1);
   Pen greenPen(Color(255, 0, 255, 0));
   graphics.DrawEllipse(&greenPen, 0, 0, 100, 20);

   // The information block identified by state2 has been lost.
   // The following call to Restore has no effect because
   // Restore(state1) removed from the stack the
   // information blocks identified by state1 and state2.
   graphics.Restore(state2);

   // The Graphics object is still in the state identified by state1.
   // The following code draws a blue ellipse on top of the previously
   // drawn green ellipse.
   Pen bluePen(Color(255, 0, 0, 255));
   graphics.DrawEllipse(&bluePen, 0, 0, 100, 20);
}

需求

   
最低支援的用戶端 Windows XP、Windows 2000 Professional [僅限傳統型應用程式]
最低支援的伺服器 Windows 2000 Server [僅限傳統型應用程式]
目標平台 Windows
標頭 gdiplusgraphics.h (包含 Gdiplus.h)
程式庫 Gdiplus.lib
Dll Gdiplus.dll

另請參閱

圖形

Graphics::Save