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

📄 iconbtn.cpp

📁 界面编程的一些实现方法打包,VC中实现,比较新颖,大家
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	// Get dialog's font
    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());
    }
    // Center text
    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->SetBkMode(TRANSPARENT);
    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))
  {
    // Draw the focus rect
    if (bIsFocused)
    {
      CRect focusRect = itemRect;
      focusRect.DeflateRect(3, 3);
      pDC->DrawFocusRect(&focusRect);
    }
  } 
} // End of DrawItem


void CIConBtn::DrawTheIcon(CDC* pDC, CString* title, RECT* rcItem, CRect* captionRect, BOOL IsPressed, BOOL IsDisabled)
{
	CRect iconRect = rcItem;
	CRect btnRect;

  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_HORIZ_RIGHT:
			GetClientRect(&btnRect);
			if (title->IsEmpty())
			{
				// Center the icon horizontally
				iconRect.left += ((iconRect.Width() - m_cxIcon)/2);
			}
			else
			{
				// L'icona deve vedersi subito dentro il focus rect
				captionRect->right = captionRect->Width() - m_cxIcon - 3;
				captionRect->left = 3;
				iconRect.left = btnRect.right - 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, 
					(IsDisabled ? DSS_DISABLED : DSS_NORMAL), 
					(CBrush*)NULL);
} // End of DrawTheIcon


void CIConBtn::PreSubclassWindow() 
{
	UINT nBS;

	nBS = GetButtonStyle();

	// Check if this is the default button
	if (nBS & BS_DEFPUSHBUTTON) m_bIsDefault = TRUE;

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

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


BOOL CIConBtn::PreTranslateMessage(MSG* pMsg) 
{
	InitToolTip();
	m_ToolTip.RelayEvent(pMsg);
	
	return CButton::PreTranslateMessage(pMsg);
} // End of PreTranslateMessage


LRESULT CIConBtn::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
{
	if (message == WM_LBUTTONDBLCLK)
	{
		message = WM_LBUTTONDOWN;
	}
	return CButton::DefWindowProc(message, wParam, lParam);
} // End of DefWindowProc


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


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


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


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


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


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


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


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


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


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


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


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


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


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


BOOL CIConBtn::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 CIConBtn::SetTooltipText(CString* spText, BOOL bActivate)
{
	// We cannot accept NULL pointer
	if (spText == NULL) return;

	// Initialize ToolTip
	InitToolTip();

	// If there is no tooltip defined then add it
	if (m_ToolTip.GetToolCount() == 0)
	{
		CRect rectBtn; 
		GetClientRect(rectBtn);
		m_ToolTip.AddTool(this, (LPCTSTR)*spText, rectBtn, 1);
	}

	// Set text for tooltip
	m_ToolTip.UpdateTipText((LPCTSTR)*spText, this, 1);
	m_ToolTip.Activate(bActivate);
} // End of SetTooltipText


void CIConBtn::SetTooltipText(int nId, BOOL bActivate)
{
	CString sText;

	// load string resource
	sText.LoadString(nId);
	// If string resource is not empty
	if (sText.IsEmpty() == FALSE) SetTooltipText(&sText, bActivate);
} // End of SetTooltipText


void CIConBtn::ActivateTooltip(BOOL bActivate)
{
	// If there is no tooltip then do nothing
	if (m_ToolTip.GetToolCount() == 0) return;

	// Activate tooltip
	m_ToolTip.Activate(bActivate);
} // End of EnableTooltip


BOOL CIConBtn::GetDefault()
{
	return m_bIsDefault;
} // End of GetDefault


void CIConBtn::DrawTransparent(BOOL bRepaint)
{
	m_bDrawTransparent = TRUE;

	// Restore old bitmap (if any)
	if (m_dcBk.m_hDC != NULL && m_pbmpOldBk != NULL)
	{
		m_dcBk.SelectObject(m_pbmpOldBk);
	}

	m_bmpBk.Detach();
	m_dcBk.Detach();

	// Repaint the button
	if (bRepaint == TRUE) Invalidate();
} // End of DrawTransparent


void CIConBtn::InitToolTip()
{
	if (m_ToolTip.m_hWnd == NULL)
	{
		// Create ToolTip control
		m_ToolTip.Create(this);
		// Create inactive
		m_ToolTip.Activate(FALSE);
	}
} // End of InitToolTip


void CIConBtn::PaintBk(CDC * pDC)
{
	CClientDC clDC(GetParent());
	CRect rect;
	CRect rect1;

	GetClientRect(rect);

	GetWindowRect(rect1);
	GetParent()->ScreenToClient(rect1);

	if (m_dcBk.m_hDC == NULL)
	{
		m_dcBk.CreateCompatibleDC(&clDC);
		m_bmpBk.CreateCompatibleBitmap(&clDC, rect.Width(), rect.Height());
		m_pbmpOldBk = m_dcBk.SelectObject(&m_bmpBk);
		m_dcBk.BitBlt(0, 0, rect.Width(), rect.Height(), &clDC, rect1.left, rect1.top, SRCCOPY);
	}

	pDC->BitBlt(0, 0, rect.Width(), rect.Height(), &m_dcBk, 0, 0, SRCCOPY);
} // End of PaintBk


HBRUSH CIConBtn::CtlColor(CDC* pDC, UINT nCtlColor) 
{
	return (HBRUSH)::GetStockObject(NULL_BRUSH); 
} // End of CtlColor


void CIConBtn::OnSysColorChange() 
{
	CButton::OnSysColorChange();

	m_dcBk.DeleteDC();
	m_bmpBk.DeleteObject();	
} // End of OnSysColorChange
void CIConBtn::DrawTheBmp(CDC *pDC, CString *title, RECT *rcItem, CRect *captionRect, BOOL IsPressed, BOOL IsDisabled)
{
	CRect bmpRect = rcItem;
	CRect btnRect;

  switch (m_nAlign)
  {
    case ST_ALIGN_HORIZ:
         if (title->IsEmpty())
         {
           // Center the bmp horizontally
           bmpRect.left += ((bmpRect.Width() - m_cxBmp)/2);
         }
         else
         {
           // L'bmpa deve vedersi subito dentro il focus rect
           bmpRect.left += 3;  
           captionRect->left += m_cxBmp + 3;
         }
         // Center the bmp vertically
         bmpRect.top += ((bmpRect.Height() - m_cyBmp)/2);
         break;
	case ST_ALIGN_HORIZ_RIGHT:
			GetClientRect(&btnRect);
			if (title->IsEmpty())
			{
				// Center the bmp horizontally
				bmpRect.left += ((bmpRect.Width() - m_cxBmp)/2);
			}
			else
			{
				// L'bmpa deve vedersi subito dentro il focus rect
				captionRect->right = captionRect->Width() - m_cxBmp - 3;
				captionRect->left = 3;
				bmpRect.left = btnRect.right - m_cxBmp - 3;
				// Center the bmp vertically
				bmpRect.top += ((bmpRect.Height() - m_cyBmp)/2);
			}
	break;
    case ST_ALIGN_VERT:
         // Center the bmp horizontally
         bmpRect.left += ((bmpRect.Width() - m_cxBmp)/2);
         if (title->IsEmpty())
         {
           // Center the bmp vertically
           bmpRect.top += ((bmpRect.Height() - m_cyBmp)/2);           
         }
         else
         {
           captionRect->top += m_cyBmp;
         }
         break;
  }
    
  // If button is pressed then press the bmp also
  if (IsPressed) bmpRect.OffsetRect(1, 1);
  // Ole'!
  pDC->DrawState(	bmpRect.TopLeft(),
					bmpRect.Size(), 
					(m_MouseOnButton == TRUE || IsPressed) ? m_hBmpIn : m_hBmpOut, 
					(IsDisabled ? DSS_DISABLED : DSS_NORMAL), 
					NULL);
}


#undef ST_USE_MEMDC
#undef ST_LIKEIE



void CIConBtn::SetBitmap(int idIn, int idOut)
{
	if (idIn==0) {//Add and Modify by Coolboy 2000-1-10
		m_hBmpIn=NULL;
		m_hBmpOut=NULL;
		return;
	}
	m_hBmpIn = (HBITMAP)::LoadImage(AfxGetInstanceHandle(),
				   MAKEINTRESOURCE(idIn),
				   IMAGE_BITMAP,
				   0, 0,
				   LR_LOADMAP3DCOLORS);
   idOut=(idOut==0)?idIn:idOut;
   m_hBmpOut = (HBITMAP)::LoadImage(AfxGetInstanceHandle(),
				   MAKEINTRESOURCE(idOut),
				   IMAGE_BITMAP,
				   0, 0,
				   LR_LOADMAP3DCOLORS);

   // Get the bitmap parameters, because we will need width and height
   BITMAP bmpval;
   ::GetObject(m_hBmpIn, sizeof(BITMAP), &bmpval);
   m_cxBmp=(BYTE)bmpval.bmWidth;
   m_cyBmp=(BYTE)bmpval.bmHeight;
}

void CIConBtn::SetText(LPCTSTR szText)
{
   m_szText=szText;
}

⌨️ 快捷键说明

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