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

📄 bcgptoolbarcomboboxbutton.cpp

📁 远程网络监视程序的源码
💻 CPP
📖 第 1 页 / 共 3 页
字号:

		return m_lstItemData.GetAt (pos);
	}
}
//**************************************************************************************
void CBCGPToolbarComboBoxButton::RemoveAllItems ()
{
	m_lstItems.RemoveAll ();
	
	ClearData ();
	m_lstItemData.RemoveAll ();

	if (m_pWndCombo->GetSafeHwnd () != NULL)
	{
		m_pWndCombo->ResetContent ();
	}

	m_strEdit.Empty ();

	if (m_pWndEdit->GetSafeHwnd () != NULL)
	{
		m_pWndEdit->SetWindowText (m_strEdit);
	}
}
//**************************************************************************************
int CBCGPToolbarComboBoxButton::GetCount () const
{
	return m_lstItems.GetCount ();
}
//**************************************************************************************
void CBCGPToolbarComboBoxButton::AdjustRect ()
{
	if (m_pWndCombo->GetSafeHwnd () == NULL ||
		m_rect.IsRectEmpty ())
	{
		m_rectCombo.SetRectEmpty ();
		m_rectButton.SetRectEmpty ();
		return;
	}

	CBCGPBaseControlBar* pParentBar = NULL;
	CWnd* pNextBar = m_pWndCombo->GetParent ();
	while (pParentBar == NULL && pNextBar != NULL)
	{
		pParentBar = DYNAMIC_DOWNCAST (CBCGPBaseControlBar, pNextBar);
		pNextBar = pNextBar->GetParent ();
	}


	
	CRect rectParent;
	m_pWndCombo->SetWindowPos (NULL,
		m_rect.left + iHorzMargin, m_rect.top,
		m_rect.Width () - 2 * iHorzMargin, m_nDropDownHeight,
		SWP_NOZORDER | SWP_NOACTIVATE);
	m_pWndCombo->SetEditSel (-1, 0);

	{
		CRect rect;
		m_pWndCombo->GetWindowRect (&m_rectCombo);
		m_pWndCombo->ScreenToClient (&m_rectCombo);
		m_pWndCombo->MapWindowPoints (m_pWndCombo->GetParent (), &m_rectCombo);

	}

	if (m_bFlat)
	{
		m_rectButton = m_rectCombo;
		m_rectButton.left = m_rectButton.right - CMenuImages::Size ().cx * 2;

		m_rectButton.DeflateRect (2, 2);

		m_rect.left = m_rectCombo.left - iHorzMargin;
		m_rect.right = m_rectCombo.right + iHorzMargin;

		if (!m_bTextBelow || m_strText.IsEmpty ())
		{
			m_rect.top = m_rectCombo.top;
			m_rect.bottom = m_rectCombo.bottom;
		}

		if (m_pWndEdit != NULL)
		{
			CRect rectEdit = m_rect;

			const int iBorderOffset = 3;

			m_pWndEdit->SetWindowPos (NULL,
				m_rect.left + iHorzMargin + iBorderOffset, m_rect.top + iBorderOffset,
				m_rect.Width () - 2 * iHorzMargin - m_rectButton.Width () - iBorderOffset - 3,
				m_rectCombo.Height () - 2 * iBorderOffset,
				SWP_NOZORDER | SWP_NOACTIVATE);
		}
	}
	else
	{
		m_rectButton.SetRectEmpty ();
	}
}
//**************************************************************************************
void CBCGPToolbarComboBoxButton::SetHotEdit (BOOL bHot)
{
	if (m_bIsHotEdit != bHot)
	{
		m_bIsHotEdit = bHot;

		if (m_pWndCombo->GetParent () != NULL)
		{
			m_pWndCombo->GetParent ()->InvalidateRect (m_rectCombo);
			m_pWndCombo->GetParent ()->UpdateWindow ();
		}
	}
}
//**************************************************************************************
BOOL CBCGPToolbarComboBoxButton::NotifyCommand (int iNotifyCode)
{
	if (m_pWndCombo->GetSafeHwnd () == NULL)
	{
		return FALSE;
	}

	if (m_bFlat && iNotifyCode == 0)
	{
		return TRUE;
	}

	if (m_bFlat && m_pWndCombo->GetParent () != NULL)
	{
		m_pWndCombo->GetParent ()->InvalidateRect (m_rectCombo);
		m_pWndCombo->GetParent ()->UpdateWindow ();
	}

	switch (iNotifyCode)
	{
	case CBN_SELENDOK:
		{
			m_iSelIndex = m_pWndCombo->GetCurSel ();
			if (m_iSelIndex < 0)
			{
				return FALSE;
			}

			m_pWndCombo->GetLBText (m_iSelIndex, m_strEdit);
			if (m_pWndEdit != NULL)
			{
				m_pWndEdit->SetWindowText (m_strEdit);
			}

			//------------------------------------------------------
			// Try set selection in ALL comboboxes with the same ID:
			//------------------------------------------------------
			CObList listButtons;
			if (CBCGPToolBar::GetCommandButtons (m_nID, listButtons) > 0)
			{
				for (POSITION posCombo = listButtons.GetHeadPosition (); posCombo != NULL;)
				{
					CBCGPToolbarComboBoxButton* pCombo = 
						DYNAMIC_DOWNCAST (CBCGPToolbarComboBoxButton, listButtons.GetNext (posCombo));

					if (pCombo != NULL && pCombo != this)
					{
						pCombo->SelectItem (m_pWndCombo->GetCurSel (), FALSE /* Don't notify */);
					}
				}
			}
		}

		if (m_pWndEdit != NULL)
		{
			m_pWndEdit->SetFocus ();
		}

		return TRUE;

	case CBN_KILLFOCUS:
	case CBN_EDITUPDATE:
		return TRUE;

	case CBN_SETFOCUS:
		if (m_pWndEdit != NULL)
		{
			m_pWndEdit->SetFocus ();
		}
		return TRUE;

	case CBN_SELCHANGE:	// yurig: process selchange
		if (m_pWndEdit != NULL)
		{
			CString strEdit;
			m_pWndCombo->GetLBText (m_pWndCombo->GetCurSel(), strEdit);
			m_pWndEdit->SetWindowText (strEdit);
		}

		return TRUE;

	case CBN_EDITCHANGE:
		{
			// BEGIN: fixed a bug by scaton
			CString strEdit;
			if (m_pWndEdit != NULL)
			{
				m_pWndEdit->GetWindowText(strEdit);
			}
			else
			{
				m_pWndCombo->GetWindowText (strEdit);
			}

			if( strEdit == m_strEdit )
			{
				return TRUE;
			}
			m_strEdit = strEdit;
			// END: fixed a bug by scaton

			//------------------------------------------------------
			// Try set text of ALL comboboxes with the same ID:
			//------------------------------------------------------
			CObList listButtons;
			if (CBCGPToolBar::GetCommandButtons (m_nID, listButtons) > 0)
			{
				for ( POSITION posCombo = listButtons.GetHeadPosition (); posCombo != NULL; )
				{
					CBCGPToolbarComboBoxButton* pCombo = 
						DYNAMIC_DOWNCAST (CBCGPToolbarComboBoxButton, listButtons.GetNext
						(posCombo));

					if (pCombo != NULL && pCombo != this)
					{
						if (pCombo->GetComboBox () != NULL)
						{
							pCombo->GetComboBox ()->SetWindowText(m_strEdit);
						}

						// BEGIN: fixed a bug by scaton
						if( pCombo->GetEditCtrl() != NULL )
						{
							pCombo->GetEditCtrl()->SetWindowText(m_strEdit);
						}
						// END: fixed a bug by scaton

						pCombo->m_strEdit = m_strEdit;
					}
				}
			}
		}
		return TRUE;
	}

	return FALSE;
}
//**************************************************************************************
void CBCGPToolbarComboBoxButton::OnAddToCustomizePage ()
{
	CObList listButtons;	// Existing buttons with the same command ID

	if (CBCGPToolBar::GetCommandButtons (m_nID, listButtons) == 0)
	{
		return;
	}

	CBCGPToolbarComboBoxButton* pOther = 
		(CBCGPToolbarComboBoxButton*) listButtons.GetHead ();
	ASSERT_VALID (pOther);
	ASSERT_KINDOF (CBCGPToolbarComboBoxButton, pOther);

	CopyFrom (*pOther);
}
//**************************************************************************************
HBRUSH CBCGPToolbarComboBoxButton::OnCtlColor (CDC* pDC, UINT /*nCtlColor*/)
{
	pDC->SetTextColor (globalData.clrWindowText);
	pDC->SetBkColor (globalData.clrWindow);

	return (HBRUSH) globalData.brWindow.GetSafeHandle ();
}
//**************************************************************************************
void CBCGPToolbarComboBoxButton::OnDraw (CDC* pDC, const CRect& rect, CBCGPToolBarImages* pImages,
						BOOL bHorz, BOOL bCustomizeMode,
						BOOL bHighlight,
						BOOL bDrawBorder, BOOL bGrayDisabledButtons)
{
	if (m_pWndCombo == NULL || m_pWndCombo->GetSafeHwnd () == NULL || !bHorz)
	{
		CBCGPToolbarButton::OnDraw (pDC, rect, pImages,
							bHorz, bCustomizeMode,
							bHighlight, bDrawBorder, bGrayDisabledButtons);
		return;
	}

	BOOL bDisabled = (bCustomizeMode && !IsEditable ()) ||
		(!bCustomizeMode && (m_nStyle & TBBS_DISABLED));
		
	pDC->SetTextColor (bDisabled ?
		globalData.clrGrayedText : 
			(bHighlight) ?	CBCGPToolBar::GetHotTextColor () :
							globalData.clrBarText);

	if (m_bFlat)
	{
		if (m_bIsHotEdit)
		{
			bHighlight = TRUE;
		}
		
		//--------------
		// Draw combbox:
		//--------------
		CRect rectCombo = m_rectCombo;

		//-------------
		// Draw border:
		//-------------
		CBCGPVisualManager::GetInstance ()->OnDrawComboBorder (
			pDC, rectCombo, bDisabled, m_pWndCombo->GetDroppedState (),
			bHighlight, this);

		rectCombo.DeflateRect (2, 2);

		pDC->FillSolidRect (rectCombo, 
			bDisabled ? globalData.clrBarFace : globalData.clrWindow);

		if (bDisabled)
		{
			pDC->Draw3dRect (&rectCombo,
				globalData.clrBarHilite,
				globalData.clrBarHilite);
		}

		int nPrevTextColor = pDC->GetTextColor ();
		//-----------------------
		// Draw drop-down button:
		//-----------------------
		CBCGPVisualManager::GetInstance ()->OnDrawComboDropButton (
			pDC, m_rectButton, bDisabled, m_pWndCombo->GetDroppedState (),
			bHighlight, this);
		
		if (bDisabled)
		{
			pDC->SetTextColor (nPrevTextColor);
		}

		//-----------------
		// Draw combo text:
		//-----------------
		if (!m_strEdit.IsEmpty ())
		{
			CRect rectText = rectCombo;
			rectText.right = m_rectButton.left;
			rectText.DeflateRect (2, 2);

			if (m_pWndEdit == NULL)
			{
				if (m_pWndCombo->GetStyle () & (CBS_OWNERDRAWFIXED | CBS_OWNERDRAWVARIABLE))
				{
					DRAWITEMSTRUCT dis;
					memset (&dis, 0, sizeof (DRAWITEMSTRUCT));

					dis.hDC = pDC->GetSafeHdc ();
					dis.rcItem = rectText;
					dis.CtlID = m_nID;
					dis.itemID = m_pWndCombo->GetCurSel ();
					dis.hwndItem = m_pWndCombo->GetSafeHwnd ();
					dis.CtlType = ODT_COMBOBOX;

					m_pWndCombo->DrawItem (&dis);
				}
				else
				{
					pDC->DrawText (m_strEdit, rectText, DT_VCENTER | DT_SINGLELINE);
				}
			}
		}

		pDC->SetTextColor (nPrevTextColor);
	}

	if ((m_bTextBelow && bHorz) && !m_strText.IsEmpty())
	{
		CRect rectText = rect;
		rectText.top = (m_rectCombo.bottom + rect.bottom - m_sizeText.cy) / 2;
		
		pDC->DrawText (m_strText, &rectText, DT_CENTER | DT_WORDBREAK);
	}
}
//**************************************************************************************
BOOL CBCGPToolbarComboBoxButton::OnClick (CWnd* pWnd, BOOL /*bDelay*/)
{	
	if (m_pWndCombo == NULL || m_pWndCombo->GetSafeHwnd () == NULL || !m_bHorz)
	{
		return FALSE;
	}

	if (m_bFlat)
	{
		if (m_pWndEdit == NULL)
		{
			m_pWndCombo->SetFocus ();
		}
		else
		{
			m_pWndEdit->SetFocus ();
		}

		m_pWndCombo->ShowDropDown ();

		if (pWnd != NULL)
		{
			pWnd->InvalidateRect (m_rectCombo);
		}
	}

	return TRUE;
}
//**************************************************************************************
BOOL CBCGPToolbarComboBoxButton::SelectItem (int iIndex, BOOL bNotify)
{
	if (iIndex < 0 || iIndex >= m_lstItems.GetCount ())
	{
		return FALSE;
	}

	m_iSelIndex = iIndex;

	if (m_pWndCombo->GetSafeHwnd () == NULL)
	{
		return TRUE;
	}

	m_pWndCombo->GetLBText (iIndex, m_strEdit);
	if (m_pWndEdit != NULL)
	{
		CString strEdit;
		m_pWndEdit->GetWindowText (strEdit);

		if (strEdit != m_strEdit)
		{
			m_pWndEdit->SetWindowText (m_strEdit);
		}
	}

	if (m_pWndCombo->GetCurSel () == iIndex)
	{
		// Already selected
		return TRUE;
	}

	if (m_pWndCombo->SetCurSel (iIndex) != CB_ERR)
	{
		if (bNotify)
		{
			NotifyCommand (CBN_SELENDOK);
		}

		return TRUE;
	}
	else
	{
		return FALSE;
	}
}
//**************************************************************************************
BOOL CBCGPToolbarComboBoxButton::SelectItem (DWORD dwData)
{
	int iIndex = 0;
	for (POSITION pos = m_lstItemData.GetHeadPosition (); pos != NULL; iIndex ++)
	{
		if (m_lstItemData.GetNext (pos) == dwData)
		{
			return SelectItem (iIndex);
		}
	}

	return FALSE;
}
//**************************************************************************************
BOOL CBCGPToolbarComboBoxButton::SelectItem (LPCTSTR lpszText)
{
	ASSERT (lpszText != NULL);

	int iIndex = 0;
	for (POSITION pos = m_lstItems.GetHeadPosition (); pos != NULL; iIndex ++)
	{
		if (m_lstItems.GetNext (pos) == lpszText)
		{
			return SelectItem (iIndex);
		}
	}

	return FALSE;
}
//******************************************************************************************
BOOL CBCGPToolbarComboBoxButton::DeleteItem (int iIndex)
{
	if (iIndex < 0 || iIndex >= m_lstItems.GetCount ())
	{
		return FALSE;
	}

	POSITION pos = m_lstItems.FindIndex (iIndex);
	if (pos == NULL)
	{
		ASSERT (FALSE);
		return FALSE;
	}

	m_lstItems.RemoveAt (pos);

	pos = m_lstItemData.FindIndex (iIndex);
	if (pos == NULL)
	{
		ASSERT (FALSE);
		return FALSE;
	}

	m_lstItemData.RemoveAt (pos);

	if (m_pWndCombo->GetSafeHwnd () != NULL)
	{
		m_pWndCombo->DeleteString (iIndex);
	}

	if (iIndex == m_iSelIndex)
	{
		int iSelIndex = m_iSelIndex;
		if (iSelIndex >= m_lstItems.GetCount ())
		{
			iSelIndex = m_lstItems.GetCount () - 1;
		}

		SelectItem (iSelIndex, FALSE);
	}

	return TRUE;
}
//**************************************************************************************
BOOL CBCGPToolbarComboBoxButton::DeleteItem (DWORD dwData)
{
	int iIndex = 0;
	for (POSITION pos = m_lstItemData.GetHeadPosition (); pos != NULL; iIndex ++)
	{
		if (m_lstItemData.GetNext (pos) == dwData)
		{
			return DeleteItem (iIndex);
		}
	}

	return FALSE;
}
//**************************************************************************************
BOOL CBCGPToolbarComboBoxButton::DeleteItem (LPCTSTR lpszText)
{
	ASSERT (lpszText != NULL);

	int iIndex = 0;
	for (POSITION pos = m_lstItems.GetHeadPosition (); pos != NULL; iIndex ++)
	{
		if (m_lstItems.GetNext (pos) == lpszText)
		{
			return DeleteItem (iIndex);
		}
	}

	return FALSE;
}
//******************************************************************************************
int CBCGPToolbarComboBoxButton::OnDrawOnCustomizeList (
	CDC* pDC, const CRect& rect, BOOL bSelected)
{
	int iWidth = CBCGPToolbarButton::OnDrawOnCustomizeList (pDC, rect, bSelected);

	//------------------------------
	// Simulate combobox appearance:
	//------------------------------
	CRect rectCombo = rect;
	int iComboWidth = rect.Width () - iWidth - 5;

	if (iComboWidth < 10)
	{
		return rect.Width ();
	}

	rectCombo.left = rectCombo.right - iComboWidth;
	rectCombo.DeflateRect (2, 2);

	pDC->FillSolidRect (rectCombo, globalData.clrWindow);

	CBCGPVisualManager::GetInstance()->OnDrawComboBorder (pDC,
		rectCombo, FALSE, FALSE, TRUE, this);

	CRect rectBtn = rectCombo;
	rectBtn.left = rectBtn.right - rectBtn.Height ();
	rectBtn.DeflateRect (2, 2);

	CBCGPVisualManager::GetInstance()->OnDrawComboDropButton(pDC, rectBtn,
						FALSE, FALSE, FALSE, NULL);
	return rect.Width ();
}
//********************************************************************************************
CComboBox* CBCGPToolbarComboBoxButton::CreateCombo (CWnd* pWndParent, const CRect& rect)
{
	CComboBox* pWndCombo = new CComboBox;
	if (!pWndCombo->Create (m_dwStyle, rect, pWndParent, m_nID))
	{
		delete pWndCombo;
		return NULL;
	}

	return pWndCombo;
}
//****************************************************************************************

⌨️ 快捷键说明

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