title: Win32: capture handle to Display monitor
body: <p>I am currently developing an application that requires an <code>HDC</code> for each of the screens connected to the system.</p>
  
  <p>I am currently using code like this: </p>
  
  <pre><code>std::vector&lt;HDC&gt; dcs;
  HDC dcMain = ::GetDC(nullptr); // &lt;-- don't understand this
  
  ::EnumDisplayMonitors(dcMain, nullptr, MONITORENUMPROC(&amp;DisplayMonitorCallback), LPARAM(&amp;dcs));
  </code></pre>
  
  <p>My callback is as follows: </p>
  
  <pre><code>BOOL DisplayMonitorCallback(const HMONITOR monitor, const HDC hdcMonitor, const LPRECT lprcMonitor, std::vector&lt;HDC&gt;&amp; dcs)
  {
      dcs.push_back(hdcMonitor);
  
      // here is where it gets weird!
      HBRUSH br = CreateSolidBrush(RGB(0, 255, 0));
  
      auto rst = FillRect(hdcMonitor, lprcMonitor, br);
  
      // Process all monitors
      return TRUE;
  }
  </code></pre>
  
  <p>Notice that I am currently rendering a green brush on each screen.  This works perfectly in <em>THIS</em> context (i.e. within the callback).</p>
  
  <p>Now, the problem is, I am capturing those <code>HDC</code>s to use at a later time.</p>
  
  <p>So a couple of lines later, I'm iterating over my <code>dcs</code> vector:</p>
  
  <pre><code>for (HDC dc : dcs)
  {
      HBRUSH br = CreateSolidBrush(RGB(255, 255, 0));
  
      RECT x = { 100, 100, 500, 500 };        
  
      auto rst = FillRect(dc, &amp;x, br);
  
      printf("%d", rst);
  }
  </code></pre>
  
  <p>So, my questions are:</p>
  
  <ol>
  <li><p>for the <code>dcMain</code>, I have to pass this in, is this the good way to get one?</p></li>
  <li><p>why does the rendering work in the callback, but does not work when I capture the <code>HDC</code>s and iterate over them later?</p></li>
  </ol>