EQuestionAnswers.com Computer/Electronics Questions and Answers
C, C++, VC++, COM/DCOM, DLL and more
#Login #Sign up  Facebook Twitter TGoogle+

#Previous question #Index of VC++/WIN32/MFC Questions #Next question

SaveDC RestoreDC

SaveDC and RestoreDC are a pair of function to save and restore device context attributes. Lets see the functionalities of these APIs with an example scenario.

SaveDC function

The SaveDC function saves the current state of the specified device context (DC) by copying data describing selected objects and graphic modes (such as the bitmap, brush, palette, font, pen, region, drawing mode, and mapping mode) to a context stack.

int SaveDC(
  HDC hdc


  • hdc - A handle to the DC whose state is to be saved.

Return value

  • If the function succeeds, the return value identifies the saved state.
  • If the function fails, the return value is zero.


The SaveDC function can be used any number of times to save any number of instances of the DC state. A saved state can be restored by using the RestoreDC function.

RestoreDC function

The RestoreDC function restores a device context (DC) to the specified state. The DC is restored by popping state information off a stack created by earlier calls to the SaveDC function.

BOOL RestoreDC(
  HDC hdc,
  int nSavedDC


  • hdc - A handle to the DC.
  • nSavedDC - The saved state to be restored. If this parameter is positive, nSavedDC represents a specific instance of the state to be restored. If this parameter is negative, nSavedDC represents an instance relative to the current state. For example, -1 restores the most recently saved state.

Return value

  • If the function succeeds, the return value is nonzero.
  • If the function fails, the return value is zero.


The stack can contain the state information for several instances of the DC. If the state specified by the specified parameter is not at the top of the stack, RestoreDC deletes all state information between the top of the stack and the specified instance.

SaveDC RestoreDC source example

int hDCLast;

SelectObject(hDC, hPrevBrush);
SelectObject(hDC, hPrevPen);
SelectObject(hDC, hPrevFont);
SelectObject(hDC, hPrevBmp);

/*Save DC context */
hDCLast = SaveDC(hDC);

SelectObject(hDC, hBrush);
SelectObject(hDC, hPen);
SelectObject(hDC, hFont);
SelectObject(hDC, hBmp);

/*Restore DC context to Prev values */
RestoreDC(hDC, hDCLast);


You have viewed 1 page out of 109. Your VC++ learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

#Programming Windows, Fifth Edition (Developer Reference)
#Programming Applications for Microsoft Windows (Microsoft Programming Series)
#Programming Windows with MFC, Second Edition
#Visual C++ Programming
#Programming Microsoft Visual C++, Fifth Edition

* #1 webmaster Fri 17 Feb/2017 22:04:28(GMT)  Like 0 Unlike 0

Dear Users,

We are pleased to inform you that a forum/blog has been incorporated with www.mybestnotes.co.in. You are welcomed to add your comments, requests, codes, solutions and feedback to it. Please login(if already a member) or signup(for free) to avail to this facility.


Your message goes here:

My Post:*
Secutiry Code: ******  *
Preview this compose before posting this in discussion forum.
  1. Use [Sxx] code to show smiles. Example [S02]
  2. Use [URL ], example [URL http://www.google.com/]
  3. To display code or un formatted text use [CODE] [/CODE]. Example: [CODE] printf("Hello world"); [/CODE]
 [S01]   [S02]   [S03]   [S04]   [S05]   [S06]   [S07]   [S08] 
 [S09]   [S10]   [S11]   [S12]   [S13]   [S14]   [S15]   [S16] 
 [S17]   [S18]   [S19]   [S20]   [S21]   [S22]   [S23]   [S24] 

Note: Only members are allowed, * fields are mandatory.