⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 graph.cxx

📁 windows mobile phone source code
💻 CXX
📖 第 1 页 / 共 2 页
字号:

HFONT PRealFont::GetHFONT() const
{
  HFONT hFont = ::CreateFont(PointsToPixelsY(size), 0, 0, 0,
                             (styles&Bold) != 0 ? FW_BOLD : FW_NORMAL,
                             (BYTE)((styles&Italic) != 0),
                             (BYTE)((styles&Underline) != 0),
                             0, 0, 0, 0, 0, 0, facename);
  return PAssertNULL(hFont);
}


PDIMENSION PRealFont::GetHeight(BOOL inPixels) const
{
  return inPixels ? PointsToPixelsY(height) : height;
}


PDIMENSION PRealFont::GetAvgWidth(BOOL inPixels) const
{
  return inPixels ? PointsToPixelsX(avgWidth) : avgWidth;
}


PDIMENSION PRealFont::GetMaxWidth(BOOL inPixels) const
{
  return inPixels ? PointsToPixelsX(maxWidth) : maxWidth;
}


PDIMENSION PRealFont::GetAscent(BOOL inPixels) const
{
  return inPixels ? PointsToPixelsY(ascent) : ascent;
}


PDIMENSION PRealFont::GetDescent(BOOL inPixels) const
{
  return inPixels ? PointsToPixelsY(descent) : descent;
}


PDIMENSION PRealFont::GetLeading(BOOL inPixels) const
{
  return inPixels ? PointsToPixelsY(leading) : leading;
}


///////////////////////////////////////////////////////////////////////////////

PPattern::PPattern()
{
  hBitmap = NULL;
  bitmap.bmWidth = bitmap.bmHeight = 1;
}


PPattern::PPattern(PRESOURCE_ID resID)
{
  Construct(LoadBitmap(PApplication::Current().GetInstance(), MAKEINTRESOURCE(resID)));
}


PPattern::PPattern(Bits bits)
{
  BYTE wordAlignedBits[16];
  memset(wordAlignedBits, 0, sizeof(wordAlignedBits));
  PINDEX i;
  for (i = 0; i < 8; i++)
    wordAlignedBits[i*2] = bits[i];
  Construct(CreateBitmap(8, 8, 1, 1, wordAlignedBits));
}


void PPattern::Construct(HBITMAP hBm)
{
  hBitmap = PAssertNULL(hBm);
  GetObject(hBitmap, sizeof(bitmap), (LPSTR)&bitmap);
}


PObject::Comparison PPattern::Compare(const PObject & obj) const
{
  PAssert(obj.IsDescendant(PPattern::Class()), PInvalidCast);
  return hBitmap == ((const PPattern &)obj).hBitmap ? EqualTo : GreaterThan;
}


void PPattern::DestroyContents()
{
  if (hBitmap != NULL)
    PAssertOS(DeleteObject((HGDIOBJ)hBitmap));
}


void PPattern::CopyContents(const PPattern & pat)
{
  hBitmap = pat.hBitmap;
  bitmap = pat.bitmap;
}


///////////////////////////////////////////////////////////////////////////////

PPixelBase::PPixelBase(PDIMENSION dx, PDIMENSION dy, BYTE depth)
  : PImageBase(dx, dy)
{
  pixelLineBytes = (PINDEX)((((long)Width()*depth+31)/32)*4);
#if defined(_WIN32)
  pixels = (PPixelDataPtr)malloc((size_t)dy*pixelLineBytes);
#else
  pixels = (PPixelDataPtr)_halloc((long)dy*pixelLineBytes, 1);
#endif
  if (pixels == NULL)
    memset(&info, 0, sizeof(info));
  else {
    info.biWidth = dx;
    info.biHeight = dy;
    info.biPlanes = 1;
    info.biBitCount = depth;
    info.biSizeImage = info.biHeight*pixelLineBytes;
    info.biCompression = BI_RGB;
    info.biXPelsPerMeter = 0;
    info.biYPelsPerMeter = 0;
    info.biClrImportant = 0;
    info.biClrUsed = info.biBitCount < 24 ? (1 << info.biBitCount) : 0;
  }
  info.biSize = sizeof(info);

  hBitmap = NULL;
}


PPixelBase::~PPixelBase()
{
  if (hBitmap != NULL)
    PAssertOS(DeleteObject((HGDIOBJ)hBitmap));
#if defined(_WIN32)
    free(pixels);
#else
    _hfree(pixels);
#endif
}


PObject::Comparison PPixelBase::Compare(const PObject & obj) const
{
  PAssert(obj.IsDescendant(PPixelBase::Class()), PInvalidCast);
  return pixels == ((const PPixelBase &)obj).pixels ? EqualTo : GreaterThan;
}


PPixelBase::DIBInfo::DIBInfo(const PPixelBase & pix, HPALETTE hPal)
{
  PINDEX numColours = pix.palette.GetSize();
  info = (BITMAPINFO *)malloc(
                       sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD)*numColours);
  PAssertNULL(info);
  info->bmiHeader = pix.info;
  if (info->bmiHeader.biBitCount >= 24)
    colourUse = DIB_RGB_COLORS;
  else if (pix.palette.GetHPALETTE() != hPal) {
    colourUse = DIB_RGB_COLORS;
    for (PINDEX i = 0; i < numColours; i++) {
      PColour c = pix.palette.GetColour(i);
      info->bmiColors[i].rgbRed   = c.GetRed();
      info->bmiColors[i].rgbGreen = c.GetGreen();
      info->bmiColors[i].rgbBlue  = c.GetBlue();
      info->bmiColors[i].rgbReserved = 0;
    }
  }
  else {
    colourUse = DIB_PAL_COLORS;
    WORD * wp = (WORD *)info->bmiColors;
    for (PINDEX i = 0; i < numColours; i++)
      *wp++ = (WORD)i;
  }
}


HBITMAP PPixelBase::GetHBITMAP(HDC hDC) const
{
  DIBInfo dib(*this, NULL);
  HDC tempDC = NULL;
  if (hDC == NULL)
    hDC = tempDC = CreateCompatibleDC(NULL);
  HBITMAP hBm = CreateDIBitmap(hDC, dib, CBM_INIT, pixels, dib, dib.Usage());
  if (tempDC)
    DeleteDC(tempDC);
  return hBm;
}


HDC PPixelBase::CreateImageDC()
{
  HDC hDC = CreateCompatibleDC(NULL);
  hBitmap = GetHBITMAP(hDC);
  PAssertOS(SelectObject(PAssertNULL(hDC), hBitmap) != NULL);
  return hDC;
}


void PPixelBase::CloseImageDC(HDC hDC)
{
  PAssertOS(SelectObject(hDC, NULL) != NULL);
  if (pixels != NULL)
    GetBitmapBits(hBitmap, info.biSizeImage, pixels);
  hBitmap = NULL;
}


PPixelDataPtr PPixelBase::GetRasterDataPtr(PORDINATE y) const
{
  PAssert(y >= 0 && (PDIMENSION)y < Height(), "Pixel out of bounds");
  PAssert(pixels != NULL, "No pixel data");
  return pixels + (long)(Height()-y-1)*pixelLineBytes;
}


PPixelImage::PPixelImage(PRESOURCE_ID resID)
{
  operator=(PPixelBase::CreateBitmap(LoadBitmap(
              PApplication::Current().GetInstance(), MAKEINTRESOURCE(resID))));
}


PPixelImage PPixelBase::CreateBitmap(HBITMAP hBm)
{
  BITMAP bm;
  PAssertOS(GetObject(PAssertNULL(hBm), sizeof(bm), (LPSTR)&bm) > 0);
  PAssert(bm.bmPlanes == 1, PUnsupportedFeature);
  PPixelImage pix((PDIMENSION)bm.bmWidth,
                               (PDIMENSION)bm.bmHeight, (BYTE)bm.bmBitsPixel);
  if (pix->pixels != NULL)
    GetBitmapBits(hBm, pix->info.biSizeImage, pix->pixels);
  return pix;
}


BOOL PPixelBase::Write(PFile & bmpFile)
{
  if (!bmpFile.IsOpen())
    return FALSE;

  BITMAPFILEHEADER bmfh;
  bmfh.bfType = 0x4d42 /*'BM'*/;
  bmfh.bfOffBits = sizeof(BITMAPFILEHEADER) +
                   sizeof(BITMAPINFOHEADER) + info.biClrUsed*sizeof(RGBQUAD);
  bmfh.bfSize = bmfh.bfOffBits + info.biSizeImage;
  bmfh.bfReserved1 = bmfh.bfReserved2 = 0;
  if (!bmpFile.Write(&bmfh, sizeof(bmfh)))
    return FALSE;

  if (!bmpFile.Write(&info, sizeof(info)))
    return FALSE;

  for (PINDEX i = 0; i < (PINDEX)info.biClrUsed; i++) {
    PColour colour = palette.GetColour(i);
    RGBQUAD rgb;
    rgb.rgbRed = colour.GetRed();
    rgb.rgbGreen = colour.GetGreen();
    rgb.rgbBlue = colour.GetBlue();
    rgb.rgbReserved = 0;
    if (!bmpFile.Write(&rgb, sizeof(rgb)))
      return FALSE;
  }

  if (pixels == NULL)
    return FALSE;

#if defined(_WIN32)
  return bmpFile.Write(pixels, (PINDEX)info.biSizeImage);
#else
  return _hwrite(bmpFile.GetHandle(),
                            pixels, info.biSizeImage) == (long)info.biSizeImage;
#endif
}


///////////////////////////////////////////////////////////////////////////////


PPictBase::PPictBase()
  : PImageBase(0, 0)
{
  hMetafile = NULL;
}


PPictBase::PPictBase(PFile & dwg)
  : PImageBase(0, 0)
{
  dwg.Close();
#if defined(_WIN32)
  hMetafile = GetEnhMetaFile(dwg.GetFilePath());
#else
  hMetafile = GetMetaFile(dwg.GetFilePath());
#endif
}


PPictBase::PPictBase(PRESOURCE_ID)
  : PImageBase(0, 0)
{
  hMetafile = NULL;
}


PPictBase::PPictBase(PMETAFILE hM)
  : PImageBase(0, 0)
{
  hMetafile = PAssertNULL(hM);
}


PPictBase::~PPictBase()
{
#if defined(_WIN32)
  PAssertOS(DeleteEnhMetaFile(hMetafile));
#else
  PAssertOS(DeleteMetaFile(hMetafile));
#endif
}


PObject::Comparison PPictBase::Compare(const PObject & obj) const
{
  PAssert(obj.IsDescendant(PPictBase::Class()), PInvalidCast);
  return hMetafile==((const PPictBase &)obj).hMetafile ? EqualTo : GreaterThan;
}


HDC PPictBase::CreateImageDC()
{
#if defined(_WIN32)
  HDC hDC = CreateEnhMetaFile(NULL, NULL, NULL, "PWLib");
#else
  HDC hDC = CreateMetaFile(NULL);
#endif
  return PAssertNULL(hDC);
}


void PPictBase::CloseImageDC(HDC hDC)
{
#if defined(_WIN32)
  hMetafile = CloseEnhMetaFile(hDC);
#else
  hMetafile = CloseMetaFile(hDC);
#endif
}


BOOL PPictBase::Write(PFile & dwg)
{
  dwg.Close();
#if defined(_WIN32)
  HENHMETAFILE hCopy = CopyEnhMetaFile(hMetafile, dwg.GetFilePath());
  return DeleteEnhMetaFile(PAssertNULL(hCopy));
#else
  HMETAFILE hCopy = CopyMetaFile(hMetafile, dwg.GetFilePath());
  return DeleteMetaFile(PAssertNULL(hCopy));
#endif
}



///////////////////////////////////////////////////////////////////////////////

PPrintInfo::PPrintInfo()
{
  form = A4;
  startPage = 1;
  endPage = UINT_MAX;
  selectionOnly = NoSelectionOnly;
  copies = 1;
  orientation = TRUE;
  draftQuality = TRUE;

  PRINTDLG printDlg;
  memset(&printDlg, 0, sizeof(printDlg));
  printDlg.lStructSize = sizeof(printDlg);
  printDlg.Flags = PD_RETURNDEFAULT;

  PAssertOS(PrintDlg(&printDlg));

  SetFromPrintDlg(printDlg);

  if (printDlg.hDevMode != NULL)
    GlobalFree(printDlg.hDevMode);
  if (printDlg.hDevNames != NULL)
    GlobalFree(printDlg.hDevNames);
}


PPrintInfo::PPrintInfo(const PString & printerType,const PString & devicePort)
  : driver("WINSPOOL"),
    device(devicePort),
    printer(printerType)
{
  form = A4;
  startPage = 0;
  endPage = (unsigned)-1;
  copies = 1;
  orientation = TRUE;
  draftQuality = TRUE;

#if defined(_WIN32)
  HANDLE          hPrinter;
  if (OpenPrinter((char *)(const char *)printerType, &hPrinter, NULL)) {
    devModeBuffer.SetSize(DocumentProperties(NULL, hPrinter, NULL, NULL, NULL, 0));
    if (DocumentProperties(NULL, hPrinter, NULL, GetDEVMODE(), NULL, DM_OUT_BUFFER) == IDOK)
      ProcessDEVMODE();
    PAssertOS(ClosePrinter(hPrinter));
  }
#else
  char devLib[_MAX_PATH];
  GetProfileString("devices", printerType, "", devLib, sizeof(devLib));
  char * comma = strchr(devLib, ',');
  if (comma != NULL)
    *comma = '\0';
  strcat(devLib, ".DRV");
  HMODULE hDriver = LoadLibrary(devLib);
  if (hDriver >= HINSTANCE_ERROR) {
    LPFNDEVMODE extDevMode = (LPFNDEVMODE)GetProcAddress(hDriver, PROC_EXTDEVICEMODE);
    if (extDevMode != NULL) {
      char devType[CCHDEVICENAME];
      strcpy(devType, printerType);
      char devPort[_MAX_PATH];
      strcpy(devPort, devicePort);
      LONG bufSize = extDevMode(NULL, hDriver, NULL, pDevMode, devPort, NULL, NULL, 0);
      devMode = (LPDEVMODE)new char[bufSize];
      if (extDevMode(NULL, hDriver, devMode, devType, devPort,
                     NULL, NULL, DM_OUT_BUFFER) == IDOK) {
        ProcessDEVMODE();
        driver = devicePort;
        printer = printerType;
      }
    }
    FreeLibrary(hDriver);
  }
#endif
}


void PPrintInfo::SetFromPrintDlg(const PRINTDLG & printDlg)
{
  if (printDlg.hDevMode != NULL) {
    devModeBuffer = PBYTEArray((BYTE *)GlobalLock(printDlg.hDevMode),
                                       GlobalSize(printDlg.hDevMode));
    GlobalUnlock(printDlg.hDevMode);
    ProcessDEVMODE();
  }

  if (printDlg.hDevNames != NULL) {
    LPSTR devNameBase = (LPSTR)GlobalLock(printDlg.hDevNames);
    LPDEVNAMES devNames = (LPDEVNAMES)devNameBase;
    driver = &devNameBase[devNames->wDriverOffset];
    printer = &devNameBase[devNames->wDeviceOffset];
    device = &devNameBase[devNames->wOutputOffset];
    GlobalUnlock(printDlg.hDevNames);
  }

  if ((printDlg.Flags&PD_NOPAGENUMS) != 0 || (printDlg.Flags&PD_PAGENUMS) == 0) {
    startPage = 0;
    endPage = UINT_MAX;
  }
  else {
    startPage = printDlg.nFromPage;
    endPage = printDlg.nToPage;
  }

  if ((printDlg.Flags&PD_NOSELECTION) != 0)
    selectionOnly = NoSelectionOnly;
  else if ((printDlg.Flags&PD_SELECTION) != 0)
    selectionOnly = SelectionOnlyOn;
  else
    selectionOnly = SelectionOnlyOff;

  copies = printDlg.nCopies;
}


void PPrintInfo::ProcessDEVMODE()
{
  LPDEVMODE devMode = GetDEVMODE();

  orientation = devMode->dmOrientation == DMORIENT_PORTRAIT;

  const static struct {
    short form;
    PDIMENSION width;
    PDIMENSION height;
  } PaperSizes[PPrintInfo::MaxForm] = {
    { 0,              0, 0 },
    { DMPAPER_A3,     2970, 4200 },
    { DMPAPER_A4,     2100, 2970 },
    { DMPAPER_A5,     1485, 2100 },
    { DMPAPER_B4,     2500, 3540 },
    { DMPAPER_B5,     1820, 2570 },
    { DMPAPER_LETTER, 2163, 2800 },
    { DMPAPER_LEGAL,  2163, 3563 }
  };

  int f;
  for (f = A3; f < MaxForm; f++) {
    if (PaperSizes[f].form == devMode->dmPaperSize)
      break;
  }

  form = (Forms)f;
  if (form == Custom)
    paperSize = PDim((PDIMENSION)devMode->dmPaperWidth,
                     (PDIMENSION)devMode->dmPaperLength);
  else if (orientation)
    paperSize = PDim(PaperSizes[f].width, PaperSizes[f].height);
  else
    paperSize = PDim(PaperSizes[f].height, PaperSizes[f].width);

  draftQuality = devMode->dmPrintQuality == DMRES_DRAFT;
  if (devMode->dmPrintQuality > 0)
    resolution = PDim((PDIMENSION)devMode->dmPrintQuality,
                      (PDIMENSION)devMode->dmYResolution);
  else
    resolution = PDim();

  copies = devMode->dmCopies;
}


LPDEVMODE PPrintInfo::GetDEVMODE() const
{
  PAssertOS(devModeBuffer.GetSize() > 0);
  return (LPDEVMODE)(const BYTE *)devModeBuffer;
}


HANDLE PPrintInfo::GetHDEVMODE() const
{
  HANDLE h = GlobalAlloc(GHND, devModeBuffer.GetSize());
  memcpy(GlobalLock(h), devModeBuffer, devModeBuffer.GetSize());
  GlobalUnlock(h);
  return h;
}


HANDLE PPrintInfo::GetDevNames() const
{
  PINDEX driverLen = driver.GetLength();
  PINDEX printerLen = printer.GetLength();
  PINDEX deviceLen = device.GetLength();

  HANDLE h = GlobalAlloc(GHND, sizeof(DEVNAMES)+driverLen+printerLen+deviceLen+4);

  LPDEVNAMES devNames = (LPDEVNAMES)GlobalLock(h);
  devNames->wDriverOffset = sizeof(DEVNAMES);
  devNames->wDeviceOffset = (WORD)(sizeof(DEVNAMES)+driverLen+1);
  devNames->wOutputOffset = (WORD)(sizeof(DEVNAMES)+driverLen+printerLen+2);

  LPSTR p = (LPSTR)devNames+sizeof(DEVNAMES);
  strcpy(p, driver);

  p += driverLen+1;
  strcpy(p, printer);

  p += printerLen+1;
  strcpy(p, device);

  GlobalUnlock(h);
  return h;
}


// End Of File ///////////////////////////////////////////////////////////////

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -