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

📄 personvi.cpp

📁 API经典入门
💻 CPP
字号:
// personvi.cpp : implementation file
//

#include "stdafx.h"
#include "tree.h"
#include "personvi.h"
#include "treeset.h"
#include "treedoc.h" 
#include "DDBitmap.h"
#include "DIBitmap.h"

#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CPersonView

IMPLEMENT_DYNCREATE(CPersonView, CView)

CPersonView::CPersonView()
{
}

CPersonView::~CPersonView()
{
}


BEGIN_MESSAGE_MAP(CPersonView, CView)
	//{{AFX_MSG_MAP(CPersonView)
	ON_COMMAND(ID_INFO_LOADDIB, OnInfoLoaddib)
	//}}AFX_MSG_MAP
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CPersonView drawing

void DrawBitmap (CDC* pDC, CBitmap* pBM, int x, int y)
{
	// Make sure pointers are valid
	if (!pDC || !pBM)
	{
		TRACE0("NULL pointer passed to DrawBitmap().\n");
		return;
	}
	
	// Get bitmap info
	BITMAP bm;
	pBM->GetObject(sizeof(bm), &bm);
	
	// Create bitmap memory context
	CDC dcBM;
	dcBM.CreateCompatibleDC(pDC);
	
	// Select the bitmap
	CBitmap* pOld = dcBM.SelectObject(pBM);
	
	// Block transfer the bitmap to the screen
	pDC->BitBlt(x, y, bm.bmWidth, bm.bmHeight,
				&dcBM, 0, 0, SRCCOPY);
	
	// return dc to original
	dcBM.SelectObject(pOld);
}

void CPersonView::OnDraw(CDC* pDC)
{
	CTreeDoc* pDoc = (CTreeDoc*)GetDocument();
    CTreeSet* pSet = &pDoc->m_treeSet;
    CString S;
    int PortraitID;
    
    int X = 10, Y = 10;
    int offset = 2*(pDC->GetTextExtent("Wpqit", 5)).cy;
    
    if (pDC->IsPrinting())
    {
       Y += 2*offset;
    }
	
	pDC->TextOut(X, Y, "Full Name: " + pSet->FullName());
	pDC->TextOut(X, Y += offset, "Maiden: " + pSet->m_MAIDEN);
	
	if (pSet->m_IS_MALE)
	{
		S = "Person is Male.";
		PortraitID = IDB_DEF_MALE;
	}
	else
	{
		S = "Person is Female.";
		PortraitID = IDB_DEF_FEMALE;
	}
	
	// Draw bitmap on pDC object
	if (!pDC->IsPrinting())
	{
		if (pSet->m_PORTRAIT.m_dwDataLength != 0)
		{
			TRACE0("Person View found a portrait!\n");
			CDIBitmap DIB;
			DIB.LoadFromHandle(pDoc->m_treeSet.m_PORTRAIT.m_hData,
					pDoc->m_treeSet.m_PORTRAIT.m_dwDataLength);
			if (!DIB.DrawOnDC(pDC, 300, 10))
				TRACE0("CDIBitmap::DrawOnDC() failed.\n");
		}
		else
		{
			TRACE0("Person View DID NOT find a portrait!\n");
			CDDBitmap DDB;
			DDB.LoadBitmap(PortraitID);
			//DDB.DrawOnDC(pDC, 300, 10);
			BITMAP bm;
			DDB.GetObject(sizeof(bm), &bm);
			DDB.StretchOnDC(pDC, 300, 10, bm.bmWidth*2, bm.bmHeight*2);
			DDB.DeleteObject();
		}
	}
	
	pDC->TextOut(X, Y += offset, S);
	pDC->TextOut(X, Y += offset, "Birth Place: " + pSet->m_WHERE_BORN);
	pDC->TextOut(X, Y += offset, "Birthdate: " + pSet->m_BIRTHDATE.DateString());
	pDC->TextOut(X, Y += offset, "Death Place: " + pSet->m_WHERE_DIED);
	pDC->TextOut(X, Y += offset, "Deathdate: " + pSet->m_DEATHDATE.DateString());
	pDC->TextOut(X, Y += offset, "Interred: " + pSet->m_INTERRED);
	pDC->TextOut(X, Y += offset, "Occupation: " + pSet->m_OCCUPATION);
	
	pDC->TextOut(X, Y += offset, "Notes: ");
	S = pSet->m_NOTES;
	// divide up the notes string into bite sized (60 character) chunks
	// so that it may be read on one screen.
	while (S.GetLength() > 60)
	{
		pDC->TextOut(X+10, Y += offset, S.Left(60));
		S = S.Right(S.GetLength() - 60);
	}
	pDC->TextOut(X+10, Y += offset, S);
}

/////////////////////////////////////////////////////////////////////////////
// CPersonView Printer Support
 
BOOL CPersonView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}


void CPersonView::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo)
{
	pFont = new CFont;
	                                 
	LOGFONT lf;
	LPLOGFONT plf = NULL;
	
	CFontDialog dlg(plf, CF_PRINTERFONTS, pDC);
	
	if (dlg.DoModal() == IDOK)
	{
		lf = dlg.m_lf;
		TRACE1("font selected %s\n", lf.lfFaceName);
        //map the log font to the screen metrics
		CDC dcScreen;
		dcScreen.Attach(::GetDC(NULL));  
		lf.lfHeight = MulDiv(lf.lfHeight,
		  pDC->GetDeviceCaps(LOGPIXELSY), dcScreen.GetDeviceCaps(LOGPIXELSY));
		lf.lfWidth = MulDiv(lf.lfWidth,
		  pDC->GetDeviceCaps(LOGPIXELSX), dcScreen.GetDeviceCaps(LOGPIXELSX));		  
        //Create the font
		pFont->CreateFontIndirect(&lf); 
	    //This reverses the attach we did earlier
	   	::ReleaseDC(NULL, dcScreen.Detach());
	}
	
}




void CPersonView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo)
{
	CView::OnPrepareDC(pDC, pInfo);
	
	if (pDC->IsPrinting() && pFont)
	{                                               
		pDC->SelectObject(pFont);
	}
}
 
void CPersonView::OnPrint(CDC *pDC, CPrintInfo* pInfo)
{
	CString strHeader = "Teach Yourself the MFC in 21 Days";
 	CString strFooter = "Person Data Printout";
 	int MyChar = 2*(pDC->GetTextExtent("Wpqit", 5)).cy;
    CRect ThePage = pInfo->m_rectDraw;

	// draw and exclude space for header
	if (!strHeader.IsEmpty())
	{
		pDC->TextOut(ThePage.left, ThePage.top, strHeader);
		ThePage.top += MyChar + MyChar / 4;
		pDC->MoveTo(ThePage.left, ThePage.top);
		pDC->LineTo(ThePage.right, ThePage.top);
		ThePage.top += MyChar / 4;
	}

	// allow space for footer
	pInfo->m_rectDraw = ThePage;
	if (!strFooter.IsEmpty())
		pInfo->m_rectDraw.bottom -= MyChar + MyChar/4 + MyChar/4;
    
	// draw body text
	CView::OnPrint(pDC, pInfo);

	// draw footer
	if (!strFooter.IsEmpty())
	{
		ThePage.bottom -= MyChar;
		pDC->TextOut(ThePage.left, ThePage.bottom, strFooter);
		ThePage.bottom -= MyChar / 4;
		pDC->MoveTo(ThePage.left, ThePage.bottom);
		pDC->LineTo(ThePage.right, ThePage.bottom);
		ThePage.bottom -= MyChar / 4;
	}
}                    

void CPersonView::OnEndPrinting(CDC* pDC, CPrintInfo* pInfo)
{
	delete pFont;
}

/////////////////////////////////////////////////////////////////////////////
// CPersonView message handlers


void CPersonView::OnInfoLoaddib()
{
	CTreeDoc* pDoc = (CTreeDoc*)GetDocument();
	CFileDialog dlg(TRUE, NULL, "C:\\WINDOWS\\*.bmp");
	
	if (dlg.DoModal() == IDOK)
	{
		BeginWaitCursor();
		pDoc->m_bmPortrait.LoadFromDIB(dlg.GetPathName());
		//pDoc->m_bmPortrait.LoadFromDIB("C:\\WINDOWS\\WINLOGO.BMP");
		//pDoc->m_bmPortrait.LoadFromDIB("C:\\WINDOWS\\256COLOR.BMP");
		EndWaitCursor();
	}
}

⌨️ 快捷键说明

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