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 draw combobox problem

Owner draw combobox problem

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
11 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.
  • G gajendrakashyap

    In my appliaction I've modeless child dialog on the main application dialog. On the child dialog there is a combobox with properties set as: Type->Drop List; Owner draw->Fixed; Vertical scroll checked. I've derived a new class from base CComboBox class and overridden the DrawItem method in base class. I used the code available in MSDN inside this method. When I execute the application it gives a runtime error. Can someone guide me how to solve the problem or implemement the owner drawn combobox ? The same code works fine when the combobox is used on the main application dialog but not on the modeless dialog. Please help. Thanks, Gajendra

    P Offline
    P Offline
    prasad_som
    wrote on last edited by
    #2

    gajendrakashyap wrote:

    When I execute the application it gives a runtime error

    Where it takes you in the code ?(can you follow callstack). I dont think, this error related to owner drawn combo box. You must be doing something wrong with other code.

    Prasad Notifier using ATL | Operator new[],delete[][^]

    G 1 Reply Last reply
    0
    • G gajendrakashyap

      In my appliaction I've modeless child dialog on the main application dialog. On the child dialog there is a combobox with properties set as: Type->Drop List; Owner draw->Fixed; Vertical scroll checked. I've derived a new class from base CComboBox class and overridden the DrawItem method in base class. I used the code available in MSDN inside this method. When I execute the application it gives a runtime error. Can someone guide me how to solve the problem or implemement the owner drawn combobox ? The same code works fine when the combobox is used on the main application dialog but not on the modeless dialog. Please help. Thanks, Gajendra

      H Offline
      H Offline
      Hamid Taebi
      wrote on last edited by
      #3

      Maybe you forgot to set a value for your combobox and I think you get a runtime error see examples of combobox(DrawItem) on codeproject


      WhiteSky


      G 1 Reply Last reply
      0
      • P prasad_som

        gajendrakashyap wrote:

        When I execute the application it gives a runtime error

        Where it takes you in the code ?(can you follow callstack). I dont think, this error related to owner drawn combo box. You must be doing something wrong with other code.

        Prasad Notifier using ATL | Operator new[],delete[][^]

        G Offline
        G Offline
        gajendrakashyap
        wrote on last edited by
        #4

        I'm getting the exception in this call, dc.DrawText( lpszText, strlen(lpszText), &lpDrawItemStruct->rcItem, DT_CENTER|DT_SINGLELINE|DT_VCENTER); I just directly reused the code from "http://msdn2.microsoft.com/en-us/library/y5hb5f9t(VS.80).aspx[^]" I think you can reproduce the problem with this code. Regards, Gajendra

        P 1 Reply Last reply
        0
        • H Hamid Taebi

          Maybe you forgot to set a value for your combobox and I think you get a runtime error see examples of combobox(DrawItem) on codeproject


          WhiteSky


          G Offline
          G Offline
          gajendrakashyap
          wrote on last edited by
          #5

          I've referred articles from there. The examples work on simple dialogs but not in my kind of scenario. Can you try to recreate the scenario as I've mentioned and then provide me a soln if possible? Thanks, Gajendra

          H 1 Reply Last reply
          0
          • G gajendrakashyap

            I'm getting the exception in this call, dc.DrawText( lpszText, strlen(lpszText), &lpDrawItemStruct->rcItem, DT_CENTER|DT_SINGLELINE|DT_VCENTER); I just directly reused the code from "http://msdn2.microsoft.com/en-us/library/y5hb5f9t(VS.80).aspx[^]" I think you can reproduce the problem with this code. Regards, Gajendra

            P Offline
            P Offline
            prasad_som
            wrote on last edited by
            #6

            gajendrakashyap wrote:

            I think you can reproduce the problem with this code

            No, I cant. Thats why I said, you are doing something wrong. Can you show how you have created this control, or you have used DDX ?

            Prasad Notifier using ATL | Operator new[],delete[][^]

            G 1 Reply Last reply
            0
            • P prasad_som

              gajendrakashyap wrote:

              I think you can reproduce the problem with this code

              No, I cant. Thats why I said, you are doing something wrong. Can you show how you have created this control, or you have used DDX ?

              Prasad Notifier using ATL | Operator new[],delete[][^]

              G Offline
              G Offline
              gajendrakashyap
              wrote on last edited by
              #7

              Yes I'm using DDX control. I created the ctrl using drag and drop. Then I created the modeless dlg in OnInitDialog()function of Parent wnd as follows. Modeless_Dlg* ptr_dlg = new Modeless_Dlg(); if(ptr_dlg) { ptr_dlg->Create(IDD_DIALOG_MODELESS, this); ptr_dlg->ShowWindow(TRUE); ptr_dlg->m_combo.AddString("FIRST"); ptr_dlg->m_combo.AddString("SECOND"); ptr_dlg->m_combo.AddString("THIRD"); } else AfxMessageBox("Error creating dlg"); The overridden drawitem method: void CCustomComboBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) { // TODO: Add your code to draw the specified item ASSERT(lpDrawItemStruct->CtlType == ODT_COMBOBOX); LPCTSTR lpszText = (LPCTSTR) lpDrawItemStruct->itemData; ASSERT(lpszText != NULL); CDC dc; dc.Attach(lpDrawItemStruct->hDC); ASSERT(lpDrawItemStruct->hwndItem = CWnd::GetSafeHwnd()); // 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. Erase // the rect by filling it with the background color. CBrush br(RGB(255, 0, 0)); if ((lpDrawItemStruct->itemAction | ODA_SELECT) && (lpDrawItemStruct->itemState & ODS_SELECTED)) { // Sets current cursor item color and selected item color dc.SetTextColor(RGB(0, 255, 255)); // has no effect //dc.SetBkColor(RGB(255, 0, 0)); // sets current cursor item background dc.FillSolidRect(&lpDrawItemStruct->rcItem, RGB(0, 0, 0)); if ((lpDrawItemStruct->itemState | ODS_SELECTED) | (lpDrawItemStruct->itemAction & ODA_SELECT) ) dc.FrameRect(&lpDrawItemStruct->rcItem, &br); } else { // Sets list opening color and default combobox item color dc.SetTextColor(RGB(0, 255, 255)); dc.FillSolidRect(&lpDrawItemStruct->rcItem, RGB(0, 0, 0)); } // 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(); } Thats all the code I've added to the generated code. I hope this helps you to figure out the problem... Thanks, Gaj

              P 1 Reply Last reply
              0
              • G gajendrakashyap

                Yes I'm using DDX control. I created the ctrl using drag and drop. Then I created the modeless dlg in OnInitDialog()function of Parent wnd as follows. Modeless_Dlg* ptr_dlg = new Modeless_Dlg(); if(ptr_dlg) { ptr_dlg->Create(IDD_DIALOG_MODELESS, this); ptr_dlg->ShowWindow(TRUE); ptr_dlg->m_combo.AddString("FIRST"); ptr_dlg->m_combo.AddString("SECOND"); ptr_dlg->m_combo.AddString("THIRD"); } else AfxMessageBox("Error creating dlg"); The overridden drawitem method: void CCustomComboBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) { // TODO: Add your code to draw the specified item ASSERT(lpDrawItemStruct->CtlType == ODT_COMBOBOX); LPCTSTR lpszText = (LPCTSTR) lpDrawItemStruct->itemData; ASSERT(lpszText != NULL); CDC dc; dc.Attach(lpDrawItemStruct->hDC); ASSERT(lpDrawItemStruct->hwndItem = CWnd::GetSafeHwnd()); // 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. Erase // the rect by filling it with the background color. CBrush br(RGB(255, 0, 0)); if ((lpDrawItemStruct->itemAction | ODA_SELECT) && (lpDrawItemStruct->itemState & ODS_SELECTED)) { // Sets current cursor item color and selected item color dc.SetTextColor(RGB(0, 255, 255)); // has no effect //dc.SetBkColor(RGB(255, 0, 0)); // sets current cursor item background dc.FillSolidRect(&lpDrawItemStruct->rcItem, RGB(0, 0, 0)); if ((lpDrawItemStruct->itemState | ODS_SELECTED) | (lpDrawItemStruct->itemAction & ODA_SELECT) ) dc.FrameRect(&lpDrawItemStruct->rcItem, &br); } else { // Sets list opening color and default combobox item color dc.SetTextColor(RGB(0, 255, 255)); dc.FillSolidRect(&lpDrawItemStruct->rcItem, RGB(0, 0, 0)); } // 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(); } Thats all the code I've added to the generated code. I hope this helps you to figure out the problem... Thanks, Gaj

                P Offline
                P Offline
                prasad_som
                wrote on last edited by
                #8

                You need to set type property to DropDown instead of DropList. Go to resource, combo box properties -->styles-->Type , set it to DropDown.

                Prasad Notifier using ATL | Operator new[],delete[][^]

                G 1 Reply Last reply
                0
                • P prasad_som

                  You need to set type property to DropDown instead of DropList. Go to resource, combo box properties -->styles-->Type , set it to DropDown.

                  Prasad Notifier using ATL | Operator new[],delete[][^]

                  G Offline
                  G Offline
                  gajendrakashyap
                  wrote on last edited by
                  #9

                  Thanks for that Prasad... I tried the option. There are two problems with this approach: 1. It does not paint the combobox with the colors that i chose. 2. The selected text is not displayed back in the combobox edit area even if I set the index in the function mapped to ON_CBN_SELCHANGE message. Can you suggest something on that? :) Thanks, Gaj

                  1 Reply Last reply
                  0
                  • G gajendrakashyap

                    I've referred articles from there. The examples work on simple dialogs but not in my kind of scenario. Can you try to recreate the scenario as I've mentioned and then provide me a soln if possible? Thanks, Gajendra

                    H Offline
                    H Offline
                    Hamid Taebi
                    wrote on last edited by
                    #10

                    if you get this error on DrawText maybe problem is of lpszText,are you sure its valid?


                    WhiteSky


                    G 1 Reply Last reply
                    0
                    • H Hamid Taebi

                      if you get this error on DrawText maybe problem is of lpszText,are you sure its valid?


                      WhiteSky


                      G Offline
                      G Offline
                      gajendrakashyap
                      wrote on last edited by
                      #11

                      Ya it should be because it gives this problem only when I set the combobox property to Drop List rather than Drop down. I'm not able to find a way to do it as per my requirements :sigh: Regards, Gaj

                      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