Copying from a comment, I did not answer this as I can’t check, and I was not sure if this is correct, please test it.
In general, it's nice to have nested calls, i.e.
HDC hDC1 = GetDC();
HDC hDC2 = CreateCompatibleDC(hDC1);
..
instead
HDC hDC = CreateCompatibleDC(GetDC());
(By the way, in your code, the HDC returned by GetDC is not released.)
source
share