控件自绘之CRadioListBox

源代码及可执行文件下载地址:https://files.cnblogs.com/rainboy2010/CRadioListBox.zip

主要代码如下:

void CRadioListBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
{
     CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
     if (lpDrawItemStruct->itemID == (UINT)-1)
     {
          if (lpDrawItemStruct->itemAction & ODA_FOCUS) pDC->DrawFocusRect(&lpDrawItemStruct->rcItem);
          return;
     }
     else
     {
          int selChange   = lpDrawItemStruct->itemAction & ODA_SELECT;
          int focusChange = lpDrawItemStruct->itemAction & ODA_FOCUS;
          int drawEntire  = lpDrawItemStruct->itemAction & ODA_DRAWENTIRE;

           BOOL sel = lpDrawItemStruct->itemState & ODS_SELECTED;

          if (selChange || drawEntire) 
          {
               CString strText;
               GetText(lpDrawItemStruct->itemID,strText);

              
               pDC->FillSolidRect(&lpDrawItemStruct->rcItem, ::GetSysColor((GetExStyle()&WS_EX_TRANSPARENT)?COLOR_BTNFACE:COLOR_WINDOW));

               // Draw radio button
               int h = lpDrawItemStruct->rcItem.bottom - lpDrawItemStruct->rcItem.top;
               CRect rect(lpDrawItemStruct->rcItem.left+2, lpDrawItemStruct->rcItem.top+2, 
                lpDrawItemStruct->rcItem.left+h-3, lpDrawItemStruct->rcItem.top+h-3);
               pDC->DrawFrameControl(&rect, DFC_BUTTON, DFCS_BUTTONRADIO | (sel?DFCS_CHECKED:0));

       
               pDC->SetTextColor(COLOR_WINDOWTEXT);
               pDC->SetBkMode(TRANSPARENT);
               lpDrawItemStruct->rcItem.left += h;

               pDC->DrawText(strText, &lpDrawItemStruct->rcItem, DT_LEFT|DT_VCENTER|DT_SINGLELINE);

            
          }

          if (focusChange  && lpDrawItemStruct->itemState & ODS_FOCUS && sel)
          {
              pDC->DrawFocusRect(&lpDrawItemStruct->rcItem);
          }
         
     }
}

 

posted @ 2015-02-26 14:33  南极冰川雪  阅读(257)  评论(0)    收藏  举报