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

📄 btnst.cpp

📁 这是一本学习 window编程的很好的参考教材
💻 CPP
📖 第 1 页 / 共 2 页
字号:
        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
    {
      // Disegno i bordi a sinistra e in alto
      // White line
      pOldPen = pDC->SelectObject(&penBtnHiLight);
      pDC->MoveTo(itemRect.left, itemRect.bottom-1);
      pDC->LineTo(itemRect.left, itemRect.top);
      pDC->LineTo(itemRect.right, itemRect.top);
      // Light gray line
      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);
      // Disegno i bordi a destra e in basso
      // Black line
      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);
      // Dark gray line
      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);
    }

	penBtnHiLight.DeleteObject();
	pen3DLight.DeleteObject();
	penBtnShadow.DeleteObject();
	pen3DDKShadow.DeleteObject();
  }

  if(m_bIsDrawNormalBorder)
  {
	  CPen penBody(PS_SOLID, 1, m_nNormalBorderColor); //
	  pOldPen = pDC->SelectObject(&penBody);
	  pDC->MoveTo(itemRect.left, itemRect.bottom);
	  pDC->LineTo(itemRect.left, itemRect.top);
	  pDC->LineTo(itemRect.right, itemRect.top);
	  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);
  }

//
  // Write the button title (if any)
  if (sTitle.IsEmpty() == FALSE)
  {
    // Disegno la caption del bottone
    // Se il bottone e' premuto muovo la captionRect di conseguenza
    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);

	if(m_bIsSelect)
	{
      pDC->SetTextColor(GetSelectFgColor());
      pDC->SetBkColor(GetSelectBgColor());
	}
    else if ((m_MouseOnButton == TRUE) || (bIsPressed)) 
	{
      pDC->SetTextColor(GetActiveFgColor());
      pDC->SetBkColor(GetActiveBgColor());
    } 
	else 
	{
      pDC->SetTextColor(GetInactiveFgColor());
      pDC->SetBkColor(GetInactiveBgColor());
    }

	if(bIsDisabled)
	{
		pDC->SetTextColor(GetDisableFgColor());
	}

	if(m_bIsTransparent)
	{
		pDC->SetBkMode(TRANSPARENT);
	}

    // Center text
    CRect centerRect = captionRect;
    pDC->DrawText(sTitle, -1, captionRect, DT_SINGLELINE|DT_CALCRECT);
	switch(m_nHeightType)
	{
	case HEIGHTTYPE_2_3:
		captionRect.OffsetRect((centerRect.Width() - captionRect.Width())/2, (centerRect.Height() - captionRect.Height())*2/3);
		break;
	case HEIGHTTYPE_3_4:
		captionRect.OffsetRect((centerRect.Width() - captionRect.Width())/2, (centerRect.Height() - captionRect.Height())*3/4);
		break;
	default:
		captionRect.OffsetRect((centerRect.Width() - captionRect.Width())/2, (centerRect.Height() - captionRect.Height())/2);
		break;
	}
	/* 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, DSS_NORMAL, //(bIsDisabled ? DSS_DISABLED : DSS_NORMAL), 
                   TRUE, 0, (CBrush*)NULL);
  }

  if (m_bIsFlat == FALSE || (m_bIsFlat == TRUE && m_bDrawFlatFocus == TRUE))
  {
    // Draw the focus rect
    if (bIsFocused)
    {
      CRect focusRect = itemRect;
      focusRect.DeflateRect(3, 3);
      pDC->DrawFocusRect(&focusRect);
    }
  }
} // End of DrawItem


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())
         {
           // Center the icon horizontally
           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;
         }
         // Center the icon vertically
         iconRect.top += ((iconRect.Height() - m_cyIcon)/2);
         break;
    case ST_ALIGN_VERT:
         // Center the icon horizontally
         iconRect.left += ((iconRect.Width() - m_cxIcon)/2);
         if (title->IsEmpty())
         {
           // Center the icon vertically
           iconRect.top += ((iconRect.Height() - m_cyIcon)/2);           
         }
         else
         {
           captionRect->top += m_cyIcon;
         }
         break;
  }
    
  // If button is pressed then press the icon also
  if (IsPressed) iconRect.OffsetRect(1, 1);
  // Ole'!
  pDC->DrawState(iconRect.TopLeft(), 
	               iconRect.Size(), 
				         (m_MouseOnButton == TRUE || IsPressed) ? m_hIconIn : m_hIconOut, 
				         DSS_NORMAL, //(IsDisabled ? DSS_DISABLED : DSS_NORMAL), 
                 (CBrush*)NULL);
} // End of DrawTheIcon

void CButtonST::DrawTheBitmap(CDC* pDC, CString* title, RECT* rcItem, CRect* captionRect, BOOL IsPressed, BOOL IsDisabled)
{
  CRect bitmapRect = rcItem;

  pDC->DrawState(bitmapRect.TopLeft(), 
	               bitmapRect.Size(), 
				   m_bIsSelect ? m_hBitmapSel : ((m_MouseOnButton == TRUE || IsPressed) ? m_hBitmapIn : m_hBitmapOut), 
				         DSS_NORMAL, //(IsDisabled ? DSS_DISABLED : DSS_NORMAL), 
                 NULL);

  // If button is pressed then press the icon also
  if (IsPressed && m_bIsDrawDown && !m_bIsSelect) 
  {
	  bitmapRect.OffsetRect(1, 1);
	  // Ole'!
	  pDC->DrawState(bitmapRect.TopLeft(), 
					   bitmapRect.Size(), 
					   m_bIsSelect ? m_hBitmapSel : ((m_MouseOnButton == TRUE || IsPressed) ? m_hBitmapIn : m_hBitmapOut), 
							 DSS_NORMAL, //(IsDisabled ? DSS_DISABLED : DSS_NORMAL), 
					 NULL);
  }
} // End of DrawTheIcon


void CButtonST::PreSubclassWindow() 
{
	CRect rect; 
	GetClientRect(rect);
	m_ToolTip.Create(this);
	m_ToolTip.AddTool(this, m_sToolTipText, rect, 1);

	// Add BS_OWNERDRAW style
	SetButtonStyle(GetButtonStyle() | BS_OWNERDRAW );

	CButton::PreSubclassWindow();
} // End of PreSubclassWindow


void CButtonST::SetDefaultInactiveBgColor(BOOL bRepaint)
{
	m_crInactiveBg = ::GetSysColor(COLOR_BTNFACE); 
	if (bRepaint == TRUE) Invalidate();
} // End of SetDefaultInactiveBgColor


void CButtonST::SetInactiveBgColor(COLORREF crNew, BOOL bRepaint)
{
	m_crInactiveBg = crNew; 
	if (bRepaint == TRUE) Invalidate();
} // End of SetInactiveBgColor


const COLORREF CButtonST::GetInactiveBgColor()
{
	return m_crInactiveBg;
} // End of GetInactiveBgColor


void CButtonST::SetDefaultInactiveFgColor(BOOL bRepaint)
{
	m_crInactiveFg = ::GetSysColor(COLOR_BTNTEXT); 
	m_crSelectFg = m_crInactiveFg;
	if (bRepaint == TRUE) Invalidate();
} // End of SetDefaultInactiveFgColor


void CButtonST::SetInactiveFgColor(COLORREF crNew, BOOL bRepaint)
{
	m_crInactiveFg = crNew; 
	m_crSelectFg = m_crInactiveFg;
	if (bRepaint == TRUE) Invalidate();
} // End of SetInactiveFgColor


const COLORREF CButtonST::GetInactiveFgColor()
{
	return m_crInactiveFg;
} // End of GetInactiveFgColor


void CButtonST::SetDefaultActiveBgColor(BOOL bRepaint)
{
	m_crActiveBg = ::GetSysColor(COLOR_BTNFACE); 
	m_crSelectBg = m_crActiveBg;
	if (bRepaint == TRUE) Invalidate();
} // End of SetDefaultActiveBgColor


void CButtonST::SetActiveBgColor(COLORREF crNew, BOOL bRepaint)
{
	m_crActiveBg = crNew; 
	m_crSelectBg = m_crActiveBg;
	if (bRepaint == TRUE) Invalidate();
} // End of SetActiveBgColor


const COLORREF CButtonST::GetActiveBgColor()
{
	return m_crActiveBg;
} // End of GetActiveBgColor


void CButtonST::SetDefaultActiveFgColor(BOOL bRepaint)
{
	m_crActiveFg = ::GetSysColor(COLOR_BTNTEXT); 
	m_crSelectFg = m_crActiveFg;
	if (bRepaint == TRUE) Invalidate();
} // End of SetDefaultActiveFgColor


void CButtonST::SetActiveFgColor(COLORREF crNew, BOOL bRepaint)
{
	m_crActiveFg = crNew; 
	m_crSelectFg = m_crActiveFg;
	if (bRepaint == TRUE) Invalidate();
} // End of SetActiveFgColor


const COLORREF CButtonST::GetActiveFgColor()
{
	return m_crActiveFg;
} // End of GetActiveFgColor


void CButtonST::SetFlatFocus(BOOL bDrawFlatFocus, BOOL bRepaint)
{
	m_bDrawFlatFocus = bDrawFlatFocus;
	
	// Repaint the button
	if (bRepaint == TRUE) Invalidate();
} // End of SetFlatFocus


BOOL CButtonST::GetFlatFocus()
{
	return m_bDrawFlatFocus;
} // End of GetFlatFocus


BOOL CButtonST::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
{
	// If a cursor was specified then use it!
	if (m_hCursor != NULL)
	{
		::SetCursor(m_hCursor);
		return TRUE;
	}

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

void CButtonST::OnSetFocus(CWnd* pOldWnd) 
{
	CButton::OnSetFocus(pOldWnd);
	
	m_bIsFocus = TRUE;

	if(m_bFocusDrawBorder)
	{
		m_MouseOnButton = TRUE;
		Invalidate();
	}
}

void CButtonST::SetFocusDrawBorder(BOOL bDrawBorder)
{
	m_bFocusDrawBorder = bDrawBorder;
}

void CButtonST::SetTransparent(BOOL bIsTransparent)
{
	m_bIsTransparent = bIsTransparent;
}

BOOL CButtonST::PreTranslateMessage(MSG* pMsg) 
{
    m_ToolTip.RelayEvent(pMsg);
    return CButton::PreTranslateMessage(pMsg);
}

void CButtonST::SetToolTipText(CString sToolTipText)
{
	m_sToolTipText = sToolTipText;

    if (::IsWindow(GetSafeHwnd())) 
        m_ToolTip.UpdateTipText(m_sToolTipText, this, 1);
}

//----------------
//
void CButtonST::SetButton(UINT nID, CWnd *pParent)
{
	SubclassDlgItem(nID, pParent);

	SetActiveBgColor(COLOR_BK_FOCUS);
	SetInactiveBgColor(COLOR_BK_NORMAL);
	SetActiveFgColor(COLOR_TEXT_FOCUS);
	SetInactiveFgColor(COLOR_TEXT_NORMAL);
	SetNormalBorder(COLOR_BORDER);
	SetSelectBgColor(COLOR_BUTTON_SELECT_BK);
	SetSelectFgColor(COLOR_TEXT_SELECT);
}

void CButtonST::SetButtonEx(UINT nID, CWnd *pParent)
{
	SubclassDlgItem(nID, pParent);

	SetActiveBgColor(COLOR_BUTTON_EX_BK_FOCUS);
	SetInactiveBgColor(COLOR_BUTTON_EX_BK_NORMAL);
	SetActiveFgColor(COLOR_BUTTON_EX_TEXT_FOCUS);
	SetInactiveFgColor(COLOR_BUTTON_EX_TEXT_NORMAL);
}


void CButtonST::SetRadioButton(UINT nID, UINT nIDNormalBitmap, UINT nIDSelectBitmap, CWnd *pParent)
{
	SubclassDlgItem(nID, pParent);
	SetBitmaps(nIDNormalBitmap, nIDNormalBitmap, nIDSelectBitmap);
	DrawBorder(FALSE);
}

void CButtonST::SetLamp(UINT nID
		, UINT nBitmapFocusId
		, UINT nBitmapNormalId
		, UINT nBitmapSelectId
		, LPCTSTR lpszToolTipText
		, CWnd *pParent
)
{
	SubclassDlgItem(nID, pParent);
   	SetBitmaps(nBitmapFocusId, nBitmapNormalId, nBitmapSelectId);
	DrawBorder(FALSE);
	SetToolTipText(lpszToolTipText);
}

#undef ST_USE_MEMDC
#undef ST_LIKE

#pragma comment( exestr, "IEAssistant")

⌨️ 快捷键说明

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