|
发表于 2012-9-12 22:07:57
|
显示全部楼层
找到個資料 但是 需要高手幫你轉嚕
/***************************************************************************/
/////////////////////////////////////////////////////////////////////////////
//函数名: Execute()
//功能说明:线程的执行函数,获得打印机的当前打印作业名及正在打印的页码.
// 并通知管理系统.
//参数:
//返回值:
/////////////////////////////////////////////////////////////////////////////
void __fastcall TthrdPrint::Execute()
{
//---- Place thread code here ----
BOOL bRval = 0;
HANDLE pHandle = NULL; //打印机句柄
JOB_INFO_2 *pJob = NULL; //打印机任务信息
DWORD dwNeeded, dwReturned;
AnsiString asMsg, asRptName, asTemp;
char cMsg[200]; //消息
DWORD iPrinted; //已打印的页数
AnsiString asDocName; //正在打印的文档名
String NodeName; //节名称字符串
String KeyName;//键名称字符串
char DefaultString[256];//在键名没有找到时默认返回的字符串
char ReturnedString[256];//在键名找到时返回的字符串
try
{
//取得默认的打印机名
NodeName = "windows "; // 节名称
KeyName = "device "; // 键名称
strcpy(DefaultString, "Unknown ");
GetProfileString(
NodeName.c_str(),
KeyName.c_str(),
DefaultString,
ReturnedString,
sizeof(ReturnedString));
asTemp = AnsiString(ReturnedString);
asTemp =asTemp.SubString(1, asTemp.Pos( ", ") - 1);
if(asTemp == " ")
{
memset(cMsg, 0x00, 200);
strcpy(cMsg, "报表服务机上没有默认打印机! ");
frmReportMain-> Udp-> SendBuffer(cMsg, 200, strlen(cMsg));
bRval = ClosePrinter(pHandle);
return;
}
//打开打印机
bRval = OpenPrinter(asTemp.c_str(), &pHandle, NULL);
if(!pHandle) return;
//while(!this-> Terminated)
{
//枚举该打印机中的作业队列
bRval = EnumJobs(pHandle,// handle to printer object
0,// location of first job in print queue to enumerate
1,// number of jobs to enumerate
2,// structure level
NULL,// pointer to structure array
0,// size of array, in bytes
&dwNeeded,// addr. of variable with no. of bytes copied (or required)
&dwReturned // addr. of variable with no. of job info. structures copied
);
if(!dwNeeded) //没有作业
{
memset(cMsg, 0x00, 200);
asDocName = asDocName + ":打印完毕! ";
strcpy(cMsg, asDocName.c_str());
frmReportMain-> Udp-> SendBuffer(cMsg, 200, strlen(cMsg));
bRval = ClosePrinter(pHandle);
return;
}
pJob = (JOB_INFO_2 *)malloc(dwNeeded);
bRval = EnumJobs(pHandle,// handle to printer object
0,// location of first job in print queue to enumerate
1,// number of jobs to enumerate
2,// structure level
(LPBYTE)pJob,// pointer to structure array
dwNeeded,// size of array, in bytes
&dwNeeded,// addr. of variable with no. of bytes copied (or required)
&dwReturned // addr. of variable with no. of job info. structures copied
);
if(asDocName != AnsiString(pJob-> pDocument) || iPrinted != pJob-> PagesPrinted)
{
iPrinted = pJob-> PagesPrinted;
asDocName = AnsiString(pJob-> pDocument);
asMsg = asDocName + ":现在正在打印第 " + IntToStr(iPrinted + 1) +
"页,总共 " + IntToStr(pJob-> TotalPages) + "页 " ;
frmReportMain-> Memo1-> Lines-> Add(asMsg);
memset(cMsg, 0x00, 200);
strcpy(cMsg, asMsg.c_str());
//把当前打印信息报告ECP
frmReportMain-> Udp-> SendBuffer(cMsg, 200, strlen(cMsg));
}
free(pJob);
pJob = NULL;
Application-> ProcessMessages();
Sleep(2000); //间隔2秒检查一次
}
frmReportMain-> threadOverFlag = 1;
}
catch(Exception &e)
{
if(pHandle) ClosePrinter(pHandle);
if(pJob) free(pJob);
pJob = NULL;
frmReportMain-> WirteLog(e.Message);
}
} |
|