home *** CD-ROM | disk | FTP | other *** search
- ////////////////////////////////////////////////////////////////////////////
- // COMCOMBO Shows how to implement a self-contained combo box with buttons.
- //
-
- #include "stdafx.h"
- #include "comcombo.h"
-
- BEGIN_MESSAGE_MAP(CComboCombo, CComboBox)
- ON_COMMAND_RANGE(0, 0xFFFF, OnCommandRange)
- ON_UPDATE_COMMAND_UI_RANGE(0, 0xFFFF, OnCommandUiRange)
- END_MESSAGE_MAP()
-
- CComboCombo::CComboCombo()
- {
- }
-
- CComboCombo::~CComboCombo()
- {
- }
-
- //////////////////
- // Subclass a the combo box and Add/Delete buttons.
- // The Dialog must call this to tell the combo box what the control IDs
- // are and to hook everything up.
- //
- BOOL CComboCombo::SubclassControls(CWnd* pParent,
- UINT idCombo, UINT idAdd, UINT idDel)
- {
- if (!SubclassDlgItem(idCombo, pParent))
- return FALSE;
-
- if (!m_buttonAdd.SubclassDlgItem(idAdd, pParent))
- return FALSE;
-
- if (!m_buttonDel.SubclassDlgItem(idDel, pParent))
- return FALSE;
-
- m_idAdd = idAdd;
- m_idDel = idDel;
-
- // I own the buttons now
- m_buttonAdd.SetOwner(this);
- m_buttonDel.SetOwner(this);
-
- UpdateButtons(); // Update (enable/disable) the buttons
-
- return TRUE;
- }
-
- //////////////////
- // When the edit text or selection changes,
- // button states may change, so I need to update them.
- //
- BOOL
- CComboCombo::OnChildNotify(UINT msg, WPARAM wp, LPARAM lp, LRESULT* pLRes)
- {
- if (msg==WM_COMMAND) {
- int nCode = HIWORD(wp);
- if (nCode==CBN_SELCHANGE || nCode==CBN_EDITCHANGE)
- UpdateButtons();
- }
- return CComboBox::OnChildNotify(msg,wp,lp,pLRes);
- }
-
- ////////////////
- // Helper function updates Add/Delete buttons
- //
- void CComboCombo::UpdateButtons()
- {
- // Update buttons, using myself as target!
- GetParent()->UpdateDialogControls(this, FALSE);
- }
-
- //////////////////
- // Handle command in range 0 to 0xFFFF (all commands). Check for Add/Delete.
- //
- void CComboCombo::OnCommandRange(UINT id)
- {
- if (id==m_idAdd) {
- // Add command: add contents of edit control to list box.
- // Beep if item already added.
- CString s;
- GetWindowText(s);
- if (FindStringExact(0, s) >= 0)
- MessageBeep(0);
- else {
- AddString(s);
- SetEditSel(0, -1);
- Clear();
- SetFocus();
- }
- UpdateButtons();
-
- } else if (id==m_idDel) {
- // Delete command: Delete selected item.
- DeleteString(GetCurSel());
- UpdateButtons();
- }
- }
-
- //////////////////
- // Handle command update (0-0xFFFF, all commands). Check for Add/Delete.
- //
- void CComboCombo::OnCommandUiRange(CCmdUI* pCmdUI)
- {
- UINT id = pCmdUI->m_nID;
- if (id==m_idAdd)
- // Add button is enabled iff there's text.
- pCmdUI->Enable(GetWindowTextLength()>0);
-
- else if (id==m_idDel)
- // Delete is enable iff an item is selected.
- pCmdUI->Enable(GetCurSel()>=0);
- }
-
- ////////////////////////////////////////////////////////////////
- // Implementation of owned buttons.
-
- //////////////////
- // Re-route WM_COMMAND notification to owner instead of parent.
- // bLockout flag prevents infinite recursion, because owner will call
- // OnChildNotify too.
- //
- BOOL
- COwnedButton::OnChildNotify(UINT msg, WPARAM wp, LPARAM lp, LRESULT* pLRes)
- {
- static BOOL bLockout = FALSE;
- if (msg==WM_COMMAND && !bLockout) {
- bLockout = TRUE;
- GetOwner()->SendMessage(msg, wp, lp);
- bLockout = FALSE;
- }
- return CButton::OnChildNotify(msg,wp,lp,pLRes);
- }
-