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

类别:VC语言 点击:0 评论:0 推荐:

利用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文件,在OnNewDocument函数中加入以
下程序段:
1 BOOL CSongDoc::onNewDocument()
2 {
3 if(!CDocument::OnNewDocument())
4 return FALSE;
5 m-songArray.SetSize(13);
6 m-songArray[0]=”tell me how long,can we go in th
is way”;
7 m-songArray[1]=”Just to be strong”;
8 m-songArray[2]=”We talk through the night”;
9 m-songArray[3]=”But never reach the other side”;
10 m-songArray[4]=”We fill our lives”;
11 m-songArray[5]=”When all that we need,we hold w
ithin our hearts”;
12 m-songArray[6]=”When will we find,our way out o
f the dark”;
13 m-songArray[7]=”chorus:”
14 m-songArray[8]=”We know that”;
15 m-songArray[9]=”Love is the power,love is the k
ey”;
16 m-songArray[10]=”Love is the reason,reason to b
elieve”;
17 m-songArray[11]=”Love is the answer,when were w
e see”;
18 m-songArray[12]=”Love is the power,love is the
power,”;
19 return TRUE;}
20 void CSongDoc::Serialize(CArchive& ar)
21 } m-songArray.Serialize(ar):}
(五)打开songvw.cpp,因为我们使用的是滚动视窗,就必须设
置可见窗口的尺寸,页的大小等。删除OnlnitialUpdate函数的内容
。在OnCreate函数中加入
1 int CSongView::OnCreate(LPCREATESTRUCT lpCreate Str
uct)
2 { if(CScrollView::OnCreate(lpCreateStruct)==-
1)
3 return -1;
4 CSize totalSize=CSize(11520,15120);
5 CSize pageSize=CSize(totalSize.cx/2,totalSize.cy
/2);
6 CSize lineSize=CSize(totalSize.cx/100,totalSize.
cy/100);
7 SetScrollSizes(MM-TWIPS,totalSize,pageSize,linSiz
e);
8 return 0;}
在OnDraw函数中加入以下程序段:
1 void CSongView::OnDraw(CDC*pDC)
2 { int i,j,nHeight;
3 char temp[33];
4 CFont font;
5 TEXTMETRIC tm;
6 CSongDoc*pDoc=GetDocument();
7 pDC->MoveTo(CPoint(0,0));
8 pDC->LineTo(CPoint(11505,0));
9 pDC->LineTo(CPoint(11505,-15105));
10 pDC->LineTo(CPoint(0,-15105));
11 pDC->LineTo(CPoint(0,0));
12 //draw external frame of article
13 for(i=0;i<=8;i=i+1){
14 wsprintf(temp,”%02d”,i);
15 pDC->TextOut(i*1440,0,temp);}
16 for(i=0;i<=10;i++)
17 { wsprintf(temp,”%02d”,i);
18 pDC->TextOut(0,-i*1440,temp):}
19 font.CreateFont(-200,0,0,0,400,FALSE,FALSE,
0,ANSI-CHARSET,OUT-DEFAULT-PRECIS,CLIP-DEFAULT-PRECI
S,DEFAULT-QUALITY,DEFAULT-PITCH|FF-ROMAN,”Times New
Roman”);
20 CFont* pOldFont=(CFont*)pDC->SelectObject(&f
ont);
21 pDC->GetTextMetrics(&tm);
22 nHeight=tm.tmHeight+tm.tmExternalLeading;
23 j=pDoc->m-songArray.GetSize();
24 for(i=0;i<j;i++){
25 pDC->TextOut(720,-i*nHeight-720,pDoc->m-so
ngArray[1]);
26 }
27 pDC->SelectObject(pOldFont);}
在OnPreparePrinting函数中加入以下程序段:
BOOL CSongView::OnPreparePrinting(CPrintlnfo* plnf
o)
// default preperation
plnfo->SetMaxPage(1);
return DoPreparePrinting(plnfo);

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

本文地址:http://com.8s8s.com/it/it3092.htm