{"id":97055,"date":"2017-09-20T07:00:00","date_gmt":"2017-09-20T21:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/?p=97055"},"modified":"2019-03-13T01:17:00","modified_gmt":"2019-03-13T08:17:00","slug":"20170920-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20170920-00\/?p=97055","title":{"rendered":"What is the correct way of using SaveDC and RestoreDC?"},"content":{"rendered":"<p>There are these two functions <code>SaveDC<\/code> and <code>RestoreDC<\/code>. How do they work? <\/p>\n<p>Each device context (DC) maintains a stack of saved states. When you call <code>SaveDC<\/code>, the current state of the DC is saved and pushed onto the stack, and you get a positive integer representing that saved state. If you call <code>SaveDC<\/code> again, a new saved state is created and pushed onto the stack, and you get a new integer that represents the second state. Each call to <code>SaveDC<\/code> pushes the current state onto this internal stack. <\/p>\n<p>There are two ways to call the <code>RestoreDC<\/code> function. One is to pass a negative number. This indicates how many states to pop off the stack, and the last state popped off the stack is applied to the DC. <\/p>\n<p>The other (more common) way to call the <code>RestoreDC<\/code> function is to pass a specific state. In that case, the specific state is restored, and that state is popped off the stack. And since it&#8217;s a stack, this also means that any states that were pushed onto the stack after that point are also popped off. <\/p>\n<p>And of course you cannot restore a state to a DC different from the DC you saved it from. (Because each DC has a separate stack of saved states.) <\/p>\n<p>For concreteness, let&#8217;s say that we&#8217;ve saved the state three times: <\/p>\n<pre>\nSelectObject(hdc, GetStockObject(NULL_BRUSH));\nint state1 = SaveDC(hdc);\nSelectObject(hdc, GetStockObject(WHITE_BRUSH));\nint state2 = SaveDC(hdc);\nSelectObject(hdc, GetStockObject(BLACK_BRUSH));\nint state3 = SaveDC(hdc);\nSelectObject(hdc, GetStockObject(DC_BRUSH));\n<\/pre>\n<p>State 1 has the null brush, state 2 has the white brush, state 3 has the black brush, and the current state (not saved) has the <a HREF=\"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/20050420-28\/?p=35843\">DC brush<\/a>. <\/p>\n<p>Here&#8217;s what could happen next: <\/p>\n<table BORDER=\"1\" CELLPADDING=\"3\" CELLSPACING=\"0\" STYLE=\"border-collapse: collapse\">\n<tr>\n<th>Negative argument<\/th>\n<th>Equivalent positive argument<\/th>\n<th>Result<\/th>\n<th>Stack<\/th>\n<\/tr>\n<tr>\n<td><code>RestoreDC(-1)<\/code><\/td>\n<td><code>RestoreDC(state3)<\/code><\/td>\n<td>Current brush is black<\/td>\n<td>state2 (top), state1<\/td>\n<\/tr>\n<tr>\n<td><code>RestoreDC(-2)<\/code><\/td>\n<td><code>RestoreDC(state2)<\/code><\/td>\n<td>Current brush is white<\/td>\n<td>state1<\/td>\n<\/tr>\n<tr>\n<td><code>RestoreDC(-3)<\/code><\/td>\n<td><code>RestoreDC(state1)<\/code><\/td>\n<td>Current brush is null<\/td>\n<td>empty<\/td>\n<\/tr>\n<\/table>\n<p>Note that the state that is applied gets popped off the stack, which means that each state can be restored at most once. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Understanding the model.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-97055","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Understanding the model.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/97055","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=97055"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/97055\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=97055"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=97055"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=97055"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}