Hi all:

I have an application who uses some BS_OWNERDRAW style buttons, and in the dialog-procedure, some like this to draw them:

case WM_DRAWITEM:
{
LPDRAWITEMSTRUCT pdis = (LPDRAWITEMSTRUCT) lParam;
HICON hIcon = (HICON)LoadImage (mhok.hInstance, MAKEINTRESOURCE(idButton), IMAGE_ICON, 0,0, LR_DEFAULTCOLOR|LR_LOADTRANSPARENT);

DrawIconEx (pdis->hDC, 0, 0, hIcon, 0, 0, 0, NULL, DI_NORMAL);

// The doc. states: If an application processes this message, it should return TRUE.
return TRUE;
}

The problem is that after running a while, the application behaves badly.

After diggin a bit in Google -in the Forger's Win32 API Programming Tutorial- I have found: "If your program runs fine for a few minutes and then starts drawing strangely or not at all, it's a good sign that you're leaking GDI resources".

Besides that, referring to the WM_DRAWITEM message, the MS doc state that: "Before returning from processing this message, an application should ensure that the device context identified by the hDC member of the DRAWITEMSTRUCT structure is in the default state".

The question is: How can I ensure that the pdis->hDC member is in the default state?

May be including

RestoreDC (pdis->hDC, -1);

before the return?

Or perhaps

ReleaseDC(pdis->hwndItem, pdis->hDC);

in that place?

Any suggestion would be appreciated.