Uzamčený What are Besic Step for OwnDraw Combo?

  • 25. července 2012 21:42
     
     

    hi All,

    i am trying to learn own draw ComboBox. i have see few example from CodeGuru and CodeProject. but i do not understand that the necessary step to make sample OwnDraw ComboBox. when i try it give error. here what i did.

    in Combo Property DropList, Veriable, and HasString. i create new class CMyCombo :public CComboBox

    i add message handle OnDrawItem. from the dialog class i AddString in Combo. it give error. "winctrl1.cpp" line 186

    and when i debug. it go and stop in comment line in winctrol1.cpp "// Derived class is responsible for implementing these handlers"

    what i want to do is, add Item and item data from dialog class and control all select event from dialog class. on Combo Class i want to control Draw Item for various Subject like Font, Color, Image, Icon itc.

    i have try with one copying code from CodeProject with ColorCombo. the combo with setting Fixed. which does not work any message handle from dialog class.

    looking for kind help.

    Thanks in Advance

    Amrit

Všechny reakce

  • 25. července 2012 22:11
     
     

    Basically you have to handle WM_DRAWITEM for owner draw combobox . Here is a nice article from MSDN it will tell you step by step what to do.

    http://msdn.microsoft.com/en-us/library/windows/desktop/hh404152(v=vs.85).aspx

    Thanks


    Rupesh Shukla

  • 25. července 2012 22:32
     
     

    hi Rupesh Shukla, thanks for reply. yes i have read that article . and i have added that WM_DRAWITEM also. i as mansion above. i got error in AddString(...); which i don't understand.

    Thanks

    Amrit

  • 26. července 2012 12:35
     
     

    AddString is a member function of your CComBoBox class. Can you Show your code here . Because i am not sure why are you getting this error post your code where you are getting error .Do one thing create a simple MFC Dialog base application and then put a combobox on that create a object of ComBoBox and then use AddString() with the member variable of the combobox .It's pretty easy and you will come to know what is the problem . Don't forget to bind your object with Control by using DDX etc.

    Thanks


    Rupesh Shukla


  • 26. července 2012 18:13
     
     

    Hi  Rupesh Shukla

    yes i did just very simple test. and i got error on AddString. i and looking for what i did wrong. check in difference way.

    here is link of what i did [Click here to Download]

    Thanks

    Amrit

  • 26. července 2012 18:27
     
     
  • 26. července 2012 19:00
     
     

    hi Renjith V R ,

    yes i have read that first articles. and the second one is difference than what i want. and i have read many other example also. but i don't understand. how i got error on AddString. using OwnDraw Fixed and Variable.

    there is an example same what i want http://www.codeproject.com/Articles/229/Color-Picker-Combo-Box.

    and did same setting also. even i get error on AddString. Could you kindly check Above reply. i have upload file in SkyDrive. i think i am missing something. it's been 3 days  i am looking for it.

    Thanks.

    Amrit

  • 28. července 2012 11:06
     
     Odpovědět Obsahuje kód

    The reason for the crash is the following implementation of CComboBox by MFC. Windows sent WM_DRAWITEM message to the owner of the window when drawing is required.In MFC this message is reflected downwards to DrawItem() virtual method.

    // Derived class is responsible for implementing these handlers
    //   for owner/self draw controls (except for the optional DeleteItem)
    void CComboBox::DrawItem(LPDRAWITEMSTRUCT)
        { ASSERT(FALSE); }
    void CComboBox::MeasureItem(LPMEASUREITEMSTRUCT)
        { ASSERT(FALSE); }
    int CComboBox::CompareItem(LPCOMPAREITEMSTRUCT)
        { ASSERT(FALSE); return 0; }

    The wrong part in your code was you implemented WM_DRAWITEM message instead of DrawItem() handler. If this is not handled, the framework invokes base method.So when drawing combbox base method CComboBox::DrawItem() is invoked. So assertion failure occured.

    To avoid this you need to do the following

    x_ComboBox.h

    virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct); virtual void MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct);

    virtual int CompareItem(LPCOMPAREITEMSTRUCT lpCompareItemStruct);

    x_ComboBox.cpp

    void x_ComboBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
    {
    	// TODO: Add your code to draw the specified item
    	
    }
    
    
    void x_ComboBox::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct) 
    {
    	// TODO: Add your code to determine the size of specified item
    	
    }
    
    
    
    int x_ComboBox::CompareItem(LPCOMPAREITEMSTRUCT lpCompareItemStruct) 
    {
    	// TODO: Add your code to determine the sorting order of the specified items
    	// return -1 = item 1 sorts before item 2
    	// return 0 = item 1 and item 2 sort the same
    	// return 1 = item 1 sorts after item 2
    	
    	return 0;
    }
    



    Thanks, Renjith V R

    • Označen jako odpověď Amrit.shr 30. července 2012 8:46
    •  
  • 30. července 2012 8:46
     
     

    Thanks a lot for kindly reply. and Now it works. i have notice that i make from Message Handler OnDrawItem.

    After reading l lot of example also. as i am beginner  i made mistake on OnDrawItem and DrawItem.

    Thanks a lot once again.

    Amrit.