如何在c + + builder中保存二进制文件?

时间:2013-06-27 02:40:22

标签: c++ c++builder .emf virtual-printer

我想在捕获打印假脱机的程序中更改此部分代码,并保留包含图像emf的打包文件。但我想在不询问路径的情况下单独保存图像,比如c:。 我从http://www.mblabsoft.com/index13.html

下载的程序
static bool __fastcall GetOutputFileName (PDEV *ppdev)
{
if (ppdev->OutputFileName.IsEmpty ())
{
OPENFILENAMEW ofn;
wchar_t OutFile[261] = {0};
WideString ATitle = WideString (EMP_PRINTERNAME) + WideString (L": save output for '") + ppdev->DocName + WideString (L"'");

memset (&ofn,0,sizeof (OPENFILENAMEW));

ofn.lStructSize = sizeof (OPENFILENAMEW);
ofn.hwndOwner = GetForegroundWindow ();
ofn.lpstrFile = OutFile;
ofn.nMaxFile = 260;
ofn.lpstrFilter = L"Enhanced Metafile Package\0*.EMP\0";
ofn.lpstrDefExt = L"emp";
ofn.nFilterIndex = 1;
ofn.lpstrTitle = ATitle.c_bstr ();
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST;

WORD fpucw = GetFPUControlWord ();
bool res = GetSaveFileNameW (&ofn);
SetFPUControlWord (fpucw);

if (res) ppdev->OutputFileName = WideString (OutFile);
else return false;
}

return true;
}

static bool __fastcall SaveSpoolData (PDEV *ppdev)
{
if (!GetOutputFileName (ppdev)) return false;

try
{
TPrintReadStream *APrnStream = new TPrintReadStream (ppdev->PrinterName,1024);
try
{
  TFileStream *APkgStream = new TFileStream (ppdev->OutputFileName,fmCreate);
  try
  {
    TZipPackage *APkg = new TZipPackage (APkgStream);
    try
    {
      TSplRecordHeader ASplRec;
      TMemoryStream *AMemStream = new TMemoryStream ();
      try
      {
        int AFileIndex = 1;

        while (APrnStream->Read (&ASplRec,sizeof (TSplRecordHeader)) == sizeof (TSplRecordHeader))
        {
          AMemStream->Position = 0;
          switch (ASplRec.Type)
          {
            case EMRI_METAFILE:
            case EMRI_FORM_METAFILE:
            case EMRI_BW_METAFILE:
            case EMRI_BW_FORM_METAFILE:
            case EMRI_METAFILE_DATA:
                 AMemStream->SetSize ((int)(ASplRec.Size));
                 if (APrnStream->Read (AMemStream->Memory,AMemStream->Size) == AMemStream->Size)
                 {
                   APkg->AddStream (AMemStream,false,AnsiString (AFileIndex) + ".emf",Now (),
                                    FILE_ATTRIBUTE_ARCHIVE,ppdev->DocName + " (page " + AnsiString (AFileIndex) + ")");
                   AFileIndex++;                
                 }
                 break;

            case EMRI_HEADER:
                 APrnStream->Skip (ASplRec.Size - sizeof (TSplRecordHeader));
                 break;

            default:
                 APrnStream->Skip (ASplRec.Size);
          }
        }
      }
      __finally
      {
        delete AMemStream;
      }
    }
    __finally
    {
      delete APkg;
    }
  }
  __finally
  {
    delete APkgStream;
  }
}
__finally
{
  delete APrnStream;
}
}
catch (...)
{
return false;
}

 return true;
}

0 个答案:

没有答案