GDI debugging
The other day I had to debug a hard to track drawing bug. The application is built with the MFC framework so it still uses GDI on places to draw custom controls.
The incorrect drawing artifact was displayed after an invocation of 'DrawText'. This was unexpected since the function only measured the size. It had the flag 'DT_CALCRECT' which does not draw to the screen. Eventually I realized that GDI batches calls so probably the buggy overdrawing had already taken place before. What was needed:
- suppress GDI's caching mechanism through 'GdiSetBatchLimit'.
- use direct drawing; so no memory device context
With this in place indeed it could be seen that the mistake happened earlier in the code and that the 'DrawText' invocation was merely a flush of the GDI batch.
Be aware that suppressing GDI's batch might not always work. When the window where the drawing took place was on the primary monitor the batch mode could be turned off but on the second monitor it still cached its calls.