コード例 #1
0
void CButtonST::DrawItem(LPDRAWITEMSTRUCT lpDIS)
{
    CDC*    pDC = CDC::FromHandle(lpDIS->hDC);

    CPen    *pOldPen;

    // Checkbox or Radiobutton style ?
    if (m_bIsCheckBox == TRUE)
    {
        m_bIsPressed  =  (lpDIS->itemState & ODS_SELECTED) || (m_nCheck != 0);
        //m_bIsPressed = TRUE;
    }
    // Normal button OR other button style ...
    else
    {
        m_bIsPressed = (lpDIS->itemState & ODS_SELECTED);
    }

    m_bIsFocused  = (lpDIS->itemState & ODS_FOCUS);
    m_bIsDisabled = (lpDIS->itemState & ODS_DISABLED);

    CRect itemRect = lpDIS->rcItem;

    pDC->SetBkMode(TRANSPARENT);

    if (m_bIsFlat == FALSE)
    {
        if (m_bIsFocused || (GetDefault() == TRUE))
        {
            CBrush br(RGB(0,0,0));  
            pDC->FrameRect(&itemRect, &br);
            itemRect.DeflateRect(1, 1);
        } // if
    } // if

    // Prepare draw... paint button background

    // Draw transparent?
    if (m_bDrawTransparent == TRUE)
    {
        PaintBk(pDC);
    }
    else
    {
        OnDrawBackground(pDC, &itemRect);
    }

    // Draw pressed button
    if (m_bIsPressed)
    {
		CRect captionRect1 = lpDIS->rcItem;
        if (m_bIsFlat == TRUE)
        {
            if (m_bDrawBorder)
            {
                OnDrawBorder(pDC, &itemRect);
		
            }
        }
        else    
        {
            CBrush brBtnShadow(GetSysColor(COLOR_BTNSHADOW));
            pDC->FrameRect(&itemRect, &brBtnShadow);
        }
    }
    else // ...else draw non pressed button
    {
        CPen penBtnHiLight(PS_SOLID, 0, GetSysColor(COLOR_BTNHILIGHT)); // White
        CPen pen3DLight(PS_SOLID, 0, GetSysColor(COLOR_3DLIGHT));       // Light gray
        CPen penBtnShadow(PS_SOLID, 0, GetSysColor(COLOR_BTNSHADOW));   // Dark gray
        CPen pen3DDKShadow(PS_SOLID, 0, GetSysColor(COLOR_3DDKSHADOW)); // Black

        if (m_bIsFlat == TRUE)
        {
            if (m_bMouseOnButton && m_bDrawBorder)
            {
                OnDrawBorder(pDC, &itemRect);
            }
        }
        else
        {
            // Draw top-left borders
            // 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);
            // Draw bottom-right borders
            // 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);
        }
    }

    // Read the button's title
    CString sTitle;
    GetWindowText(sTitle);

    CRect captionRect = lpDIS->rcItem;

    // Draw the icon
    if (m_csIcons[0].hIcon != NULL)
    {
        DrawTheIcon(pDC, !sTitle.IsEmpty(), &lpDIS->rcItem, &captionRect, m_bIsPressed, m_bIsDisabled);
    }

    if (m_csBitmaps[0].hBitmap != NULL)
    {
        pDC->SetBkColor(RGB(255,255,255));
        DrawTheBitmap(pDC, !sTitle.IsEmpty(), &lpDIS->rcItem, &captionRect, m_bIsPressed, m_bIsDisabled);
    } // if

    // Write the button title (if any)
    if (sTitle.IsEmpty() == FALSE)
    {
        // Draw the button's title
        // If button is pressed then "press" title also
        if (m_bIsPressed && m_bIsCheckBox == FALSE)
        {
            //将偏移1像素的粗体显示效果去除。
            captionRect.OffsetRect(0, 0);
        }
    
        // ONLY FOR DEBUG 
        //CBrush brBtnShadow(RGB(255, 0, 0));
        //pDC->FrameRect(&captionRect, &brBtnShadow);

        /*
        if ((m_bMouseOnButton == 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_WORDBREAK | DT_CENTER | 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);
        */
        if (m_bIsDisabled)
        {
            captionRect.OffsetRect(1, 1);
            pDC->SetTextColor(::GetSysColor(COLOR_3DHILIGHT));
            pDC->DrawText(sTitle, -1, captionRect, DT_WORDBREAK | DT_CENTER);
            captionRect.OffsetRect(-1, -1);
            pDC->SetTextColor(::GetSysColor(COLOR_3DSHADOW));
            pDC->DrawText(sTitle, -1, captionRect, DT_WORDBREAK | DT_CENTER);
        }
        else
        {
            if (m_bMouseOnButton || m_bIsPressed) 
            {
                pDC->SetTextColor(m_crColors[BTNST_COLOR_FG_IN]);
                pDC->SetBkColor(m_crColors[BTNST_COLOR_BK_IN]);
            }
            else 
            {
                pDC->SetTextColor(m_crColors[BTNST_COLOR_FG_OUT]);
                pDC->SetBkColor(m_crColors[BTNST_COLOR_BK_OUT]);
            }

            if(m_bBigDaddy)
            {
                if(m_bIsFlat == TRUE)
                {
                    if (m_bDrawBorder)
                    {
                        OnDrawBorder(pDC, &itemRect);
                    }
                }
                else
                {
                    CBrush brBtnShadow(RGB(255,255,0));
                    pDC->FrameRect(&itemRect, &brBtnShadow);
                }
            }
            pDC->DrawText(sTitle, -1, captionRect, DT_WORDBREAK | DT_CENTER);
        }
    }

    if (m_bIsFlat == FALSE || (m_bIsFlat == TRUE && m_bDrawFlatFocus == TRUE))
    {
        // Draw the focus rect
        if (m_bIsFocused)
        {
            CRect focusRect = itemRect;
            focusRect.DeflateRect(3, 3);
            pDC->DrawFocusRect(&focusRect);
        } // if
    } // if
} // End of DrawItem
コード例 #2
0
ファイル: BtnST.cpp プロジェクト: CrawlingForward/BWChess
void CButtonST::DrawItem(LPDRAWITEMSTRUCT lpDIS)
{
#ifdef ST_USE_MEMDC
  CDC  *pdrawDC = CDC::FromHandle(lpDIS->hDC);
  CMemDC memDC(pdrawDC);
  CDC  *pDC = &memDC;
#else	
  CDC* pDC = CDC::FromHandle(lpDIS->hDC);
#endif

  CPen *pOldPen;
  BOOL bIsPressed  = (lpDIS->itemState & ODS_SELECTED);
  BOOL bIsFocused  = (lpDIS->itemState & ODS_FOCUS);
  BOOL bIsDisabled = (lpDIS->itemState & ODS_DISABLED);

  CRect itemRect = lpDIS->rcItem;

  if (m_bIsFlat == FALSE)
  {
	if (bIsFocused)
	{
	  CBrush br(RGB(0,0,0));  
	  pDC->FrameRect(&itemRect, &br);
	  itemRect.DeflateRect(1, 1);
	}
  }

  // Prepare draw... paint button's area with background color
  COLORREF bgColor;
  if ((m_MouseOnButton == TRUE) || (bIsPressed))
	bgColor = GetActiveBgColor();
  else
	bgColor = GetInactiveBgColor();

  CBrush br(bgColor);
  pDC->FillRect(&itemRect, &br);

	// Disegno lo sfondo del bottone
//CBrush br(GetSysColor(COLOR_BTNFACE));  
//pDC->FillRect(&itemRect, &br);

  // Draw pressed button
  if (bIsPressed)
  {
	if (m_bIsFlat == TRUE)
	{
	  if (m_bDrawBorder == TRUE)
	  {
		CPen penBtnHiLight(PS_SOLID, 0, GetSysColor(COLOR_BTNHILIGHT)); // Bianco
		CPen penBtnShadow(PS_SOLID, 0, GetSysColor(COLOR_BTNSHADOW));   // Grigio scuro

		// Disegno i bordi a sinistra e in alto
		// Dark gray line
		pOldPen = pDC->SelectObject(&penBtnShadow);
		pDC->MoveTo(itemRect.left, itemRect.bottom-1);
		pDC->LineTo(itemRect.left, itemRect.top);
		pDC->LineTo(itemRect.right, itemRect.top);
		// Disegno i bordi a destra e in basso
		// White line
		pDC->SelectObject(penBtnHiLight);
		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    
	{
	  CBrush brBtnShadow(GetSysColor(COLOR_BTNSHADOW));
	  pDC->FrameRect(&itemRect, &brBtnShadow);
	}
  }
  else // ...else draw non pressed button
  {
	CPen penBtnHiLight(PS_SOLID, 0, GetSysColor(COLOR_BTNHILIGHT)); // White
	CPen pen3DLight(PS_SOLID, 0, GetSysColor(COLOR_3DLIGHT));       // Light gray
	CPen penBtnShadow(PS_SOLID, 0, GetSysColor(COLOR_BTNSHADOW));   // Dark gray
	CPen pen3DDKShadow(PS_SOLID, 0, GetSysColor(COLOR_3DDKSHADOW)); // Black

	if (m_bIsFlat == TRUE)
	{
	  if (m_MouseOnButton == TRUE && m_bDrawBorder == TRUE)
	  {
		// 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);
		// Disegno i bordi a destra e in basso
		// Dark gray line
		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);
	}
  }

  // Read the button title
  CString sTitle;
  GetWindowText(sTitle);

  // If we don't want the title displayed
  if (m_bShowText == FALSE) sTitle.Empty();

  CRect captionRect = lpDIS->rcItem;

  // Draw the icon
  if (m_hIconIn != NULL)
  {
	DrawTheIcon(pDC, &sTitle, &lpDIS->rcItem, &captionRect, bIsPressed, bIsDisabled);
  }

  // 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);

#ifdef ST_USE_MEMDC
	// 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->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
コード例 #3
0
void CCoolButton::DrawItem(LPDRAWITEMSTRUCT lpDIS) 
{
	// TODO: Add your code to draw the specified item
	CDC * pDC=CDC::FromHandle(lpDIS->hDC);
	
	unsigned int  IsPressed =(lpDIS->itemState&ODS_SELECTED);
	unsigned int  IsFocused =(lpDIS->itemState&ODS_FOCUS);
	unsigned int  IsDisabled =(lpDIS->itemState&ODS_DISABLED);

	CRect itemRect = lpDIS->rcItem;

#ifndef XS_FLAT_BUTTON
	if(IsFocused)
	{
		CBrush br(RGB(0,0,0));
		pDC->FrameRect(&itemRect,&br);
		itemRect.DeflateRect(1,1);
	}
#endif
	//Fill with bkcolor
	CBrush br(GetSysColor(COLOR_BTNFACE));
	pDC->FillRect(&itemRect,&br);

	//Is pressed?
	if(IsPressed)
	{
#ifdef XS_FLAT_BUTTON
		//浅边界笔
		CPen penBtnHiLight(PS_SOLID,0,GetSysColor(COLOR_BTNHILIGHT));
		//阴影笔
		CPen penBtnShadow(PS_SOLID,0,GetSysColor(COLOR_BTNSHADOW));
		
		//绘边界阴影
		pDC->SelectObject(penBtnShadow);
		pDC->MoveTo(itemRect.left,itemRect.bottom-1);
		pDC->LineTo(itemRect.left,itemRect.top);
		pDC->LineTo(itemRect.right,itemRect.top);
		//绘浅边界
		pDC->SelectObject(penBtnHiLight);
		pDC->MoveTo(itemRect.left,itemRect.bottom-1);
		pDC->LineTo(itemRect.right-1,itemRect.bottom-1);
		pDC->LineTo(itemRect.right-1,itemRect.top-1);
#else
		CBrush brBtnShadow(GetSysColor(COLOR_BTNSHADOW));
		pDC->FrameRect(&itemRect,&brBtnShadow);
#endif
	}
	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));

#ifdef XS_FLAT_BUTTON
		if(m_MouseOnButton==TRUE)
		{
			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);
		}
#else
		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);
#endif
	}

#ifndef XS_FLAT_BUTTON
	//
	if(IsFocused)
	{
		CRect focusRect = itemRect;
		focusRect.DeflateRect(3,3);
		pDC->DrawFocusRect(&focusRect);
	}
#endif
	
	//获取文本
	CString title;
	GetWindowText(title);

	//绘制图标
	if(m_hIcon!=NULL)
	{
		CRect iconRect = lpDIS->rcItem;
		//根据标题是否存在来设置不同的图标位置
		if(title.IsEmpty()==TRUE)
		{
			iconRect.left+=((iconRect.Width()-m_cxIcon)/2);
		}
		else
		{
			iconRect.left+=6;
		}
		iconRect.top+=((iconRect.Height()-m_cyIcon)/2);
	
		if(IsPressed)iconRect.OffsetRect(1,1);


		pDC->DrawIcon(iconRect.TopLeft(),m_hIcon);
	}
	//绘标题
	CRect captionRect = lpDIS->rcItem;
	captionRect.left+=m_cxIcon;
	
	if(title.IsEmpty()==FALSE)
	{
		pDC->SetBkMode(TRANSPARENT);
		captionRect.OffsetRect(0,-1);
		
		if(IsPressed)
			captionRect.OffsetRect(1,1);

		if(IsDisabled)
		{
			captionRect.OffsetRect(1,1);
			pDC->SetTextColor(GetSysColor(COLOR_BTNHILIGHT));
			pDC->DrawText(title,-1,captionRect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
			captionRect.OffsetRect(-1,-1);
			pDC->SetTextColor(GetSysColor(COLOR_BTNSHADOW));
			pDC->DrawText(title,-1,captionRect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
		}
		else
		{
			pDC->DrawText(title,-1,captionRect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
		}
	}
}