15 мая 2023 года "Исходники.РУ" отмечают своё 23-летие!
Поздравляем всех причастных и неравнодушных с этим событием!
И огромное спасибо всем, кто был и остаётся с нами все эти годы!

Главная Форум Журнал Wiki DRKB Discuz!ML Помощь проекту


Problem on expanding the height of the CHeaderCtrl

edwardlau@hactl.com.hk
Monday, July 29, 1996

     Environment: VC++ 4.0 under Win 95
     
     I have a subclassed CHeaderCtrlEn, which is derived from CHeaderCtrl, 
     with owner-drawn set. My purpose is to let the control to 'wrap' the 
     text when the divider has been moved and the resultant rectangle's 
     width is smaller than enough for occupying the text.What I have done 
     is after I have subclassed the control, I use a for loop to set each 
     item to be owner-drawn. Everything works fine except the height cannot 
     be expanded to what I expected. I have read the on-line help about the 
     DrawText and followed its instruction. I have included DT_WORDBREAK | 
     DT_CALCRECT as the format to let it calculated the RECT size first. It 
     saids it can automatically 'extend the base of the rectangle to bound 
     the last line of text', but I can't find any changes to the height. 
     (However, I can see that the text has been 'wrapped' though there is 
     not enough height for displaying)...
     
     By the way, I also cannot set the height even I specified HDI_HEIGHT 
     before calling SetItem. It always sets the width instead......
     
     Below is my codes :
     
     void CHeaderCtrlEn::SetOwnerDraw(BOOL bOwnerDraw)
     {
        // This function set the control to be owner-drawn
     
        TCHAR szBuffer[MAX_PATH];
        HD_ITEM hdi;
        
        for (int idx = 0; idx < GetItemCount(); idx++)
        {
            hdi.mask = HDI_FORMAT | HDI_HEIGHT | HDI_TEXT;
            hdi.pszText = szBuffer;
            hdi.cchTextMax = sizeof(szBuffer);
            GetItem(idx, &hdi);
            hdi.fmt |= HDF_OWNERDRAW;
            // For testing purpose, manually set the height.....
            // hdi.cxy += 12;
            SetItem(idx, &hdi);
        }
     }
     
     void CHeaderCtrlEn::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
     {
        CDC* pDC=CDC::FromHandle(lpDrawItemStruct->hDC);
        int nItem = lpDrawItemStruct->itemID;
        static _TCHAR szBuffer[MAX_PATH];
        HD_ITEM hdi;
        
        hdi.mask = HDI_TEXT;
        hdi.pszText = szBuffer;
        hdi.cchTextMax = sizeof(szBuffer);
        GetItem(nItem, &hdi);
     
        // The purpose of the below 4 lines were to keep the text
        // from being drawn on the frame line of the bounding rectangle
        lpDrawItemStruct->rcItem.top += 2;
        lpDrawItemStruct->rcItem.bottom -= 2;
        lpDrawItemStruct->rcItem.left += 2;
        lpDrawItemStruct->rcItem.right -= 2;
        
        int rcHeight = pDC->DrawText(hdi.pszText, -1,                       
                        &lpDrawItemStruct->rcItem, DT_NOCLIP | DT_NOPREFIX  
                        | DT_WORDBREAK | DT_CALCRECT); 
     
        // I have intentionally try to set the size, but with no success..
        //lpDrawItemStruct->rcItem.bottom = lpDrawItemStruct->rcItem.top + 
     rcHeight;
        pDC->DrawText(hdi.pszText, -1, &lpDrawItemStruct->rcItem, DT_NOCLIP 
                      | DT_NOPREFIX | DT_WORDBREAK); 
     }
     
     
     
     Does anyone encounter similar problem? Do I miss something important?
     
     Thanks in advance.......
     
     Edward, PEI, edwardlau@hactl.com.hk
     



Chad Yost -- cyost@research.westlaw.com
Thursday, August 01, 1996

edwardlau@hactl.com.hk wrote:
> 
>      Environment: VC++ 4.0 under Win 95

I looked at the MFC source, and noticed that when CDC::DrawText() is 
called, it never returns the calculated height.  It returns 0 unless 
DT_CALCRECT is not specified and the TextAlign attribute contains 
TA_UPDATECP.  (Is this right Microsoft!!)

I would suggest trying to call the Windows API DrawText directly, which 
does return the height of the rectangle, as suspected.

Chad




| Вернуться в корень Архива |