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

📄 cepngbtnst.cpp

📁 改代码可实现wince button上帖png透明图片
💻 CPP
📖 第 1 页 / 共 3 页
字号:
//		[IN]	bIsPressed
//				TRUE if the button is currently pressed
//		[IN]	dwWidth
//				Width of the image (icon or bitmap)
//		[IN]	dwHeight
//				Height of the image (icon or bitmap)
//		[OUT]	rpImage
//				A pointer to a CRect object that will receive the area available to the image
//
void CCePngButtonST::PrepareImageRect(BOOL bHasTitle, RECT* rpItem, CRect* rpTitle, BOOL bIsPressed, DWORD dwWidth, DWORD dwHeight, CRect* rpImage)
{
	CRect rBtn;

	rpImage->CopyRect(rpItem);

	switch (m_byAlign)
	{
		case ST_ALIGN_HORIZ:
			if (bHasTitle == FALSE)
			{
				// Center image horizontally
				rpImage->left += ((rpImage->Width() - dwWidth)/2);
			}
			else
			{
				// Image must be placed just inside the focus rect
				rpImage->left += 3;  
				rpTitle->left += dwWidth + 3;
			}
			// Center image vertically
			rpImage->top += ((rpImage->Height() - dwHeight)/2);
			break;

		case ST_ALIGN_HORIZ_RIGHT:
			GetClientRect(&rBtn);
			if (bHasTitle == FALSE)
			{
				// Center image horizontally
				rpImage->left += ((rpImage->Width() - dwWidth)/2);
			}
			else
			{
				// Image must be placed just inside the focus rect
				rpTitle->right = rpTitle->Width() - dwWidth - 3;
				rpTitle->left = 3;
				rpImage->left = rBtn.right - dwWidth - 3;
				// Center image vertically
				rpImage->top += ((rpImage->Height() - dwHeight)/2);
			}
			break;
		
		case ST_ALIGN_VERT:
			// Center image horizontally
			rpImage->left += ((rpImage->Width() - dwWidth)/2);
			if (bHasTitle == FALSE)
			{
				// Center image vertically
				rpImage->top += ((rpImage->Height() - dwHeight)/2);           
			}
			else
			{
				rpImage->top = 0;//modify from 3
				rpTitle->top += dwHeight-14;//modify form dwHeight
			}
			break;
		case ST_ALIGN_CENTER:
			// Center image horizontally
			rpImage->left += ((rpImage->Width() - dwWidth)/2);
			if (bHasTitle == FALSE)
			{
				// Center image vertically
				rpImage->top += ((rpImage->Height() - dwHeight)/2);           
			}
			else
			{
				rpImage->top = 0;//modify from 3
				rpTitle->top += 0;//modify form dwHeight
			}
			break;
	}
	//add by fuxing 2008-7-26
	rpImage->right = rpImage->left + dwWidth;
	rpImage->bottom = rpImage->top + dwHeight;
    
	// If button is pressed then press image also
	if (bIsPressed && m_bIsCheckBox == FALSE && m_byAlign != ST_ALIGN_CENTER)//add && m_byAlign != ST_ALIGN_CENTER for press static button not move
		rpImage->OffsetRect(1, 1);
		//rpImage->DeflateRect(1,1);
} // End of PrepareImageRect

void CCePngButtonST::DrawTheIcon(CDC* pDC, BOOL bHasTitle, RECT* rpItem, CRect* rpTitle, BOOL bIsPressed, BOOL bIsDisabled)
{
	BYTE		byIndex		= 0;

	// Select the icon to use
	if (bIsDisabled)
		byIndex = (m_csIcons[2].hIcon == NULL ? 0 : 2);
	else
	{
		if ((m_bIsCheckBox && bIsPressed) || (!m_bIsCheckBox && (bIsPressed || m_bMouseOnButton)))
			byIndex = 0;
		else
			byIndex = (m_csIcons[1].hIcon == NULL ? 0 : 1);
	} // else

	CRect	rImage;
	PrepareImageRect(bHasTitle, rpItem, rpTitle, bIsPressed, m_csIcons[byIndex].dwWidth, m_csIcons[byIndex].dwHeight, &rImage);

	// Ole'!
	pDC->DrawIcon(rImage.TopLeft(), m_csIcons[byIndex].hIcon);
} // End of DrawTheIcon

void CCePngButtonST::DrawTheBitmap(HDC h_destDC, BOOL bHasTitle, RECT* rpItem, CRect* rpTitle, BOOL bIsPressed, BOOL bIsDisabled)
{
	BYTE		byIndex		= 0;
	CRect		rImage;
	BITMAP		bmp;
	HBITMAP		hBmp;
	HDC			hDC;
	// Select the icon to use
	// Select the icon to use
	if (bIsDisabled)
		byIndex = (m_csBitmaps[2].hBitmap == NULL ? 0 : 2);
	else
	{
		if ((m_bIsCheckBox && bIsPressed) || (!m_bIsCheckBox && (bIsPressed || m_bMouseOnButton)))
			byIndex = 0;
		else
			byIndex = (m_csBitmaps[1].hBitmap == NULL ? 0 : 1);
	} // else

	//GetClientRect(&cltRect);
	//hBmp = ::LoadBitmap(::AfxGetInstanceHandle(), MAKEINTRESOURCE(byIndex));
	hBmp = m_csBitmaps[byIndex].hBitmap;
	::GetObject(hBmp, sizeof(bmp), &bmp);
	hDC = ::CreateCompatibleDC(NULL);
	SelectObject(hDC, hBmp);

	PrepareImageRect(bHasTitle, rpItem, rpTitle, bIsPressed, m_csBitmaps[byIndex].dwWidth, m_csBitmaps[byIndex].dwHeight, &rImage);
	::TransparentImage(h_destDC, rImage.left, rImage.top, rImage.Width(), rImage.Height(), hDC, 0, 0, rImage.Width(), rImage.Height(), m_transpCREF);
	//BLENDFUNCTION   bf;   
	//bf.AlphaFormat=0;   
	//bf.BlendFlags=0;   
	//bf.BlendOp=AC_SRC_OVER;   
	//bf.SourceConstantAlpha=255;//透明度0~255   
	//::AlphaBlend(h_destDC, rImage.left, rImage.top, rImage.Width(), rImage.Height(), hDC, 0, 0, rImage.Width(), rImage.Height(), bf);
	::DeleteObject(hBmp);
	::DeleteDC(hDC);
} // End of DrawTheBitmap

void CCePngButtonST::DrawTheImage(HDC h_destDC, BOOL bHasTitle, RECT* rpItem, CRect* rpTitle, BOOL bIsPressed, BOOL bIsDisabled)
{
	BYTE		byIndex		= 0;
	CRect		rImage;
	// Select the image to use
	if (bIsDisabled)
		byIndex = (m_csImages[2].pIImage == NULL ? 1 : 2);
	else
	{
		if ((m_bIsCheckBox && bIsPressed) || (!m_bIsCheckBox && (bIsPressed)))//|| m_bMouseOnButton
			byIndex = 0;
		else
			byIndex = (m_csImages[1].pIImage == NULL ? 0 : 1);
	}
	PrepareImageRect(bHasTitle, rpItem, rpTitle, bIsPressed, m_csImages[byIndex].dwWidth, m_csImages[byIndex].dwHeight, &rImage);
	
	m_csImages[byIndex].pIImage->Draw(h_destDC, rImage, NULL);

}	// End of DrawTheImage

BOOL CCePngButtonST::ImageFromIDResource(UINT nID, LPCTSTR sTR, IImage** pIImage)
{
	HINSTANCE hInst = AfxGetResourceHandle();
	HRSRC hRsrc = ::FindResource (hInst,MAKEINTRESOURCE(nID),sTR); // type
	if (!hRsrc)
		return FALSE;

	// load resource into memory
	DWORD len = SizeofResource(hInst, hRsrc);
	BYTE* lpRsrc = (BYTE*)LoadResource(hInst, hRsrc);
	if (!lpRsrc)
		return FALSE;

	// Allocate global memory on which to create stream
	HGLOBAL m_hMem = GlobalAlloc(GMEM_FIXED, len);
	BYTE* pmem = (BYTE*)GlobalLock(m_hMem);
	memcpy(pmem,lpRsrc,len);
	IStream* pStream;
	CreateStreamOnHGlobal(m_hMem,FALSE,&pStream);
	// load from stream
	pIImgFactory->CreateImageFromStream(pStream, pIImage);

	// free/release stuff
	GlobalUnlock(m_hMem);
	pStream->Release();
	FreeResource(lpRsrc);
	if(!pIImage)
		return FALSE;

	return TRUE;
}

// This function assigns icons to the button.
// Any previous icon will be removed.
//
// Parameters:
//		[IN]	nIconIn
//				ID number of the icon resource to show when the mouse is over the button.
//				Pass NULL to remove any icon from the button.
//		[IN]	sizeIn
//				Size of the icon.
//		[IN]	nIconOut
//				ID number of the icon resource to show when the mouse is outside the button.
//				Can be NULL.
//		[IN]	sizeOut
//				Size of the icon.
//		[IN]	nIconDis
//				ID number of the icon resource to show when the button is disabled.
//				Can be NULL.
//		[IN]	sizeDis
//				Size of the icon.
//
// Return value:
//		BTNST_OK
//			Function executed successfully.
//
DWORD CCePngButtonST::SetIcon(int nIconIn, CSize sizeIn, int nIconOut, CSize sizeOut, int nIconDis, CSize sizeDis)
{
	HICON		hIconIn			= NULL;
	HICON		hIconOut		= NULL;
	HICON		hIconDis		= NULL;
	HINSTANCE	hInstResource	= NULL;

	// Find correct resource handle
	hInstResource = AfxFindResourceHandle(MAKEINTRESOURCE(nIconIn), RT_GROUP_ICON);

	// Set icon when the mouse is IN the button
	hIconIn = (HICON)::LoadImage(hInstResource, MAKEINTRESOURCE(nIconIn), IMAGE_ICON, 0, 0, 0);
  	// Set icon when the mouse is OUT the button
	if (nIconOut)
		hIconOut = (HICON)::LoadImage(hInstResource, MAKEINTRESOURCE(nIconOut), IMAGE_ICON, 0, 0, 0);
  	// Set icon when the mouse is DISABLED
	if (nIconDis)
		hIconDis = (HICON)::LoadImage(hInstResource, MAKEINTRESOURCE(nIconDis), IMAGE_ICON, 0, 0, 0);

	return SetIcon(hIconIn, sizeIn, hIconOut, sizeOut, hIconDis, sizeDis);
} // End of SetIcon

// This function assigns icons to the button.
// Any previous icon or bitmap will be removed.
//
// Parameters:
//		[IN]	hIconIn
//				Handle fo the icon to show when the mouse is over the button.
//				Pass NULL to remove any icon from the button.
//		[IN]	sizeIn
//				Size of the icon.
//		[IN]	hIconOut
//				Handle to the icon to show when the mouse is outside the button.
//				Can be NULL.
//		[IN]	sizeOut
//				Size of the icon.
//		[IN]	hIconDis
//				ID number of the icon resource to show when the button is disabled.
//				Can be NULL.
//		[IN]	sizeDis
//				Size of the icon.
//
// Return value:
//		BTNST_OK
//			Function executed successfully.
//
DWORD CCePngButtonST::SetIcon(HICON hIconIn, CSize sizeIn, HICON hIconOut, CSize sizeOut, HICON hIconDis, CSize sizeDis)
{
	// Free any loaded resource
	FreeResources();

	if (hIconIn)
	{
		// Icon when mouse over button?
		m_csIcons[0].hIcon = hIconIn;
		// Get icon dimension
		m_csIcons[0].dwWidth	= (DWORD)sizeIn.cx;
		m_csIcons[0].dwHeight	= (DWORD)sizeIn.cy;
		
		// Icon when mouse outside button?
		if (hIconOut)
		{
			m_csIcons[1].hIcon = hIconOut;
			// Get icon dimension
			m_csIcons[1].dwWidth	= (DWORD)sizeOut.cx;
			m_csIcons[1].dwHeight	= (DWORD)sizeOut.cy;
		} // if

		// Icon when button disabled?
		if (hIconDis)
		{
			m_csIcons[2].hIcon = hIconDis;
			// Get icon dimension
			m_csIcons[2].dwWidth	= (DWORD)sizeDis.cx;
			m_csIcons[2].dwHeight	= (DWORD)sizeDis.cy;
		} // if
	} // if

	Invalidate();

	return BTNST_OK;
} // End of SetIcon

// This function assigns bitmaps to the button.
// Any previous bitmap will be removed.
//
// Parameters:
//		[IN]	nBitmapIn
//				ID number of the icon resource to show when the mouse is over the button.
//				Pass NULL to remove any icon from the button.
//		[IN]	sizeIn
//				Size of the icon.
//		[IN]	nBitmapOut
//				ID number of the icon resource to show when the mouse is outside the button.
//				Can be NULL.
//		[IN]	sizeOut
//				Size of the icon.
//		[IN]	nBitmapDis
//				ID number of the icon resource to show when the button is disabled.
//				Can be NULL.
//		[IN]	sizeDis
//				Size of the icon.
//
// Return value:
//		BTNST_OK
//			Function executed successfully.
//
DWORD CCePngButtonST::SetBitmap(int nBitmapIn, CSize sizeIn, int nBitmapOut, CSize sizeOut, int nBitmapDis, CSize sizeDis)
{
	HBITMAP		hBitmapIn		= NULL;
	HBITMAP		hBitmapOut		= NULL;
	HBITMAP		hBitmapDis		= NULL;
	HINSTANCE	hInstResource	= NULL;

	// Find correct resource handle
	hInstResource = AfxFindResourceHandle(MAKEINTRESOURCE(nBitmapIn), RT_BITMAP);

	// Set icon when the mouse is IN the button
	hBitmapIn = (HBITMAP)::LoadImage(hInstResource, MAKEINTRESOURCE(nBitmapIn), IMAGE_BITMAP, 0, 0, 0);
  	// Set icon when the mouse is OUT the button
	if (nBitmapOut)
		hBitmapOut = (HBITMAP)::LoadImage(hInstResource, MAKEINTRESOURCE(nBitmapOut), IMAGE_BITMAP, 0, 0, 0);
  	// Set icon when the mouse is DISABLED
	if (nBitmapDis)
		hBitmapDis = (HBITMAP)::LoadImage(hInstResource, MAKEINTRESOURCE(nBitmapDis), IMAGE_BITMAP, 0, 0, 0);

	return SetBitmap(hBitmapIn, sizeIn, hBitmapOut, sizeOut, hBitmapDis, sizeDis);
} // End of SetIcon

// This function assigns icons to the button.
// Any previous icon or bitmap will be removed.
//
// Parameters:
//		[IN]	hBitmapIn
//				Handle fo the icon to show when the mouse is over the button.
//				Pass NULL to remove any icon from the button.
//		[IN]	sizeIn
//				Size of the bitmap.
//		[IN]	hBitmapOut
//				Handle to the icon to show when the mouse is outside the button.
//				Can be NULL.
//		[IN]	sizeOut
//				Size of the bitmap.
//		[IN]	hBitmapDis
//				ID number of the icon resource to show when the button is disabled.
//				Can be NULL.
//		[IN]	sizeDis
//				Size of the bitmap.
//
// Return value:
//		BTNST_OK
//			Function executed successfully.
//
DWORD CCePngButtonST::SetBitmap(HBITMAP hBitmapIn, CSize sizeIn, HBITMAP hBitmapOut, CSize sizeOut, HBITMAP hBitmapDis, CSize sizeDis)
{
	// Free any loaded resource
	FreeResources();

	if (hBitmapIn)
	{
		// Icon when mouse over button?
		m_csBitmaps[0].hBitmap = hBitmapIn;
		// Get icon dimension
		m_csBitmaps[0].dwWidth	= (DWORD)sizeIn.cx;
		m_csBitmaps[0].dwHeight	= (DWORD)sizeIn.cy;
		
		// Icon when mouse outside button?
		if (hBitmapOut)
		{
			m_csBitmaps[1].hBitmap = hBitmapOut;
			// Get icon dimension
			m_csBitmaps[1].dwWidth	= (DWORD)sizeOut.cx;
			m_csBitmaps[1].dwHeight	= (DWORD)sizeOut.cy;
		} // if

		// Icon when button disabled?
		if (hBitmapDis)
		{
			m_csBitmaps[2].hBitmap = hBitmapDis;
			// Get icon dimension
			m_csBitmaps[2].dwWidth	= (DWORD)sizeDis.cx;
			m_csBitmaps[2].dwHeight	= (DWORD)sizeDis.cy;
		} // if
	} // if

	Invalidate();

	return BTNST_OK;
} // End of SetBitmap

DWORD CCePngButtonST::SetImage(int nBkgndID, int nImageIn, CSize sizeIn, int nImageOut, CSize sizeOut, int nImageDis, CSize sizeDis)
{
	IImage*	pImageIn	= NULL;
	IImage*	pImageOut	= NULL;
	IImage*	pImageDis	= NULL;

	nBkgndImageID = nBkgndID;
	CoInitializeEx(NULL,COINIT_MULTITHREADED);
	//initialize com 
	if(!SUCCEEDED(CoCreateInstance(CLSID_ImagingFactory,NULL,CLSCTX_INPROC_SERVER,IID_IImagingFactory,(void **)&pIImgFactory)))
	{
		return BTNST_FAILEDMASK;
	}
	// Set image when the mouse is IN the button
	ImageFromIDResource(nImageIn, L"PNG", &pImageIn);
  	// Set image when the mouse is OUT the button
	if (nImageOut)
		ImageFromIDResource(nImageOut, L"PNG", &pImageOut);
  	// Set image when the mouse is DISABLED
	if (nImageDis)
		ImageFromIDResource(nImageDis, L"PNG", &pImageDis);
	return SetImage(pImageIn, sizeIn, pImageOut, sizeOut, pImageDis, sizeDis);
} // End of SetImage

DWORD CCePngButtonST::SetImage(IImage* pIImageIn, CSize sizeIn, IImage* pIImageOut, CSize sizeOut, IImage* pIImageDis, CSize sizeDis)
{
	// Free any loaded resource
	FreeResources();

	if (pIImageIn)
	{
		// Image when mouse over button?
		m_csImages[0].pIImage = pIImageIn;
		// Get image dimension
		m_csImages[0].dwWidth	= (DWORD)sizeIn.cx;
		m_csImages[0].dwHeight	= (DWORD)sizeIn.cy;
		
		// Image when mouse outside button?
		if (pIImageOut)
		{
			m_csImages[1].pIImage = pIImageOut;
			// Get image dimension
			m_csImages[1].dwWidth	= (DWORD)sizeOut.cx;
			m_csImages[1].dwHeight	= (DWORD)sizeOut.cy;
		} // if

		// Image when button disabled?
		if (pIImageDis)
		{
			m_csImages[2].pIImage = pIImageDis;
			// Get icon dimension
			m_csImages[2].dwWidth	= (DWORD)sizeDis.cx;
			m_csImages[2].dwHeight	= (DWORD)sizeDis.cy;
		} // if
	} // if

	Invalidate();

	return BTNST_OK;
} // End of SetImage

// This functions sets the button to have a standard or flat style.
//
// Parameters:
//		[IN]	bFlat
//				If TRUE the button will have a flat style, else
//				will have a standard style.
//		[IN]	bRepaint
//				If TRUE the control will be repainted.
//
// Return value:
//		BTNST_OK
//			Function executed successfully.
//
DWORD CCePngButtonST::SetFlat(BOOL bFlat, BOOL bRepaint)
{
	m_bIsFlat = bFlat;
	if (bRepaint)	Invalidate();

	return BTNST_OK;
} // End of SetFlat

// This function sets the alignment type between icon/bitmap and text.
//
// Parameters:
//		[IN]	byAlign
//				Alignment type. Can be one of the following values:
//				ST_ALIGN_HORIZ			Icon/bitmap on the left, text on the right
//				ST_ALIGN_VERT			Icon/bitmap on the top, text on the bottom
//				ST_ALIGN_HORIZ_RIGHT	Icon/bitmap on the right, text on the left
//				By default, CButtonST buttons have ST_ALIGN_HORIZ alignment.
//		[IN]	bRepaint
//				If TRUE the control will be repainted.
//
// Return value:
//		BTNST_OK
//			Function executed successfully.
//		BTNST_INVALIDALIGN
//			Alignment type not supported.
//
DWORD CCePngButtonST::SetAlign(BYTE byAlign, BOOL bRepaint)
{

⌨️ 快捷键说明

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