Como solucionar problemas en ventana GDI en c++

Carlos Sottana 0 Reputation points
2025-07-17T15:12:27.8566667+00:00

Porque despues de esconder y mostrar varias veces una ventana GDI en c++ empieza a dibujar cualquier cosa o se pone gris la zona que estaba oculta, a pesar de cumplir con todas las condiciones de eliminar y borrar objetos (DeleteObjet, Delete DC y EndPaint) al salir de WS_PAINT. Tambien luego mover un objeto con movimientos del mouse, luego de un tiempo que WS_PAINT viene dibujando bien y es como si la ventana se bloqueara y ya no dibuja corretamente y en blanco y negro.

Developer technologies | C++
Developer technologies | C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Surya Amrutha Vaishnavi Lanka (INFOSYS LIMITED) 1,120 Reputation points Microsoft External Staff
    2025-11-11T10:12:50.1566667+00:00

    Thanks for reaching out!

    Here are some fixes you can try

    1.Paint only in WM_PAINT. In input handlers (mouse/timer), update state and call InvalidateRect

    2.Always pair APIs correctly.

    BeginPaint and EndPaint ,also GetDC and ReleaseDC,

    CreateCompatibleDC/CreatePen/CreateBrush/CreateFont and DeleteDC/DeleteObject.

    3.Never delete a selected object. Save the old object from SelectObject(hdc, newObj), then re-select it back before DeleteObject.

    4.Draw everything into the memory DC, then BitBlt it to the paint DC in WM_PAINT.

    Try these quick debugging steps

    1.Run with GDI object count visible. If it climbs continuously, you’re leaking.

    2.Temporarily assert that every SelectObject returns non-NULL and that you re-select the original object before DeleteObject. 

    3.Verify you never call DeleteDC on an HDC from BeginPaint/GetDC.

    4.If you ever see black/white frames: check where you call CreateCompatibleBitmap ensure it uses a screen DC.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.