PDEVMODE GetDefaultPrinterDevMode(LPTSTR lpszPrinterName)
{
LONG lDevModeSize;
HANDLE hDevMode;
PDEVMODE pDevMode = NULL;
lDevModeSize = DocumentProperties(NULL, NULL, lpszPrinterName, NULL, NULL, 0);
if (lDevModeSize > 0)
{
hDevMode = GlobalAlloc(GHND, lDevModeSize);
pDevMode = (PDEVMODE) GlobalLock(hDevMode);
DocumentProperties(NULL, NULL, lpszPrinterName, pDevMode, NULL, DM_OUT_BUFFER);
}
else
{
return NULL; }
return pDevMode;
} HDC GetPrinterDC(LPTSTR lpszFriendlyName, PDEVMODE pDevMode)
{
HDC hDC;
BOOL bFreeDevMode = FALSE; // Initialize variables
hDC = NULL;
if (lpszFriendlyName != NULL)
{ // Make sure that we have a devmode.
if (NULL == pDevMode)
{
pDevMode = GetDefaultPrinterDevMode(lpszFriendlyName);
bFreeDevMode = TRUE;
} // Now get a DC for the printer
hDC = CreateDC(NULL, lpszFriendlyName, NULL, pDevMode);
// Free devmode if created in routine.
if (bFreeDevMode)
{
GlobalFree((HANDLE)pDevMode);
}
}
else
{
return NULL;
}
return hDC;
} // End of function GetPrinterDC
extern "C" int PASCAL EXPORT GetPrinterSate()//char *PrintName)
{
CPrintInfo printInfo;
HANDLE hPrinter;
char PrintName[100];
DWORD len,type;
// RegQueryValueEx(HKEY_CURRENT_CONFIG,_T("SYSTEM\\CurrentControlSet\\Control\\Print\\Printers\\Default"),NULL,&type,PrintName,&len);
::GetPrivateProfileString("HARD","Default Printer","",PrintName,100,".\\ssn.ini"); BOOL b = OpenPrinter(PrintName,&hPrinter,NULL);
if(!b || !hPrinter) return -1;
PRINTER_INFO_2 *printinfo;
DWORD dwNeeded;
b = GetPrinter(hPrinter,2,0,0,&dwNeeded); printinfo = (PRINTER_INFO_2 *)GlobalAlloc(GPTR, dwNeeded);
GetPrinter(hPrinter, 2, (LPBYTE)printinfo, dwNeeded, &dwNeeded); DWORD statue = printinfo->Status;
ClosePrinter(hPrinter);
if(statue & PRINTER_STATUS_PAPER_JAM) return PAPER_BLOCKED;
if(statue & PRINTER_STATUS_PAPER_OUT) return PAPER_OUT; if(statue & (PRINTER_STATUS_PAGE_PUNT | PRINTER_STATUS_BUSY | PRINTER_STATUS_DOOR_OPEN\
| PRINTER_STATUS_ERROR | PRINTER_STATUS_INITIALIZING | PRINTER_STATUS_IO_ACTIVE\
| PRINTER_STATUS_MANUAL_FEED | PRINTER_STATUS_NO_TONER | PRINTER_STATUS_NOT_AVAILABLE\
| PRINTER_STATUS_OFFLINE | PRINTER_STATUS_OUT_OF_MEMORY | PRINTER_STATUS_OUTPUT_BIN_FULL\
| PRINTER_STATUS_PAPER_PROBLEM | PRINTER_STATUS_PAUSED | PRINTER_STATUS_PENDING_DELETION\
| PRINTER_STATUS_PRINTING | PRINTER_STATUS_PROCESSING | PRINTER_STATUS_TONER_LOW\
| PRINTER_STATUS_USER_INTERVENTION | PRINTER_STATUS_WAITING | PRINTER_STATUS_WARMING_UP)) return -1;
return 0;
}
{
LONG lDevModeSize;
HANDLE hDevMode;
PDEVMODE pDevMode = NULL;
lDevModeSize = DocumentProperties(NULL, NULL, lpszPrinterName, NULL, NULL, 0);
if (lDevModeSize > 0)
{
hDevMode = GlobalAlloc(GHND, lDevModeSize);
pDevMode = (PDEVMODE) GlobalLock(hDevMode);
DocumentProperties(NULL, NULL, lpszPrinterName, pDevMode, NULL, DM_OUT_BUFFER);
}
else
{
return NULL; }
return pDevMode;
} HDC GetPrinterDC(LPTSTR lpszFriendlyName, PDEVMODE pDevMode)
{
HDC hDC;
BOOL bFreeDevMode = FALSE; // Initialize variables
hDC = NULL;
if (lpszFriendlyName != NULL)
{ // Make sure that we have a devmode.
if (NULL == pDevMode)
{
pDevMode = GetDefaultPrinterDevMode(lpszFriendlyName);
bFreeDevMode = TRUE;
} // Now get a DC for the printer
hDC = CreateDC(NULL, lpszFriendlyName, NULL, pDevMode);
// Free devmode if created in routine.
if (bFreeDevMode)
{
GlobalFree((HANDLE)pDevMode);
}
}
else
{
return NULL;
}
return hDC;
} // End of function GetPrinterDC
extern "C" int PASCAL EXPORT GetPrinterSate()//char *PrintName)
{
CPrintInfo printInfo;
HANDLE hPrinter;
char PrintName[100];
DWORD len,type;
// RegQueryValueEx(HKEY_CURRENT_CONFIG,_T("SYSTEM\\CurrentControlSet\\Control\\Print\\Printers\\Default"),NULL,&type,PrintName,&len);
::GetPrivateProfileString("HARD","Default Printer","",PrintName,100,".\\ssn.ini"); BOOL b = OpenPrinter(PrintName,&hPrinter,NULL);
if(!b || !hPrinter) return -1;
PRINTER_INFO_2 *printinfo;
DWORD dwNeeded;
b = GetPrinter(hPrinter,2,0,0,&dwNeeded); printinfo = (PRINTER_INFO_2 *)GlobalAlloc(GPTR, dwNeeded);
GetPrinter(hPrinter, 2, (LPBYTE)printinfo, dwNeeded, &dwNeeded); DWORD statue = printinfo->Status;
ClosePrinter(hPrinter);
if(statue & PRINTER_STATUS_PAPER_JAM) return PAPER_BLOCKED;
if(statue & PRINTER_STATUS_PAPER_OUT) return PAPER_OUT; if(statue & (PRINTER_STATUS_PAGE_PUNT | PRINTER_STATUS_BUSY | PRINTER_STATUS_DOOR_OPEN\
| PRINTER_STATUS_ERROR | PRINTER_STATUS_INITIALIZING | PRINTER_STATUS_IO_ACTIVE\
| PRINTER_STATUS_MANUAL_FEED | PRINTER_STATUS_NO_TONER | PRINTER_STATUS_NOT_AVAILABLE\
| PRINTER_STATUS_OFFLINE | PRINTER_STATUS_OUT_OF_MEMORY | PRINTER_STATUS_OUTPUT_BIN_FULL\
| PRINTER_STATUS_PAPER_PROBLEM | PRINTER_STATUS_PAUSED | PRINTER_STATUS_PENDING_DELETION\
| PRINTER_STATUS_PRINTING | PRINTER_STATUS_PROCESSING | PRINTER_STATUS_TONER_LOW\
| PRINTER_STATUS_USER_INTERVENTION | PRINTER_STATUS_WAITING | PRINTER_STATUS_WARMING_UP)) return -1;
return 0;
}
#define LINE_ONE_PAGE 40
#define BYTE_ONE_LEN 256
#define BYTE_ONE_PAGE 5000extern "C" int PASCAL EXPORT PrintFile(CFont *pFont,char *fileName)
{
HDC hDC;
DOCINFO diDocInfo;
BOOL bRetVal;
hDC = NULL; bRetVal = TRUE;
char PrintName[100];
DWORD len,type = REG_SZ;
SetLastError(0);
hDC = GetPrinterDC(PrintName, NULL);
if (hDC == NULL) return -1; diDocInfo.cbSize = sizeof(DOCINFO);
diDocInfo.lpszDocName = "haha";
diDocInfo.lpszOutput = NULL;
diDocInfo.lpszDatatype = NULL;
diDocInfo.fwType = 0; // Start the document
CDC PrintDc;
PrintDc.Attach(hDC);
PrintDc.m_bPrinting = TRUE;
CFont *pold = PrintDc.SelectObject(pFont); CRect rc(100, 0, PrintDc.GetDeviceCaps(HORZRES), PrintDc.GetDeviceCaps(VERTRES));
FILE *fp;
fp = fopen(_T(fileName),"r");
//Get Lines
char temp[BYTE_ONE_LEN];
int Line = 0;
while(!feof(fp)){
fgets(temp,BYTE_ONE_LEN,fp);
Line++;
}
//GetPage
int myrest;
myrest=Line%LINE_ONE_PAGE;
if(myrest==0)
myrest=0;
else myrest=1;
int Page = int(Line/LINE_ONE_PAGE)+myrest;
int LastPageLine = Line % LINE_ONE_PAGE; //StartDoc
if (PrintDc.StartDoc(&diDocInfo) == SP_ERROR){
TRACE("\nError");
PrintDc.Detach(); // will be cleaned up by CPrintInfo destructor
AfxMessageBox(AFX_IDP_FAILED_TO_START_PRINT); }
//StartPage
fseek(fp,0,SEEK_SET);
char buf[BYTE_ONE_PAGE]; for(int i=0;i<Page-1;i++){
buf[0] = '\0';
if(i != 0) strcat(buf,head);
//ReadBuf
for(int j=0;j<LINE_ONE_PAGE;j++){
fgets(temp,BYTE_ONE_LEN,fp);
strcat(buf,temp);
}
//Add Tail
PrintDc.StartPage();
PrintDc.DrawText(buf,strlen(buf),&rc,DT_VCENTER);
PrintDc.EndPage();
} //Print Last Page
buf[0] = '\0';
if(Page != 1) strcat(buf,head);
for(int j=0;j<LastPageLine;j++){
fgets(temp,100,fp);
strcat(buf,temp);
}
PrintDc.StartPage();
PrintDc.DrawText(buf,strlen(buf),&rc,DT_VCENTER);
PrintDc.EndPage(); fclose(fp);
PrintDc.EndDoc(); PrintDc.Detach();
return 0;}
如果你知道怎么得到打印机状态,请发mail给我[email protected]
用我程序里那些方法,总是不对