Example 2:
Drawing an Up/Down button on a dialog
LRESULT DrawUpDownButton (HWND hDlg, LPDRAWITEMSTRUCT lpdis) { int nCtrlId = lpdis->CtlID; int bmpId = 0; int nDraw = 0; int nIncDec = -1; int xPos = 0; int yPos = 0; int nImgSize = 2; RECT rct; // Set enable/disable bitmap for up/down buttons. switch (nCtrlId) { case IDC_LOG_UP: bmpId = IDB_UP_TOOLBAR; break; case IDC_LOG_DOWN: bmpId = IDB_DOWN_TOOLBAR; break; } // ODT_BUTTON => Owner-drawn button if (lpdis->CtlType == ODT_BUTTON) { SetRectEmpty (&rct); GetClientRect (GetDlgItem(hDlg, nCtrlId), &rct); if (lpdis->itemState & ODS_SELECTED) DrawFrameControl (lpdis->hDC, &rct, DFC_BUTTON, DFCS_BUTTONPUSH | DFCS_PUSHED); else DrawFrameControl (lpdis->hDC, &rct, DFC_BUTTON, DFCS_BUTTONPUSH); // This bit is set if the item has input focus. If (lpdis->itemState & ODS_FOCUS) { InflateRect (&rct, nIncDec, nIncDec); DrawFocusRect (lpdis->hDC, &rct); InflateRect (&rct, nIncDec, nIncDec); } else InflateRect (&rct, -2, -2); Rectangle (lpdis->hDC, rct.left, rct.top, rct.right, rct.bottom); InflateRect (&rct, -1, -1); // Before returning from processing WM_DRAWITEM message, // an application should ensure that the device context // identified by the hDC member of the DRAWITEMSTRUCT // structure is in the default state. int savedDC = SaveDC (lpdis->hDC); if (lpdis->itemState == ODS_DISABLED) nDraw = 1; xPos = (rct.right - rct.left)/2 - 3; yPos = (rct.bottom - rct.top)/2 - 3; if ((nCtrlId==IDC_LOG_UP)||(nCtrlId==IDC_LOG_DOWN) { g_hCLogBitMap = ImageList_LoadBitmap ( g_hResInst, MAKEINTRESOURCE (bmpId), 16, 2, RGB(255, 255, 255)); ImageList_Draw (g_hCLogBitMap, nDraw, lpdis->hDC, xPos, yPos, ILD_NORMAL); // Destroying the ImageList right away seems // to help the memory leak a lot. ImageList_Remove (g_hCLogBitMap, 0); ImageList_Destroy (g_hCLogBitMap); } // restoring the DC. RestoreDC (lpdis->hDC, savedDC); } return TRUE; }