来源(null)

<center>来源 (-)</center>

利用VC++开发所见即所得的打印程序

所见即所得的打印方式探受大家的欢迎,读者开发自己的所见即
所得的打印程序也不是很困难,只要掌握了WINDOWS字体大小以及了
解OnDraw函数与OnPrint函数中应做的工作就能作出比较满意的程序,
以下是程序生成的步骤:
(一)使用Appwizard来创建Prt_View.注意:
1.在classes选择中将文档Document的Class Name中的CPrt_
viewDoc类改为CsongDoc类,PrtvDoc.h和PrtvDoc.cpp分别改为s
ongDoc.h和songDoc.cpp;视View的prtvvw.h和Prtvvw.cpp分别
改为songvw.h和songvw.cpp(这样做的目的是使程序的可读物性更
好,因为我们要打印的内容是一首歌词);另外,将songvw.cpp的基
类改成带有滚动条的CScrollView类。 2.在Option选择中选取prin
ting And preview选项。
(二)利用ClassWizard为Csongvw类加入WM_CREATE消息控制函数。
(三)打开songdoc.h文件,加入
public:
	CStringArray m_songArray;
(四)打开songdoc.cpp文件,在OnNewDocument函数中加入以
下程序段:

BOOL CSongDoc::onNewDocument() {
	if(!CDocument::OnNewDocument())
		return FALSE;
	m_songArray.SetSize(13);
	m_songArray[0]="tell me how long,can we go in this way";
	m_songArray[1]="Just to be strong";
	m_songArray[2]="We talk through the night";
	m_songArray[3]="But never reach the other side";
	m_songArray[4]="We fill our lives";
	m_songArray[5]="When all that we need,we hold within our hearts";
	m_songArray[6]="When will we find,our way out of the dark";
	m_songArray[7]="chorus:"
	m_songArray[8]="We know that";
	m_songArray[9]="Love is the power,love is the key";
	m_songArray[10]="Love is the reason,reason to believe";
	m_songArray[11]="Love is the answer,when were we see";
	m_songArray[12]="Love is the power,love is the power,";
	return TRUE; 
}

void CSongDoc::Serialize(CArchive& ar){
	m_songArray.Serialize(ar);
}

(五)打开songvw.cpp,因为我们使用的是滚动视窗,就必须设
置可见窗口的尺寸,页的大小等。删除OnlnitialUpdate函数的内容
。在OnCreate函数中加入

int CSongView::OnCreate(LPCREATESTRUCT lpCreate Struct) { 
	if(CScrollView::OnCreate(lpCreateStruct)==-1)
		return -1;
	CSize totalSize=CSize(11520,15120);
	CSize pageSize=CSize(totalSize.cx/2,totalSize.cy/2);
	CSize lineSize=CSize(totalSize.cx/100,totalSize.cy/100);
	SetScrollSizes(MM_TWIPS,totalSize,pageSize,linSize);
	return 0;
}

在OnDraw函数中加入以下程序段:

void CSongView::OnDraw(CDC *pDC) {
	int i,j,nHeight;
	char temp[33];
	CFont font;
	TEXTMETRIC tm;
	CSongDoc *pDoc=GetDocument();
	DC->MoveTo(CPoint(0,0));
	pDC->LineTo(CPoint(11505,0));
	pDC->LineTo(CPoint(11505,-15105));
	pDC->LineTo(CPoint(0,-15105));
	pDC->LineTo(CPoint(0,0));
//	draw external frame of article
	for(i=0;i<=8;i=i+1){
		wsprintf(temp,"%02d",i);
		pDC->TextOut(i*1440,0,temp);
	}
	for(i=0;i<=10;i++){
		wsprintf(temp,"%02d",i);
		pDC->TextOut(0,-i*1440,temp);
	}
	font.CreateFont(-200,0,0,0,400,FALSE,FALSE,0,ANSI_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH|FF_ROMAN,"Times New Roman");
	CFont* pOldFont=(CFont*)pDC->SelectObject(&font);
	pDC->GetTextMetrics(&tm);
	nHeight=tm.tmHeight+tm.tmExternalLeading;
	j=pDoc->m_songArray.GetSize();
	for(i=0;i<j;i++){
		pDC->TextOut(720,-i*nHeight-720,pDoc->m_songArray[1]);
	}
	pDC->SelectObject(pOldFont);
}

在OnPreparePrinting函数中加入以下程序段:

BOOL CSongView::OnPreparePrinting(CPrintlnfo* plnfo){
//	default preperation
	plnfo->SetMaxPage(1);
	return DoPreparePrinting(plnfo);
}

(六)Build该程序,运行它就可以看到这个只占一页的歌词了。
□成都 田星

Link: http://www.asm32.net/article_details.aspx?id=77