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

📄 btnst.cpp

📁 一个有VC++开发的计算建筑工程行业软件 可以计算挖孔桩的方量,方格网的计算,支持保存和加载数据
💻 CPP
📖 第 1 页 / 共 2 页
字号:
        //
        pDC->SelectObject(pOldPen);
      }
    }
    else    
    {
      CBrush brBtnShadow(GetSysColor(COLOR_BTNSHADOW));
      pDC->FrameRect(&itemRect, &brBtnShadow);
    }
  }
  else // ...否则画非下压式按纽
  {
    CPen penBtnHiLight(PS_SOLID, 0, GetSysColor(COLOR_BTNHILIGHT)); // 白
    CPen pen3DLight(PS_SOLID, 0, GetSysColor(COLOR_3DLIGHT));       // 亮灰
    CPen penBtnShadow(PS_SOLID, 0, GetSysColor(COLOR_BTNSHADOW));   // 暗灰
    CPen pen3DDKShadow(PS_SOLID, 0, GetSysColor(COLOR_3DDKSHADOW)); // 黑

    if (m_bIsFlat == TRUE)
    {
      if (m_MouseOnButton == TRUE && m_bDrawBorder == TRUE)
      {

        // 白线
        pOldPen = pDC->SelectObject(&penBtnHiLight);
        pDC->MoveTo(itemRect.left, itemRect.bottom-1);
        pDC->LineTo(itemRect.left, itemRect.top);
        pDC->LineTo(itemRect.right, itemRect.top);

        // 暗灰线
        pDC->SelectObject(penBtnShadow);
        pDC->MoveTo(itemRect.left, itemRect.bottom-1);
        pDC->LineTo(itemRect.right-1, itemRect.bottom-1);
        pDC->LineTo(itemRect.right-1, itemRect.top-1);
        //
        pDC->SelectObject(pOldPen);
      }
    }
    else
    {
      //白线
      pOldPen = pDC->SelectObject(&penBtnHiLight);
      pDC->MoveTo(itemRect.left, itemRect.bottom-1);
      pDC->LineTo(itemRect.left, itemRect.top);
      pDC->LineTo(itemRect.right, itemRect.top);
      // 亮灰线
      pDC->SelectObject(pen3DLight);
      pDC->MoveTo(itemRect.left+1, itemRect.bottom-1);
      pDC->LineTo(itemRect.left+1, itemRect.top+1);
      pDC->LineTo(itemRect.right, itemRect.top+1);
 
      // 黑线
      pDC->SelectObject(pen3DDKShadow);
      pDC->MoveTo(itemRect.left, itemRect.bottom-1);
      pDC->LineTo(itemRect.right-1, itemRect.bottom-1);
      pDC->LineTo(itemRect.right-1, itemRect.top-1);
      //暗灰线
      pDC->SelectObject(penBtnShadow);
      pDC->MoveTo(itemRect.left+1, itemRect.bottom-2);
      pDC->LineTo(itemRect.right-2, itemRect.bottom-2);
      pDC->LineTo(itemRect.right-2, itemRect.top);
      //
      pDC->SelectObject(pOldPen);
    }
  }

  // 读按纽标题
  CString sTitle;
  GetWindowText(sTitle);

  // 假若我们不想要标签文本显示
  if (m_bShowText == FALSE) sTitle.Empty();

  CRect captionRect = lpDIS->rcItem;

  // 画图标
  if (m_hIconIn != NULL)
  {
    DrawTheIcon(pDC, &sTitle, &lpDIS->rcItem, &captionRect, bIsPressed, bIsDisabled);
  }

  // 写按纽标签文本 (if any)
  if (sTitle.IsEmpty() == FALSE)
  { 

    if (bIsPressed)
      captionRect.OffsetRect(1, 1);
    
    // ONLY FOR DEBUG 
    // Evidenzia il rettangolo in cui verra' centrata la caption 
    //CBrush brBtnShadow(RGB(255, 0, 0));
    //pDC->FrameRect(&captionRect, &brBtnShadow);

#ifdef ST_USE_MEMDC
	// 得到对话框字体
    CFont *pCurrentFont = GetFont(); 
    CFont *pOldFont = pDC->SelectObject(pCurrentFont);
#endif
    if ((m_MouseOnButton == TRUE) || (bIsPressed)) 
	{
      pDC->SetTextColor(GetActiveFgColor());
      pDC->SetBkColor(GetActiveBgColor());
    } 
	else 
	{
      pDC->SetTextColor(GetInactiveFgColor());
      pDC->SetBkColor(GetInactiveBgColor());
    }
    // 文本居中
    CRect centerRect = captionRect;
    pDC->DrawText(sTitle, -1, captionRect, DT_SINGLELINE|DT_CALCRECT);
    captionRect.OffsetRect((centerRect.Width() - captionRect.Width())/2, (centerRect.Height() - captionRect.Height())/2);
	/* RFU
    captionRect.OffsetRect(0, (centerRect.Height() - captionRect.Height())/2);
    captionRect.OffsetRect((centerRect.Width() - captionRect.Width())-4, (centerRect.Height() - captionRect.Height())/2);
	*/

    pDC->DrawState(captionRect.TopLeft(), captionRect.Size(), (LPCTSTR)sTitle, (bIsDisabled ? DSS_DISABLED : DSS_NORMAL), 
                   TRUE, 0, (CBrush*)NULL);
#ifdef ST_USE_MEMDC
    pDC->SelectObject(pOldFont);
#endif
  }

  if (m_bIsFlat == FALSE || (m_bIsFlat == TRUE && m_bDrawFlatFocus == TRUE))
  {
    // 画焦点矩形框
    if (bIsFocused)
    {
      CRect focusRect = itemRect;
      focusRect.DeflateRect(3, 3);
      pDC->DrawFocusRect(&focusRect);
    }
  }
} // End of DrawItem

////////////////////////////////////////////////////////////////////
//函数: DrawTheIcon(CDC* pDC, CString* title, RECT* rcItem, CRect* captionRect, BOOL IsPressed, BOOL IsDisabled)
//参数:CDC,标签文本,图标矩形区域,标签矩形区域,是否下压按纽,是否失效
//用途:画按纽上的图标
/////////////////////////////////////////////////////////////////////
void CButtonST::DrawTheIcon(CDC* pDC, CString* title, RECT* rcItem, CRect* captionRect, BOOL IsPressed, BOOL IsDisabled)
{
  CRect iconRect = rcItem;

  switch (m_nAlign)
  {
    case ST_ALIGN_HORIZ:
         if (title->IsEmpty())
         {
           // 图标水平居中
           iconRect.left += ((iconRect.Width() - m_cxIcon)/2);
         }
         else
         {
           // L'icona deve vedersi subito dentro il focus rect
           iconRect.left += 3;  
           captionRect->left += m_cxIcon + 3;
         }
         // 图标垂直居中
         iconRect.top += ((iconRect.Height() - m_cyIcon)/2);
         break;
    case ST_ALIGN_VERT:
         // 水平居中
         iconRect.left += ((iconRect.Width() - m_cxIcon)/2);
         if (title->IsEmpty())
         {
           // 垂直居中
           iconRect.top += ((iconRect.Height() - m_cyIcon)/2);           
         }
         else
         {
           captionRect->top += m_cyIcon;
         }
         break;
  }
    
  //假若按纽被压下图标也下移
  if (IsPressed) iconRect.OffsetRect(1, 1);
  // Ole'!
  pDC->DrawState(iconRect.TopLeft(), 
	               iconRect.Size(), 
				         (m_MouseOnButton == TRUE || IsPressed) ? m_hIconIn : m_hIconOut, 
				         (IsDisabled ? DSS_DISABLED : DSS_NORMAL), 
                 (CBrush*)NULL);
} // End of DrawTheIcon

////////////////////////////////////////////////////////////////////
//函数:CButtonST::PreSubclassWindow()  
//用途:当前类窗口设置
/////////////////////////////////////////////////////////////////////
void CButtonST::PreSubclassWindow() 
{
  //增加按纽的自画类型
  SetButtonStyle(GetButtonStyle() | BS_OWNERDRAW);
  CButton::PreSubclassWindow();
} // End of PreSubclassWindow

////////////////////////////////////////////////////////////////////
//函数:CButtonST::SetDefaultInactiveBgColor(BOOL bRepaint)
//参数:是否重画
//用途:设置按纽默认非活动时的背景色
/////////////////////////////////////////////////////////////////////
void CButtonST::SetDefaultInactiveBgColor(BOOL bRepaint)
{
	m_crInactiveBg = ::GetSysColor(COLOR_BTNFACE); 
	if (bRepaint == TRUE) Invalidate();
} // End of SetDefaultInactiveBgColor

////////////////////////////////////////////////////////////////////
//函数:CButtonST::SetDefaultInactiveBgColor(COLORREF crNew,BOOL bRepaint)
//参数:按纽背景色,是否重画
//用途:设置按纽默认非活动时的背景色
/////////////////////////////////////////////////////////////////////
void CButtonST::SetInactiveBgColor(COLORREF crNew, BOOL bRepaint)
{
	m_crInactiveBg = crNew; 
	if (bRepaint == TRUE) Invalidate();
} // End of SetInactiveBgColor

////////////////////////////////////////////////////////////////////
//函数:CButtonST::GetInactiveBgColor()
//用途:得到按纽非活动时的背景色
/////////////////////////////////////////////////////////////////////
const COLORREF CButtonST::GetInactiveBgColor()
{
	return m_crInactiveBg;
} // End of GetInactiveBgColor

////////////////////////////////////////////////////////////////////
//函数: CButtonST::SetDefaultInactiveFgColor(BOOL bRepaint)
//参数:是否重画
//用途:设置按纽默认非活动时的前景色
/////////////////////////////////////////////////////////////////////
void CButtonST::SetDefaultInactiveFgColor(BOOL bRepaint)
{
	m_crInactiveFg = ::GetSysColor(COLOR_BTNTEXT); 
	if (bRepaint == TRUE) Invalidate();
} // End of SetDefaultInactiveFgColor

//////////////////////////////////////////////////////////////////////////////
//函数: CButtonST::SetDefaultInactiveFgColor(COLORREF crNew,BOOL bRepaint)
//参数:前景色,是否重画
//用途:设置按纽非活动时的前景色
/////////////////////////////////////////////////////////////////////////////
void CButtonST::SetInactiveFgColor(COLORREF crNew, BOOL bRepaint)
{
	m_crInactiveFg = crNew; 
	if (bRepaint == TRUE) Invalidate();
} // End of SetInactiveFgColor

//////////////////////////////////////////////////////////////////////////////
//函数:  CButtonST::GetInactiveFgColor()
//用途:得到按纽非活动时的前景色
/////////////////////////////////////////////////////////////////////////////
const COLORREF CButtonST::GetInactiveFgColor()
{
	return m_crInactiveFg;
} // End of GetInactiveFgColor

////////////////////////////////////////////////////////////////////
//函数: CButtonST::SetDefaultActiveBgColor(BOOL bRepaint)
//参数:是否重画
//用途:设置按纽默认活动时的背景色
/////////////////////////////////////////////////////////////////////
void CButtonST::SetDefaultActiveBgColor(BOOL bRepaint)
{
	m_crActiveBg = ::GetSysColor(COLOR_BTNFACE); 
	if (bRepaint == TRUE) Invalidate();
} // End of SetDefaultActiveBgColor

////////////////////////////////////////////////////////////////////
//函数:CButtonST::SetActiveBgColor(COLORREF crNew, BOOL bRepaint)
//参数:按纽背景色,是否重画
//用途:设置按纽活动时的背景色
/////////////////////////////////////////////////////////////////////
void CButtonST::SetActiveBgColor(COLORREF crNew, BOOL bRepaint)
{
	m_crActiveBg = crNew; 
	if (bRepaint == TRUE) Invalidate();
} // End of SetActiveBgColor

////////////////////////////////////////////////////////////////////
//函数:CButtonST::GetActiveBgColor()
//用途:设置按纽活动时的背景色
/////////////////////////////////////////////////////////////////////
const COLORREF CButtonST::GetActiveBgColor()
{
	return m_crActiveBg;
} // End of GetActiveBgColor

////////////////////////////////////////////////////////////////////
//函数:CButtonST::SetDefaultActiveFgColor(BOOL bRepaint)
//参数:是否重画
//用途:设置按纽默认活动时的前景色
/////////////////////////////////////////////////////////////////////
void CButtonST::SetDefaultActiveFgColor(BOOL bRepaint)
{
	m_crActiveFg = ::GetSysColor(COLOR_BTNTEXT); 
	if (bRepaint == TRUE) Invalidate();
} // End of SetDefaultActiveFgColor

////////////////////////////////////////////////////////////////////
//函数:CButtonST::SetActiveFgColor(COLORREF crNew, BOOL bRepaint)
//参数:按纽前景色,是否重画
//用途:设置按纽活动时的前景色
/////////////////////////////////////////////////////////////////////
void CButtonST::SetActiveFgColor(COLORREF crNew, BOOL bRepaint)
{
	m_crActiveFg = crNew; 
	if (bRepaint == TRUE) Invalidate();
} // End of SetActiveFgColor

////////////////////////////////////////////////////////////////////
//函数:CButtonST::GetActiveFgColor()
//用途:得到按纽活动时的前景色
/////////////////////////////////////////////////////////////////////
const COLORREF CButtonST::GetActiveFgColor()
{
	return m_crActiveFg;
} // End of GetActiveFgColor

////////////////////////////////////////////////////////////////////
//函数:CButtonST::SetFlatFocus(BOOL bDrawFlatFocus, BOOL bRepaint)
//参数:按纽是否是平面按纽类型,是否重画
//用途:设置按纽平面焦点
/////////////////////////////////////////////////////////////////////
void CButtonST::SetFlatFocus(BOOL bDrawFlatFocus, BOOL bRepaint)
{
	m_bDrawFlatFocus = bDrawFlatFocus;
	
	//重画按纽
	if (bRepaint == TRUE) Invalidate();
} // End of SetFlatFocus

////////////////////////////////////////////////////////////////////
//函数:CButtonST::GetFlatFocus()
//用途:得到按纽平面焦点
/////////////////////////////////////////////////////////////////////
BOOL CButtonST::GetFlatFocus()
{
	return m_bDrawFlatFocus;
} // End of GetFlatFocus

////////////////////////////////////////////////////////////////////
//函数:CButtonST::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
//参数:按纽对象句柄,nHitTest,消息
//用途:设置按纽平面焦点
/////////////////////////////////////////////////////////////////////
BOOL CButtonST::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
{
	// 假若光标被指定那时使用它!
	if (m_hCursor != NULL)
	{
		::SetCursor(m_hCursor);
		return TRUE;
	}

	return CButton::OnSetCursor(pWnd, nHitTest, message);
} // End of OnSetCursor

#undef ST_USE_MEMDC
#undef ST_LIKE

⌨️ 快捷键说明

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