Wing Ding symbol in a CListCtrl.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19

Thread: Wing Ding symbol in a CListCtrl.

  1. #1
    Join Date
    May 2000
    Location
    Southern Germany
    Posts
    213

    Wing Ding symbol in a CListCtrl.

    I have already posted this several times with no response, so I am going to try one more time.
    I am trying to display checkmark characters from the WingDings font in a CListCtrl column.
    First I create a Font, using SYMBOL_CHARSET and "Wingdings".

    I can get this to work in CView with the following code:

    CFont f;
    f.CreateFont(....,SYMBOL_CHARSET,...,"Wingdings");
    pDC->SelectObject(&f)
    pDC->TextOut("ü");//prints a checkmark



    In the CListCtrl class,however, I used the following:
    CFont f;
    f.CreateFont(....,SYMBOL_CHARSET,...,"Wingdings");
    SetFont(&f);
    SetItemText(1, 1, "ü");
    //prints a "ü", not a checkmark




    Does anyone know why this doesn't work and how I can solve it?
    I'm sure somebody must know.
    Thank you and I always rate my replies.












  2. #2
    Join Date
    May 1999
    Location
    West Sussex, England
    Posts
    1,939

    Re: Wing Ding symbol in a CListCtrl.

    The problem is due to the CFont objects scope. At the end of the procedure the CFont object gets destroyed, destorying the font you selected in the the CListCtrl at the same time. The CListCtrl then defaults back to the default font.

    Make the CFont object part of the controlling class for the dialog / window that owns the CListCtrl. Then the CFont will be in existance for the whole lifetime of the CListCtrl window. Allowing it to draw correctly.


    Roger Allen
    Roger.Allen@<a rel="nofollow" href="...ytical.com</a>
    Ok, Points make prizes, and ratings make points.
    Did I help?
    Please use meaningful question titles - "Help me" does not let me know whether I can help with your question, and I am unlikely to bother reading it.
    Please remember to rate useful answers. It lets us know when a question has been answered.

  3. #3
    igbrus is offline Elite Member Power Poster
    Join Date
    Aug 2000
    Location
    Los Angeles
    Posts
    4,658

    Re: Wing Ding symbol in a CListCtrl.

    You can override OnDrawItem and will be the same effect as for CView


  4. #4
    Join Date
    May 2000
    Location
    Southern Germany
    Posts
    213

    Re: Wing Ding symbol in a CListCtrl.

    That was the major part of the problem.
    The next problem is that I only need this font for several columns.
    The return value for CListCtrl::SetFont is BOOL, not the old font.
    So how do I get the System Font back?
    Is there some method like GetSystemFont? (I couldn't find this)
    Otherwise my entire ListCtrl is with Wingdings.


  5. #5
    Join Date
    May 1999
    Location
    West Sussex, England
    Posts
    1,939

    Re: Wing Ding symbol in a CListCtrl.

    What you need to do is a bit more difficult. To support more than 1 font in the CListCtrl, you will have to make it ownerdrawn. Then you need to have some way of determining which font you need to draw the text in.

    You need to write your own control that inherits from CListCtrl. Override the DrawItem() function as follows. Also add a member variable that will hold the CFont object with the wingdings font created in it (in this example m_cfWingDing).

    When you need to display text in wingdings, you prepend the text with a flag such as "&lt;WD&gt;". If this is found in the string during owner draw, then the control will switch fonts.

    Here is some code that should do the drawing for you:


    void CMyListCtrl:rawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
    {
    CDC *pDC = CDC::FromHandle(lpDrawItemStruct-&gt;hDC);
    CRect rcItem(lpDrawItemStruct-&gt;rcItem);
    int nItem = lpDrawItemStruct-&gt;itemID;
    CImageList *pImageList; // Save dc state
    int nImage;
    int nSavedDC = pDC-&gt;SaveDC();

    // Get item image and state info
    LV_ITEM lvi;
    lvi.mask = LVIF_IMAGE | LVIF_STATE;
    lvi.iItem = nItem;
    lvi.iSubItem = 0;
    lvi.stateMask = 0xFFFF; // get all state flags
    GetItem(&lvi);

    nImage = ((lvi.state & LVIS_STATEIMAGEMASK)&gt;&gt;12) - 1;

    // Should the item be highlighted
    BOOL bHighlight = ((lvi.state & LVIS_DROPHILITED) ||
    ((lvi.state & LVIS_SELECTED) && (GetFocus() == this || (GetStyle() & LVS_SHOWSELALWAYS))));

    // Get rectangles for drawing
    CRect rcBounds, rcLabel, rcIcon;
    GetItemRect(nItem, rcBounds, LVIR_BOUNDS);
    GetItemRect(nItem, rcLabel, LVIR_LABEL);
    GetItemRect(nItem, rcIcon, LVIR_ICON);
    CRect rcCol(rcBounds);

    CString sLabel = GetItemText(nItem, 0);

    // Labels are offset by a certain amount
    // This offset is related to the width of a space character
    int offset = pDC-&gt;GetTextExtent(_T(" "), 1 ).cx*2;
    CRect rcHighlight;
    CRect rcWnd;
    int nExt;

    switch(m_nHighlight)
    {
    case 0:
    nExt = pDC-&gt;GetOutputTextExtent(sLabel).cx + offset;
    rcHighlight = rcLabel;
    if (rcLabel.left + nExt &lt; rcLabel.right)
    rcHighlight.right = rcLabel.left + nExt;
    break;
    case 1:
    rcHighlight = rcBounds;
    rcHighlight.left = rcLabel.left;
    break;
    case 2:
    GetClientRect(&rcWnd);
    rcHighlight = rcBounds;
    rcHighlight.left = rcLabel.left;
    rcHighlight.right = rcWnd.right;
    break;
    default:
    rcHighlight = rcLabel;
    }

    // Draw the background color
    CBrush *brush = NULL;

    if (bHighlight)
    {
    if (GetFocus() == this)
    {
    if (GetItemData(nItem) && !m_bBoldTitles)
    {
    COLORREF col = ::GetSysColor(COLOR_HIGHLIGHT);

    if (GetItemData(nItem) & LSTCTRL_ERROR)
    {
    col |= RGB(255,0,0);
    }
    if (GetItemData(nItem) & LSTCTRL_MARK)
    {
    col |= RGB(0,0,128);
    }

    brush = new CBrush(col);
    pDC-&gt;SetTextColor(((col &gt;&gt; 16) + ((col &gt;&gt; 8) & 0xFF) + (col & 0xFF)) / 3 &gt; 0x7F ? RGB(0,0,0) : RGB(255,255,255));
    pDC-&gt;SetBkColor(col);
    pDC-&gt;FillRect(rcHighlight, brush);
    }
    else
    {
    brush = new CBrush(::GetSysColor(COLOR_HIGHLIGHT));
    pDC-&gt;SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
    pDC-&gt;SetBkColor(::GetSysColor(COLOR_HIGHLIGHT));
    pDC-&gt;FillRect(rcHighlight, brush);
    }
    }
    else
    {
    if (GetItemData(nItem) & LSTCTRL_ERROR && !m_bBoldTitles)
    {
    COLORREF col = RGB(255,0,0) | ::GetSysColor(COLOR_MENU);

    brush = new CBrush(col);
    pDC-&gt;SetTextColor(((col &gt;&gt; 16) + ((col &gt;&gt; 8) & 0xFF) + (col & 0xFF)) / 3 &gt; 0x7F ? RGB(0,0,0) : RGB(255,255,255));
    pDC-&gt;SetBkColor(col);
    pDC-&gt;FillRect(rcHighlight, brush);
    }
    else
    {
    brush = new CBrush(::GetSysColor(COLOR_MENU));
    pDC-&gt;SetTextColor(::GetSysColor(COLOR_MENUTEXT));
    pDC-&gt;SetBkColor(::GetSysColor(COLOR_MENU));
    pDC-&gt;FillRect(rcHighlight, brush);
    }
    }
    }
    else
    {
    if (GetItemData(nItem) && !m_bBoldTitles)
    {
    COLORREF col = RGB(0,0,0);

    if (GetItemData(nItem) & LSTCTRL_ERROR)
    {
    col |= RGB(255,0,0);
    }
    if (GetItemData(nItem) & LSTCTRL_MARK)
    {
    col |= RGB(0,0,128);
    }

    brush = new CBrush(col);
    pDC-&gt;SetTextColor(RGB(255,255,255));
    pDC-&gt;SetBkColor(RGB(255,0,0));
    pDC-&gt;FillRect(rcHighlight, brush);
    }
    else
    {
    brush = new CBrush(::GetSysColor(COLOR_WINDOW));
    pDC-&gt;FillRect(rcHighlight, brush);
    }
    }

    if (brush != NULL)
    {
    delete brush;
    }

    // Set clip region
    rcCol.right = rcCol.left + GetColumnWidth(0);
    CRgn rgn;
    rgn.CreateRectRgnIndirect(&rcCol);
    pDC-&gt;SelectClipRgn(&rgn);
    rgn.DeleteObject(); // Draw state icon

    if (lvi.state & LVIS_STATEIMAGEMASK)
    {
    pImageList = GetImageList(LVSIL_STATE);

    if (pImageList)
    {
    pImageList-&gt;Draw(pDC, nImage, CPoint(rcCol.left, rcCol.top), ILD_TRANSPARENT);
    }
    }

    // Draw normal and overlay icon
    pImageList = GetImageList(LVSIL_SMALL);

    if (pImageList)
    {
    UINT nOvlImageMask=lvi.state & LVIS_OVERLAYMASK;
    pImageList-&gt;Draw(pDC, lvi.iImage, CPoint(rcIcon.left, rcIcon.top),
    (bHighlight && GetFocus() == this ? ILD_BLEND50 : 0) | ILD_TRANSPARENT | nOvlImageMask);
    }

    // Draw item label - Column 0
    rcLabel.left += offset/2;
    rcLabel.right -= offset;
    if (sLabel.Find("&lt;WD&gt;") == 0)
    {
    // switch to the wing dings font
    pDC-&gt;SelectObject(&m_cfWingDing) ;
    sLabel = sLabel.Right(sLabel.GetLength() - 4) ; // lose "&lt;WD&gt;"
    pDC-&gt;SaveDC() ;
    pDC-&gt;DrawText(sLabel, -1, rcLabel, DT_LEFT | DT_SINGLELINE | DT_NOPREFIX | DT_NOCLIP | DT_VCENTER | DT_END_ELLIPSIS); // Draw labels for remaining columns
    pDC-&gt;RestoreDC(-1) ;
    }
    else
    {
    // use the regular font
    pDC-&gt;DrawText(sLabel, -1, rcLabel, DT_LEFT | DT_SINGLELINE | DT_NOPREFIX | DT_NOCLIP | DT_VCENTER | DT_END_ELLIPSIS); // Draw labels for remaining columns
    }
    LV_COLUMN lvc;
    lvc.mask = LVCF_FMT | LVCF_WIDTH;

    if (m_nHighlight == 0) // Highlight only first column
    {
    pDC-&gt;SetTextColor(::GetSysColor(COLOR_WINDOWTEXT));
    pDC-&gt;SetBkColor(::GetSysColor(COLOR_WINDOW));
    }
    rcBounds.right = rcHighlight.right &gt; rcBounds.right ? rcHighlight.right : rcBounds.right;
    rgn.CreateRectRgnIndirect(&rcBounds);

    pDC-&gt;SelectClipRgn(&rgn);

    for (int nSubItem = 1; GetColumn(nSubItem, &lvc); nSubItem++)
    {
    rcCol.left = rcCol.right;
    rcCol.right += lvc.cx;

    // Draw the background if needed
    brush = new CBrush(::GetSysColor(COLOR_WINDOW));
    if (m_nHighlight == HIGHLIGHT_NORMAL)
    {
    pDC-&gt;FillRect(rcCol, brush);
    }
    if (brush != NULL)
    {
    delete brush;
    }

    sLabel = GetItemText(nItem, nSubItem);

    if (sLabel.GetLength() == 0)
    {
    continue; // Get the text justification
    }

    UINT nJustify = DT_LEFT;

    switch(lvc.fmt & LVCFMT_JUSTIFYMASK)
    {
    case LVCFMT_RIGHT:
    nJustify = DT_RIGHT;
    break;
    case LVCFMT_CENTER:
    nJustify = DT_CENTER;
    break;
    default:
    break;
    }

    rcLabel = rcCol;
    rcLabel.left += offset;
    rcLabel.right -= offset;

    if (sLabel.Find("&lt;WD&gt;") == 0)
    {
    // switch to the wing dings font
    pDC-&gt;SelectObject(&m_cfWingDing) ;
    sLabel = sLabel.Right(sLabel.GetLength() - 4) ; // lose "&lt;WD&gt;"
    pDC-&gt;SaveDC() ;
    pDC-&gt;DrawText(sLabel, -1, rcLabel, nJustify | DT_SINGLELINE | DT_NOPREFIX | DT_VCENTER | DT_END_ELLIPSIS);
    pDC-&gt;RestoreDC(-1) ;
    }
    else
    {
    // use the regular font
    pDC-&gt;DrawText(sLabel, -1, rcLabel, nJustify | DT_SINGLELINE | DT_NOPREFIX | DT_VCENTER | DT_END_ELLIPSIS);
    }
    }

    // Draw focus rectangle if item has focus
    if (lvi.state & LVIS_FOCUSED && (GetFocus() == this))
    {
    pDC-&gt;DrawFocusRect(rcHighlight); // Restore dc
    }

    pDC-&gt;RestoreDC(nSavedDC);
    }




    Hope this helps



    Roger Allen
    Roger.Allen@<a rel="nofollow" href="...ytical.com</a>
    Ok, Points make prizes, and ratings make points.
    Did I help?
    Please use meaningful question titles - "Help me" does not let me know whether I can help with your question, and I am unlikely to bother reading it.
    Please remember to rate useful answers. It lets us know when a question has been answered.

  6. #6
    Join Date
    May 2000
    Location
    Southern Germany
    Posts
    213

    Re: Wing Ding symbol in a CListCtrl.

    Thanks for a very thorough answer, I added it to my CListCtrl, but the problem is that DrawItem is never called.(I tested it with break points, messageboxes, etc.)
    Any ideas?


  7. #7
    Join Date
    May 1999
    Location
    West Sussex, England
    Posts
    1,939

    Re: Wing Ding symbol in a CListCtrl.

    Is the control ownerdraw in the resource template? And is the control be subclassed properly to the CMyListCtrl object ?


    Roger Allen
    Roger.Allen@<a rel="nofollow" href="...ytical.com</a>
    Ok, Points make prizes, and ratings make points.
    Did I help?
    Please use meaningful question titles - "Help me" does not let me know whether I can help with your question, and I am unlikely to bother reading it.
    Please remember to rate useful answers. It lets us know when a question has been answered.

  8. #8
    Join Date
    May 2000
    Location
    Southern Germany
    Posts
    213

    Re: Wing Ding symbol in a CListCtrl.

    I set the Control to Owner draw and now the function is called.
    I have several questions about your code, however:

    m_nHighlight?
    m_bBoldTitles?
    I understand that these are member variables, but I don't now what they refer to or where/what I should set them to.

    LSTCTRL_ERROR?
    LSTCTRL_MARK?
    HIGHLIGHT_NORMAL?
    What are they and where are they defined?

    Also, is there anything in your code that I can delete? I don't have colors or bold textin my control.
    I tried deleting everything except the part with the Fonts, but in this case, all the text is in Wingdings.

    Thanks.





  9. #9
    Join Date
    May 1999
    Location
    West Sussex, England
    Posts
    1,939

    Re: Wing Ding symbol in a CListCtrl.

    The code I gave you was stripped directly from one of my own owner drawn list controls. The missing definitions are:

    m_nHighlight - this determines how the line is hilighted
    m_bBoldTitles - If the data set for an item is &lt; 0, then a bold font is used to display the text

    LSTCTRL_ERROR, LSTCTRL_MARK, HIGHLIGHT_NORMAL - enums defined in the header.

    I will send the actual .h/.cpp files to you directly





    Roger Allen
    Roger.Allen@<a rel="nofollow" href="...ytical.com</a>
    Ok, Points make prizes, and ratings make points.
    Did I help?
    Please use meaningful question titles - "Help me" does not let me know whether I can help with your question, and I am unlikely to bother reading it.
    Please remember to rate useful answers. It lets us know when a question has been answered.

  10. #10
    Join Date
    May 2000
    Location
    Southern Germany
    Posts
    213

    Re: Wing Ding symbol in a CListCtrl.

    I almost got it working. I made one change to your code (below). I added a member variable, pOldfont and a flag bWD. Otherwise, it didn't switch back to the regular font after being switched to wingdings.

    Now I have the correct fonts, but I am no longer able to select anything. The only part of the code I really need is that below, the rest should be default.I am still not sure what your code is doing in a lot of places. Is there any way to simplify it? Or do you know where I can read more about DrawItem?



    if (sLabel.Find("&lt;WD&gt;") == 0)
    {
    // switch to the wing dings font
    pOldFont = pDC-&gt;SelectObject(&m_wdFont) ;
    bWD = TRUE;
    sLabel = sLabel.Right(sLabel.GetLength() - 4) ;// lose "&lt;WD&gt;"
    pDC-&gt;SaveDC()
    pDC-&gt;DrawText(sLabel, -1, rcLabel, DT_SINGLELINE | DT_NOPREFIX | DT_VCENTER | DT_END_ELLIPSIS);
    pDC-&gt;RestoreDC(-1) ;
    }
    else
    {
    // use the regular font
    if (bWD) pDC-&gt;SelectObject(pOldFont);
    pDC-&gt;DrawText(sLabel, -1, rcLabel, DT_SINGLELINE | DT_NOPREFIX | DT_VCENTER | DT_END_ELLIPSIS);
    }



    I also sent you several e-mails, but we were having problems with our server so I don't know if you got them. You can answer me here.

    Thanks




  11. #11
    Join Date
    May 1999
    Location
    West Sussex, England
    Posts
    1,939

    Re: Wing Ding symbol in a CListCtrl.

    I cut down the source code I sent you. I didn;t get to compile it, but it will probably work without little modification.


    .h file

    #if !defined(AFX_MYLISTCTRL_H__A9387DB2_56AE_11D2_AFD4_0000216D30F3__INCLUDED_)
    #define AFX_MYLISTCTRL_H__A9387DB2_56AE_11D2_AFD4_0000216D30F3__INCLUDED_

    #if _MSC_VER &gt;= 1000
    #pragma once
    #endif // _MSC_VER &gt;= 1000
    // MyListCtrl.h : header file
    //

    /////////////////////////////////////////////////////////////////////////////
    // CMyListCtrl window
    class CMyListCtrl : public CListCtrl
    {
    // Construction
    public:
    CMyListCtrl();

    // Attributes
    public:
    // Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CMyListCtrl)
    //}}AFX_VIRTUAL
    CFont m_WingDing ;
    bool m_font_created ;
    // Implementation
    public:
    virtual ~CMyListCtrl();

    // Generated message map functions
    protected:
    void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
    //{{AFX_MSG(CMyListCtrl)
    afx_msg void OnPaint();
    afx_msg void OnKillFocus(CWnd* pNewWnd);
    afx_msg void OnSetFocus(CWnd* pOldWnd);
    //}}AFX_MSG

    DECLARE_MESSAGE_MAP()
    };

    /////////////////////////////////////////////////////////////////////////////

    //{{AFX_INSERT_LOCATION}}
    // Microsoft Developer Studio will insert additional declarations immediately before the previous line.

    #endif // !defined(AFX_MYLISTCTRL_H__A9387DB2_56AE_11D2_AFD4_0000216D30F3__INCLUDED_)




    .cpp file

    // MyListCtrl.cpp : implementation file
    //

    #include &lt;stdafx.h&gt;
    #include &lt;..\src\afximpl.h&gt; //for afxData

    #include "MyListCtrl.h"

    //#ifdef _DEBUG
    //#define new DEBUG_NEW
    //#undef THIS_FILE
    //static char THIS_FILE[] = __FILE__;
    //#endif

    /////////////////////////////////////////////////////////////////////////////
    // CMyListCtrl

    CMyListCtrl::CMyListCtrl()
    {
    m_font_created = false ;
    }

    CMyListCtrl::~CMyListCtrl()
    {
    if (m_font_created)
    m_WingDing.DeleteObject() ;
    }

    BEGIN_MESSAGE_MAP(CMyListCtrl, CListCtrl)
    //{{AFX_MSG_MAP(CMyListCtrl)
    ON_WM_PAINT()
    ON_WM_KILLFOCUS()
    ON_WM_SETFOCUS()
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()


    /////////////////////////////////////////////////////////////////////////////
    // CMyListCtrl operations

    void CMyListCtrl:rawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
    {
    // TODO: Add your message handler code here and/or call default
    CDC *pDC = CDC::FromHandle(lpDrawItemStruct-&gt;hDC);
    CRect rcItem(lpDrawItemStruct-&gt;rcItem);
    int nItem = lpDrawItemStruct-&gt;itemID;
    CImageList *pImageList; // Save dc state
    int nImage;
    int nSavedDC = pDC-&gt;SaveDC();

    if (!m_font_created)
    {
    // need to create the wing ding font
    LOGFONT lf ;
    GetFont()-&gt;GetLogFont(&lf) ; // get current font settings
    strcpy(lf.lfFaceName, "WingDings") ; // switch to wing dings
    m_WingDing.CreateFontIndirect(&lf) ;
    m_font_created = true ;
    }

    // Get item image and state info
    LV_ITEM lvi;
    lvi.mask = LVIF_IMAGE | LVIF_STATE;
    lvi.iItem = nItem;
    lvi.iSubItem = 0;
    lvi.stateMask = 0xFFFF; // get all state flags
    GetItem(&lvi);

    nImage = ((lvi.state & LVIS_STATEIMAGEMASK)&gt;&gt;12) - 1;

    // Should the item be highlighted
    BOOL bHighlight = ((lvi.state & LVIS_DROPHILITED) ||
    ((lvi.state & LVIS_SELECTED) && (GetFocus() == this || (GetStyle() & LVS_SHOWSELALWAYS))));

    // Get rectangles for drawing
    CRect rcBounds, rcLabel, rcIcon;
    GetItemRect(nItem, rcBounds, LVIR_BOUNDS);
    GetItemRect(nItem, rcLabel, LVIR_LABEL);
    GetItemRect(nItem, rcIcon, LVIR_ICON);
    CRect rcCol(rcBounds);

    CString sLabel = GetItemText(nItem, 0);

    // Labels are offset by a certain amount
    // This offset is related to the width of a space character
    int offset = pDC-&gt;GetTextExtent(_T(" "), 1 ).cx*2;
    CRect rcHighlight;
    CRect rcWnd;
    int nExt;

    switch(m_nHighlight)
    {
    case 0:
    nExt = pDC-&gt;GetOutputTextExtent(sLabel).cx + offset;
    rcHighlight = rcLabel;
    if (rcLabel.left + nExt &lt; rcLabel.right)
    rcHighlight.right = rcLabel.left + nExt;
    break;
    case 1:
    rcHighlight = rcBounds;
    rcHighlight.left = rcLabel.left;
    break;
    case 2:
    GetClientRect(&rcWnd);
    rcHighlight = rcBounds;
    rcHighlight.left = rcLabel.left;
    rcHighlight.right = rcWnd.right;
    break;
    default:
    rcHighlight = rcLabel;
    }

    // Draw the background color
    CBrush *brush = NULL;

    if (bHighlight)
    {
    if (GetFocus() == this)
    {
    brush = new CBrush(::GetSysColor(COLOR_HIGHLIGHT));
    pDC-&gt;SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
    pDC-&gt;SetBkColor(::GetSysColor(COLOR_HIGHLIGHT));
    pDC-&gt;FillRect(rcHighlight, brush);
    }
    else
    {
    brush = new CBrush(::GetSysColor(COLOR_MENU));
    pDC-&gt;SetTextColor(::GetSysColor(COLOR_MENUTEXT));
    pDC-&gt;SetBkColor(::GetSysColor(COLOR_MENU));
    pDC-&gt;FillRect(rcHighlight, brush);
    }
    }
    else
    {
    brush = new CBrush(::GetSysColor(COLOR_WINDOW));
    pDC-&gt;FillRect(rcHighlight, brush);
    }

    if (brush != NULL)
    {
    delete brush;
    }

    // Set clip region
    rcCol.right = rcCol.left + GetColumnWidth(0);
    CRgn rgn;
    rgn.CreateRectRgnIndirect(&rcCol);
    pDC-&gt;SelectClipRgn(&rgn);
    rgn.DeleteObject(); // Draw state icon

    if (lvi.state & LVIS_STATEIMAGEMASK)
    {
    pImageList = GetImageList(LVSIL_STATE);

    if (pImageList)
    {
    pImageList-&gt;Draw(pDC, nImage, CPoint(rcCol.left, rcCol.top), ILD_TRANSPARENT);
    }
    }

    // Draw normal and overlay icon
    pImageList = GetImageList(LVSIL_SMALL);

    if (pImageList)
    {
    UINT nOvlImageMask=lvi.state & LVIS_OVERLAYMASK;
    pImageList-&gt;Draw(pDC, lvi.iImage, CPoint(rcIcon.left, rcIcon.top),
    (bHighlight && GetFocus() == this ? ILD_BLEND50 : 0) | ILD_TRANSPARENT | nOvlImageMask);
    }

    // Draw item label - Column 0
    rcLabel.left += offset/2;
    rcLabel.right -= offset;
    if (sLabel.Find("&lt;WD&gt;") == 0)
    {
    // this is wing ding font output
    CFont *pOldFont = pDC-&gt;SelectObject(&m_WingDing) ;
    sLabel = sLabel.Right(sLabel.GetLength() - 4) ; // lose "&lt;WD&gt;" from string
    pDC-&gt;DrawText(sLabel, -1, rcLabel, DT_LEFT | DT_SINGLELINE | DT_NOPREFIX | DT_NOCLIP | DT_VCENTER | DT_END_ELLIPSIS); // Draw labels for remaining columns
    pDC-&gt;SelectObject(pOldFont) ;
    }
    else // draw normal text
    pDC-&gt;DrawText(sLabel, -1, rcLabel, DT_LEFT | DT_SINGLELINE | DT_NOPREFIX | DT_NOCLIP | DT_VCENTER | DT_END_ELLIPSIS); // Draw labels for remaining columns
    LV_COLUMN lvc;
    lvc.mask = LVCF_FMT | LVCF_WIDTH;

    if (m_nHighlight == 0) // Highlight only first column
    {
    pDC-&gt;SetTextColor(::GetSysColor(COLOR_WINDOWTEXT));
    pDC-&gt;SetBkColor(::GetSysColor(COLOR_WINDOW));
    }
    rcBounds.right = rcHighlight.right &gt; rcBounds.right ? rcHighlight.right : rcBounds.right;
    rgn.CreateRectRgnIndirect(&rcBounds);

    pDC-&gt;SelectClipRgn(&rgn);

    for (int nSubItem = 1; GetColumn(nSubItem, &lvc); nSubItem++)
    {
    rcCol.left = rcCol.right;
    rcCol.right += lvc.cx;

    // Draw the background if needed
    brush = new CBrush(::GetSysColor(COLOR_WINDOW));
    if (m_nHighlight == HIGHLIGHT_NORMAL)
    {
    pDC-&gt;FillRect(rcCol, brush);
    }
    if (brush != NULL)
    {
    delete brush;
    }

    sLabel = GetItemText(nItem, nSubItem);

    if (sLabel.GetLength() == 0)
    {
    continue; // Get the text justification
    }

    UINT nJustify = DT_LEFT;

    switch(lvc.fmt & LVCFMT_JUSTIFYMASK)
    {
    case LVCFMT_RIGHT:
    nJustify = DT_RIGHT;
    break;
    case LVCFMT_CENTER:
    nJustify = DT_CENTER;
    break;
    default:
    break;
    }

    rcLabel = rcCol;
    rcLabel.left += offset;
    rcLabel.right -= offset;
    pDC-&gt;DrawText(sLabel, -1, rcLabel, nJustify | DT_SINGLELINE | DT_NOPREFIX | DT_VCENTER | DT_END_ELLIPSIS);
    }

    // Draw focus rectangle if item has focus
    if (lvi.state & LVIS_FOCUSED && (GetFocus() == this))
    {
    pDC-&gt;DrawFocusRect(rcHighlight); // Restore dc
    }

    pDC-&gt;RestoreDC(nSavedDC);
    }

    void CMyListCtrl::RepaintSelectedItems()
    {
    CRect rcBounds, rcLabel; // Invalidate focused item so it can repaint
    int nItem = GetNextItem(-1, LVNI_FOCUSED);
    if(nItem != -1)
    {
    GetItemRect(nItem, rcBounds, LVIR_BOUNDS);
    GetItemRect(nItem, rcLabel, LVIR_LABEL);
    rcBounds.left = rcLabel.left;
    InvalidateRect(rcBounds, FALSE);
    }
    // Invalidate selected items depending on LVS_SHOWSELALWAYS
    if(!(GetStyle() & LVS_SHOWSELALWAYS))
    {
    for(nItem = GetNextItem(-1, LVNI_SELECTED); nItem != -1; nItem = GetNextItem(nItem, LVNI_SELECTED))
    {
    GetItemRect(nItem, rcBounds, LVIR_BOUNDS);
    GetItemRect(nItem, rcLabel, LVIR_LABEL);
    rcBounds.left = rcLabel.left;
    InvalidateRect(rcBounds, FALSE);
    }
    }
    UpdateWindow();
    }

    void CMyListCtrl::OnKillFocus(CWnd* pNewWnd)
    {
    CListCtrl::OnKillFocus(pNewWnd);

    // check if we are losing focus to label edit box
    if(pNewWnd != NULL && pNewWnd-&gt;GetParent() == this)
    return;
    // repaint items that should change appearance
    if((GetStyle() & LVS_TYPEMASK) == LVS_REPORT)
    RepaintSelectedItems();
    }

    void CMyListCtrl::OnSetFocus(CWnd* pOldWnd)
    {
    if (pOldWnd != NULL && ::IsWindow(pOldWnd-&gt;m_hWnd))
    {
    CListCtrl::OnSetFocus(pOldWnd);

    // check if we are getting focus from label edit box
    if(pOldWnd!=NULL && pOldWnd-&gt;GetParent()==this)
    return;
    }

    // repaint items that should change appearance
    if((GetStyle() & LVS_TYPEMASK)==LVS_REPORT)
    RepaintSelectedItems();
    }






    Roger Allen
    Roger.Allen@<a rel="nofollow" href="...ytical.com</a>
    Ok, Points make prizes, and ratings make points.
    Did I help?
    Please use meaningful question titles - "Help me" does not let me know whether I can help with your question, and I am unlikely to bother reading it.
    Please remember to rate useful answers. It lets us know when a question has been answered.

  12. #12
    Join Date
    May 2000
    Location
    Southern Germany
    Posts
    213

    Re: Wing Ding symbol in a CListCtrl.

    whew! This is much more complicated than I thought.Thanks for taking so much time for me.

    I am still not sure what to do with m_nHighlight.
    Should it be a member variable? When is it assigned a value.

    Also, HIGHLIGHT_NORMAL. Where is that defined? Where is this value assigned?

    I still cannot highlight anything except in the first column, which is also painted black.

    The fonts are correct.


  13. #13
    Join Date
    May 2000
    Location
    Southern Germany
    Posts
    213

    Re: Wing Ding symbol in a CListCtrl.

    I set HIGHLIGHT_NORMAL = 2 and m_nHighlight = 1.
    Now the rows are correctly highlighted.
    The problem I now have is that the WingDing font only works in the first column.
    I'll keep looking.


  14. #14
    Join Date
    Oct 1999
    Location
    Broomfield, CO
    Posts
    3,382

    Re: Wing Ding symbol in a CListCtrl.

    Custom Draw might be a lot easier in the list control instead of doing Owner Draw.

    See the "Custom Draw Reference" section of MSDN and this post:
    http://codeguru.earthweb.com/bbs/wt/...c&Number=87810

    The post shows color changing, but you can just as easily control the font for each item and subitem drawn without the additional work of owner-drawn controls.


  15. #15
    Join Date
    May 2000
    Location
    Southern Germany
    Posts
    213

    Re: Wing Ding symbol in a CListCtrl.

    I will give your method a try.
    Currently, however, the only problem I have with the DrawItem function is that the WingDings Font only works in the first column.

    Can I insert this same code segment in the OnCustomDraw handler?


    case CDDSS_ITEMPREPAINT:
    if (sLabel.Find("&lt;WD&gt;") == 0)
    {
    // this is wing ding font output
    pOldFont = pDC-&gt;SelectObject(&m_WingDing) ;
    sLabel = sLabel.Right(sLabel.GetLength() - 4) ; // lose "&lt;WD&gt;" from string
    pDC-&gt;DrawText(sLabel, -1, rcLabel, DT_LEFT | DT_SINGLELINE | DT_NOPREFIX | DT_NOCLIP | DT_VCENTER | DT_END_ELLIPSIS); // Draw labels for remaining columns
    pDC-&gt;SelectObject(pOldFont) ;
    }
    else // draw normal text
    pDC-&gt;DrawText(sLabel, -1, rcLabel, DT_LEFT | DT_SINGLELINE | DT_NOPREFIX | DT_NOCLIP | DT_VCENTER | DT_END_ELLIPSIS); // Draw labels for remaining columns





Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center