Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Text color in the CListBox Control

Posted on 2001-07-12
8
Medium Priority
?
456 Views
Last Modified: 2013-11-20
All,

I am having problem coloring the text in the list box. My dialog class(i.e. CMyDlg) is derived from CDialog and defind a variable CListBox *m_pAgentLB in the private part of my dialog class.
The following is the method that I use to populate the list box with the red text. However, Text is always display as black.

void CMyDlg::BuildAgentLB()
{
    char zText[80] = {0};
    char zPrinterName[33] = {0};
   
    m_pAgentLB->ResetContent();
   
    for (int iNdx=0;iNdx<m_dwaAgents.GetSize();iNdx++)
    {
        agent_def *pAgent = (agent_def *)(m_dwaAgents[iNdx]);
       
        zPrinterName[0] = ' ';
        zPrinterName[1] = 0;
       
        wsprintf(zText, "%-20.20s\t"
            "%s%06lu\t"
            "%u\t"
            "%c\t"
            "%s",
            pAgent->zAgentName,
            pAgent->caEmpInit,
            pAgent->ulEmpNum,
            pAgent->uSecurityLevel,
            pAgent->cDfltPrintType,
            zPrinterName);

             COLORREF clrText = RGB(255, 0, 0);
     int nItem = m_pAgentLB->AddString(zText);
     int iReturn = m_pAgentLB->SetItemData(nItem,clrText);
     RedrawWindow();
       }
}

Where agent-def struct is the following:

typedef struct
{
    ULONG  ulUserID;          // user ID
    ULONG  ulDfltUserId;      // default user ID
    ULONG  ulDfltLocalPrinter;
    ULONG  ulEmpNum;          
    USHORT uSecurityLevel;    
    short  uLocked;
    short  iDenyHostPrint;    
    char   cDfltPrintType;    
    char   caEmpInit[SZ_AGENT_ID_INITIALS+1];  
    char   zAgentName[SZ_AGENT_NAME+1];      
    char   caPassword [SZ_AGENT_PSWD+1];      
} agent_def;

Can someone tell what could be the problem or, is there a easy way to chage the color of the text in the list box in my dialog?. Thanks for your helo in advance.

EKarim
 
0
Comment
Question by:EKarim
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 4

Expert Comment

by:captainkirk
ID: 6278999
int iReturn = m_pAgentLB->SetItemData(nItem,clrText);

this line will just store a 32 bit value (your color value) along with the item. Ite will not set the color of the text.... will get back to you in a few minutes...
0
 
LVL 4

Expert Comment

by:captainkirk
ID: 6279009
0
 

Author Comment

by:EKarim
ID: 6279058
Dear Captainkirk,

I had a mistake on the program above. The following line is what it should be:

int nItem = m_pAgentLB->AddString(zText);
int iReturn = m_pAgentLB->SetItemData(nItem,(DWORD)pAgent);

I understand that the SetItemData is implemented as
int SetItemData( int nIndex, DWORD dwItemData );
   
I looked at the program on the above URL. I do not understand how to get the SetItemData() with the second parameter of COLORREF rgb. According to the program, the SetItemData take int nItem and COLORREF rgb parameter. I do not know how he get the SetItemData function. Do you have any idea how to solve this problem? Thanks,

EKarim
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:captainkirk
ID: 6280553
SetItemData() will not make the text change color - it just stores a 32 bit value along with a list item. You can cast any 32 bit value to a DWORD.


The example in the link creates colored strings by overriding DrawItem() and MeasureItem() - the color information is stored along with each item (string) in the list, nad this may be where you are getting a bit confused. AddString() and InsertString() just take the color you want to draw the string with and stashes that along with the string using SetItemData(). DrawItem() takes that color info out and draws the string in the desired color.
0
 

Author Comment

by:EKarim
ID: 6281527
Dear Captainkirk,

Yes, I had to create the list box as LBS_OWNERDRAW and LBS_HASSTRINGS. Now, I can see the color of the text. However, the tab in the text coming as the funny character.

When I use the following line of code in the OnDraw function, I got this funny character for the tab:

     // Get and display item text.
        m_pAgentLB->GetText(lpDIS->itemID, sText);

If I use the list box as non ownerdraw, than I do not get this problem. However, If do not use the LBS_OWNERDRAW for the list box, I can not color the text in the list box. The OnDraw()funcion is pretty much the same function as in the URL. Do you have any idea how to get rid of the special character for the tab in the string? I used the  following code to add string in the list box:      
       wsprintf(zText, "%-20.20s\t"
           "%s%06lu\t"
           "%u\t"
           "%c\t"
           "%s",
           pAgent->zAgentName,
           pAgent->caEmpInit,
           pAgent->ulEmpNum,
           pAgent->uSecurityLevel,
           pAgent->cDfltPrintType,
           zPrinterName);

     m_pAgentLB->SetItemData(
            m_pAgentLB->AddString(m_szText),
            (DWORD)pAgent);

Thanks,

EKarim            
0
 
LVL 4

Accepted Solution

by:
captainkirk earned 225 total points
ID: 6281537
You are probably seeing the nasty "vertical bar" that shows up when a control does not handle tabs....

I would just replace the tab character with spaces....
0
 

Author Comment

by:EKarim
ID: 6286741
Dear Captainkirk,

Since I have an ownerdraw combobox in the dialog, I have to code for that control in the OnDrawItem function. I can successfully code for the black text on a white background. However when I dropdown the combobox, the list will always display as the text of the first index unless I select the items in the dropdown list. Another word, I have to select an item to see the actual text for that item. Is this a refresh problem? How do I display all the text in the list when I dropdown the combobox?
Here is the code for the OnDrawItem:

void CCapsAdminDlg::OnDrawItem(int  iIDCtl ,LPDRAWITEMSTRUCT lpDIS)
//
// Return Value:     None.
//
// Parameters     :     lpDIS - A long pointer to a DRAWITEMSTRUCT structure
//                                   that contains information about the type of drawing required.
//
// Remarks          :     Called by the framework when a visual aspect of
//                              an owner-draw list box changes.
//
{
     if ((int)lpDIS->itemID < 0)
          return;
     CDC* pDC = CDC::FromHandle(lpDIS->hDC);

     COLORREF crText;
     CString sText;
     COLORREF crNorm = (COLORREF)lpDIS->itemData;          // Color information is in item data.
     COLORREF crHilite = RGB(255-GetRValue(crNorm), 255-GetGValue(crNorm), 255-GetBValue(crNorm));

     switch(lpDIS->CtlID)
     {
     case IDC_SECURITYCOMBO:
     {
     // If item has been selected, draw the highlight rectangle using the item's color.
     if ((lpDIS->itemState & ODS_SELECTED) &&
(lpDIS->itemAction & (ODA_SELECT | ODA_DRAWENTIRE)))
     {
          CBrush brush(RGB(255,255,255));
          pDC->FillRect(&lpDIS->rcItem, &brush);
     }

     // If item has been deselected, draw the rectangle using the window color.
     if (!(lpDIS->itemState & ODS_SELECTED) &&     (lpDIS->itemAction & ODA_SELECT))
     {
     CBrush brush(::GetSysColor(COLOR_WINDOW));
     pDC->FillRect(&lpDIS->rcItem, &brush);
     }          

     // If item has focus, draw the focus rect.
     if ((lpDIS->itemAction & ODA_FOCUS) && (lpDIS->itemState & ODS_FOCUS))
     pDC->DrawFocusRect(&lpDIS->rcItem);

     // If item does not have focus, redraw (erase) the focus rect.
     if ((lpDIS->itemAction & ODA_FOCUS) &&     !(lpDIS->itemState & ODS_FOCUS))
     pDC->DrawFocusRect(&lpDIS->rcItem);
     // Set the background mode to TRANSPARENT to draw the text.
     int nBkMode = pDC->SetBkMode(TRANSPARENT);  
               
     if (lpDIS->itemData)          
     {
     crText = pDC->SetTextColor(RGB(0,0,0));   //Black color RGB(0,0,0)

     // Get and display item text.
     sText = m_pSecurityCB->GetSelectedListItem()->GetItemText();  
     CRect rect = lpDIS->rcItem;

     // Setup the text format.
     UINT nFormat = DT_LEFT | DT_SINGLELINE | DT_VCENTER;
     if (GetStyle() & LBS_USETABSTOPS)
          nFormat |= DT_EXPANDTABS;    
     // Calculate the rectangle size before drawing the text.
     pDC->DrawText(sText, -1, &rect, nFormat | DT_CALCRECT);
     pDC->DrawText(sText, -1, &rect, nFormat);

     pDC->SetTextColor(crText);
     pDC->SetBkColor(RGB(255,255,255));                pDC->SetBkMode(nBkMode);
     }
     }
     break;
     } //End of swith
}     // DrawItem

Thanks for your help.

EKarim
0
 
LVL 4

Expert Comment

by:captainkirk
ID: 6286831
Now we're is getting into an entirely different problem -- how about closing this one out and starting another post???
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
In this post we will learn different types of Android Layout and some basics of an Android App.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question