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

📄 foimagestatic.cpp

📁 visual c++ 实例编程
💻 CPP
字号:
// Copyright UCanCode Software Technology Inc, All Rights Reserved
// You can contact us.
// Support@UCanCode.net
// http://www.ucancode.net
/********************************************************************/
// FOImageStatic.cpp : implementation file
//

#include "stdafx.h"
#include "newpicker.h"
#include "FOImageStatic.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


const UINT c_uValidHAligns = DT_LEFT|DT_CENTER|DT_RIGHT;
const UINT c_uValidVAligns = DT_TOP|DT_VCENTER|DT_BOTTOM;
const UINT c_uValidAligns = c_uValidHAligns|c_uValidVAligns;

/////////////////////////////////////////////////////////////////////////////
// CFOImageStatic
IMPLEMENT_DYNAMIC(CFOImageStatic, CFOCustomStatic)
BEGIN_MESSAGE_MAP(CFOImageStatic, CFOCustomStatic)
//{{AFX_MSG_MAP(CFOImageStatic)
ON_WM_GETDLGCODE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFOImageStatic construction
CFOImageStatic::CFOImageStatic()
{
	m_cxImageSpacing = 1;
	m_cxImageBorder = 1;
	m_uImageAlign = DT_LEFT | DT_VCENTER;
	m_uCaptionAlign = DT_LEFT | DT_VCENTER;
}

CFOImageStatic::~CFOImageStatic()
{
}

/////////////////////////////////////////////////////////////////////////////
// CFOImageStatic diagnostics

#ifdef _DEBUG
void CFOImageStatic::AssertValid() const
{
	CFOCustomStatic::AssertValid();
	ASSERT((m_uImageAlign & ~c_uValidAligns) == 0);
	ASSERT((m_uCaptionAlign & ~c_uValidAligns) == 0);
}

void CFOImageStatic::Dump(CDumpContext& dc) const
{
	CFOCustomStatic::Dump(dc);
}
#endif //_DEBUG

BOOL CFOImageStatic::AttachStatic(int nCtlID, CWnd* pParentWnd)
{
	// This code from AFCOwnerDrawButton
	ASSERT_VALID(this);
	ASSERT(pParentWnd != NULL);
	
	ASSERT(::IsWindow(pParentWnd->m_hWnd));
	ASSERT(::GetDlgItem(pParentWnd->m_hWnd, nCtlID) != NULL);	
	
	ASSERT(FromHandlePermanent(::GetDlgItem(pParentWnd->m_hWnd, nCtlID)) == NULL);
	
	if(SubclassDlgItem(nCtlID, pParentWnd))
	{
		ASSERT_VALID(this);
		
		return TRUE;
	}
	
	TRACE1("AFCMarqueeCtrl could not attach to static %d\n", nCtlID );
	return FALSE;
}

/////////////////////////////////////////////////////////////////////////////
// CFOImageStatic operations
int CFOImageStatic::InsertImage(LPCTSTR lpszBitmapID, int cx, COLORREF crMask)
{
	CBitmap bmp;
	if (!bmp.LoadBitmap(lpszBitmapID))
	{
		TRACE("CFOImageStatic failed to load the specified bitmap.\n");
		return -1;
	}
	
	int nImage = -1; // Assume failure
	
	if (!HasImages())
	{
		if (cx < 0)
		{
			// Assume the bitmap is a single image
			// and determine the width of that image.
			BITMAP bmpInfo;
			VERIFY(bmp.GetBitmap(&bmpInfo));
			cx = bmpInfo.bmWidth;
		}
		
		if (m_images.Create(lpszBitmapID, cx, 1, crMask))
		{
			nImage = 0; // Index of the first image
			RecalcLayout();
		}
	}
	else
	{
		nImage = InsertImage(&bmp, crMask);
	}
	
	return nImage;
}

int CFOImageStatic::InsertImage(UINT nBitmapID, int cx, COLORREF crMask)
{
	return InsertImage(MAKEINTRESOURCE(nBitmapID), cx, crMask);
}

int CFOImageStatic::InsertImage(CBitmap* pBitmap, COLORREF crMask)
{
	int nImage = m_images.Add(pBitmap, crMask);
	
	RecalcLayout();
	
	return nImage;
}

BOOL CFOImageStatic::RemoveImage(int nImage)
{
	if (!m_images.Remove(nImage))
		return FALSE;
	
	RecalcLayout();
	
	return TRUE;
}

BOOL CFOImageStatic::HasImages() const
{
	return (m_images.GetSafeHandle() && (GetImageCount() > 0));
}

int CFOImageStatic::GetImageCount() const
{
	return (m_images.GetSafeHandle()) ? m_images.GetImageCount() : 0;
}

void CFOImageStatic::SetImageBorder(int cx)
{
	m_cxImageBorder = cx;
	RecalcLayout();
}

void CFOImageStatic::SetImageSpacing(int cx)
{
	m_cxImageSpacing = cx;
	RecalcLayout();
}

void CFOImageStatic::SetCaptionAlign(UINT uAlign)
{
	ASSERT((uAlign & ~c_uValidAligns) == 0);
	m_uCaptionAlign = uAlign;
	RecalcLayout();
}

void CFOImageStatic::SetImageAlign(UINT uAlign)
{
	ASSERT((uAlign & ~c_uValidAligns) == 0);
	m_uImageAlign = uAlign;
	RecalcLayout();
}

UINT CFOImageStatic::GetCaptionAlign() const
{
	return m_uCaptionAlign;
}

UINT CFOImageStatic::GetImageAlign() const
{
	return m_uImageAlign;
}

UINT CFOImageStatic::OnGetDlgCode() 
{
	return DLGC_WANTARROWS | DLGC_WANTTAB;
}

/////////////////////////////////////////////////////////////////////////////
// CFOImageStatic overridables

void CFOImageStatic::GetImageRect(CDC& dc, CRect rcButton,
								   CRect& rcImage, CRect& rcCaption)
{
	CString strCaption;
	GetWindowText(strCaption);
	
	// If the image is centered when there is a caption,
	// we align them differently.
	BOOL bCentered = (m_uImageAlign & DT_CENTER);
	
	if (bCentered && HasImages() && !strCaption.IsEmpty())
	{
		AdjustSizeCentered(dc, rcButton, rcImage, rcCaption);
	}
	else
	{
		AdjustSize(dc, rcButton, rcImage, rcCaption);
	}
}

/////////////////////////////////////////////////////////////////////////////
// CFOImageStatic overrides

void CFOImageStatic::GetFaceRect(CDC& dc, CRect rcButton, CRect& rcFace,
								  BOOL bSizeToContent)
{
	CFOCustomStatic::GetFaceRect(dc, rcButton, rcFace, bSizeToContent);
	
	if (bSizeToContent)
	{
		CRect rcImage;
		CRect rcCaption;
		GetImageRect(dc, rcButton, rcImage, rcCaption);
		
		CRect rcUnion;
		rcUnion.UnionRect(rcImage, rcCaption);
		
		rcFace.SetRect(0, 0, rcUnion.Width(), rcUnion.Height());
		
		// Leave a slight border between the face and the focus.
		if (!rcFace.IsRectEmpty())
			rcFace.InflateRect(1, 1);
	}
}

void CFOImageStatic::OnDrawFace(CDC& dc, const CRect& rc, UINT uState)
{
	CRect rcImage;
	CRect rcCaption;
	GetImageRect(dc, rc, rcImage, rcCaption);
	
	CString strCaption;
	GetWindowText(strCaption);
	DrawCaption(dc, rcCaption.TopLeft(), strCaption, uState);
	
	int nStartImage,nEndImage;
	nStartImage = 0;
	nEndImage = -1;
	
	if (nEndImage < 0)
		nEndImage = m_images.GetImageCount();
	else if (nEndImage == nStartImage)
		nEndImage = nStartImage + 1;
	CPoint pt = rcImage.TopLeft();
	for (int i=nStartImage; i < nEndImage; i++)
	{
		COLORREF clrBkColor = CLR_NONE;
		
		if (uState & DSS_DISABLED)
			clrBkColor = RGB(255,255,255);
		
		COLORREF clrOldBkColor = dc.SetBkColor(clrBkColor);
		
		if (!m_images.Draw(&dc, i, pt, ILD_NORMAL))
		{
			dc.SetBkColor(clrOldBkColor);
			return;
		}
		dc.SetBkColor(clrOldBkColor);
		
		CSize size;
		size.cx = 0;
		size.cy = 0;
		
		IMAGEINFO imageInfo;
		ZeroMemory(&imageInfo, sizeof(imageInfo));
		if (!m_images.GetImageInfo(i, &imageInfo))
			return;
		
		CRect rc(imageInfo.rcImage);
		size.cx = rc.Width();
		size.cy = rc.Height();
		
		pt.x += size.cx + m_cxImageSpacing;
	}
}

/////////////////////////////////////////////////////////////////////////////
// CFOImageStatic implementation

void CFOImageStatic::AdjustSize(CDC& dc, CRect rc,
								 CRect& rcImage, CRect& rcCaption)
{
	UNUSED_ALWAYS(dc);
	
	rcImage = rc;
	rcCaption = rc;
	
	if (HasImages())
	{
		int nStartImage,nEndImage;
		nStartImage = 0;
		nEndImage = -1;
		
		if (nEndImage < 0)
			nEndImage = m_images.GetImageCount();
		else if (nEndImage == nStartImage)
			nEndImage = nStartImage + 1;
		
		CSize totalSize;
		totalSize.cx = 0;
		totalSize.cy = 0;
		
		if (nEndImage < 0)
			nEndImage = GetImageCount();
		if (nEndImage == nStartImage)
			nEndImage = nStartImage + 1;
		
		
		for (int i=nStartImage; i < nEndImage; i++)
		{
			CSize size;
			size.cx = 0;
			size.cy = 0;
			
			IMAGEINFO imageInfo;
			ZeroMemory(&imageInfo, sizeof(imageInfo));
			if (m_images.GetImageInfo(i, &imageInfo))
			{
				CRect rc(imageInfo.rcImage);
				size.cx = rc.Width();
				size.cy = rc.Height();
			}
			
			if (i > nStartImage)
				totalSize.cx += m_cxImageSpacing;
			totalSize.cx += size.cx;
			totalSize.cy = max(totalSize.cy, size.cy);
		}
		CSize imageSize;
		imageSize = totalSize;
		
		CPoint ptImage;
		
		if (m_uImageAlign & DT_CENTER)
		{
			ptImage.x = rcImage.left + (rcImage.Width() - imageSize.cx) / 2;
		}
		else if (m_uImageAlign & DT_RIGHT)
		{
			// Image on the right, caption on the left.
			ptImage.x = rcImage.right - imageSize.cx - 1;
			rcCaption.right = ptImage.x - m_cxImageBorder;
		}
		else // DT_LEFT == 0
		{
			// Image on the left, caption on the right.
			ptImage.x = rcImage.left;
			rcCaption.left = ptImage.x + imageSize.cx + m_cxImageBorder;
		}
		
		if (m_uImageAlign & DT_VCENTER)
			ptImage.y = max(rcImage.top, rcImage.top + (rcImage.Height() - imageSize.cy) / 2 - 1);
		else if (m_uImageAlign & DT_BOTTOM)
			ptImage.y = rcImage.bottom - imageSize.cy - 1;
		else // DT_TOP == 0
			ptImage.y = rcImage.top;
		
		rcImage.OffsetRect(ptImage - rcImage.TopLeft());
		rcCaption.OffsetRect(ptImage - rcImage.TopLeft());
		
		rcImage.right = rcImage.left + imageSize.cx;
		rcImage.bottom = rcImage.top + imageSize.cy;
	}
	else
	{
		rcImage.SetRectEmpty();
	}
	
	CString strCaption;
	GetWindowText(strCaption);
	
	if (!strCaption.IsEmpty())
	{
		CSize textExtent = GetTextExtent(dc, strCaption);
		
		CPoint ptCaption;
		
		if (m_uCaptionAlign & DT_CENTER)
			ptCaption.x = max(rcCaption.left, rcCaption.left + (rcCaption.Width() - textExtent.cx) / 2);
		else if (m_uCaptionAlign & DT_RIGHT)
			ptCaption.x = rcCaption.right - textExtent.cx - 1;
		else // DT_LEFT == 0
			ptCaption.x = rcCaption.left;
		
		if (m_uCaptionAlign & DT_VCENTER)
			ptCaption.y = max(rcCaption.top, rcCaption.top + (rcCaption.Height() - textExtent.cy) / 2 - 1);
		else if (m_uCaptionAlign & DT_BOTTOM)
			ptCaption.y = rcCaption.bottom - textExtent.cy - 1;
		else // DT_TOP == 0
			ptCaption.y = rcCaption.top;
		
		rcCaption.OffsetRect(ptCaption - rcCaption.TopLeft());
		
		rcCaption.right = rcCaption.left + textExtent.cx;
		rcCaption.bottom = rcCaption.top + textExtent.cy;
	}
	else
	{
		rcCaption.SetRectEmpty();
	}
}

void CFOImageStatic::AdjustSizeCentered(CDC& dc, CRect rc,
										 CRect& rcImage, CRect& rcCaption)
{
	rcImage.SetRectEmpty();
	rcCaption.SetRectEmpty();
	
	int nStartImage,nEndImage;
	nStartImage = 0;
	nEndImage = -1;
	
	if (nEndImage < 0)
		nEndImage = m_images.GetImageCount();
	else if (nEndImage == nStartImage)
		nEndImage = nStartImage + 1;
	
	CSize totalSize;
	totalSize.cx = 0;
	totalSize.cy = 0;
	
	if (nEndImage < 0)
		nEndImage = GetImageCount();
	if (nEndImage == nStartImage)
		nEndImage = nStartImage + 1;
	
	for (int i=nStartImage; i < nEndImage; i++)
	{
		CSize size;
		size.cx = 0;
		size.cy = 0;
		
		IMAGEINFO imageInfo;
		ZeroMemory(&imageInfo, sizeof(imageInfo));
		if (m_images.GetImageInfo(i, &imageInfo))
		{
			CRect rc(imageInfo.rcImage);
			size.cx = rc.Width();
			size.cy = rc.Height();
		}
		
		if (i > nStartImage)
			totalSize.cx += m_cxImageSpacing;
		totalSize.cx += size.cx;
		
		totalSize.cy = max(totalSize.cy, size.cy);
	}
	CSize imageSize;
	imageSize = CSize(0,0);
	imageSize = totalSize;
	
	CString strCaption;
	GetWindowText(strCaption);
	CSize textExtent = GetTextExtent(dc, strCaption);
	
	const int cxImage = max(textExtent.cx, imageSize.cx);
	const int cyImage = textExtent.cy + imageSize.cy + m_cxImageBorder;
	
	CRect rcImageAndCaption(0, 0, cxImage, cyImage);
	
	// Horizontally center the image and caption.
	int xOffset = rc.left + (rc.Width() - rcImageAndCaption.Width()) / 2;
	
	// Caption alignment is now based on the image alignment.
	int yOffset = 0;
	if (m_uImageAlign & DT_VCENTER)
		yOffset = max(rc.top, rc.top + (rc.Height() - rcImageAndCaption.Height()) / 2);
	else if (m_uImageAlign & DT_BOTTOM)
		yOffset = rc.bottom - rcImageAndCaption.Height() - 1;
	else // DT_TOP = 0
		yOffset = rc.top;
	
	rcImageAndCaption.OffsetRect(xOffset, yOffset);
	
	rcImage = rcImageAndCaption;
	rcImage.OffsetRect(
		(rcImageAndCaption.Width() - imageSize.cx) / 2,
		0);
	
	rcCaption = rcImageAndCaption;
	rcCaption.OffsetRect(
		(rcImageAndCaption.Width() - textExtent.cx) / 2,
		rcImageAndCaption.Height() - textExtent.cy);
	
	rcImage.right = rcImage.left + imageSize.cx;
	rcImage.bottom = rcImage.top + imageSize.cy;
	
	rcCaption.right = rcCaption.left + textExtent.cx;
	rcCaption.bottom = rcCaption.top + textExtent.cy;
}

⌨️ 快捷键说明

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