⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jblistbox.cpp

📁 Visual C++下的界面设计
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		   }

		   // deselect or delete items in reverse order so that we don't have 
         // to reshuffle the indexes as we delete

         // we can also move down in reverse order, but we can't move up as
         // cases where we have 2 items next to each other that both need to
         // be moved up will not work.

         bool bMoveUpRequired = false;

		   for (i = selected - 1; i >= 0; i--)
		   {
			   if (Delete == pActions[i])
			   {
				   RemoveItem((UINT)pSelectedItems[i]);
			   }
            else if (Unselect == pActions[i])
            {
               CancelSelection(pSelectedItems[i]);
            }
            else if (pActions[i] & MoveUp)
            {
               bMoveUpRequired = true;
            }
            else if (pActions[i] & MoveDown)
            {
               MoveItemDown(pSelectedItems[i], !(pActions[i] & Unselect));
            }
		   }

         if (bMoveUpRequired)
         {
            // For a multiple selection move up to work we need to process
            // the items in order..

            for (i = 0; i < selected; i++)
		      {
               if (pActions[i] & MoveUp)
               {
                  MoveItemUp(pSelectedItems[i], !(pActions[i] & Unselect));
               }
            }
         }

		   delete[] pSelectedItems;
		   delete[] pActions;
	   }
   }
}

///////////////////////////////////////////////////////////////////////////////
// Button association
///////////////////////////////////////////////////////////////////////////////

// AssociateButton
// Associates the supplied button with the list box.
// The button's state (enabled/disabled) will be managed by the list box.
// The button can be set to be enabled when a single item is selected, 
// when multiple items are selected and when there are more than n items 
// in the list box.

void CJBListBox::AssociateButton(
	CButton &theButton, 
   SelectionType_e selection  /* = AnySelection*/,
	int nItems				      /* = 1 */)
{
   if (!m_pButtonList)
   {
      m_pButtonList = new ButtonList;

      if (!m_pButtonList)
      {
         throw ENoMemory();
      }
   }

	m_pButtonList->AddButton(
      theButton, 
      selection,
      nItems);

	SetButtonState();
}

// AssociateButton
// As above but the button with the supplied ID is associated to the list box.

void CJBListBox::AssociateButton(
	UINT nButtonID, 
   ItemProcessor *pProcess    /* = NULL */, 
   SelectionType_e selection  /* = AnySelection */,
	int nItems				      /* = 1 */)
{
	CWnd *pParent = GetParent();
	
	ASSERT(pParent);

   if (!m_pButtonList)
   {
      m_pButtonList = new ButtonList;

      if (!m_pButtonList)
      {
         throw ENoMemory();
      }
   }

	m_pButtonList->AddButton(
      *pParent, 
      nButtonID, 
      *this, 
      pProcess, 
      selection,
      nItems);

	SetButtonState();
}

///////////////////////////////////////////////////////////////////////////////
// Helper functions
///////////////////////////////////////////////////////////////////////////////

// GetSelectionCount()
// Returns the number of selected items in the list box. Handles the fact that
// GetSelCount() only works for multi selection list boxes and you need to 
// use GetCurSel() if you have a single selection box.

int CJBListBox::GetSelectionCount()
{
	int numSelected = GetSelCount();

	if (LB_ERR == numSelected)
	{
		numSelected = (GetCurSel() != LB_ERR) ? 1 : 0;
	}

	return numSelected;
}

// GetSelectedItems()
// Returns an array of selected items.
// Handles the fact that GetSelItems() only works for multi selection list
// boxes and you have to use GetCurSel() if the box is single selection.

int CJBListBox::GetSelectedItems(const int nMaxItems, LPINT rgIndex)
{
   int numItems = GetSelItems(nMaxItems, rgIndex);
   
   if (LB_ERR == numItems && nMaxItems > 0)
   {
      int nIndex = GetCurSel();
      
      if (LB_ERR != nIndex)
      {
         rgIndex[0] = nIndex;
         numItems = 1;
      }
   }
   
   return numItems;
}

// SetButtonState()
// Updates the state of all associated buttons based on the number of items
// in the box and the number of items that are selected.

void CJBListBox::SetButtonState()
{
   if (m_pButtonList)
   {
      bool bTopSelected;
      bool bBottomSelected;
      
      TopOrBottomSelected(bTopSelected, bBottomSelected);

	   m_pButtonList->SetButtonState(
         GetCount(), 
         GetSelectionCount(),
         bTopSelected,
         bBottomSelected);
   }
}

// MoveItem
// Does the grunt work for MoveItemUp() and MoveItemDown()
// Could be used to move an item by any offset...

int CJBListBox::MoveItem(
   const UINT nIndex, 
   const int nOffset,
   const bool bSelectAfterMove)
{
   int result = LB_ERR;

   CString theString;

   GetText((int)nIndex, theString);

   if (!theString.IsEmpty())
   {
      void *pData = GetItemDataPtr((int)nIndex);

      if (LB_ERR != RemoveItem(nIndex))
      {
         result = InsertItem(nIndex + nOffset, theString, pData);

         if (LB_ERR !=  result && bSelectAfterMove)
         {
            result = SelectItem(result);
         }
      }
   }

   return result;
}

void CJBListBox::TopOrBottomSelected(bool &bTopSelected, bool &bBottomSelected)
{
   bTopSelected = false;
   bBottomSelected = false;

   int numSelected = GetSelectionCount();

   if (0 != numSelected)
   {
      int *pSelectedItems = new int[numSelected];  // cache this?
	   
      int selected = GetSelectedItems(numSelected, pSelectedItems);

	   ASSERT(selected == numSelected);

      if (pSelectedItems[0] == 0)
      {
         bTopSelected = true;
      }

      // The index of the last item is 1 less than the number of items in
      // the box.

      if (pSelectedItems[numSelected - 1] == GetCount() - 1)
      {
         bBottomSelected = true;
      }

      delete[] pSelectedItems;
   }
}

// ProcessSelectedItem()
// Obtains the item's string and item data and lets the supplied item
// processor process the item.

void CJBListBox::ProcessSelectedItem(
   ItemProcessor *pProcessor, 
   const int nIndex,
   PostProcessAction_e &action)
{
	ASSERT(LB_ERR != nIndex);

	if (LB_ERR != nIndex)
	{
		CString theString;

		GetText(nIndex, theString);

		void *pData = GetItemDataPtr(nIndex);

		if (pProcessor)
		{
			 pProcessor->ProcessSelectedItem(nIndex, 
             theString, 
             pData, 
             action);
		}
	}
}

///////////////////////////////////////////////////////////////////////////////
// Namespace: JetByteTools
///////////////////////////////////////////////////////////////////////////////

} // End of namespace JetByteTools 

///////////////////////////////////////////////////////////////////////////////
// End of file
///////////////////////////////////////////////////////////////////////////////





⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -