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

📄 ddi.cpp

📁 source code of printer under WinCE5.0!
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*++
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.
Copyright (c) 1995-1998  Microsoft Corporation

Module Name:

    ddi.cpp

Abstract:

    This file implements the DDI-related portions of the printer driver.

Functions:




Notes:


--*/

#include "precomp.hxx"
#include "compress.h"
#include "local.h"
#include "resource.h"
#include "dialog.h"
#include "profile.h"
#include "serial.h"

LPA6PRINTDLG g_pParam=NULL;

//#define VERBOSE
#ifdef VERBOSE
	#define TRACE(cond, args) \
        do { if (cond) { MessageBox(GetFocus(), args, TEXT("A6PRN"), MB_OK); /*DebugBreak();*/ } } while (0)
//		do { if (cond) { DEBUGMSG(TRUE, args); /*DebugBreak();*/ } } while (0)
#else
    #define TRACE(cond, args)
#endif

#define A6_xMM          105
#define A6_yMM          149
#define A6_xPels        1496
#define A6_yPels        2110

//#define B5_PRINTABLE_x  2080
//#define PRINTABLE_x     2400
#define MARGIN_x        75      // 5L
#define MARGIN_y        112     // 6 color
//#define MARGIN_y        94

#define LJ_MARGIN_x     63
#define LJ_MARGIN_y     50

#define RESOLUTION	400

#define SPEC_VERSION    0X400

DWORD   dxPagePixels; 
DWORD   dyPagePixels; 

int iPage;
int iBand;


// 
// This global variable caches all of the entry points into
// the display driver.  We will need some of these pfns when
// we subclass the display driver.
//
DRVENABLEDATA g_drvenabledataDisplay;

//
// This is the driver's handle to the physical device.
//
DHPDEV g_dhpdev;

//
// Main surface for driver.  Not actually drawn on.
//
HBITMAP g_hbitmapSurface;

//
// We need to store these MGDI callbacks to manage g_hbitmapSurface
//
HSURF (APIENTRY *pfnEngCreateDeviceSurface)(DHSURF, SIZEL, ULONG);
BOOL  (APIENTRY *pfnEngDeleteSurface)(HSURF);
HPALETTE (*pfnEngCreatePalette)(ULONG, ULONG, PULONG, FLONG, FLONG, FLONG);


// util.h
BOOL WriteDIB(LPCTSTR szFile, SURFOBJ *psoSrc, RECTL *prclDest);

#define BFT_BITMAP 0x4d42   // 'BM'

#define DibSize(lpbi)           ((lpbi)->biSize + (lpbi)->biSizeImage + (int)(lpbi)->biClrUsed * sizeof(RGBQUAD))
#define DibNumColors(lpbi)      ((lpbi)->biClrUsed == 0 && (lpbi)->biBitCount <= 8 \
                                    ? (int)(1 << (int)(lpbi)->biBitCount)          \
                                    : (int)(lpbi)->biClrUsed)

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

Function:

    DrvEnablePDEV


Description:

    Enables the P(hysical)DEV associated with this driver.  We pass
    this along to the display driver, and substitute in our devcaps.

-------------------------------------------------------------------*/
DHPDEV APIENTRY 
DrvEnablePDEV(
    DEVMODEW *pdm,
    LPWSTR    pwszLogAddress,
    ULONG     cPat,
    HSURF    *phsurfPatterns,
    ULONG     cjCaps,
    ULONG    *pdevcaps,
    ULONG     cjDevInfo,
    DEVINFO  *pdi,
    HDEV      hdev,
    LPWSTR    pwszDeviceName,
    HANDLE    hDriver
    )
{
    TRACE(TRUE, (TEXT("A6PRN: DrvEnablePDEV\r\n")));

    //
    // Associate a palette with this driver.  If we were rendering
    // in, say, 16bpp, then we'd call EngCreatePalette and set up
    // the appropriate RGB masks.  Ditto if this printer wants
    // to handle calls to RealizePalette (which would be done by 
    // setting RC_PALETTE, below).
    //
    // This sample, with the display driver
    // doing all of the rendering, we don't want to support 
    // RealizePalette.  
    //

    pdi->hpalDefault = pfnEngCreatePalette(
                                PAL_RGB,
                                0,   
                                0,
                                0,
                                0,
                                0);
    
    if (pdi->hpalDefault == NULL)
	{
	    TRACE(TRUE, (TEXT("A6PRN: pfnEngCreatePalette error\r\n")));
        return 0;
	}
/*
    HBITMAP h;

    h = CreateBitmap(1, 1, 1, 16, 0);
    GetObject(h,);
    DeleteObject(h);
*/
    //
    // Call display driver to get dhpdev.  Note that we're passing in the
    // *printer's* DEVMODE to the display driver!  This works because the
    // display driver only sets its mode the first time it's called.
    // Other display drivers may behave differently.
    //
    g_dhpdev = g_drvenabledataDisplay.DrvEnablePDEV(
                                    pdm,
                                    pwszLogAddress,
                                    cPat,
                                    phsurfPatterns,
                                    cjCaps,
                                    pdevcaps,
                                    cjDevInfo,
                                    pdi,
                                    hdev,
                                    pwszDeviceName,
                                    hDriver
                                    );


    
	SetCursor(LoadCursor(NULL,IDC_ARROW));
	ShowCursor(FALSE);

	int nResult;

	if (g_pParam)
	{
		free(g_pParam);
		g_pParam = NULL;
	}

	g_pParam = (LPA6PRINTDLG)malloc(sizeof(A6PRINTDLG));

	// default values
	g_pParam->nPaper = 0;
	g_pParam->nWidth = A6PAPER_WIDTH;
	g_pParam->nHeight = A6PAPER_HEIGHT;
	g_pParam->bLandscape = 0;
	g_pParam->nDither = 0;
	g_pParam->nForm = 0;
	g_pParam->nScroll = DEFAULT_SCROLL;
	g_pParam->rcMargin.top = MIN_MARGIN_TOP;
	g_pParam->rcMargin.bottom = MIN_MARGIN_BOTTOM;
	g_pParam->rcMargin.left = MIN_MARGIN_LEFT;
	g_pParam->rcMargin.right = MIN_MARGIN_RIGHT;
	// user preferred values
	LoadProfileSettings(g_pParam);
	// current settings
	if (wcscmp(pwszLogAddress,TEXT("IRDA")) == 0)
		g_pParam->nPort = 0;
	else if (wcsncmp(pwszLogAddress,TEXT("COM1"),4) == 0)
		g_pParam->nPort = 1;
	else if (wcsncmp(pwszLogAddress,TEXT("COM2"),4) == 0)
		g_pParam->nPort = 2;
	else
		g_pParam->nPort = 3;
	g_pParam->bLandscape = (DMORIENT_LANDSCAPE == pdm->dmOrientation);
//	g_pParam->rcMargin.top = MIN_MARGIN_TOP;
//	g_pParam->rcMargin.bottom = MIN_MARGIN_BOTTOM;
//	g_pParam->rcMargin.left = MIN_MARGIN_LEFT;
//	g_pParam->rcMargin.right = MIN_MARGIN_RIGHT;

	nResult = DialogBoxParam(g_hInstance,
					MAKEINTRESOURCE(IDD_PRINT_DIALOG),
					NULL,
					(DLGPROC)PrintDlg,
					NULL);

	if (nResult != IDOK)
	{
        DeleteObject(pdi->hpalDefault);
        DrvDisablePDEV(g_dhpdev);
		g_dhpdev = 0;
        return NULL;
	}

	SaveProfileSettings(g_pParam);

	//
    // Overwrite the display driver's capabilities with the printer's.
    // Note that many of these values would usually be derived from
    // the DEVMODEW parameter.
    //
    GDIINFO *pgdiinfo = (GDIINFO *)pdevcaps;

	g_pParam->nXpixel = 1475;
	g_pParam->nYpixel = 2100;
	g_pParam->nWidth = A6PAPER_WIDTH;
	g_pParam->nHeight = A6PAPER_HEIGHT;

    if (g_pParam->bLandscape) 
	{
		pgdiinfo->ulHorzRes = g_pParam->nYpixel; // pixels
		pgdiinfo->ulVertRes = g_pParam->nXpixel; // pixels
		pgdiinfo->szlPhysSize.cx = g_pParam->nHeight * RESOLUTION / 254; // pixels
		pgdiinfo->szlPhysSize.cy = g_pParam->nWidth * RESOLUTION / 254; // pixels
	}
	else
	{
		pgdiinfo->ulHorzRes = g_pParam->nXpixel; // pixels
		pgdiinfo->ulVertRes = g_pParam->nYpixel; // pixels
		pgdiinfo->szlPhysSize.cx = g_pParam->nWidth * RESOLUTION / 254; // pixels
		pgdiinfo->szlPhysSize.cy = g_pParam->nHeight * RESOLUTION / 254; // pixels
	}

	pgdiinfo->ulLogPixelsX = pgdiinfo->ulLogPixelsY = RESOLUTION/2; // dpi
	pgdiinfo->ulHorzSize = pgdiinfo->ulHorzRes * 254 / RESOLUTION /10; // mm
    pgdiinfo->ulVertSize = pgdiinfo->ulVertRes * 254 / RESOLUTION /10; // mm
	pgdiinfo->ptlPhysOffset.x = (pgdiinfo->szlPhysSize.cx-pgdiinfo->ulHorzRes)/2; // pixels
	pgdiinfo->ptlPhysOffset.y = (pgdiinfo->szlPhysSize.cy-pgdiinfo->ulVertRes)/2; // pixels

    dxPagePixels    = pgdiinfo->ulHorzRes; 
    dyPagePixels    = pgdiinfo->ulVertRes; 

    pgdiinfo->ulTechnology      = DT_RASPRINTER;
    pgdiinfo->cBitsPixel        = 24;
    pgdiinfo->ulNumColors       = 1 << pgdiinfo->cBitsPixel;

    pgdiinfo->flRaster         &= ~RC_PALETTE;  // Printer doesn't support palettes
	pdm->dmDriverVersion = 67; // version 0.67
	pdm->dmScale = 50;
	if (g_pParam->nPort == 0)
		wsprintf(pwszLogAddress,TEXT("IRDA"));
	else
	if (g_pParam->nPort == 1)
		wsprintf(pwszLogAddress,TEXT("COM1: 115200"));
	else
	if (g_pParam->nPort == 2)
		wsprintf(pwszLogAddress,TEXT("COM2: 115200"));
	else
		wsprintf(pwszLogAddress,TEXT("COM6: 115200"));

	if (g_pParam->bLandscape)
		pdm->dmOrientation = DMORIENT_LANDSCAPE;
	else
		pdm->dmOrientation = DMORIENT_PORTRAIT;

    if (printer.AllocateBuffers(pgdiinfo, pdm) &&
        INVALID_HANDLE_VALUE != printer.Open(pwszLogAddress))
    {
        return g_dhpdev;
    }
    else 
    {
        DeleteObject(pdi->hpalDefault);
        DrvDisablePDEV(g_dhpdev);
        g_dhpdev = 0;
        return 0;
    }
}


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

Function:

    DrvDisablePDEV


Description:

    Disables the P(hysical)DEV associated with this driver.  

-------------------------------------------------------------------*/
void APIENTRY 
DrvDisablePDEV(
    DHPDEV dhpdev
    )
{
    TRACE(TRUE, (TEXT("A6PRN: DrvDisablePDEV\r\n")));

    // 
    // Since we're piggy-backed on the display driver, do NOT forward
    // the DrvDisablePDEV call.  Otherwise, the display driver would
    // unload itself.  Ouch!
    //
    //    g_drvenabledataDisplay.DrvDisablePDEV(g_dhpdev);
    //

    printer.Close();
}


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

Function:

    DrvEnableSurface


Description:

    Allocates an HSURF for the given DHPDEV.  This is called at
    driver initialization.

-------------------------------------------------------------------*/
HSURF APIENTRY 
DrvEnableSurface(
    DHPDEV dhpdev
    )
{
    //
    // If more than one print job can be executing simultaneously, we
    // should allocate a SURFOBJ here and delete it in DrvDisableSurface.
    // For now, we just use one static SURFOBJ.
    //
    static SURFOBJ surfobj;
    SIZEL sizl;

    TRACE(TRUE, (TEXT("A6PRN: DrvEnableSurface\r\n")));

    sizl.cx = dxPagePixels;
    sizl.cy = dyPagePixels;
    
    surfobj.iBitmapFormat = BMF_24BPP;

    g_hbitmapSurface = (HBITMAP) pfnEngCreateDeviceSurface(
                            (DHSURF)&surfobj, 
                            sizl, 
                            BMF_24BPP);

    return (HSURF) g_hbitmapSurface;
}


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

Function:

    DrvDisableSurface


Description:

    Cleans up any allocations done in DrvEnableSurface.

-------------------------------------------------------------------*/
void APIENTRY 
DrvDisableSurface(
    DHPDEV dhpdev
    )
{
    TRACE(TRUE, (TEXT("A6PRN: DrvDisableSurface\r\n")));

    pfnEngDeleteSurface((HSURF)g_hbitmapSurface);
}


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

Function:

⌨️ 快捷键说明

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