标签:style http ar io os 使用 sp for strong
http://cache.baiducontent.com/c?m=9f65cb4a8c8507ed4fece763105790245b09c0252bd7a74a2485d315d2390f07506694ea7a7d0d5d83d87f6305ac4957f7b86c65377571eb8f8dd50a8bb485582a9f5631671df65663d50edcba5154cb37e12efeae69f0caf625e7aec5a5de4320c944040a9780fb4d7467&p=c4759a46d7c002fb09be9b7c5b5390&newp=b471c54ad5c242fe19f3cb2d02148c231610db2151ddd4122e958d17&user=baidu&fm=sc&query=%B7%C7%C4%AC%C8%CF%B4%F2%D3%A1%BB%FA&qid=f3d87aaa0004b57b&p1=2
文章编号: 166129 - 查看本文应用于的产品
#include <winspool.h> // returns a DEVMODE and DEVNAMES for the printer name specified BOOL GetPrinterDevice(LPTSTR pszPrinterName, HGLOBAL* phDevNames, HGLOBAL* phDevMode) { // if NULL is passed, then assume we are setting app object‘s // devmode and devnames if (phDevMode == NULL || phDevNames == NULL) return FALSE; // Open printer HANDLE hPrinter; if (OpenPrinter(pszPrinterName, &hPrinter, NULL) == FALSE) return FALSE; // obtain PRINTER_INFO_2 structure and close printer DWORD dwBytesReturned, dwBytesNeeded; GetPrinter(hPrinter, 2, NULL, 0, &dwBytesNeeded); PRINTER_INFO_2* p2 = (PRINTER_INFO_2*)GlobalAlloc(GPTR, dwBytesNeeded); if (GetPrinter(hPrinter, 2, (LPBYTE)p2, dwBytesNeeded, &dwBytesReturned) == 0) { GlobalFree(p2); ClosePrinter(hPrinter); return FALSE; } ClosePrinter(hPrinter); // Allocate a global handle for DEVMODE HGLOBAL hDevMode = GlobalAlloc(GHND, sizeof(*p2->pDevMode) + p2->pDevMode->dmDriverExtra); ASSERT(hDevMode); DEVMODE* pDevMode = (DEVMODE*)GlobalLock(hDevMode); ASSERT(pDevMode); // copy DEVMODE data from PRINTER_INFO_2::pDevMode memcpy(pDevMode, p2->pDevMode, sizeof(*p2->pDevMode) + p2->pDevMode->dmDriverExtra); GlobalUnlock(hDevMode); // Compute size of DEVNAMES structure from PRINTER_INFO_2‘s data DWORD drvNameLen = lstrlen(p2->pDriverName)+1; // driver name DWORD ptrNameLen = lstrlen(p2->pPrinterName)+1; // printer name DWORD porNameLen = lstrlen(p2->pPortName)+1; // port name // Allocate a global handle big enough to hold DEVNAMES. HGLOBAL hDevNames = GlobalAlloc(GHND, sizeof(DEVNAMES) + (drvNameLen + ptrNameLen + porNameLen)*sizeof(TCHAR)); ASSERT(hDevNames); DEVNAMES* pDevNames = (DEVNAMES*)GlobalLock(hDevNames); ASSERT(pDevNames); // Copy the DEVNAMES information from PRINTER_INFO_2 // tcOffset = TCHAR Offset into structure int tcOffset = sizeof(DEVNAMES)/sizeof(TCHAR); ASSERT(sizeof(DEVNAMES) == tcOffset*sizeof(TCHAR)); pDevNames->wDriverOffset = tcOffset; memcpy((LPTSTR)pDevNames + tcOffset, p2->pDriverName, drvNameLen*sizeof(TCHAR)); tcOffset += drvNameLen; pDevNames->wDeviceOffset = tcOffset; memcpy((LPTSTR)pDevNames + tcOffset, p2->pPrinterName, ptrNameLen*sizeof(TCHAR)); tcOffset += ptrNameLen; pDevNames->wOutputOffset = tcOffset; memcpy((LPTSTR)pDevNames + tcOffset, p2->pPortName, porNameLen*sizeof(TCHAR)); pDevNames->wDefault = 0; GlobalUnlock(hDevNames); GlobalFree(p2); // free PRINTER_INFO_2 // set the new hDevMode and hDevNames *phDevMode = hDevMode; *phDevNames = hDevNames; return TRUE; }
// commdlg.h // the first three members are character offsets from the beginning of the // structure pointing to the specific string typedef struct tagDEVNAMES { WORD wDriverOffset; // file name of driver (without extension) WORD wDeviceOffset; // device name WORD wOutputOffset; // device name of physical output medium WORD wDefault; // DN_DEFAULTPRN if default printer chosen } DEVNAMES;
void CMainFrame::OnActionSetnondefaultprinter() { HGLOBAL hDevMode = NULL; HGLOBAL hDevNames = NULL; if (GetPrinterDevice(_T("\\\\RED-PRN-25\\PRIV0006"), &hDevNames, &hDevMode)) AfxGetApp()->SelectPrinter(hDevNames, hDevMode); else AfxMessageBox(_T("Failed to select custom printer")); }
BOOL CNonDefPrinterView::OnPreparePrinting(CPrintInfo* pInfo) { pInfo->m_bDirect = TRUE; // TRUE if bypassing Print Dialog return DoPreparePrinting(pInfo); }
BOOL CNonDefPrinterView::OnPreparePrinting(CPrintInfo* pInfo) { // set to non-default printer without changing default app printer HGLOBAL hDevMode; HGLOBAL hDevNames; if (!GetPrinterDevice(_T("\\\\RED-PRN-25\\PRIV0006"), &hDevNames, &hDevMode)) AfxMessageBox(_T("GetPrinterDevice called failed\n")); pInfo->m_pPD->m_pd.hDevMode = hDevMode; pInfo->m_pPD->m_pd.hDevNames = hDevNames; pInfo->m_pPD->CreatePrinterDC(); return TRUE; }
标签:style http ar io os 使用 sp for strong
原文地址:http://www.cnblogs.com/CodeGear/p/4166491.html