Owner draw combobox problem
-
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
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[][^]
-
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
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
-
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[][^]
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
-
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
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
-
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
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[][^]
-
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[][^]
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
-
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
You need to set type property to
DropDown
instead ofDropList
. Go to resource, combo boxproperties -->styles-->Type
, set it toDropDown
.Prasad Notifier using ATL | Operator new[],delete[][^]
-
You need to set type property to
DropDown
instead ofDropList
. Go to resource, combo boxproperties -->styles-->Type
, set it toDropDown
.Prasad Notifier using ATL | Operator new[],delete[][^]
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
-
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
if you get this error on DrawText maybe problem is of lpszText,are you sure its valid?
WhiteSky
-
if you get this error on DrawText maybe problem is of lpszText,are you sure its valid?
WhiteSky
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