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. Control Toolbars with Owner Drawn Combo Boxes

Control Toolbars with Owner Drawn Combo Boxes

Scheduled Pinned Locked Moved C / C++ / MFC
c++
4 Posts 2 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.
  • J Offline
    J Offline
    John Clump
    wrote on last edited by
    #1

    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

    W 1 Reply Last reply
    0
    • J John Clump

      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

      W Offline
      W Offline
      wangyiming
      wrote on last edited by
      #2

      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;

      J 1 Reply Last reply
      0
      • W wangyiming

        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;

        J Offline
        J Offline
        John Clump
        wrote on last edited by
        #3

        I tried making it own drawn fixed, but it also asserts :(

        W 1 Reply Last reply
        0
        • J John Clump

          I tried making it own drawn fixed, but it also asserts :(

          W Offline
          W Offline
          wangyiming
          wrote on last edited by
          #4

          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

          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