打印程序HELLOWORLD
来源:互联网 发布:炫酷黑客主页源码 编辑:程序博客网 时间:2024/05/21 10:30
#include<windows.h>HDC GetPrinterDC(void){PRINTER_INFO_4 * pi;DWORD pcbNeed,pcReturned;EnumPrinters(PRINTER_ENUM_LOCAL,NULL,4,NULL,0,&pcbNeed,&pcReturned);//用于返回需要为这个类型4返回的结果体分配的内存字节数pi=(PRINTER_INFO_4 *)malloc(pcbNeed);EnumPrinters(PRINTER_ENUM_LOCAL,NULL,4,(PBYTE)pi,pcbNeed,&pcbNeed,&pcReturned);//将返回信息存储在pi中HDC hdc=CreateDC(NULL,pi->pPrinterName,NULL,NULL);if(hdc==NULL)return NULL;free(pi);//注意养成好习惯释放内存return hdc;}int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int iCmdShow){static DOCINFO di={0};di.cbSize=sizeof(DOCINFO);di.lpszDocName=TEXT("test");HDC hdcPrint=GetPrinterDC();if(hdcPrint!=NULL){if(StartDoc(hdcPrint,&di)>0){if(StartPage(hdcPrint)>0){Rectangle(hdcPrint,20,20,500,400);TextOut(hdcPrint,240,190,TEXT("这是我的第一个打印程序"),sizeof("这是我的第一个打印程序"));EndPage(hdcPrint);EndDoc(hdcPrint);}DeleteDC(hdcPrint);}} return 0 ;}
安装的虚拟打印机是SmartPrinter
后面出现的奇怪字符还不知道为什么?有知道的可以留言一下,谢谢
对于上面出现问题的解决
这个问题是由于前后使用的字符不一样导致的,前面是宽字符集wchar_t*,后面是char*类型计算的,所以
更改如下
TextOut(hdcPrint,240,190,TEXT("这是我的第一个打印程序"),sizeof(TEXT("这是我的第一个打印程序"))/2);
因为一个Unicode字符是两个字节的,所以这里要除以2
这样,后面出现的奇怪字符就不会出现了
如果未使用unicode字符,那么
TextOut(hdcPrint,240,190,TEXT("this is my first printer hello world"),sizeof("this is my first printer hello world"));
不用除以2就可以完整打印,而不会出现奇怪字符
好了,HelloWord到此打印结束
- 打印程序HELLOWORLD
- 在Java程序中调用C函数--打印"HelloWorld"
- 在Java程序中调用C函数--打印"HelloWorld"
- helloworld程序
- HelloWorld程序
- Lua打印HelloWorld
- ”打印helloworld“代码
- ”打印helloworld“代码
- MPI打印helloworld
- 在龙芯1c开发板上用裸机程序打印helloworld
- python初步(打印HelloWorld!)
- 配置环境并打印HelloWorld
- OpenGL的HelloWorld程序
- Struts2的HelloWorld程序
- Struts2之HelloWorld程序
- ant运行HelloWorld程序
- Struts2的HelloWorld程序
- Struts2的HelloWorld程序
- Android引路蜂地图开发示例:离线地图示例
- 个人简历
- 替换或者删除Android 4.0的app
- QT小技巧
- Android ApiDemos示例解析(53):Graphics->Arcs
- 打印程序HELLOWORLD
- 使用api
- FusionCharts+Struts2+AJAX动态报表
- JAVA中replace和replaceAll的区别(转)
- 自定义Application时的一个Trick
- Android RoboGuice2 使用指南(2): 第一个例子Hello World
- Emacs 入门指引(三) 开始使用
- bottle做web开发的物理设计
- H - Roller Coaster