Solved

CListBox ownerdraw

Posted on 2000-04-05
11
849 Views
Last Modified: 2013-11-20
I'm writing a VC++ Dialog type application. Since I want to have some listbox with black background and white text, I created my own class  CModListBox which is based on CListBox.

so.. the followng listed what I did:
1. Create a class:  Class CModListBox : public CListBox
2. Add virtual function Drawitem and I use exactly the same code list in MSDN:
void CModListBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
      // TODO: Add your code to draw the specified item
   //ASSERT(lpDrawItemStruct->CtlType == ODT_LISTBOX);
   LPCTSTR lpszText = (LPCTSTR) lpDrawItemStruct->itemData;
   //ASSERT(lpszText != NULL);  
   CDC dc;
   dc.Attach(lpDrawItemStruct->hDC);
   // Save these value to restore them when done drawing.
   COLORREF crOldTextColor = dc.GetTextColor();
   COLORREF crOldBkColor = dc.GetBkColor();
   // If this item is selected, set the background color
   // and the text color to appropriate values. Also, erase
   // rect by filling it with the background color.
   if ((lpDrawItemStruct->itemAction | ODA_SELECT) &&
      (lpDrawItemStruct->itemState & ODS_SELECTED))  
   {
      dc.SetTextColor(RGB(255,255,255));
      dc.SetBkColor(RGB(0,0,25));
      dc.FillSolidRect(&lpDrawItemStruct->rcItem,
         RGB(50,50,50));  
   }
   else
      dc.FillSolidRect(&lpDrawItemStruct->rcItem,RGB(20,20,20));
   // If this item has the focus, draw a red frame around the   // item's rect.
   if ((lpDrawItemStruct->itemAction | ODA_FOCUS) &&
      (lpDrawItemStruct->itemState & ODS_FOCUS))  
   {
      CBrush br(RGB(255, 0, 0));
      dc.FrameRect(&lpDrawItemStruct->rcItem, &br);  
   }   // Draw the text.
   dc.DrawText(      lpszText,      strlen(lpszText),
      &lpDrawItemStruct->rcItem,      DT_CENTER|DT_SINGLELINE|DT_VCENTER);
   // Reset the background color and the text color back to their
   // original values.   dc.SetTextColor(crOldTextColor);
   dc.SetBkColor(crOldBkColor);  
   dc.Detach();
}




3. Add a variable CModListBox m_ctlScheme in my main dialog box
class

4. Add this line in the OnCreate() (of main dialog box)
      m_ctlScheme.Create(WS_CHILD|WS_VISIBLE|WS_BORDER|WS_VSCROLL|LBS_OWNERDRAWVARIABLE,CRect(400,550,600,590),this, IDC_LISTSCHEME);

.......

Then whenever I try to click on the listbox, or try to use "addstring" to do something to the listbox, it gives an error msg to me.
Is there anything else I need to do ??  This is the first time I create a class to use the ownerdraw function so... :)
0
Comment
Question by:rw8
[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
  • 5
11 Comments
 
LVL 10

Accepted Solution

by:
RONSLOW earned 100 total points
ID: 2688415
don't create it like that.

Put a normal listbox in your dialog (make sure it is owner-drawn etc).

Then use DDX_Control to subclass it.  Alternatively, use SubclassWindow if you don't like MFC's easy dataexchange and ClassWizard

Then that listbox in the dialog will be associated with you m_ctlScheme and m_ctlScheme will get the message etc.

BTW: instead of attaching and detaching from a DC, use this...

CDC* pDC = CDC::FromHandle(lpDIS->hDC);

then use pDC-> and no need to detatch, delete etc the pDC.


0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2688416
You _could_ use OnCtlColor to change the colour (background and text) instead.  Then you don't need to make it owner drawn.

0
 

Author Comment

by:rw8
ID: 2688714
RONSLOW,
can you give me an example of how to use OnCtlColor to change the colour? Do I need to derive a new class?
0
Independent Software Vendors: 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 3

Expert Comment

by:V_Bapat
ID: 2688946
Hi rw8
   RONSLOW is right. Just for changing the text and background color you need not make is owner drawn. OnCtlColor works but if I am right, text color and only background color of the text changes and not the background color of the control.
   Take a look at the Technical article 62(TN062) in MSDN. You will find a step by step procedure to change the text and bkground color of an edit box. Just apply it for the list box.

All the best.

Vicky
0
 

Author Comment

by:rw8
ID: 2691571
Yes. It works! But I have a problem... the listbox remains the default colour (black text over white background) unless I put "invalidate" in the CtlColor function..  however, if I do this.. the listbox keeps on redrawing itself ..
should I put Invalidate() in somewhere else?
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2691729
show us your code .. that behaviour shouldn't be happening
0
 

Author Comment

by:rw8
ID: 2691776
this is the class I created (base on CListBox)

CBlackListBox::CBlackListBox()
{
      m_text = RGB(255, 0, 0 );
    m_background = RGB( 0, 255, 0 );
    m_brBkgnd.CreateSolidBrush( m_background );
}

CBlackListBox::~CBlackListBox()
{
}


BEGIN_MESSAGE_MAP(CBlackListBox, CListBox)
      //{{AFX_MSG_MAP(CBlackListBox)
      ON_WM_CTLCOLOR_REFLECT()
      //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBlackListBox message handlers

HBRUSH CBlackListBox::CtlColor(CDC* pDC, UINT nCtlColor)
{
      // TODO: Change any attributes of the DC here
     pDC->SetTextColor( m_text );    // text
     pDC->SetBkColor( m_background );    // text bkgnd
      
     return m_brBkgnd;                // ctl bkgnd
      // return NULL;

}


and I add a ListBox in my main dialog box and attach a CBlacklistbox ctl varible to it.. and I didn't change any settings of the the listbox.
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2691792
On first inspection it looks ok .. I'll give it a try when I havea few mintes.

0
 

Author Comment

by:rw8
ID: 2691815
wait ronslow, I just created another listbox and it seems ok now. I'll do some more tests now
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2691832
the plot thickens....
0
 

Author Comment

by:rw8
ID: 2691976
Thanx RONSLOW. It's working now. And thanx  V Bapat too. :)
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Windows Drag & Drop Location 2 117
How to create frequencies of a variable from SAS dataset? 10 146
Folder Comparison 12 56
Way to decrease size of apk file 9 132
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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…
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.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

751 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