大雀软件园

首页 软件下载 安卓市场 苹果市场 电脑游戏 安卓游戏 文章资讯 驱动下载
技术开发 网页设计 图形图象 数据库 网络媒体 网络安全 站长CLUB 操作系统 媒体动画 安卓相关
当前位置: 首页 -> 技术开发 -> 程序开发 -> VC5打印字体的控制

VC5打印字体的控制

时间: 2021-07-31 作者:daque

vc5.0 为windows 的步调员供给了一个很好的c++ 开拓情况,缩小了很多编制程序承担,但同声也为咱们在步调中介入本人的思维减少了难度。自己在一软硬件开拓中,想遏制笔墨打字与印刷时的字体,使字体巨细减少一倍,以俭朴打字与印刷纸。过程一段功夫的探求,毕竟处置了这一题目,底下分几步向大师做一引见。---- 一、对vc5 机动天生的步调框架举行矫正---- 这边用vc5 机动创造一个例子步调test,单文书档案界面,提防在结果一步窜改view 的接受类为ceditview。---- 在view 类中,vc5 仍旧机动创造了三个用来扶助打字与印刷的因变量:onprepareprinting,onbeginprinting,onendprinting。为了实行咱们的功效,须要再接受以次几个因变量:onpreparedc,onprint。并将onpreparedc 和onendprinting 改为如次实行:// onpreparedc()void ctestview::onpreparedc(cdc* pdc, cprintinfo* pinfo){cview::onpreparedc(pdc, pinfo);}// onendprinting()void ctestview::onendprinting(cdc* pdc, cprintinfo* pinfo){cview::onendprinting(pdc, pinfo);}---- 用cview 来代替从来的ceditview,用以制止ceidtview 对打字与印刷的遏制。遏制字体及输入的功效重要在onbeginprinting 和onprint 两个因变量来实行。---- 二、实行onbeginprinting 因变量---- 按照vc5 编制程序体制,在onbeginprinting 因变量实行打字与印刷前的筹备处事,囊括树立打字与印刷字体,按照打字与印刷机暂时页面尺寸计划所需页数等。底下的步调是对打字与印刷字体的从新树立和计划所需打字与印刷纸页数。---- 步调中开始博得打字与印刷机的横向和纵向辨别率,再获得暂时打字与印刷字体的巨细,而后计划出新的字体巨细,为默许字体的一半。读者群不妨按照须要设定本人的打字与印刷字体巨细。---- 接着,博得暂时打字与印刷纸的宽窄和莫大,再按照新字体的宽窄和莫大计划出每行的最大字符数和每页的最大行数。---- 因为打字与印刷文献中有些行的宽窄大概胜过每行的最大字符数,以是步调中挪用因变量redealtextdata() 对打字与印刷文献举行从新整治,因变量的实行在底下引见。---- 结果,步调入彀算并树立所需的打字与印刷页数。onbeginprinting()因变量实行如次://====================================// onbeginprinting//====================================void ctestview::onbeginprinting(cdc* pdc,cprintinfo* pinfo) {//树立新的? 字体//////////////////取打字与印刷机的横目标和纵目标的辨别率//即每英尺点数short cxinch = pdc- >getdevicecaps(logpixelsx);short cyinch = pdc- >getdevicecaps(logpixelsy);// 取暂时字体巨细cfont *curfont = pdc- >getcurrentfont();logfont curlogfont;logfont newlogfont;curfont- >getlogfont( &curlogfont );long newfontwidth = curlogfont.lfwidth;long newfontheight = curlogfont.lfheight;newlogfont = curlogfont;//计划新的字体巨细--减少一倍newlogfont.lfwidth =(long)((float)newfontwidth/2.0* ((float)cxinch / 72.0));newlogfont.lfheight =(long)((float)newfontheight/2.0* ((float)cyinch / 72.0)); //创造并树立新的字体,保持往日的字体cfont newfont;cfont *oldfont;newfont.createfontindirect(&newlogfont);oldfont = pdc- >selectobject(&newfont );///////////////////////////////////按照字体宽窄、莫大计划//每行最大篇幅及每页最大行数//取打字与印刷纸张莫大和宽窄int npageheight, npagewidth;npageheight = pdc- >getdevicecaps(vertres);npagewidth = pdc- >getdevicecaps(horzres);textmetric textm;pdc- >gettextmetrics(&textm);//字体莫大m_lineheight = (unsigned short)textm.tmheight;//字体平衡宽窄m_charwidth=(unsigned short)textm.tmavecharwidth;//每行最大篇幅m_maxlinechar = npagewidth / m_charwidth - 8; //每页最大行数m_linesperpage = npageheight/ m_lineheight; //按照每行最大篇幅对笔墨举行从新安排redealtextdata(); ////////////////////////////////////////计划所需打字与印刷纸张数量int nprintablelinecount = int_max/m_lineheight;// m_lines为文献总行数if (m_lines < nprintablelinecount)nprintablelinecount = m_lines;unsigned short maxpage = (nprintablelinecount+ m_linesperpage - 1) / m_linesperpage;//树立所需打字与印刷纸张数量pinfo- >setmaxpage(maxpage);pinfo- >m_ncurpage = 1;////////////////////////////////////////////结果不要忘怀将字体恢复,这一句是必定的pdc- >selectobject(oldfont );}---- redealtextdata 因变量按照每行最大宽窄对文献举行从新安排。主假如计划文献中每行的宽窄,即使胜过最大宽窄则介入换行符(0x0d,0x0a)。因变量实行如次://=======================================// redealtextdata//注://pdoc- >buffer为文献缓冲区//pdoc- >file_length为文献字节长度//pdoc- >textlines为文献原行数//pdoc- >maxlinelength为文献原最大行字节宽窄//=======================================void ctextview::redealtextdata(){cdocviewdoc* pdoc = getdocument();assert_valid(pdoc);short linelengthmax = m_maxlinechar;unsigned short lines=0;unsigned long i,j;//请求新的缓冲区生存安排后的文献long size = pdoc- >file_length + pdoc- >textlines*(pdoc- >maxlinelength/m_maxlinechar+1);m_newbuffer = new char [size ];lpstr newtempptr = m_newbuffer;m_file_length =pdoc- >file_length;//生存文献新的行数m_lines = 1;i = 0;//记载暂时行的宽窄short thelinelength=0; //记载暂时行中中国字字节数,//以提防将一半中国字分为两行unsigned short halfchinese=0;while(i < pdoc- >file_length){*newtempptr++ = pdoc- >buffer[i];j=i+1;if( (pdoc- >buffer[i] == 0x0d && pdoc- >buffer[j] == 0x0a)){m_lines++;thelinelength = 0;}else{//即使是tab字符,宽窄加8if(pdoc- >buffer[i] == vk_tab)thelinelength += 8;else {//大于0xa1的字节为中国字字节if((unsigned char)pdoc- >buffer[i] >= 0xa1)halfchinese++;thelinelength++;}//即使行款待于每行最大宽窄,举行特出处置if(thelinelength > linelengthmax){char buff[256];short m=255;newtempptr--;if((unsigned char )*newtempptr < 0xa1){//即使暂时字符的前一个字符是数字、//假名或少许特出的前置标记时,//南针轮回向前取,//以提防将一个单词分为两行。while((*newtempptr >=0 && *newtempptr< =9)||(*newtempptr >=a && *newtempptr < = z) ||(*newtempptr >=a && *newtempptr < = z) ||*newtempptr == _ || *newtempptr == * ||*newtempptr == ^ || *newtempptr == ~ )buff[m--] = *newtempptr--;}else //中国字{ //提防将一个中国字分为两行。if(halfchinese%2)buff[m--] = *newtempptr--;}newtempptr++;//介入换行符,分为两行*newtempptr++ = 0x0d;*newtempptr++ = 0x0a;for(short k=m+1; k< 256; k++)*newtempptr++ = buff[k];m_lines++;thelinelength = 0;m_file_length += 2;}}i++;}}---- 三、实行onprint 因变量---- 在onprint 因变量中实行真实的笔墨输入,重要功效囊括树立打字与印刷字体巨细,计划暂时页号笔墨输入场所,以及笔墨的输入打字与印刷。---- 步调中开始计划打字与印刷地区,笔墨在这个打字与印刷地区内输入。而后树立新的打字与印刷字体。---- 因为onprint 因变量是每打字与印刷一页被挪用一次,以是须要按照暂时打字与印刷页号计划出暂时页的笔墨在所有笔墨缓冲区的开始偏移量和中断偏移量。这历程序中挪用了因变量getoffset(),此因变量在底下引见。---- 结果挪用windows 的drawtext() 因变量实行笔墨的输入。onprint()因变量实行如次://====================================// onprint//========================================void ctestview::onprint(cdc* pdc, cprintinfo* pinfo) {//计划打字与印刷地区//////////////////long ytopofpage =(pinfo- >m_ncurpage -1) * m_linesperpage * m_lineheight;//左边空出两个字符宽窄pdc- >setviewportorg(m_charwidth * 2, -ytopofpage);int npagewidth = pdc- >getdevicecaps(horzres);crect rectclip = crect(0, ytopofpage, npagewidth,ytopofpage + m_linesperpage * m_lineheight);/////树立减少字体/////////////////////取打字与印刷机的横目标和纵目标的辨别率//即每英尺点数short cxinch=pdc- >getdevicecaps(logpixelsx);short cyinch= dc- >getdevicecaps(logpixelsy);//取暂时字体巨细cfont *curfont = pdc- >getcurrentfont();logfont curlogfont;logfont newlogfont;curfont- >getlogfont( &curlogfont );long newfontwidth = curlogfont.lfwidth;long newfontheight = curlogfont.lfheight;newlogfont = curlogfont;//计划新的字体巨细--减少一倍newlogfont.lfwidth = (long)((float)newfontwidth/2.0 * ((float)cxinch / 72.0));newlogfont.lfheight = (long)((float)newfontheight/2.0 * ((float)cyinch / 72.0)); //创造并树立新的字体,保持往日的字体cfont newfont;cfont *oldfont;newfont.createfontindirect(&newlogfont);oldfont = pdc- >selectobject(&newfont );/////笔墨打字与印刷输入/////////////////unsigned short currentstartline ,currentendline;long startprintoffset, endprintoffset, printsize;lpstr tempptr;rect rect1,rect2;//按照暂时打字与印刷页号计划笔墨开始行currentstartline=(pinfo- >m_ncurpage-1) * m_linesperpage;//笔墨中断行currentendline = currentstartline+m_linesperpage;if(currentendline > m_lines)currentendline = m_lines;//计划打字与印刷笔墨的开始场所和中断场所startprintoffset=getoffset(m_newbuffer,m_file_length, currentstartline);endprintoffset = getoffset(m_newbuffer,m_file_length,currentendline);printsize = endprintoffset - startprintoffset;tempptr = m_newbuffer + startprintoffset;//笔墨输入pdc- >drawtext(tempptr, printsize,&rectclip,dt_noclip |dt_noprefix|dt_expandtabs);//恢复旧的打字与印刷字体pdc- >selectobject(oldfont );}---- 步调中的getoffset 因变量是按照给定的行号计划笔墨的场所,本来现如次://========================================// getoffset () //========================================long ctestview::getoffset(lpstr buffer, long buffer_length, unsigned short startline){if(startline == 0) return 0;unsigned short lines=0;long i,j;i = 0;while(i < buffer_length){j=i+1;if( (buffer[i++] == 0x0d && buffer[j] == 0x0a)){lines++;if(lines == startline)return i;}}return buffer_length;}---- 之上是自己在编制程序中的一点心得,欢送和大师共通交谈。

热门阅览

最新排行

Copyright © 2019-2021 大雀软件园(www.daque.cn) All Rights Reserved.