only LBS_OWNERDRAWVARIABLE CListBox can be moved(MoveWindow) as you will.

Discussion in 'MFC' started by 燕山剑, Jun 6, 2007.

  1. 燕山剑

    燕山剑 New Member

    Joined:
    May 31, 2007
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    爱国者同盟 Chinese Patriot League
    Home Page:
    http://projects.takingitglobal.org/tds
    CListBox with Font and Color can be moved(MoveWindow) as you will.

    Code:
    pAAList.Create(...|LBS_OWNERDRAWVARIABLE|LBS_HASSTRINGS...);
    //Or,it adjusts      its size automaticly when moved(MoveWindow) or font-changed.
    
    
    void CAAList::MeasureItem(LPMEASUREITEMSTRUCT)
    {
       ASSERT(lpMeasureItemStruct->CtlType == ODT_LISTBOX);
       LPCTSTR lpszText = (LPCTSTR) lpMeasureItemStruct->itemData;
       ASSERT(lpszText != NULL);
       CSize   sz;
       CDC*    pDC = GetDC();
    
       sz = pDC->GetTextExtent(lpszText);
    
       ReleaseDC(pDC);
    
       lpMeasureItemStruct->itemHeight = 2*sz.cy;
    }
    
    void CAAList::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
    {
    //CRect members to store the position of the items
    CRect rItem;
    CDC* dcBNJ = CDC::FromHandle(lpDrawItemStruct->hDC);
    dcBNJ->SetBkMode(TRANSPARENT);
    
    if ((int)lpDrawItemStruct->itemID < 0)
    {
    // If there are no elements in the CListBox
    // based on whether the list box has Focus  or not 
    // draw the Focus Rect or Erase it,
    if ((lpDrawItemStruct->itemAction & ODA_FOCUS) && (lpDrawItemStruct->itemState & ODS_FOCUS))
    {
        dcBNJ->DrawFocusRect(&lpDrawItemStruct->rcItem);
    }
    else if ((lpDrawItemStruct->itemAction & ODA_FOCUS) && !(lpDrawItemStruct->itemState & ODS_FOCUS)) 
    {
        dcBNJ->DrawFocusRect(&lpDrawItemStruct->rcItem); 
    }
       return;
    }
    
    
    // String to store the text
    CString strText;
    
    // Get the item text.
    GetText(lpDrawItemStruct->itemID, strText);
    
    //Initialize the CListBox Item's row size
    rItem = lpDrawItemStruct->rcItem;
    
    UINT nFormat = DT_LEFT | DT_SINGLELINE | DT_VCENTER;
    if (GetStyle() & LBS_USETABSTOPS)
    nFormat |= DT_EXPANDTABS;
    
    
    // If CListBox item selected, draw the highlight rectangle.
    // Or if CListBox item deselected, draw the rectangle using the window color.
    if ((lpDrawItemStruct->itemState & ODS_SELECTED) &&
    (lpDrawItemStruct->itemAction & (ODA_SELECT | ODA_DRAWENTIRE)))
    {
        CBrush br(::GetSysColor(COLOR_HIGHLIGHT));
        dcBNJ->FillRect(&rItem, &br);
    }
    else if (!(lpDrawItemStruct->itemState & ODS_SELECTED) && (lpDrawItemStruct->itemAction & ODA_SELECT)) 
    {
        CBrush br(m_compBase.GetBkColor());
        dcBNJ->FillRect(&rItem, &br);
    }
    
    // If the CListBox item has focus, draw the focus rect.
    // If the item does not have focus, erase the focus rect.
    if ((lpDrawItemStruct->itemAction & ODA_FOCUS) && (lpDrawItemStruct->itemState & ODS_FOCUS))
    {
        dcBNJ->DrawFocusRect(&rItem); 
    }
    else if ((lpDrawItemStruct->itemAction & ODA_FOCUS) && !(lpDrawItemStruct->itemState & ODS_FOCUS))
    {
        dcBNJ->DrawFocusRect(&rItem); 
    }
    
    dcBNJ->SetTextColor(m_compBase.GetFontColor());
    CFont *pFont = m_compBase.GetUFont(0,((CUFillerCtrl*)(dynamic_cast<CWnd*>(this)->GetParent()->GetParent()))->m_ihorzScale);
    dcBNJ->SelectObject(pFont);
    
    //Draw the Text
    dcBNJ->TextOut(rItem.left,rItem.top,strText);
    }
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice