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

📄 custombutton.cpp

📁 Wifi扫描器和自定义MFC控件 Wifi scanner + custom MFC controls 在WINCE环境下工作。
💻 CPP
📖 第 1 页 / 共 3 页
字号:

	//draw radio background
	//paint with active/inactive color depending on state
	if(m_bPressed == true)
	{
		crColor=m_crBkgPressed;
	}
	else
	{
		crColor=m_crBkgIdle;
	}

	CBrush NewBrush(crColor); 
	pDC->SetBrushOrg(0,0);
	HGDIOBJ hOldBrush = pDC->SelectObject(&NewBrush);   

	//no border, rect filled with color
	pDC->PatBlt(pRect->left, pRect->top, pRect->Width(), pRect->Height(), PATCOPY);
	pDC->SelectObject(hOldBrush);

	//prepare to draw text/image
	GetClientRect(rcBtn);

	//get style
	lStyle=GetWindowLong(GetSafeHwnd(), GWL_STYLE);

	//setup colors
/*
	if(m_bTransparent == true)
	{
		pDC->SetBkMode(TRANSPARENT);
	}
	else
	{
		pDC->SetBkMode(OPAQUE);
	}
*/

	pDC->SetBkMode(TRANSPARENT);
	if(m_bPressed == true)
	{
		pDC->SetTextColor(m_crTxtPressed);
		pDC->SetBkColor(m_crBkgPressed);
	}
	else
	{
		pDC->SetTextColor(m_crTxtIdle);
		pDC->SetBkColor(m_crBkgIdle);
	}

	//get current bitmap handle
	if(m_bPressed == true)
	{
		hCurrentBmp=m_hBmpActive;
	}
	else
	{
		hCurrentBmp=m_hBmpInactive;
	}

	//create image rect
	if(hCurrentBmp == NULL)
	{
		//no bitmap, will draw default
		m_dwBmpW=m_dwBmpH=16;
	}
	rcImg.SetRect(0, 0, m_dwBmpW, m_dwBmpH);

	//create text rect
	GetWindowText(strTitle);
	GetClientRect(rcText);

	rcText.right-=(m_dwBmpW+4);
	lRight=rcText.right;

	if(strTitle != _T(""))
	{
		//calculate text height
		nDTFormat = GetDTFlag(lStyle);
		pDC->DrawText(strTitle, &rcText, nDTFormat | DT_EXPANDTABS | DT_CALCRECT | DT_WORDBREAK);
	}

	rcText.right=lRight;

	//image on left, text on right:
	if((m_cfFlags & bfMask) == bfNone || m_cfFlags & bfTextRight)
	{
		//text & image positions
		ix=0;
		iy=(rcBtn.Height()-m_dwBmpH)/2;
		if(lStyle & BS_RIGHT)
		{
			tx=ix+m_dwBmpW+2;
		}
		else
		{
			tx=ix+m_dwBmpW+4;
		}
		ty=((rcBtn.Height()-rcText.Height())/2 )-1;
	}//BS_RIGHT
	else
	//text on left, image on right:
	if(m_cfFlags & bfTextLeft)
	{
		//text & image positions
		tx=0;
		ty=((rcBtn.Height()-rcText.Height())/2)-1;
		//text image y position
		iy=(rcBtn.Height()-m_dwBmpH)/2;
		ix=rcBtn.Width()-m_dwBmpW;
	}

	//draw image
	if(hCurrentBmp != NULL)		//if some image loaded
	{
		DisplayTransparentBMP(pDC, (HBITMAP)hCurrentBmp, m_dwBmpW, m_dwBmpH, ix, iy);
	}
	else
	{
		//draw default
		DrawFrameControl(pDC->GetSafeHdc(), CRect(ix, iy, ix+m_dwBmpW, iy+m_dwBmpH), DFC_BUTTON, ( m_ctType == btCustomCheckBox ? DFCS_BUTTONCHECK : DFCS_BUTTONRADIO) | (m_bPressed==true ? DFCS_CHECKED : 0) );	
	}
	//draw text
	if(strTitle != _T(""))
	{
		rcText.OffsetRect(tx, ty);
		nDTFormat = GetDTFlag(lStyle);
		pDC->DrawText(strTitle, rcText, nDTFormat | DT_EXPANDTABS | DT_WORDBREAK);
	}
}

//////////////////////////////////////////////////////////////////////////
//draw push button
void CCustomButton::DrawPB(CDC* pDC, CRect* pRect)
{
COLORREF crColor;
CString strTitle;
CRect rcText, rcBtn, rcImg;
HANDLE hCurrentBmp;
LONG lStyle;
int tx, ty, ix, iy, xsp, ysp;
bool bOffs, bTextFits, bHasText, bHasImage;
UINT nDTFormat;

	//draw button background
	//paint with active/inactive color depending on state
	if(m_bPressed == true)
	{
		crColor=m_crBkgPressed;
	}
	else
	{
		crColor=m_crBkgIdle;
	}

	CBrush NewBrush(crColor); 
	pDC->SetBrushOrg(0,0);
	HGDIOBJ hOldBrush = pDC->SelectObject(&NewBrush);   

//	if(m_bFlat == true)
	{
		//no border, rect filled with color
		pDC->PatBlt(pRect->left, pRect->top, pRect->Width(), pRect->Height(), PATCOPY);
	}
/*
	else
	{
		//leave out border
		pDC->PatBlt(pRect->left+2, pRect->top+2, pRect->Width()-2, pRect->Height()-2, PATCOPY);
	}
*/
	pDC->SelectObject(hOldBrush);

	//draw gradient if required
	if(m_bHGradient == true)
	{
		DrawGradient(&m_DrawDC);
	}

	//prepare to draw text/image
	GetClientRect(rcBtn);

	//get style
	lStyle=GetWindowLong(GetSafeHwnd(), GWL_STYLE);

	//get current bitmap handle
	if(m_bPressed == true)
	{
		hCurrentBmp=m_hBmpActive;
	}
	else
	{
		hCurrentBmp=m_hBmpInactive;
	}

	//create image rect
	rcImg.SetRect(0, 0, m_dwBmpW, m_dwBmpH);

	if(hCurrentBmp == NULL)
	{
		bHasImage=false;
	}
	else
	{
		bHasImage=true;
	}

	//create text rect
	GetWindowText(strTitle);
	if(strTitle != _T(""))
	{
		bHasText=true;
		//setup DC
/*
		if(m_bTransparent == true)
		{
			pDC->SetBkMode(TRANSPARENT);
		}
		else
		{
			pDC->SetBkMode(OPAQUE);
		}
*/
		pDC->SetBkMode(TRANSPARENT);
		if(m_bPressed == true)
		{
			pDC->SetTextColor(m_crTxtPressed);
			pDC->SetBkColor(m_crBkgPressed);
		}
		else
		{
			pDC->SetTextColor(m_crTxtIdle);
			pDC->SetBkColor(m_crBkgIdle);
		}

		//calculate text width/height
		GetClientRect(rcText);

		//if has image, text must fit into smaller rect
		if(bHasImage == true)
		{
			switch(m_cfFlags & bfMask)
			{
				case bfTextBottom:
				case bfTextTop:
				{
					//text must fit into lower rect
					rcText.bottom-=m_dwBmpH;
					break;
				}
				default:
				{
					//text must fit into narrower rect
					rcText.right-=m_dwBmpW;
				}
			}//switch text alignment flags
		}//if has image

		nDTFormat = GetDTFlag(lStyle);
		pDC->DrawText(strTitle, &rcText, nDTFormat | DT_EXPANDTABS | DT_CALCRECT);
	}
	else
	{
		bHasText=false;
		rcText.SetRectEmpty();
	}
	
	//need to offset text/image for pressed button?
	if(m_bFlat == false && m_bPressed == true)
	{
		bOffs=true;	
	}
	else
	{
		bOffs=false;
	}

	//if text and image would be too long, omit text
	bTextFits=true;
/*
	if(m_cfFlags == CustomControlData::cfTextTop || m_cfFlags == CustomControlData::cfTextBottom)
	{
		if(rcBtn.Height() > rcImg.Height() + rcText.Height())
		{
			bTextFits=true;
		}
	}
	else
	{
		if(rcBtn.Width() > rcImg.Width() + rcText.Width())
		{
			bTextFits=true;
		}
	}
*/

	//calculate text and image x,y positions
	if(bTextFits == true)
	{
		//image on left, text on right:
		if((m_cfFlags & bfMask) == bfNone || m_cfFlags & bfTextRight)
		{
			//x spacing
			xsp=(rcBtn.Width()-rcImg.Width()-rcText.Width());
			if(bHasImage == true && bHasText == true)
			{
				xsp/=3;
			}
			else
			{
				//no text or no image
				xsp/=2;
			}
			ix=xsp;
			//text & image y position
			iy=(rcBtn.Height()-m_dwBmpH)/2;
			ty=(rcBtn.Height()-rcText.Height())/2;

			if(bHasImage == true)
			{
				tx=xsp+ix+m_dwBmpW;
			}
			else
			{
				tx=xsp;
			}
		}//BS_RIGHT
		else
		//text on left, image on right:
		if(m_cfFlags & bfTextLeft)
		{
			//x spacing
			xsp=(rcBtn.Width()-rcImg.Width()-rcText.Width());
			if(bHasImage == true && bHasText == true)
			{
				xsp/=3;
			}
			else
			{
				xsp/=2;
			}
			tx=xsp;
			//text image y position
			iy=(rcBtn.Height()-m_dwBmpH)/2;
			ty=(rcBtn.Height()-rcText.Height())/2;

			if(bHasText == true)
			{
				ix=xsp+tx+rcText.Width();
			}
			else
			{
				ix=xsp;
			}
		}
		else
		//text on top, image on bottom:
		if(m_cfFlags & bfTextTop)
		{
			//y spacing
			ysp=(rcBtn.Height()-rcImg.Height()-rcText.Height());
			if(bHasImage == true && bHasText == true)
			{
				ysp/=3;
			}
			else
			{
				ysp/=2;
			}
			ty=ysp;
			tx=(rcBtn.Width()-rcText.Width())/2;
			ix=(rcBtn.Width()-rcImg.Width())/2;
			
			if(bHasText == true)
			{
				iy=ysp+ty+rcText.Height();
			}
			else
			{
				iy=ysp;
			}
		}
		else
		//image on top, text on bottom:i
		if(m_cfFlags & bfTextBottom)
		{
			//y spacing
			ysp=(rcBtn.Height()-rcImg.Height()-rcText.Height());
			if(bHasImage == true && bHasText == true)
			{
				ysp/=3;
			}
			else
			{
				ysp/=2;
			}
			iy=ysp;
			tx=(rcBtn.Width()-rcText.Width())/2;
			ix=(rcBtn.Width()-rcImg.Width())/2;

			if(bHasImage == true)
			{
				ty=ysp+iy+m_dwBmpH;
			}
			else
			{
				ty=ysp;
			}
		}//BS_BOTTOM

		//offset pushed button
		if(bOffs == true)
		{
			//offset
			tx+=1;
			ty+=1;
			ix+=1;
			iy+=1;
		}
	}//if text fits
	else
	{
#ifdef _DEBUG
		OutputDebugString(_T("Button text does not fit in: \"") + strTitle + _T("\"\n"));
#endif
		if(rcBtn.Width() > rcImg.Width())
		{
			ix=(rcBtn.Width()-rcImg.Width())/2;
		}
		else
		{
			ix=0;
		}
		if(rcBtn.Height() > rcImg.Height())
		{
			iy=(rcBtn.Height()-rcImg.Height())/2;
		}
		else
		{
			iy=0;
		}

		//offset pressed non-flat button
		if(bOffs == true)
		{
			ix++;
			iy++;
		}
	}

	//at this point tx, ty, ix, iy values are set up for text and image x,y positions

	//draw image
	if(lStyle & BS_BITMAP)
	{
		DisplayTransparentBMP(pDC, (HBITMAP)hCurrentBmp, m_dwBmpW, m_dwBmpH, ix, iy);
	}
	//draw text
	if(bTextFits == true && strTitle != _T(""))
	{
		rcText.OffsetRect(tx, ty);
		nDTFormat = GetDTFlag(lStyle);
		pDC->DrawText(strTitle, rcText, nDTFormat | DT_EXPANDTABS);
	}

	//draw border
	DrawPBBorder(pDC, pRect);

	//done!
}

///////////////////////////////////////////////////////////////
//draw push button border
void CCustomButton::DrawPBBorder(CDC* pDC, CRect* pRect)
{
	if(m_bFlat == true)
	{
		//no border for flat buttons
		return;
	}

	if(m_bHGradient == true)
	{
		if(m_bPressed == true)
		{
			COLORREF crAvg, crDarker, crLighter;

			crAvg=RGB_AVERAGE(m_crHGStart, m_crHGEnd);
			crLighter=OffsetColor(crAvg, -0.1);
			crDarker=OffsetColor(crAvg, 0.2);

			CPen penLighter(PS_SOLID, 0, crLighter);
			CPen penLight(PS_SOLID, 0, crAvg);
			CPen penDark(PS_SOLID, 0, crAvg);
			CPen penDarker(PS_SOLID, 0, crDarker);

			//top-left border
			//darker line
			CPen* pOldPen = pDC->SelectObject(&penDarker);
			pDC->MoveTo(pRect->left, pRect->top+3);
			pDC->LineTo(pRect->left, pRect->bottom-3);
			pDC->MoveTo(pRect->left+3, pRect->top);
			pDC->LineTo(pRect->right-4, pRect->top);

			//dark
			pDC->SelectObject(penDark);
			pDC->MoveTo(pRect->left+1, pRect->bottom-3);
			pDC->LineTo(pRect->left+1, pRect->top+2);
			pDC->MoveTo(pRect->left+3, pRect->top+1);
			pDC->LineTo(pRect->right-2, pRect->top+1);

			//bottom-right border
			//lighter line
			pDC->SelectObject(penLighter);
			pDC->MoveTo(pRect->left+3, pRect->bottom-1);
			pDC->LineTo(pRect->right-3, pRect->bottom-1);
			pDC->MoveTo(pRect->right-1, pRect->bottom-4);
			pDC->LineTo(pRect->right-1, pRect->top+2);			

			pDC->SetPixel(pRect->left, pRect->top+3, crDarker);
			pDC->SetPixel(pRect->left+1, pRect->top+2, crDarker);
			pDC->SetPixel(pRect->left+2, pRect->top+1, crDarker);

			pDC->SetPixel(pRect->right-3, pRect->bottom-1, crLighter);
			pDC->SetPixel(pRect->right-2, pRect->bottom-2, crLighter);
			pDC->SetPixel(pRect->right-1, pRect->bottom-3, crLighter);

			//light line
			pDC->SelectObject(penLight);
			pDC->MoveTo(pRect->left+2, pRect->bottom-2);
			pDC->LineTo(pRect->right-2, pRect->bottom-2);
			pDC->MoveTo(pRect->right-2, pRect->bottom-3);
			pDC->LineTo(pRect->right-2, pRect->top+1);
			//
			pDC->SelectObject(pOldPen);
		}
		else
			//draw not pressed button border
		{
			COLORREF crAvg, crDarker, crLighter;

			crAvg=RGB_AVERAGE(m_crHGStart, m_crHGEnd);
			crLighter=OffsetColor(crAvg, -0.1);
			crDarker=OffsetColor(crAvg, 0.2);

			CPen penLighter(PS_SOLID, 0, crLighter);
			CPen penLight(PS_SOLID, 0, crAvg);
			CPen penDark(PS_SOLID, 0, crAvg);
			CPen penDarker(PS_SOLID, 0, crDarker);

			//top-left border
			//lighter line
			CPen* pOldPen = pDC->SelectObject(&penLighter);
			pDC->MoveTo(pRect->left, pRect->top+4);
			pDC->LineTo(pRect->left, pRect->bottom-3);
			pDC->MoveTo(pRect->left+4, pRect->top);
			pDC->LineTo(pRect->right-4, pRect->top);

			//light
			pDC->SelectObject(penLight);
			pDC->MoveTo(pRect->left+1, pRect->bottom-3);
			pDC->LineTo(pRect->left+1, pRect->top+2);
			pDC->MoveTo(pRect->left+3, pRect->top+1);
			pDC->LineTo(pRect->right-2, pRect->top+1);

			//bottom-right border
			//darker line
			pDC->SelectObject(penDarker);
			pDC->MoveTo(pRect->left+3, pRect->bottom-1);
			pDC->LineTo(pRect->right-3, pRect->bottom-1);
			pDC->MoveTo(pRect->right-1, pRect->bottom-4);
			pDC->LineTo(pRect->right-1, pRect->top+2);			

			pDC->SetPixel(pRect->right-3, pRect->bottom-1, crDarker);
			pDC->SetPixel(pRect->right-2, pRect->bottom-2, crDarker);
			pDC->SetPixel(pRect->right-1, pRect->bottom-3, crDarker);

			pDC->SetPixel(pRect->left+2, pRect->top+2, crLighter);

			//dark line
			pDC->SelectObject(penDark);

⌨️ 快捷键说明

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