C++ 如何调用windows API实现图片打印
头文件
#include "io.h"
#include <winspool.h>
#include <iostream>
#include <vectod>
using namespace std;
定义变量
CString m_PrinterDriverName;
CString m_PrinterPort;
CString m_PrinterName;
//步骤一 将图片绘制到HDC上
/*
PicPath:图片路径
Lcoordinate、Rcoordinate 绘制起始坐标
wratio:宽(放大/缩小)比率
hratio:高(放大/缩小)比率
*/
bool PictureToDc(HDC hDC,CString PicPath,int Lcoordinate,int Rcoordinate,float wratio,float hratio)
{
ATL::CImage image; //使用图片类
image.Load(strPic);
int pwidth=(int)(image.GetWidth()) * wratio;
int phight=(int)(image.GetHeight()) * hratio;
image.Draw(hDC,Lcoordinate,Rcoordinate,pwidth,phight);
DeleteObject(image);
return 0;
}
步骤二
枚举本地打印机
LONG DEV_FindPrinter(CString devName)
{
int iRet = 0 ;
int i ;
DWORD dwNeed ;
DWORD dwRet ;
DWORD dwBuffLen = 0;
BYTE *pBuffer ;
BYTE *pTmpBuffer ;
PRINTER_INFO_2 pInfo ;
EnumPrinters( PRINTER_ENUM_LOCAL, NULL, 2, NULL, 0, &dwNeed, &dwRet ) ;
pBuffer = new BYTE[dwNeed] ;
if ( pBuffer == NULL )
{
iRet = -1;
}
else
{
dwBuffLen = dwNeed ;
}
if ( iRet == 0 )
{
Need = dwNeed;
iRet = EnumPrinters( PRINTER_ENUM_LOCAL, NULL, 2, pBuffer, dwBuffLen, &dwNeed, &dwRet ) ;
if ( iRet == 0 )
{
iRet = -1
}
else
{
iRet = 0 ;
}
}
if ( iRet == 0 )
{
pTmpBuffer = pBuffer ;
for ( i = 0 ; i < (int)dwRet ; i++ )
{
memcpy( &pInfo, pTmpBuffer, sizeof(pInfo) ) ;
CString spPrinterName = pInfo.pPrinterName;
if (spPrinterName == devName)
{
m_PrinterPort= pInfo.pPortName;
m_PrinterDriverName= pInfo.pDriverName;
iRet = 0 ;
break;
}
else
{
iRet = -1;
}
pTmpBuffer += sizeof(pInfo) ;
}
}
if (iRet!=0)
{
m_PrinterName="No printer available";
m_PrinterPort= "";
}
return iRet ;
}
步骤三:打印图片
int PrintPicture(CString devName,CString picPath,int Lcord,int Rcord,float wRatio,float hRatio)
{
CString str;
DWORD dwError;
int iRet=0;
DOCINFO di;
ZeroMemory(&di,sizeof(DOCINFO));
di.cbSize = sizeof(DOCINFO);
di.lpszDocName = NULL;
di.lpszOutput = NULL;//"FILE:";
di.lpszDatatype =_T("raw");
iRet = DEV_FindPrinter(devName);
if(iRet != 0)
return iRet;
HDC hdcPrint=CreateDC(m_PrinterDriverName,devName,NULL,NULL);
if(hdcPrint != NULL)
{
if(StartDoc(hdcPrint,&di)>0) //开始执行一个打印作业
{
if(StartPage(hdcPrint)>0) //走纸,开始打印
{
CString strPath(picPath);
iRet =PrintPicture(hdcPrint,strPath,Lcoordinate,Rcoordinate,wratio,hratio);
if(iRet != 0)
{
AfxMessageBox("打印失败");
return iRet;
}
EndPage(hdcPrint);
EndDoc(hdcPrint);
}
else
{
iRet= GetLastError();
return iRet;
}
DeleteDC(hdcPrint);
}
dwError = GetLastError(); //
}
else
{
iRet = GetLastError();
return iRet;
}
return 0;
}
打印图片这个过程就完成了。
C++ 如何调用windows API实现图片打印相关推荐
- pythonenumapi_Python调用windows API实现屏幕截图
Python调用windows API实现屏幕截图 好处是 灵活 速度快 缺点是: 写法繁琐 不跨平台 import time import win32gui, win32ui, win32con, ...
- python windows api截图_Python调用windows API实现屏幕截图
Python调用windows API实现屏幕截图 好处是 灵活 速度快 缺点是: 写法繁琐 不跨平台 import time import win32gui, win32ui, win32con, ...
- 调用Windows api 窗口截图
分别调用windows api函数及CVI自带函数,完成对GRAPH界面的截图 界面设计如下图,右侧为graph控件的位置信息,调用API函数是完成对于选择窗口的固定位置进行截图,调用CVI自带函数则 ...
- VBS调用Windows API函数
Demon's Blog 忘记了,喜欢一个人的感觉 Demon's Blog » 程序设计 » VBS调用Windows API函数 « 用VBS修改Windows用户密码 在VB中创建和使用 ...
- Linux python PyQt5调用百度API实现图片文字转换
系统:Linux Mint 18.3 xfce 64bit 参考链接:http://blog.csdn.net/u012236875/article/details/74726035 根据参考链接的代 ...
- Java 中调用 Apache API 实现图片文件的 压缩 与 解压 实例
< Java 中调用 Apache API 实现图片文件的 压缩 与 解压 > 为什么不直接使用 Java JDK 中自带的 API 呢?必须使用 Apache API 实现文件的压缩与解 ...
- 如何在go中调用windows api
1.cgo环境搭建 初入go坑,记录一次工作中需要封装windows api 的过程.既然是go调用C++那么首先要配置cgo的环境了.要使用CGO特性,需要安装C/C++构建工具链,在macOS和L ...
- C#中调用Windows API时的数据类型对应关系
C#中调用Windows API时的数据类型对应关系 原文 C#中调用Windows API时的数据类型对应关系 BOOL=System.Int32 BOOLEAN=System.Int32 BYTE ...
- C#调用windows api的要点
在.Net Framework SDK文档中,关于调用Windows API的指示比较零散,并且其中稍全面一点的是针对Visual Basic .net讲述的.本文将C#中调用API的要点汇集如下,希 ...
最新文章
- 【OpenCV 4开发详解】边缘检测原理
- Node入门--6--文件系统-创建删除
- 标 题: 腾讯面试题目(PHP程序员)
- 怎样判断RadioButtonList控件是否有选择
- pandas(七) -- 数据分组
- Scala伴生类与伴生对象:apply方法/伴生对象可以访问伴生类的私有属性
- 项目G2SVG.text转换
- 获取iOS设备当前ip地址
- delphi xe http 收不到反馈消息_好消息!浦东这个小区3幢多层住宅都要加装电梯啦→...
- PPTV Docker集群的网络方案选型
- Kubernetes 证书过期
- (15)QuartusII 17.1开发流程(FPGA不积跬步101)
- 深度剖析Spring Cloud底层原理
- P1081 开车旅行
- 你还在全量加载iView么?试试手动配一个吧
- OptiStruct] Altair OptiStruct之前世今生
- TwinCAT学习笔记3--创建PLC程序控制2台EtherCAT变频器
- 进程之间有哪些通信方式
- 【LeetCode 1240】 Tiling a Rectangle with the Fewest Squares
- adb 判断imei_adb 获取imei
热门文章
- 收藏!机器学习相关的人工智能公众号推荐
- oppo如何改计算机颜色,oppo怎么设置桌面图标的样式及颜色
- 双屏玩游戏鼠标滑到另外一个屏幕_灵耀X2 Duo双屏操作!边上课边做笔记秀了,旦用难回的创新体验...
- C++ 模式设计 原型模式(深拷贝/克隆)
- 【如何给iOS APP加固】之代码混淆及加密 第一章【附代码】
- sql字符串拼接或给某个字段值添加字符串前后缀
- (Modern Family S01E03) Part 2 JayGloria Jay玩弄飞机 Gloria说兴起对于男人的重要性
- 解决“The channel is not accessible or is invalid”问题
- lol手游服务器什么时候维修好,拳头哭了!刚修好英雄联盟手游服务器,结果又被中国玩家挤爆了...
- 运动蓝牙耳机哪个品牌好、最好的五款运动蓝牙耳机品牌排行推荐