dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Owner Drawn Combo Boxes

  1. #1
    Join Date
    Dec 2008
    Posts
    5

    Owner Drawn Combo Boxes

    I writing code to develop a symbol selector for a plotting algorithm using C++ with the Win32 API. I used the Microsoft Win32 SDK example to help formulate my method but in the end I still have a problem. I managed to get everything working except one piece. When an item is selected it will not display that item in the selected field of the combo box (ie what you see of the combo box when the Drop Down Menu is collapsed. I know I must be missing something, but no examples and no other forum posts have been able to help me so far. I attached some snippets of code for reference.

    Here is where I create my combo box

    Code:
    LineType = CreateWindow("COMBOBOX", "", CBS_DROPDOWN | WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_TABSTOP | CBS_OWNERDRAWFIXED ,
           126, 120, 250, 100,hwnd, (HMENU)11010, g_hInst, NULL); 
       
    int In = (UINT)SendMessage(LineType, CB_ADDSTRING, 0, (LPARAM)"None"); 
    
    SendMessage(LineType, CB_SETITEMDATA, In, (LPARAM)NUM_NONE);  
     
    In = SendMessage(LineType, CB_ADDSTRING, 0, (LPARAM)"Circle"); 
        
    SendMessage(LineType, CB_SETITEMDATA, In, (LPARAM)NUM_CIRCLE); 
        
    In = SendMessage(LineType, CB_ADDSTRING, 0, (LPARAM)"Square"); 
        
    SendMessage(LineType, CB_SETITEMDATA, In, (LPARAM)NUM_SQUARE);  
        
    In = SendMessage(LineType, CB_ADDSTRING, 0, (LPARAM)"Star"); 
        
    SendMessage(LineType, CB_SETITEMDATA, In, (LPARAM)NUM_STAR);   
        
    SendMessage(LineType, CB_SETCURSEL, 1, 0);


    Here is my WM_DRAWITEM command
    Code:
      case WM_DRAWITEM:{
        LPDRAWITEMSTRUCT lp =LPDRAWITEMSTRUCT(lParam);    
        int nSearches = ModWindows.size();
        int i;
        for(i=0;i<nSearches;i++){
          if(ModWindows[i].LineType==lp->hwndItem){
            int wid = (lp->rcItem.bottom - lp->rcItem.top)/2-1;
            void (*draw)(HDC, int, int, int);
            string text = "";
            switch(lp->itemID){
              case NUM_ASTERICK:{
                draw = &Asterick;
                text = "Asterick";
                break;
              } 
              case NUM_PLUS:{
                draw = &Plus;
                text = "Plus";
                break;
              } 
              case NUM_SIXPOINTEDSTAR:{
                draw = &SixPointStar;
                text = "Six Pointed Star";
                break;
              } 
              case NUM_INVERTEDTRIANGLE:{
                draw = &InvertedTriangle;
                text = "Inverted Triangle";
                break;
              } 
              case NUM_TRIANGLE:{
                draw = &Triangle;
                text = "Triangle";
                break;
              } 
              case NUM_PENTAGON:{
                draw = &Pentagon;
                text = "Pentagon";
                break;
              } 
              case NUM_DIAMOND:{
                draw = &Diamond;
                text = "Diamond";
                break;
              } 
              case NUM_SQUARE:{
                draw = &Square;
                text = "Square";
                break;
              } 
              case NUM_CIRCLE:{
                draw = &Circle;
                text = "Circle";
                break;
              } 
              case NUM_XSIGN:{
                draw = &XSign;
                text = "X Sign";
                break;
              } 
              case NUM_STAR:{
                draw = &Star;
                text = "Star";
                break;
              } 
              case NUM_NONE:{
                draw = &DummySym;
                text = "<none>";
                break;
              }                
            }     
            COLORREF tembck;
            if(lp->itemState == ODS_SELECTED){
              tembck = SetBkColor(lp->hDC, RGB(200,200,200));
            }else{
              tembck = SetBkColor(lp->hDC, RGB(255,255,255));
            }
            TEXTMETRIC tm;
            GetTextMetrics(lp->hDC, &tm);
            ExtTextOut(lp->hDC, wid*2+2, (lp->rcItem.top+lp->rcItem.bottom - tm.tmHeight)/2, ETO_CLIPPED |ETO_OPAQUE, &lp->rcItem, text.c_str(), text.length(),NULL);
            if(lp->itemAction == ODA_SELECT)
              SetBkColor(lp->hDC, tembck);
            draw(lp->hDC, wid+1, lp->rcItem.top+wid+1, wid);
            if(lp->itemState & ODS_FOCUS)
              DrawFocusRect(lp->hDC, &lp->rcItem);
            return true;
          }
        } 
      }

    And Finally my WM_MEASUREITEM command

    Code:
      case WM_MEASUREITEM:{  
        int nSearches = ModWindows.size();
        int i;
        for(i=0;i<nSearches;i++){
          if(ModWindows[i].hwnd==hwnd){
            RECT rc;
            LPMEASUREITEMSTRUCT lpmis; 
            lpmis = (LPMEASUREITEMSTRUCT) lParam; 
            GetWindowRect(GetDlgItem(hwnd, lpmis->CtlID), &rc);
            lpmis->itemHeight = (rc.bottom-rc.top)-2; 
            lpmis->itemWidth = (rc.right-rc.left)-2;
            break;
          }
        }
        break;
      }
    Last edited by madhad15; December 16th, 2008 at 12:34 PM.

  2. #2
    Join Date
    Nov 2003
    Posts
    1,902

    Re: Owner Drawn Combo Boxes

    When posting code, please use code tags as described here: http://www.codeguru.com/forum/misc.php?do=bbcode#code

    Please edit your post, and re-paste the code within code tags so that it has proper indentation.

    >> if(lp->itemAction == ODA_SELECT) [edit]itemState is bit field, not itemAction[/edit]
    This is a bit field. You should be using the bit-wise AND operator, not ==.

    gg
    Last edited by Codeplug; December 16th, 2008 at 01:18 PM.

  3. #3
    Join Date
    Dec 2008
    Posts
    5

    Re: Owner Drawn Combo Boxes

    Thanks for the link on the code tags, I was wondering how to do it and was planning on editting my post once I had time to look it up today. Saved me some time.

    I fixed the operator but that was not the problem. Any ideas anyone?

  4. #4
    Join Date
    Jan 2008
    Posts
    178

    Re: Owner Drawn Combo Boxes

    Just see MS owner-draw combo box samples (PSDK, KB , ...)

  5. #5
    Join Date
    Nov 2003
    Posts
    1,902

    Re: Owner Drawn Combo Boxes

    Sorry, I pasted the wrong bit of code. itemAction is not a bit field. You can use == with any of the ODA_xxx constants. itemState is a bit field. Don't use == with this member.

    gg

  6. #6
    Join Date
    Dec 2008
    Posts
    5

    Re: Owner Drawn Combo Boxes

    thanks Codeplug, have them all fixed and correct.

    Fred, that still doesn't solve my problem. I have been comparing my code with multiple examples from all over the web and I can't seem to determine where mine differs in a way to cause this behavior. I have a feeling it is something either in the way I declare my combobox, or some higher level system is overwriting the default state of drawing the combobox. But I don't know for certain. I even tried overriding how the combobox itself is drawn and that only caused more problems.

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




On-Demand Webinars (sponsored)