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

📄 ddi.cpp

📁 source code of printer under WinCE5.0!
💻 CPP
📖 第 1 页 / 共 2 页
字号:

    DrvCopyBits


Description:

    This method is called when an output band has been composed, and
    should be sent to the printer.  The data to be printed is
    found in psoSrc.

-------------------------------------------------------------------*/
BOOL APIENTRY 
DrvCopyBits(
    SURFOBJ  *psoDest,
    SURFOBJ  *psoSrc,
    CLIPOBJ  *pco,
    XLATEOBJ *pxlo,
    RECTL    *prclDest,
    POINTL   *pptlSrc
    )
{
//	TRACE(TRUE, (TEXT("A6PRN: DrvCopyBits\r\n")));

    BOOL f = TRUE; 
/*
	TCHAR szMsg[MAX_PATH];
	wsprintf(szMsg, TEXT("(%d,%d)-(%d,%d)\niType=%X, %X, %X\nsize=(%d,%d)\npvBits=%X, %X, len=%d, %d"), 
		prclDest->left, prclDest->top, prclDest->right, prclDest->bottom,
		psoSrc->iType, psoSrc->fjBitmap, psoSrc->iBitmapFormat,
		psoSrc->sizlBitmap.cx, psoSrc->sizlBitmap.cy,
		psoSrc->pvBits, psoSrc->pvScan0, psoSrc->cjBits, psoSrc->lDelta);
	TRACE(TRUE, (szMsg));

	if (psoSrc->iBitmapFormat != BMF_24BPP)
		return FALSE;

//	TCHAR szOutFile[MAX_PATH];
//	wsprintf(szOutFile, TEXT("\\temp\\A6Out%d_%02d.BMP"), iPage, iBand++);
//	WriteDIB(szOutFile, psoSrc, prclDest);
*/

    if (psoSrc->sizlBitmap.cx > psoSrc->sizlBitmap.cy) {
        for (int i = prclDest->left; i < prclDest->right; i++) {
            for (int j = 0; j < psoSrc->sizlBitmap.cy; j++) {
                printer.pColorRow[j * 3 + 0] = *((BYTE *)psoSrc->pvBits - psoSrc->lDelta * j + (i - prclDest->left) * 3 + 0);
                printer.pColorRow[j * 3 + 1] = *((BYTE *)psoSrc->pvBits - psoSrc->lDelta * j + (i - prclDest->left) * 3 + 1);
                printer.pColorRow[j * 3 + 2] = *((BYTE *)psoSrc->pvBits - psoSrc->lDelta * j + (i - prclDest->left) * 3 + 2);
            }
            if (!(f = printer.RasterRow(printer.pColorRow)))
                break;
        }
    }
    else {
        for (int i = prclDest->top; i < prclDest->bottom; i++) {
            if (!(f = printer.RasterRow((BYTE *)psoSrc->pvScan0 + psoSrc->lDelta * i)))
                break;
        }
    }

    return f;
}


/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Function:

    DrvSetPalette


Description:

    Called when a change is requested of the device's palette.

-------------------------------------------------------------------*/
BOOL APIENTRY 
DrvSetPalette(
    DHPDEV  dhpdev,
    PALOBJ *ppalo,
    FLONG   fl,
    ULONG   iStart,
    ULONG   cColors
    )
{
    //
    // Once MGDI recognizes that RC_PALETTE is not set for this driver,
    // this routine will never be called, and we should delete it.
    //
    TRACE(TRUE, (TEXT("A6PRN: DrvSetPalette (NOT IMPLEMENTED)\r\n")));

    return FALSE;
}


/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Function:

    DrvGetModes


Description:

    Returns information about the supported printer modes.

-------------------------------------------------------------------*/
ULONG APIENTRY 
DrvGetModes(
    HANDLE    hDriver,
    ULONG     cjSize,
    DEVMODEW *pdm
    )
{
    TRACE(TRUE, (TEXT("A6PRN: DrvGetModes\r\n")));

    //
    // Retrieve a "default" devmode.
    //

    ULONG cbNeeded = sizeof(DEVMODEW);

    //
    // If pdm is NULL, GDI is asking how much memory is 
    // required for the entire mode list
    //
    if (pdm == NULL) {

        return cbNeeded;
    }

    //
    // GDI must pass in a cjSize that exactly matches the size 
    // of the mode list.  Fail if anything else.
    //
    if (cjSize != cbNeeded) {

        return 0;
    }

    WCHAR wszName[] = TEXT("SiPix PocketPrinter A6");

    //
    // Return settings for a color printer set to 300dpi,
    // with a printable area of 8.5" x 11".
    //
    memset(pdm, 0, cbNeeded);
    memcpy(pdm->dmDeviceName, wszName, sizeof(wszName));

    pdm->dmSize         = sizeof(DEVMODEW);
    pdm->dmSpecVersion  = SPEC_VERSION;
    pdm->dmOrientation  = DMORIENT_PORTRAIT;
    pdm->dmPaperSize    = DMPAPER_A4;
    pdm->dmCopies       = 1;
    pdm->dmPrintQuality = DMRES_HIGH;
    pdm->dmColor        = DMCOLOR_MONOCHROME;
    pdm->dmBitsPerPel   = 24;

    return cbNeeded;
}


/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Function:

    DrvEndDoc


Description:

    Called to complete the print job.  If fl == ED_ABORTDOC, then
    the print job should be aborted.

-------------------------------------------------------------------*/
BOOL APIENTRY 
DrvEndDoc(
    SURFOBJ *pso,   
    FLONG    fl 
    )
{
    TRACE(TRUE, (TEXT("A6PRN: DrvEndDoc\r\n")));

    printer.NextPage();

    return TRUE;
}


/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Function:

    DrvStartDoc


Description:

    Called to start a print job.

-------------------------------------------------------------------*/
BOOL APIENTRY 
DrvStartDoc(
    SURFOBJ *pso,   
    PWSTR    pwszDocName,   
    DWORD    dwJobId    
    )
{
    TRACE(TRUE, (TEXT("A6PRN: DrvStartDoc\r\n")));
    
    iPage = 0;

    return TRUE;
}


/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Function:

    DrvStartPage


Description:

    Called to begin printing the next page of the job.  If there is one,
    also ejects the previous page.

-------------------------------------------------------------------*/
BOOL APIENTRY 
DrvStartPage(
    SURFOBJ *pso    
    )
{
    TRACE(TRUE, (TEXT("A6PRN: DrvStartPage\r\n")));

    if (iPage++) {
        printer.NextPage();
    }
	iBand = 0;

    printer.RasterStart();

    return TRUE;
}

/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Function:

    DrvPowerHandler


Description:

     The function PCL should execute when the machine is suspended
     or resumed. 
-------------------------------------------------------------------*/
VOID DrvPowerHandler
    (
    	DHPDEV     dhpdev,
        BOOL       bOff
    )
{
   return;                          //Do nothing
}

/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Function:

    DrvEnableDriver


Description:

    Main entry point into the driver.  This is where we register
    all of our DDI entry points with MGDI.

-------------------------------------------------------------------*/
BOOL APIENTRY 
DrvEnableDriver(
    ULONG          iEngineVersion,
    ULONG          cj,
    DRVENABLEDATA *pdrvenabledata,
    PENGCALLBACKS  pEngCallbacks)
{
    TRACE(TRUE, (TEXT("A6PRN: DrvEnableDriver\r\n")));

    //
    // REVIEW: Now that the printer driver is NOT subclassing
    // the display driver, we should zero-init the incoming
    // DRVENABLEDATA block.
    //
    // The DRVENABLEDATA that gets passed in is already initialized with
    // the entry points for the display driver.  Save all of these pfns
    // in g_drvenabledataDisplay, and then change the only ones we want 
    // MGDI to call directly.  All other calls from MGDI will go directly
    // to the display driver.
    //
    g_drvenabledataDisplay = *pdrvenabledata;

    pdrvenabledata->DrvEnablePDEV     = DrvEnablePDEV;
    pdrvenabledata->DrvDisablePDEV    = DrvDisablePDEV;
    pdrvenabledata->DrvEnableSurface  = DrvEnableSurface;
    pdrvenabledata->DrvDisableSurface = DrvDisableSurface;
    pdrvenabledata->DrvCopyBits       = DrvCopyBits;
    pdrvenabledata->DrvSetPalette     = DrvSetPalette;
    pdrvenabledata->DrvGetModes       = DrvGetModes;
    pdrvenabledata->DrvEndDoc         = DrvEndDoc;
    pdrvenabledata->DrvStartDoc       = DrvStartDoc;
    pdrvenabledata->DrvStartPage      = DrvStartPage;
    pdrvenabledata->DrvPowerHandler   = DrvPowerHandler;

    //
    // Need to remember this MGDI callback for later
    //
    pfnEngCreateDeviceSurface = pEngCallbacks->EngCreateDeviceSurface;
    pfnEngCreatePalette       = pEngCallbacks->EngCreatePalette;
    pfnEngDeleteSurface       = pEngCallbacks->EngDeleteSurface;

    //
    // Assume that the display driver is already initialized.  No need
    // to initialize it again.
    //

    return TRUE;
}

/****************************************************************************
 *                                                                          *
 *  FUNCTION   :  PaletteSize(VOID FAR * pv)				                *
 *                                                                          *
 *  PURPOSE    :  Calculates the palette size in bytes. If the info. block  *
 *                is of the BITMAPCOREHEADER type, the number of colors is  *
 *                multiplied by 3 to give the palette size, otherwise the   *
 *                number of colors is multiplied by 4.                      *
 *                                                                          *
 *  RETURNS    :  Palette size in number of bytes.                          *
 *                                                                          *
 ****************************************************************************/

WORD PaletteSize(VOID FAR * pv)
{
	LPBITMAPINFOHEADER lpbi;
	WORD	       NumColors;

	lpbi = (LPBITMAPINFOHEADER) pv;
	NumColors = DibNumColors(lpbi);

	if (lpbi->biSize == sizeof(BITMAPCOREHEADER))
		return NumColors * sizeof(RGBTRIPLE);
	else 
		return NumColors * sizeof(RGBQUAD);
}

/****************************************************************************
 *	                                                                        *
 *  FUNCTION   : WriteDIB(LPSTR szFile, LPBITMAP lpbi)                      *
 *                                                                          *
 *  PURPOSE    : Write a BITMAP object to a file.                           *
 *                                                                          *
 *  RETURNS    : TRUE  - if successful.                                     *
 *		         FALSE - otherwise                                          *
 *                                                                          *
 ****************************************************************************/

BOOL WriteDIB(LPCTSTR szFile, SURFOBJ *psoSrc, RECTL *prclDest)
{
	BITMAPFILEHEADER hdr;
	BITMAPINFOHEADER bi;
	HANDLE fh=INVALID_HANDLE_VALUE;
	DWORD dwWritten;

	if (!psoSrc || !prclDest)
		return FALSE;

	fh = CreateFile(szFile, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
	if (fh == INVALID_HANDLE_VALUE)
		return FALSE;

	int nWidth = prclDest->right - prclDest->left;
	int nHeight = prclDest->bottom - prclDest->top;
	int nWidthBytes = abs(psoSrc->lDelta);
	bi.biSize = sizeof(BITMAPINFOHEADER); 
	bi.biWidth = nWidth; 
	bi.biHeight = nHeight; 
	bi.biPlanes = 1; 
	bi.biBitCount = 24;
	bi.biCompression = BI_RGB; 
	bi.biSizeImage = nWidthBytes * nHeight; 
	bi.biXPelsPerMeter = 0; 
	bi.biYPelsPerMeter = 0; 
	bi.biClrUsed = 0; 
	bi.biClrImportant = 0; 

    /* Fill in the fields of the file header */
	hdr.bfType = BFT_BITMAP;
	hdr.bfSize = DibSize(&bi) + sizeof(BITMAPFILEHEADER);
	hdr.bfReserved1 = 0;
	hdr.bfReserved2 = 0;
	hdr.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) + bi.biSize + 
		 PaletteSize(&bi);

    /* Write the file header */
	WriteFile(fh, (LPSTR) &hdr, sizeof(BITMAPFILEHEADER), &dwWritten, NULL);

    /* Write the DIB header */
	WriteFile(fh, (LPSTR) &bi, DibSize(&bi)-bi.biSizeImage, &dwWritten, NULL);
	
	/* Write the bits */
	for (int y=0; y<nHeight; y++) {
		WriteFile(fh, (BYTE *)psoSrc->pvBits + nWidthBytes * y, nWidthBytes, &dwWritten, NULL);
    }

	CloseHandle(fh);
	return TRUE;
}

⌨️ 快捷键说明

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