Control Toolbars with Owner Drawn Combo Boxes
-
Hi everyone. I have a dialog with a control toolbar, which I have been able to successfully create. It has 2 combo boxes, both were simple combo boxes, and it worked fine. Then, I made one of the combo boxes a CFontPreviewCombo (which is a combo box which displays all available system fonts, which is a new control added to CodeProject, by Chris Losinger). I did this by changing the type from CComboBox to CFontPreviewCombo, and it compiled and ran fine. It did display the fonts in the combo box, but box itself did not draw properly. I realized it was supposed to be owner drawn variable, so I made it owner drawn variable by using the CBS_OWNERDRAWVARIABLE style. When I did that, I got a daocore.cpp, line 42. It asserts on the cToolBar.m_cboFont.Create() line, when the combo box is created. It does not assert when not owner drawn, but does when it is. I have my combo box code below, I am doing this in the OnCreate() of the dialog. I was told this was the right place to put it, but I am not completely sure, I have to admit I am new to manipulating toolbars directly.
int CDlgReport::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CDialog::OnCreate(lpCreateStruct) == -1) return -1; CRect rect; ///////////////////////////////////////// //Body of On Create goes here //add the tool bar to the dialog cToolBar.Create(this); cToolBar.LoadToolBar(IDR_TBR_REPORT); #define SNAP_WIDTH_FONT_SIZE 50 //the width of the combo box #define SNAP_WIDTH_FONT 150 //the width of the combo box //////////////////////// // FONT COMBO BOX //next convert that button to a seperator and get its position cToolBar.SetButtonInfo(0, ID_TBR_REPORT_CBO_FONT, TBBS_SEPARATOR, SNAP_WIDTH_FONT); cToolBar.GetItemRect(0, &rect); //expand the rectangle to allow the combo box room to drop down rect.top+=2; rect.bottom += 200; // then create the combo box and show it /* THIS CODE WORKS, NOT OWNER DRAWN if (!cToolBar.m_cboFont.Create(WS_CHILD|WS_VISIBLE|CBS_AUTOHSCROLL| CBS_DROPDOWNLIST|CBS_HASSTRINGS | CBS_SORT, rect, &cToolBar, IDC_CBO_FONT)) { TRACE0("Failed to create combo-box\n"); return FALSE; } */ /* THIS CODE ASSERTS, OWNER DRAWN */ if (!cToolBar.m_cboFont.Create(WS_CHILD|WS_VISIBLE|CBS_AUTOHSCROLL| CBS_OWNERDRAWVARIABLE | CBS_DROPDOWNLIST|CBS_HASSTRINGS | CBS_S
-
Hi everyone. I have a dialog with a control toolbar, which I have been able to successfully create. It has 2 combo boxes, both were simple combo boxes, and it worked fine. Then, I made one of the combo boxes a CFontPreviewCombo (which is a combo box which displays all available system fonts, which is a new control added to CodeProject, by Chris Losinger). I did this by changing the type from CComboBox to CFontPreviewCombo, and it compiled and ran fine. It did display the fonts in the combo box, but box itself did not draw properly. I realized it was supposed to be owner drawn variable, so I made it owner drawn variable by using the CBS_OWNERDRAWVARIABLE style. When I did that, I got a daocore.cpp, line 42. It asserts on the cToolBar.m_cboFont.Create() line, when the combo box is created. It does not assert when not owner drawn, but does when it is. I have my combo box code below, I am doing this in the OnCreate() of the dialog. I was told this was the right place to put it, but I am not completely sure, I have to admit I am new to manipulating toolbars directly.
int CDlgReport::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CDialog::OnCreate(lpCreateStruct) == -1) return -1; CRect rect; ///////////////////////////////////////// //Body of On Create goes here //add the tool bar to the dialog cToolBar.Create(this); cToolBar.LoadToolBar(IDR_TBR_REPORT); #define SNAP_WIDTH_FONT_SIZE 50 //the width of the combo box #define SNAP_WIDTH_FONT 150 //the width of the combo box //////////////////////// // FONT COMBO BOX //next convert that button to a seperator and get its position cToolBar.SetButtonInfo(0, ID_TBR_REPORT_CBO_FONT, TBBS_SEPARATOR, SNAP_WIDTH_FONT); cToolBar.GetItemRect(0, &rect); //expand the rectangle to allow the combo box room to drop down rect.top+=2; rect.bottom += 200; // then create the combo box and show it /* THIS CODE WORKS, NOT OWNER DRAWN if (!cToolBar.m_cboFont.Create(WS_CHILD|WS_VISIBLE|CBS_AUTOHSCROLL| CBS_DROPDOWNLIST|CBS_HASSTRINGS | CBS_SORT, rect, &cToolBar, IDC_CBO_FONT)) { TRACE0("Failed to create combo-box\n"); return FALSE; } */ /* THIS CODE ASSERTS, OWNER DRAWN */ if (!cToolBar.m_cboFont.Create(WS_CHILD|WS_VISIBLE|CBS_AUTOHSCROLL| CBS_OWNERDRAWVARIABLE | CBS_DROPDOWNLIST|CBS_HASSTRINGS | CBS_S
Replace with : CBS_OWNERDRAWFIXED my part codes: // Create the Line Style combo box SetButtonInfo(6,ID_PRTFMTLINESTYLE,TBBS_SEPARATOR,80); GetItemRect(6, &rect); rect.top = 2; rect.bottom = rect.top + 18*8; if (!m_LineStyleSelect.Create( CBS_DROPDOWNLIST|CBS_OWNERDRAWFIXED|CBS_SORT |WS_VISIBLE|WS_TABSTOP, rect, this, ID_PRTFMTLINESTYLE)) return -1;
-
Replace with : CBS_OWNERDRAWFIXED my part codes: // Create the Line Style combo box SetButtonInfo(6,ID_PRTFMTLINESTYLE,TBBS_SEPARATOR,80); GetItemRect(6, &rect); rect.top = 2; rect.bottom = rect.top + 18*8; if (!m_LineStyleSelect.Create( CBS_DROPDOWNLIST|CBS_OWNERDRAWFIXED|CBS_SORT |WS_VISIBLE|WS_TABSTOP, rect, this, ID_PRTFMTLINESTYLE)) return -1;
I tried making it own drawn fixed, but it also asserts :(
-
I tried making it own drawn fixed, but it also asserts :(
my codes was run normally! if possible, please send your more codes *.h ---------------------------------------- class AFX_EXT_CLASS CLineStyleComboBox : public CComboBox { protected: virtual void MeasureItem( LPMEASUREITEMSTRUCT lpMIS); virtual void DrawItem( LPDRAWITEMSTRUCT lpDIS); virtual int CompareItem(LPCOMPAREITEMSTRUCT lpCIS); //{{AFX_MSG(CLineStyleComboBox) afx_msg int OnCreate( LPCREATESTRUCT lpCreateStruct ); //}}AFX_MSG DECLARE_MESSAGE_MAP() public: void AddStyleItem(int Style) { AddString((LPCTSTR)Style);} int SelectItem(int Style); }; *.cpp ---------------------------------------- #define LSTYLE_ITEM_HEIGHT 18 BEGIN_MESSAGE_MAP(CLineStyleComboBox, CComboBox) //{{AFX_MSG_MAP(CLineStyleComboBox) ON_WM_CREATE() //}}AFX_MSG_MAP END_MESSAGE_MAP() int CLineStyleComboBox::OnCreate( LPCREATESTRUCT lpCreateStruct ) { int nRet = CComboBox::OnCreate(lpCreateStruct); if( nRet==-1 ) return nRet; static int styles[] = { PS_SOLID,PS_DASH,PS_DOT,PS_DASHDOT,PS_DASHDOTDOT,PS_NULL }; for(int i=0;iitemHeight = LSTYLE_ITEM_HEIGHT; } void CLineStyleComboBox::DrawItem(LPDRAWITEMSTRUCT lpDIS) { CDC* pDC = CDC::FromHandle(lpDIS->hDC); COLORREF Color = (lpDIS->itemState & ODS_SELECTED)?RGB(0xff,0,0):RGB(0,0,0); int nPenStyle = int(lpDIS->itemData); CPen Pen( nPenStyle,1, Color ); CPen * pOldPen = pDC->SelectObject( &Pen ); pDC->SetBkMode(TRANSPARENT); int y = (lpDIS->rcItem.top+lpDIS->rcItem.bottom)>>1; pDC->MoveTo( lpDIS->rcItem.left,y ); pDC->LineTo( lpDIS->rcItem.right,y ); pDC->SelectObject( pOldPen ); } int CLineStyleComboBox::CompareItem(LPCOMPAREITEMSTRUCT lpCIS) { return int(lpCIS->itemData1)-int(lpCIS->itemData2); } int CLineStyleComboBox::SelectItem(int Style) { int n = GetCount(); for(int i=0;i