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

📄 bcgpeditlistbox.cpp

📁 远程网络监视程序的源码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
	CStatic::OnSize(nType, cx, cy);
	AdjustLayout ();
}
//***********************************************************************************
void CBCGPEditListBase::AdjustLayout ()
{
	if (GetSafeHwnd () == NULL)
	{
		return;
	}

	CRect rectClient;
	GetClientRect (rectClient);

	m_rectCaption = rectClient;

	CClientDC dc (this);

	CFont* pOldFont = NULL;
	if (m_font.GetSafeHandle () != NULL)
	{
		pOldFont = dc.SelectObject (&m_font);
	}
	else
	{
		CFont* pParentFont = GetParent ()->GetFont ();

		if (pParentFont != NULL)
		{
			pOldFont = dc.SelectObject (pParentFont);
			ASSERT (pOldFont != NULL);
		}
	}

	TEXTMETRIC tm;
	dc.GetTextMetrics (&tm);

	if (pOldFont != NULL)
	{
		dc.SelectObject (pOldFont);
	}

	m_rectCaption.bottom = m_rectCaption.top +
		max (tm.tmHeight * 4 / 3, m_sizeButton.cy);

	int x = rectClient.right - 1 - m_sizeButton.cx;
	for (POSITION pos = m_lstButtons.GetTailPosition (); pos != NULL;)
	{
		CBCGPButton* pButton = m_lstButtons.GetPrev (pos);
		ASSERT (pButton != NULL);

		pButton->MoveWindow (x, rectClient.top + 1, m_sizeButton.cx, 
			m_rectCaption.Height () - 2);
		x -= m_sizeButton.cx;
	}

	CWnd* pWndList = CWnd::FromHandle (GetListHwnd ());
	if (pWndList == NULL)
	{
		ASSERT (FALSE);
		return;
	}

	pWndList->MoveWindow (	rectClient.left,
							rectClient.top + m_rectCaption.Height (),
							rectClient.Width (),
							rectClient.Height () - m_rectCaption.Height ());
	OnSizeList ();
}
//************************************************************************************
BOOL CBCGPEditListBase::OnEraseBkgnd(CDC* /*pDC*/) 
{
	return TRUE;
}
//************************************************************************************
BOOL CBCGPEditListBase::OnCommand(WPARAM wParam, LPARAM lParam) 
{
	HWND hwnd = (HWND) lParam;

	int iButton = 0;
	for (POSITION pos = m_lstButtons.GetHeadPosition (); pos != NULL; iButton ++)
	{
		CBCGPButton* pButton = m_lstButtons.GetNext (pos);
		ASSERT (pButton);
	
		if (pButton->GetSafeHwnd () == hwnd)
		{
			CWnd* pWndList = CWnd::FromHandle (GetListHwnd ());
			if (pWndList == NULL)
			{
				ASSERT (FALSE);
			}
			else
			{
				pWndList->SetFocus ();
			}

			OnClickButton (iButton);
			return TRUE;
		}
	}
	
	return CStatic::OnCommand(wParam, lParam);
}
//***************************************************************************
void CBCGPEditListBase::OnSetFocus(CWnd* /*pOldWnd*/)
{
	CWnd* pWndList = CWnd::FromHandle (GetListHwnd ());
	if (pWndList == NULL)
	{
		ASSERT (FALSE);
	}
	else
	{
		pWndList->SetFocus ();
	}
}
//***************************************************************************
void CBCGPEditListBase::OnClickButton (int iButton)
{
	if (m_uiStandardBtns == 0)
	{
		return;
	}

	int iSelItem = GetSelItem ();
	UINT uiBtnID = GetButtonID (iButton);

	switch (uiBtnID)
	{ 
	case BGCEDITLISTBOX_BTN_NEW_ID:
		CreateNewItem ();
		return;

	case BGCEDITLISTBOX_BTN_DELETE_ID:
		if (iSelItem >= 0)
		{
			if (OnBeforeRemoveItem (iSelItem))
			{
				RemoveItem (iSelItem);
			}
		}
		break;

	case BGCEDITLISTBOX_BTN_UP_ID:
	case BGCEDITLISTBOX_BTN_DOWN_ID:
		if (iSelItem >= 0)
		{
			BOOL bIsUp = (uiBtnID == BGCEDITLISTBOX_BTN_UP_ID);
			if (bIsUp)
			{
				if (iSelItem == 0)
				{
					return;
				}
			}
			else
			{
				if (iSelItem == GetCount () - 1)
				{
					return;
				}
			}

			// Adjust list control:
			SetRedraw (FALSE);

			CString strLabel = GetItemText (iSelItem);
			DWORD dwData = GetItemData (iSelItem);

			m_bIsActualDelete = FALSE;
			RemoveItem (iSelItem);
			m_bIsActualDelete = TRUE;
			
			if (bIsUp)
			{
				iSelItem --;
			}
			else
			{
				iSelItem ++;
			}

			AddItem (strLabel, dwData, iSelItem);
			SelectItem (iSelItem);

			SetRedraw ();

			CWnd* pWndList = CWnd::FromHandle (GetListHwnd ());
			if (pWndList == NULL)
			{
				ASSERT (FALSE);
			}
			else
			{
				pWndList->Invalidate ();
			}

			if (bIsUp)
			{
				OnAfterMoveItemUp (iSelItem);
			}
			else
			{
				OnAfterMoveItemDown (iSelItem);
			}
		}
	}
}
//****************************************************************************
int CBCGPEditListBase::GetStdButtonNum (UINT uiStdBtn) const
{
	if ((m_uiStandardBtns & uiStdBtn) == 0)
	{
		return -1;
	}

	switch (uiStdBtn)
	{
	case BGCEDITLISTBOX_BTN_NEW:
		return GetButtonNum(BGCEDITLISTBOX_BTN_NEW_ID);	

	case BGCEDITLISTBOX_BTN_DELETE:
		return GetButtonNum(BGCEDITLISTBOX_BTN_DELETE_ID);

	case BGCEDITLISTBOX_BTN_UP:
		return GetButtonNum(BGCEDITLISTBOX_BTN_UP_ID);

	case BGCEDITLISTBOX_BTN_DOWN:
		return GetButtonNum(BGCEDITLISTBOX_BTN_DOWN_ID);
	}

	ASSERT (FALSE);
	return -1;
}
//***********************************************************************************
void CBCGPEditListBase::CreateNewItem ()
{
	int iLastItem = AddItem (_T(""));
	ASSERT (iLastItem >= 0);

	m_bNewItem = TRUE;
	EditItem (iLastItem);
}
//**************************************************************************
void CBCGPEditListBase::OnKey (WORD wKey, BYTE fFlags)
{
	int iSelItem = GetSelItem ();
	TCHAR cKey = (TCHAR) LOWORD (::MapVirtualKey (wKey, 2));

	if (fFlags == 0 &&	// No Ctrl, Shift or Alt
		iSelItem >= 0 &&
		(cKey == _T(' ') || wKey == VK_F2))
	{
		int iSelItem = GetSelItem ();

		if (iSelItem >= 0)
		{
			EditItem (iSelItem);
		}
	}
}
//**************************************************************************
LRESULT CBCGPEditListBase::OnGetFont(WPARAM /*wParam*/, LPARAM /*lParam*/)
{
    return (LRESULT) m_font.GetSafeHandle ();
}
//**************************************************************************
LRESULT CBCGPEditListBase::OnSetFont(WPARAM wParam, LPARAM lParam)
{
	LRESULT lResult = Default();

	CFont* pFont = CFont::FromHandle ((HFONT) wParam);
	if (pFont != NULL)
	{
		LOGFONT lf;
		pFont->GetLogFont (&lf);

		m_font.DeleteObject ();
		m_font.CreateFontIndirect (&lf);
	}

	if (::IsWindow(GetSafeHwnd ()) != NULL)
	{
		AdjustLayout ();

		if (lParam != 0)
		{
			Invalidate ();
			UpdateWindow ();
		}
	}

	return lResult;
}
//**************************************************************************
void CBCGPEditListBase::OnEndEditLabel (LPCTSTR lpszLabel)
{
	int iSelItem = GetSelItem ();
	if (iSelItem < 0)
	{
		ASSERT (FALSE);
		return;
	}

	CString strLabel = (lpszLabel != NULL) ? lpszLabel : _T("");

	if (!strLabel.IsEmpty ())
	{
		SetItemText (iSelItem, strLabel);

		if (m_bNewItem)
		{
			OnAfterAddItem (iSelItem);
		}
		else
		{
			OnAfterRenameItem (iSelItem);
		}
	}
	else
	{
		if (m_bNewItem)
		{
			RemoveItem (iSelItem);
		}
	}

	m_bNewItem = FALSE;
}
//****************************************************************************
BOOL CBCGPEditListBase::EnableButton (int iButtonNum, BOOL bEnable/* = TRUE*/)
{
	POSITION pos = m_lstButtons.FindIndex (iButtonNum);
	if (pos == NULL)
	{
		ASSERT (FALSE);
		return FALSE;
	}

	CBCGPButton* pButton = m_lstButtons.GetAt (pos);
	ASSERT_VALID (pButton);

	pButton->EnableWindow (bEnable);
	return TRUE;
}
//****************************************************************************
UINT CBCGPEditListBase::GetButtonID (int iButtonNum) const
{
	UINT uiID = 0;
	m_mapButtonIDs.Lookup (iButtonNum, uiID);

	return uiID;
}
//****************************************************************************
int CBCGPEditListBase::GetButtonNum (UINT uiID) const
{
	for (POSITION pos = m_mapButtonIDs.GetStartPosition (); pos != NULL;)
	{
		int iNum = -1;
		UINT uiButtonID = 0;

		m_mapButtonIDs.GetNextAssoc (pos, iNum, uiButtonID);

		if (uiButtonID == uiID)
		{
			return iNum;
		}
	}

	return -1;
}

//****************************************************************************
void CBCGPEditListBase::EnableBrowseButton (BOOL bEnable/* = TRUE*/)
{
	m_bBrowseButton = bEnable;
}
//****************************************************************************
void CBCGPEditListBase::SetGrayDisabledButtons (BOOL bOn)
{
	m_bGrayDisabledButtons = bOn;
}
//****************************************************************************
void CBCGPEditListBase::OnDrawBrowseButton (CDC* pDC, CRect rectBtn, 
										   BOOL bPressed, BOOL /*bHighlighted*/)
{
	ASSERT (m_bBrowseButton);
	ASSERT_VALID (pDC);

	pDC->FillRect (&rectBtn, &globalData.brBtnFace);

	COLORREF clrText = pDC->SetTextColor (globalData.clrBtnText);
	int nTextMode = pDC->SetBkMode (TRANSPARENT);
	CFont* pFont = (CFont*) pDC->SelectStockObject (DEFAULT_GUI_FONT);

	CString strText = _T("...");

	CRect rectText = rectBtn;
	rectText.DeflateRect (1, 2);
	rectText.OffsetRect (0, -2);

	if (bPressed)
	{
		rectText.OffsetRect (1, 1);
	}

	pDC->DrawText (strText, rectText, DT_SINGLELINE | DT_CENTER | DT_VCENTER);

	pDC->SetTextColor (clrText);
	pDC->SetBkMode (nTextMode);
	pDC->SelectObject (pFont);

	pDC->Draw3dRect (rectBtn, globalData.clrBtnDkShadow, globalData.clrBtnDkShadow);

	rectBtn.DeflateRect (1, 1);
	pDC->DrawEdge (rectBtn, bPressed ? BDR_SUNKENINNER : BDR_RAISEDINNER, BF_RECT);
}
//************************************************************************************
void CBCGPEditListBase::OnEnable(BOOL bEnable) 
{
	CStatic::OnEnable(bEnable);
	
	for (POSITION pos = m_lstButtons.GetTailPosition (); pos != NULL;)
	{
		CBCGPButton* pButton = m_lstButtons.GetPrev (pos);
		ASSERT_VALID (pButton);

		pButton->m_bGrayDisabled = !bEnable || m_bGrayDisabledButtons;
		pButton->EnableWindow (bEnable);
	}

	CWnd* pWndList = CWnd::FromHandle (GetListHwnd ());
	if (pWndList != NULL)
	{
		pWndList->EnableWindow (bEnable);
	}

	RedrawWindow ();
}

/////////////////////////////////////////////////////////////////////////////
// CBCGPEditListBox

CBCGPEditListBox::CBCGPEditListBox()
{
	m_pWndList = NULL;

⌨️ 快捷键说明

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