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

📄 bcgtoolbarbutton.cpp

📁 一个完整的编辑器的代码(很值得参考
💻 CPP
📖 第 1 页 / 共 2 页
字号:
										globalData.clrBtnText);
		CString strText = m_strText;
		CRect rectText = rect;
		UINT uiTextFormat = 0;

		if (m_bTextBelow && bHorz)
		{
			ASSERT (bHorz);

			ptTextOffset.y += sizeImage.cy + TEXT_MARGIN;
			uiTextFormat = DT_CENTER | DT_WORDBREAK;

			rectText.left = (rect.left + rect.right - m_sizeText.cx) / 2 + ptTextOffset.x;
			rectText.right = (rect.left + rect.right + m_sizeText.cx) / 2;
		}
		else
		{
			if (IsDrawImage ())
			{
				if (bHorz)
				{
					ptTextOffset.x += ptImageOffset.x + sizeImage.cx;
				}
				else
				{
					ptTextOffset.y += TEXT_MARGIN;
				}
			}

			rectText.left = x + ptTextOffset.x + TEXT_MARGIN;
			uiTextFormat = DT_SINGLELINE;
		}

		if (bHorz)
		{
			rectText.top += ptTextOffset.y;
		}
		else
		{
			rectText.top = rect.bottom - ptTextOffset.y - TEXT_MARGIN;
			rectText.bottom = rect.top;

			rectText.right = rectText.left;
			rectText.left = x + ptTextOffset.x;

			strText.Remove (_T('&'));
			uiTextFormat |= DT_NOCLIP;
		}

		pDC->DrawText (strText, &rectText, uiTextFormat);
	}

	//--------------------
	// Draw button border:
	//--------------------
	if (!bCustomizeMode && HaveHotBorder () && bDrawBorder)
	{
		if (m_nStyle & (TBBS_PRESSED | TBBS_CHECKED))
		{
			//-----------------------
			// Pressed in or checked:
			//-----------------------
			pDC->Draw3dRect (&rect,
				afxData.clrBtnShadow,
				afxData.clrBtnHilite);
		}
		else if (bHot && !(m_nStyle & TBBS_DISABLED) &&
			!(m_nStyle & (TBBS_CHECKED | TBBS_INDETERMINATE)))
		{
			pDC->Draw3dRect (&rect, afxData.clrBtnHilite,
									afxData.clrBtnShadow);
		}
	}

}
//***********************************************************************************
SIZE CBCGToolbarButton::OnCalculateSize (
								CDC* pDC,
								const CSize& sizeDefault,
								BOOL bHorz)
{
	ASSERT_VALID (pDC);

	CSize size = sizeDefault;

	if (m_nStyle & TBBS_SEPARATOR)
	{
		if (bHorz)
		{
			size.cx = m_iImage > 0 ? m_iImage : SEPARATOR_WIDTH;
		}
		else
		{
			size.cy = SEPARATOR_WIDTH;
		}
	}
	else
	{
		if (!IsDrawImage () || GetImage () < 0)
		{
			if (bHorz)
			{
				size.cx = 0;
			}
			else
			{
				size.cy = 0;
			}
		}

		m_sizeText = CSize (0, 0);

		if (!m_strText.IsEmpty ())
		{
			if (m_bTextBelow && bHorz)
			{
				//----------------------------------------------------------
				// Try format text that it ocuppies no more tow lines an its
				// width less than 3 images:
				//----------------------------------------------------------
				CRect rectText (0, 0, 
					sizeDefault.cx * 3, sizeDefault.cy);
				pDC->DrawText (	m_strText, rectText, 
								DT_CENTER | DT_CALCRECT | DT_WORDBREAK);
				m_sizeText = rectText.Size ();
				m_sizeText.cx += 2 * TEXT_MARGIN;

				size.cx = max (size.cx, m_sizeText.cx) + 4 * TEXT_MARGIN;
				size.cy += m_sizeText.cy + 2 * TEXT_MARGIN;
			}
			else if (IsDrawText ())
			{
				CString strWithoutAmp = m_strText;
				strWithoutAmp.Remove (_T('&'));

				int iTextLen = pDC->GetTextExtent (strWithoutAmp).cx + 2 * TEXT_MARGIN + 6;

				if (bHorz)
				{
					size.cx += iTextLen;
				}
				else
				{
					size.cy += iTextLen;
				}
			}
		}
	}

	return size;
}
//************************************************************************************
BOOL CBCGToolbarButton::PrepareDrag (COleDataSource& srcItem)
{
	if (!CanBeStored ())
	{
		return TRUE;
	}

	try
	{
		CSharedFile globFile;
		CArchive ar (&globFile,CArchive::store);

		//---------------------------------
		// Save run-time class information:
		//---------------------------------
		CRuntimeClass* pClass = GetRuntimeClass ();
		ASSERT (pClass != NULL);

		ar.WriteClass (pClass);

		//---------------------
		// Save button context:
		//---------------------
		Serialize (ar);
		ar.Close();

		srcItem.CacheGlobalData (GetClipboardFormat (), globFile.Detach());
	}
	catch (COleException* pEx)
	{
		TRACE(_T("CBCGToolbarButton::PrepareDrag. OLE exception: %x\r\n"),
			pEx->m_sc);
		pEx->Delete ();
		return FALSE;
	}
	catch (CArchiveException* pEx)
	{
		TRACE(_T("CBCGToolbarButton::PrepareDrag. Archive exception\r\n"));
		pEx->Delete ();
		return FALSE;
	}

	return TRUE;
}
//****************************************************************************************
void CBCGToolbarButton::SetImage (int iImage)
{
	if (m_nStyle & TBBS_SEPARATOR)
	{
		m_iImage = iImage;	// Actualy, separator width!
		return;
	}

	if (m_bUserButton)
	{
		m_iUserImage = iImage;
	}
	else
	{
		m_iImage = iImage;
	}

	if (!m_bLocked)
	{
		if (m_nID != 0 && iImage != -1)
		{
			CMD_MGR.SetCmdImage (m_nID, iImage, m_bUserButton);
		}
		else if (m_nID != 0)
		{
			m_iImage = CMD_MGR.GetCmdImage (m_nID, FALSE);
			m_iUserImage = CMD_MGR.GetCmdImage (m_nID, TRUE);

			if (m_iImage == -1 && !m_bUserButton)
			{
				m_bUserButton = TRUE;
			}
			else if (m_iImage == -1 && m_bUserButton)
			{
				m_bUserButton = FALSE;
			}
		}
	}
}

/////////////////////////////////////////////////////////////////////////////
// CBCGToolbarButton diagnostics

#ifdef _DEBUG
void CBCGToolbarButton::AssertValid() const
{
	CObject::AssertValid();
}
//******************************************************************************************
void CBCGToolbarButton::Dump(CDumpContext& dc) const
{
	CObject::Dump (dc);

	CString strId;
	strId.Format (_T("%x"), m_nID);

	dc << "[" << strId << " " << m_strText << "]";
	dc << "\n";
}

#endif

int CBCGToolbarButton::OnDrawOnCustomizeList (
	CDC* pDC, const CRect& rect, BOOL bSelected)
{
	int iWidth = 0;

	ASSERT_VALID (pDC);

	CBCGToolBarImages* pImages = m_bUserButton ?
		CBCGToolBar::GetUserImages () :
		CBCGToolBar::GetImages ();
	
	CBCGToolBarImages* pCommonImages = CBCGToolBar::GetImages ();
	
	CSize sizeButton (0, 0);
	if (pCommonImages != NULL)
	{
		sizeButton = CSize (pCommonImages->GetImageSize ().cx + 6,
							pCommonImages->GetImageSize ().cy + 6);
	}

	CRect rectRow = rect;
	CRect rectText = rect;
	rectText.left += sizeButton.cx;

	iWidth = sizeButton.cx;

	//-------------------
	// Draw button image:
	//-------------------
	if (GetImage () >= 0 && pImages != NULL)
	{
		CRect rectImage = rect;
		rectImage.right = rectImage.left + sizeButton.cx;

		CBCGDrawState ds;
		pImages->PrepareDrawImage (ds);

		OnDraw (pDC, rectImage, pImages, TRUE, TRUE, bSelected);
		pImages->EndDrawImage (ds);

		rectRow.left = rectImage.right;
	}

	//-------------------
	// Draw button text:
	//-------------------
	if (!m_strText.IsEmpty ())
	{
		COLORREF clrTextOld;

		if (bSelected) 
		{
			pDC->FillSolidRect (rectRow, ::GetSysColor (COLOR_HIGHLIGHT));
			clrTextOld = pDC->SetTextColor (::GetSysColor (COLOR_HIGHLIGHTTEXT));
		} 
		else
		{
			pDC->FillRect (rectRow, &globalData.brBtnFace);
			clrTextOld = pDC->SetTextColor (::GetSysColor (COLOR_BTNTEXT));
		}

		pDC->SetBkMode (TRANSPARENT);
		pDC->DrawText (m_strText, rectText, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
		pDC->SetTextColor (clrTextOld);

		int iTextWidth = min (rectText.Width (), pDC->GetTextExtent (m_strText).cx);
		iWidth += iTextWidth;
	}

	if (bSelected)
	{
		pDC->DrawFocusRect (rectRow);
	}

	return iWidth;
}
//*************************************************************************************
BOOL CBCGToolbarButton::OnToolHitTest(const CWnd* pWnd, TOOLINFO* pTI)
{
	CFrameWnd* pTopFrame = (pWnd == NULL) ? 
		(CFrameWnd*) AfxGetMainWnd () : 
		pWnd->GetTopLevelFrame ();

	CBCGMDIFrameWnd* pMainFrame = DYNAMIC_DOWNCAST (CBCGMDIFrameWnd, pTopFrame);
	if (pMainFrame != NULL)
	{
		return pMainFrame->OnMenuButtonToolHitTest (this, pTI);
	}
	else	// Maybe, SDI frame...
	{
		CBCGFrameWnd* pFrame = DYNAMIC_DOWNCAST (CBCGFrameWnd, pTopFrame);
		if (pFrame != NULL)
		{
			return pFrame->OnMenuButtonToolHitTest (this, pTI);
		}
		else	// Maybe, OLE frame...
		{
			CBCGOleIPFrameWnd* pOleFrame = 
				DYNAMIC_DOWNCAST (CBCGOleIPFrameWnd, pFrame);
			if (pOleFrame != NULL)
			{
				return pOleFrame->OnMenuButtonToolHitTest (this, pTI);
			}
		}
	}

	return FALSE;
}

⌨️ 快捷键说明

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