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);


About our authors: Team EQA

Further readings

Where is WinMain() function in MFC application ?

MFC hides WinMain in its framework and includes source file on WinMain(). This explains how framework calls global CWinApp::Initinstance() from entry WinMain.

What is the utility of CWinApp class?

This is constructed during global C++ objects are constructed and is already available when Windows calls the WinMain function, which is supplied by the ...

Basic steps in Win32 GUI Application with source code.

Define a custom Window class structure, Register the class name, CreateWindow, Show windows and write message get and dispatch loop statements. Define the Window CallBack procedure and write the handlers.

What is a Window CallBack procedure and what is its utility?

DispatchMessage() is a API which indirectly triggers the Window CallBack procedure. Message structure members from this function are passed to the CallBack procedure. CallBack procedure should implement event handlers depending on the need of the application.

What are LPARAM and WPARAM in window proc function?

LPARAM and WPARAM are the two parameters in Window CallBack procedure. They signifies parameters of various events. They are used in handing individual events.

What are the basic steps of a typical MFC based application?

We need to write WinMain and need to follow all these in a Win32 application. However we need not to write much if we are writing an application with MFC ...