title: How to smooth ugly jitter/flicker/jumping when resizing windows, especially dragging left/top border (Win 7-10; bg, bitblt and DWM)?
body: <p><strong>THE PROBLEM:</strong> When I grab the resize border of my Windows app, especially the top or left borders, and resize the window, the contents of the window do resize &quot;live&quot; as I drag, but they resize in a hideous manner that looks like a blatant bug to even the most novice user: the contents at the opposite edge of the window from the edge I am dragging jitter/flicker/jump back and forth wildly.  Depending on the situation, the phenomenon may look like:</p>
  <ul>
  <li>contents that seem to walk off the edge of the window and snap back when we slow down or stop dragging</li>
  <li>contents that seem to pull into the window, intermittently displaced by a border of varying colors, often black or white</li>
  <li>a seriously ugly &quot;double image&quot; with two overlapping copies of the content displaced by a distance proportional to how much/how fast we are dragging</li>
  </ul>
  <p>The ugly phenomenon stops as soon as I stop dragging, but during the dragging it makes the app look amateurish and unprofessional.</p>
  <p>It is not an understatement to say this Windows problem has <strong>driven thousands of app developers crazy</strong>.</p>
  <p>Here are two example pictures of the phenomenon, kindly prepared for <a href="https://stackoverflow.com/q/1382915">a related question</a> by <a href="https://stackoverflow.com/users/33080/roman-starkov">Roman Starkov</a>:</p>
  <p>Jitter:<br />
  <img src="https://i.stack.imgur.com/R2sLZ.gif" alt="example 1: jitter" /></p>
  <p>Border:<br />
  <img src="https://i.stack.imgur.com/0ihm4.png" alt="example 2: border" /></p>
  <p>Another example showing the evil &quot;double image&quot; phenomenon (note the quick flash) from <a href="https://stackoverflow.com/users/9610290/kenny-liu">Kenny Liu</a>:</p>
  <p><img src="https://i.stack.imgur.com/vVuO0.gif" alt="example 2: double-image" /></p>
  <p>Another example video of the phenomenon with Task Manager is <a href="https://www.videosprout.com/video?id=244938c2-134e-43c7-bd0c-b77a34f80977" rel="noreferrer">here</a>.</p>
  <p><strong>THE QUESTION:</strong>  Any developer who has experienced this problem quickly finds that there are at least 30 Stack Overflow questions, some recent and some dating from 2008, full of promising-sounding answers that rarely work.  The reality is that this one problem has <strong>many causes</strong>, and the existing Stack Overflow questions/answers never make the wider context clear.  This question seeks to answer:</p>
  <ul>
  <li>what are the most likely causes of this kind of ugly jitter/flicker/jumping?</li>
  <li>how do I tell which cause I am seeing?</li>
  <li>is this cause specific to particular graphics drivers or general for Windows?</li>
  <li>how do I fix each cause?  can an app fix it?</li>
  </ul>
  <p>(This is meant as a canonical Q&amp;A to explain all the different causes of window resize jitter so that users can identify which of the causes is causing their problem and solve it. As the answers explain, all the permutations above (native/managed, window/dialog, XP-10) boil down to only two root causes, but identifying which you have is the tricky part.)</p>
  <p><strong>SCOPE OF THIS QUESTION:</strong> For the scope of this question, the phenomenon happens with:</p>
  <ul>
  <li>both native Win32 and managed .NET/WPF/Windows Forms apps</li>
  <li>both normal Win32 windows and Win32 Dialog windows</li>
  <li>Windows versions including XP, Vista, 7, 8, and 10 (but see below for the dark truth of multiple causes)</li>
  </ul>
  <p><strong>NOT IN SCOPE OF THIS QUESTION:</strong></p>
  <ul>
  <li><p>If your app has one or more child windows (child HWNDs), the info in this question is useful to you (since the jerk-causing <code>BitBlts</code> we will describe are applied to your child windows along with the parent window), but during window resize you have an additional problem to handle that is beyond the scope of this question: you need to make all your child windows move atomically and in sync with the parent window.  For this task, you will probably want  <code>BeginDeferWindowPos/DeferWindowPos/EndDeferWindowPos</code> and you can find out about them <a href="https://stackoverflow.com/questions/2165759/how-do-i-force-windows-not-to-redraw-anything-in-my-dialog-when-the-user-is-resiz">here</a> and <a href="https://blogs.msdn.microsoft.com/oldnewthing/20050706-26/?p=35023" rel="noreferrer">here</a>.</p>
  </li>
  <li><p>This question assumes that if your app draws to a window using GDI, DirectX, or OpenGL, then you have already implemented a <code>WM_ERASEBKGND</code> handler in your <code>wndproc</code> that simply returns 1.  <code>WM_ERASEBKGND</code> is an arcane Windows remnant from Windows 3.1 that comes before <code>WM_PAINT</code> to give your app a chance to &quot;erase the background&quot; of your window before you draw your window...uh huh. If you let the <code>WM_ERASEBKGND</code> message go into <code>DefWindowProc()</code>, that will cause your entire window to get painted a solid color, usually white, on each redraw, including redraws that happen during live window resizing.  The result is an ugly full-window flicker that is gross, but not the type of jitter/flicker/jumping we are talking about in this question.  Intercepting <code>WM_ERASEBKGND</code> fixes this problem immediately.</p>
  </li>
  <li><p>This question is primarily about live-resize by dragging window borders with the mouse.  However, much of what is written here also applies to ugly artifacts you can see when an app manually does a one-time window resize using <code>SetWindowPos()</code>.  These are less visible though because they only flick on the screen for one instant, rather than over a long period of dragging.</p>
  </li>
  <li><p>This question is not about how to make your app-specific drawing code go faster, even though doing so may be a solution to the ugly resizing problem in many cases.  If your app really does take huge amounts of time to redisplay its contents during live window resize, consider optimizing your drawing code in general or at least switching to a faster, lower-quality drawing mode during resize by intercepting the <code>WM_ENTERSIZEMOVE/WM_EXITSIZEMOVE</code> messages to detect resize.</p>
  </li>
  <li><p>If your app fails to resize at all during app resizing (e.g. it &quot;hangs&quot; during resizing, especially if it is OpenGL using GLFW or other library), see these other questions which explain about Microsoft's hideous nested/modal event loop inside <code>WM_SYSCOMMAND</code> during dragging: <a href="https://stackoverflow.com/questions/18041622/how-do-i-stop-windows-from-blocking-the-program-during-a-window-drag-or-menu-but/18043461#18043461">here</a> especially <a href="https://stackoverflow.com/a/18043461/1046167">this good answer</a>, <a href="https://stackoverflow.com/questions/50828241/resize-a-window-on-windows-os-while-rendering">here</a>, <a href="https://stackoverflow.com/questions/6593014/how-to-draw-opengl-content-while-resizing-win32-window">here</a>, <a href="https://stackoverflow.com/questions/43478357/how-to-resize-a-glut-window">here</a>, and <a href="https://www.gamedev.net/forums/topic/488074-win32-message-pump-and-opengl---rendering-pauses-while-draggingresizing/?tab=comments#comment-4189300" rel="noreferrer">here</a>.</p>
  </li>
  </ul>