我不确定提出这个问题的最佳位置,所以如果这是错误的论坛请告知。
我在用于保存打印机设置的MFC
派生项目中有以下两种方法:
void CCommunityTalksApp::RestorePrinterSettings()
{
DWORD dwSize, dwVersion;
LPVOID lpData;
DEVMODE *pDevMode;
HANDLE hPrinter;
CFile fileIn;
CString strError, strTag;
CWaitCursor wait;
// open file
if(!fileIn.Open(GetPRNPath(), CFile::modeRead | CFile::typeBinary))
{
// unable to open file
return;
}
CArchive ar(&fileIn, CArchive::load);
ar >> strTag;
if (strTag != _T("PTS-Printer Settings"))
{
// old-style printer file
return;
}
ar >> dwVersion;
// free existing memory and assign to NULL
if(m_hDevMode != NULL)
{
::GlobalFree(m_hDevMode);
m_hDevMode = NULL;
}
if(m_hDevNames != NULL)
{
::GlobalFree(m_hDevNames);
m_hDevNames = NULL;
}
// read in size of m_hDevMode structure and the structure data
ar >> dwSize;
// allocate memory for the structure
m_hDevMode = ::GlobalAlloc(GHND, dwSize);
if(m_hDevMode != NULL)
{
lpData = ::GlobalLock(m_hDevMode);
ar.Read(lpData, dwSize);
::GlobalUnlock(m_hDevMode);
}
// read in size of m_hDevNames structure and the structure data
ar >> dwSize;
// allocate memory for the structure
m_hDevNames = ::GlobalAlloc(GHND, dwSize);
if(m_hDevNames != NULL)
{
lpData = ::GlobalLock(m_hDevNames);
ar.Read(lpData, dwSize);
::GlobalUnlock(m_hDevNames);
}
ar.Close();
fileIn.Close();
// we must now validate the printer settings
if(m_hDevMode != NULL)
{
pDevMode = (DEVMODE*)::GlobalLock(m_hDevMode);
if(!OpenPrinter((LPTSTR)(LPCTSTR)pDevMode->dmDeviceName, &hPrinter, NULL))
{
strError.Format(ID_STR_INVALIDPRINTER_TPL, pDevMode->dmDeviceName);
if(AfxMessageBox(strError, MB_YESNO|MB_ICONQUESTION) == IDYES)
{
// delete settings file
DeleteFile(theApp.GetPRNPath());
}
::GlobalUnlock(m_hDevMode);
// no printer found, so release memory and set to NULL
if(m_hDevMode != NULL)
{
::GlobalFree(m_hDevMode);
m_hDevMode = NULL;
}
if(m_hDevNames != NULL)
{
::GlobalFree(m_hDevNames);
m_hDevNames = NULL;
}
}
else
{
// printer settings must be OK
ClosePrinter(hPrinter);
::GlobalUnlock(m_hDevMode);
}
}
}
void CCommunityTalksApp::SavePrinterSettings()
{
CFile fileOut;
SIZE_T dwSize;
LPVOID lpData;
if(m_hDevMode == NULL && m_hDevNames == NULL)
{
// nothing to save
return;
}
// create file
if(!fileOut.Open(GetPRNPath(), CFile::modeCreate | CFile::modeWrite | CFile::typeBinary))
{
// unable to create printer file
return;
}
CArchive ar(&fileOut, CArchive::store);
CString strTag;
strTag = _T("PTS-Printer Settings");
ar << strTag;
ar << (DWORD)100; // 1.00 (printer settings has it's own version number)
// write out m_hDevMode size and structure
if(m_hDevMode != NULL)
{
dwSize = ::GlobalSize(m_hDevMode);
lpData = ::GlobalLock(m_hDevMode);
BOOL bResetCopies = (BOOL)GetNumberSetting(
_T("Preferences"),_T("ResetPrinterCopies"),FALSE);
if (bResetCopies)
{
// Convert to DEVMODE structure and alter
PDEVMODE pDevMode = (PDEVMODE)lpData;
pDevMode->dmCopies = 1;
}
// write out size of structure
ar << dwSize;
// write out structure
ar.Write(lpData, (UINT)dwSize);
::GlobalUnlock(m_hDevMode);
}
// write out m_hDevNames size and structure
if(m_hDevNames != NULL)
{
dwSize = ::GlobalSize(m_hDevNames);
lpData = ::GlobalLock(m_hDevNames);
// write out size of structure
ar << dwSize;
// write out structure
ar.Write(lpData, (UINT)dwSize);
::GlobalUnlock(m_hDevNames);
}
ar.Close();
fileOut.Close();
}
上面的代码工作正常。问题是当用户在另一台计算机上安装程序时,该程序具有不同的打印机。
用户使用我程序中的功能备份数据文件,在另一台计算机上恢复。因此,打印机设置被覆盖,当您启动程序时,它会发出警告,重置打印机设置。
是否可以在数据文件中支持多个默认打印机设置?然后有能力启动,使用数据文件中的有效打印机?
如果我的问题不在话题,我会根据您的要求将其删除。