Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Owner drawn ListBox problem

Owner drawn ListBox problem

Scheduled Pinned Locked Moved C / C++ / MFC
c++wpfgraphicshelptutorial
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Manmohan29
    wrote on last edited by
    #1

    I created a listbox (owner drawn) using the code given below. the problem with my application is that when i click on any item in the listbox it gets selected :confused:. how to rectify that ? Here's my code :- CChatHistory is my ListBox's class name

    // ChatHistory.cpp : implementation file
    //

    #include "stdafx.h"
    #include "sock.h"
    #include "ChatHistory.h"

    // CChatHistory

    IMPLEMENT_DYNAMIC(CChatHistory, CListBox)

    CChatHistory::CChatHistory()
    {

    }

    CChatHistory::~CChatHistory()
    {
    }

    BEGIN_MESSAGE_MAP(CChatHistory, CListBox)
    END_MESSAGE_MAP()

    // CChatHistory message handlers

    // Colored entries in List Box

    //		Override the CListBox::MeasureItem virtual function to specify the height for each item.
    
    
    //		Override the CListBox::DrawItem virtual function to perform the painting.
    
    
    //		Override the CListBox::CompareItem virtual function to determine the order in which the a string has to be added. This is necessary only if you wish to have a sorted list. 
    //		Note that the list box should have the LBS\_OWNERDRAWVARIABLE and LBS\_HASSTRINGS styles set. You need to select these styles for the list box when you create it using a resource editor. Otherwise, if you create the list box dynamically, specify these styles during creation. 
    
    //		The following CLineListBox class provides an implementation. Here the color for the text is stored as item data and retrieved during painting. First, we implement the AddItem function, which adds the string to the list box and stores the color in the 32-bit item data associated with the string:
    

    void CChatHistory::AddItem(const CString& str, COLORREF rgbText)
    {
    int nIndex;
    nIndex = AddString(str);
    if( CB_ERR != nIndex )
    SetItemData(nIndex, rgbText);
    }
    // Next, we override DrawItem to draw the string in the color stored in the item data:

    void CChatHistory::DrawItem(LPDRAWITEMSTRUCT lpDIS)
    {
    CDC dc;
    CRect rcItem(lpDIS->rcItem);
    UINT nIndex = lpDIS->itemID;
    COLORREF rgbBkgnd = ::GetSysColor((lpDIS->itemState & ODS_SELECTED) ?COLOR_HIGHLIGHT : COLOR_WINDOW);
    dc.Attach(lpDIS->hDC);

    		// Save these value to restore them when done drawing.
    		COLORREF crOldTextColor = dc.GetTextColor();
    		COLORREF crOldBkColor = dc.GetBkColor();
    
    		CBrush br(rgbBkgnd);
    		dc.FillRect(rcItem, &br);
    		if( lpDIS->itemState & ODS\_FOCUS )
    			dc.DrawFocusRect(rcItem);
    		if( nIndex != (UINT)-1 )
    		{
    			// The text color is stored
    
    _ 1 Reply Last reply
    0
    • M Manmohan29

      I created a listbox (owner drawn) using the code given below. the problem with my application is that when i click on any item in the listbox it gets selected :confused:. how to rectify that ? Here's my code :- CChatHistory is my ListBox's class name

      // ChatHistory.cpp : implementation file
      //

      #include "stdafx.h"
      #include "sock.h"
      #include "ChatHistory.h"

      // CChatHistory

      IMPLEMENT_DYNAMIC(CChatHistory, CListBox)

      CChatHistory::CChatHistory()
      {

      }

      CChatHistory::~CChatHistory()
      {
      }

      BEGIN_MESSAGE_MAP(CChatHistory, CListBox)
      END_MESSAGE_MAP()

      // CChatHistory message handlers

      // Colored entries in List Box

      //		Override the CListBox::MeasureItem virtual function to specify the height for each item.
      
      
      //		Override the CListBox::DrawItem virtual function to perform the painting.
      
      
      //		Override the CListBox::CompareItem virtual function to determine the order in which the a string has to be added. This is necessary only if you wish to have a sorted list. 
      //		Note that the list box should have the LBS\_OWNERDRAWVARIABLE and LBS\_HASSTRINGS styles set. You need to select these styles for the list box when you create it using a resource editor. Otherwise, if you create the list box dynamically, specify these styles during creation. 
      
      //		The following CLineListBox class provides an implementation. Here the color for the text is stored as item data and retrieved during painting. First, we implement the AddItem function, which adds the string to the list box and stores the color in the 32-bit item data associated with the string:
      

      void CChatHistory::AddItem(const CString& str, COLORREF rgbText)
      {
      int nIndex;
      nIndex = AddString(str);
      if( CB_ERR != nIndex )
      SetItemData(nIndex, rgbText);
      }
      // Next, we override DrawItem to draw the string in the color stored in the item data:

      void CChatHistory::DrawItem(LPDRAWITEMSTRUCT lpDIS)
      {
      CDC dc;
      CRect rcItem(lpDIS->rcItem);
      UINT nIndex = lpDIS->itemID;
      COLORREF rgbBkgnd = ::GetSysColor((lpDIS->itemState & ODS_SELECTED) ?COLOR_HIGHLIGHT : COLOR_WINDOW);
      dc.Attach(lpDIS->hDC);

      		// Save these value to restore them when done drawing.
      		COLORREF crOldTextColor = dc.GetTextColor();
      		COLORREF crOldBkColor = dc.GetBkColor();
      
      		CBrush br(rgbBkgnd);
      		dc.FillRect(rcItem, &br);
      		if( lpDIS->itemState & ODS\_FOCUS )
      			dc.DrawFocusRect(rcItem);
      		if( nIndex != (UINT)-1 )
      		{
      			// The text color is stored
      
      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      Remove the following lines from the DrawItem method.

      if( lpDIS->itemState & ODS_FOCUS )
      dc.DrawFocusRect(rcItem);
      if( nIndex != (UINT)-1 )
      {
      // The text color is stored as the item data.
      COLORREF rgbText = (lpDIS->itemState & ODS_SELECTED) ?::GetSysColor(COLOR_HIGHLIGHTTEXT) : GetItemData(nIndex);
      CString str;
      GetText(nIndex, str);
      dc.SetBkColor(rgbBkgnd);
      dc.SetTextColor(rgbText);
      dc.TextOut(rcItem.left + 2, rcItem.top + 2, str);
      }

      «_Superman_» I love work. It gives me something to do between weekends.
      Microsoft MVP (Visual C++)

      M L 2 Replies Last reply
      0
      • _ _Superman_

        Remove the following lines from the DrawItem method.

        if( lpDIS->itemState & ODS_FOCUS )
        dc.DrawFocusRect(rcItem);
        if( nIndex != (UINT)-1 )
        {
        // The text color is stored as the item data.
        COLORREF rgbText = (lpDIS->itemState & ODS_SELECTED) ?::GetSysColor(COLOR_HIGHLIGHTTEXT) : GetItemData(nIndex);
        CString str;
        GetText(nIndex, str);
        dc.SetBkColor(rgbBkgnd);
        dc.SetTextColor(rgbText);
        dc.TextOut(rcItem.left + 2, rcItem.top + 2, str);
        }

        «_Superman_» I love work. It gives me something to do between weekends.
        Microsoft MVP (Visual C++)

        M Offline
        M Offline
        Manmohan29
        wrote on last edited by
        #3

        removing these lines causes my control to not even display a single line. now my listbox is not displaying anything in it.

        Future Lies in Present. Manmohan Bishnoi

        _ 1 Reply Last reply
        0
        • M Manmohan29

          removing these lines causes my control to not even display a single line. now my listbox is not displaying anything in it.

          Future Lies in Present. Manmohan Bishnoi

          _ Offline
          _ Offline
          _Superman_
          wrote on last edited by
          #4

          OK. Try this - Replace

          COLORREF rgbBkgnd = ::GetSysColor((lpDIS->itemState & ODS_SELECTED) ?COLOR_HIGHLIGHT : COLOR_WINDOW);

          with

          COLORREF rgbBkgnd = COLOR_WINDOW;

          Remove

          if( lpDIS->itemState & ODS_FOCUS )
          dc.DrawFocusRect(rcItem);

          Replace

          if( nIndex != (UINT)-1 )
          {
          // The text color is stored as the item data.
          COLORREF rgbText = (lpDIS->itemState & ODS_SELECTED) ?::GetSysColor(COLOR_HIGHLIGHTTEXT) : GetItemData(nIndex);
          CString str;
          GetText(nIndex, str);
          dc.SetBkColor(rgbBkgnd);
          dc.SetTextColor(rgbText);
          dc.TextOut(rcItem.left + 2, rcItem.top + 2, str);
          }

          with

          if( nIndex != (UINT)-1 )
          {
          // The text color is stored as the item data.
          COLORREF rgbText = GetItemData(nIndex);
          CString str;
          GetText(nIndex, str);
          dc.SetBkColor(rgbBkgnd);
          dc.SetTextColor(rgbText);
          dc.TextOut(rcItem.left + 2, rcItem.top + 2, str);
          }

          «_Superman_» I love work. It gives me something to do between weekends.
          Microsoft MVP (Visual C++)

          M 2 Replies Last reply
          0
          • _ _Superman_

            OK. Try this - Replace

            COLORREF rgbBkgnd = ::GetSysColor((lpDIS->itemState & ODS_SELECTED) ?COLOR_HIGHLIGHT : COLOR_WINDOW);

            with

            COLORREF rgbBkgnd = COLOR_WINDOW;

            Remove

            if( lpDIS->itemState & ODS_FOCUS )
            dc.DrawFocusRect(rcItem);

            Replace

            if( nIndex != (UINT)-1 )
            {
            // The text color is stored as the item data.
            COLORREF rgbText = (lpDIS->itemState & ODS_SELECTED) ?::GetSysColor(COLOR_HIGHLIGHTTEXT) : GetItemData(nIndex);
            CString str;
            GetText(nIndex, str);
            dc.SetBkColor(rgbBkgnd);
            dc.SetTextColor(rgbText);
            dc.TextOut(rcItem.left + 2, rcItem.top + 2, str);
            }

            with

            if( nIndex != (UINT)-1 )
            {
            // The text color is stored as the item data.
            COLORREF rgbText = GetItemData(nIndex);
            CString str;
            GetText(nIndex, str);
            dc.SetBkColor(rgbBkgnd);
            dc.SetTextColor(rgbText);
            dc.TextOut(rcItem.left + 2, rcItem.top + 2, str);
            }

            «_Superman_» I love work. It gives me something to do between weekends.
            Microsoft MVP (Visual C++)

            M Offline
            M Offline
            Manmohan29
            wrote on last edited by
            #5

            now background goes black for each line added to the listbox.

            Future Lies in Present. Manmohan Bishnoi

            1 Reply Last reply
            0
            • _ _Superman_

              OK. Try this - Replace

              COLORREF rgbBkgnd = ::GetSysColor((lpDIS->itemState & ODS_SELECTED) ?COLOR_HIGHLIGHT : COLOR_WINDOW);

              with

              COLORREF rgbBkgnd = COLOR_WINDOW;

              Remove

              if( lpDIS->itemState & ODS_FOCUS )
              dc.DrawFocusRect(rcItem);

              Replace

              if( nIndex != (UINT)-1 )
              {
              // The text color is stored as the item data.
              COLORREF rgbText = (lpDIS->itemState & ODS_SELECTED) ?::GetSysColor(COLOR_HIGHLIGHTTEXT) : GetItemData(nIndex);
              CString str;
              GetText(nIndex, str);
              dc.SetBkColor(rgbBkgnd);
              dc.SetTextColor(rgbText);
              dc.TextOut(rcItem.left + 2, rcItem.top + 2, str);
              }

              with

              if( nIndex != (UINT)-1 )
              {
              // The text color is stored as the item data.
              COLORREF rgbText = GetItemData(nIndex);
              CString str;
              GetText(nIndex, str);
              dc.SetBkColor(rgbBkgnd);
              dc.SetTextColor(rgbText);
              dc.TextOut(rcItem.left + 2, rcItem.top + 2, str);
              }

              «_Superman_» I love work. It gives me something to do between weekends.
              Microsoft MVP (Visual C++)

              M Offline
              M Offline
              Manmohan29
              wrote on last edited by
              #6

              It's solved :cool::thumbsup: just done as you said with some modification:- REPLACE

              COLORREF rgbBkgnd = COLOR_WINDOW;

              WITH

              COLORREF rgbBkgnd = ::GetSysColor(COLOR_WINDOW);

              Thanks SUPERMAN

              Future Lies in Present. Manmohan Bishnoi

              1 Reply Last reply
              0
              • _ _Superman_

                Remove the following lines from the DrawItem method.

                if( lpDIS->itemState & ODS_FOCUS )
                dc.DrawFocusRect(rcItem);
                if( nIndex != (UINT)-1 )
                {
                // The text color is stored as the item data.
                COLORREF rgbText = (lpDIS->itemState & ODS_SELECTED) ?::GetSysColor(COLOR_HIGHLIGHTTEXT) : GetItemData(nIndex);
                CString str;
                GetText(nIndex, str);
                dc.SetBkColor(rgbBkgnd);
                dc.SetTextColor(rgbText);
                dc.TextOut(rcItem.left + 2, rcItem.top + 2, str);
                }

                «_Superman_» I love work. It gives me something to do between weekends.
                Microsoft MVP (Visual C++)

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                At best, that does not prevent selection, all it attempts to do is not show the selected status... :)

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.


                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups