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

📄 preprint.cpp

📁 Delphi数据库
💻 CPP
字号:
// PrePrint.cpp : implementation file
//

#include "stdafx.h"
#include "PrePrint.h"
#include "PreGraphic.H"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include <winspool.h>
/////////////////////////////////////////////////////////////////////////////
// CPrePrint dialog
//PDATAWINDOWVIEW CPrePrint::m_pDataWindow = NULL;









LPTSTR CPrePrint::GetRegistryString(HKEY hKeyClass, LPTSTR lpszSubKey, LPTSTR lpszValueName)
{
    // Local variables
    HKEY          hKey;                 // Registry key
    LPTSTR         lpszKeyValue;         // Buffer for key name
    DWORD         dwKeySize;            // Size of key value
    DWORD         dwKeyDataType;        // Type of data stored in key
    LONG          lRC;                  // Return code
	
    //  Initialize variables
    dwKeyDataType = 0;
    dwKeySize = 0;
    hKey = NULL;
	
    lRC = RegOpenKey(hKeyClass, lpszSubKey, &hKey);
    if (lRC != ERROR_SUCCESS)
    {
        return(NULL);
    }
	
    // Got key, get value.  First, get the size of the key.
    lRC = RegQueryValueEx(hKey, lpszValueName, NULL, NULL, NULL, &dwKeySize);
    if (lRC != ERROR_SUCCESS)
    {
        return(NULL);
    }
    if (dwKeySize <= 1)  // Registry will return "" if no printers installed
    {
        return(NULL);
    }
	
    lpszKeyValue = (LPTSTR)GlobalAlloc(GPTR, (++dwKeySize));
    if (lpszKeyValue == NULL)
    {
        return(NULL);
    }
	
    lRC = RegQueryValueEx(hKey, lpszValueName, NULL, &dwKeyDataType, (LPBYTE)lpszKeyValue, &dwKeySize);
    return(lpszKeyValue);
}   // End of function GetRegistryString

LPTSTR CPrePrint::CopyString(LPTSTR  lpszSrc)
{
    // Local variables
    LPTSTR    lpszDest;
    int       iStrLen;
	
    //  Initialize variables
    lpszDest = NULL;
	
    if (lpszSrc == NULL)
	{
        DebugMsg(__TEXT("ICMVIEW.C : CopyString : lpszSrc == NULL\r\n"));
        return(NULL);
	}
    iStrLen = ((int)(lstrlen(lpszSrc) +1) * sizeof(TCHAR));
    lpszDest = (LPTSTR)GlobalAlloc(GPTR, iStrLen);
    _tcscpy(lpszDest, lpszSrc);
    return(lpszDest);
}   // End of function CopyString

PDEVMODE CPrePrint::GetDefaultPrinterDevMode(LPTSTR lpszPrinterName)
{
    LONG        lDevModeSize;
    HANDLE      hDevMode;
    PDEVMODE    pDevMode = NULL;
	
	
    lDevModeSize = DocumentProperties(NULL, NULL, lpszPrinterName, NULL, NULL, 0);
    if (lDevModeSize > 0)
    {
        hDevMode = GlobalAlloc(GHND, lDevModeSize);
        pDevMode = (PDEVMODE) GlobalLock(hDevMode);
        DocumentProperties(NULL, NULL, lpszPrinterName, pDevMode, NULL, DM_OUT_BUFFER);
    }
    else
    {
        DebugMsg(__TEXT("GetDefaultPrinterDevMode:  Could not obtain printer's devmode.\r\n"));
    }
	
    return pDevMode;
}


void CPrePrint::DebugMsg (LPTSTR lpszMessage,...)
{
#ifdef _DEBUG
    va_list VAList;
    TCHAR   szMsgBuf[256];
	
    // Pass the variable parameters to wvsprintf to be formated.
    va_start(VAList, lpszMessage);
    wvsprintf(szMsgBuf, lpszMessage, VAList);
    va_end(VAList);
	
    ASSERT(lstrlen((LPTSTR)szMsgBuf) < MAX_DEBUG_STRING);
    OutputDebugString(szMsgBuf);
#endif
    lpszMessage = lpszMessage;  // Eliminates 'unused formal parameter' warning
}

void DebugMsgA (LPSTR lpszMessage,...)
{
#ifdef _DEBUG
    va_list VAList;
    char    szMsgBuf[256];
	
    // Pass the variable parameters to wvsprintf to be formated.
    va_start(VAList, lpszMessage);
    wvsprintfA(szMsgBuf, lpszMessage, VAList);
    va_end(VAList);
	
    ASSERT(strlen((LPSTR)szMsgBuf) < MAX_DEBUG_STRING);
    OutputDebugStringA(szMsgBuf);
#endif
    lpszMessage = lpszMessage;  // Eliminates 'unused formal parameter' warning
}


HDC CPrePrint::GetPrinterDC(LPTSTR lpszFriendlyName, PDEVMODE pDevMode)
{
    HDC     hDC;
    BOOL    bFreeDevMode = FALSE;


    //  Initialize variables
    hDC = NULL;

    if (lpszFriendlyName != NULL)
    {
        // Make sure that we have a devmode.
        if (NULL == pDevMode)
        {
            pDevMode = GetDefaultPrinterDevMode(lpszFriendlyName);
            bFreeDevMode = TRUE;
        }

        // Now get a DC for the printer
        hDC = CreateDC(NULL, lpszFriendlyName, NULL, pDevMode);

        // Free devmode if created in routine.
        if (bFreeDevMode)
        {
            GlobalFree((HANDLE)pDevMode);
        }
    }
    else
    {
        DebugMsg(__TEXT("GetPrinterDC:  lpszFriendlyName == NULL"));
    }

    return hDC;
}   // End of function GetPrinterDC

HDC CPrePrint::GetDefaultPrinterDC()
{
    HDC     hDC;
    LPTSTR  lpszPrinterName;


    //  Initialize variables
    hDC = NULL;

    lpszPrinterName = GetDefaultPrinterName();
    if (lpszPrinterName != NULL)
    {
        hDC = GetPrinterDC(lpszPrinterName, NULL);
        GlobalFree(lpszPrinterName);
    }
    else
    {
        DebugMsg(__TEXT("GetDefaultPrinterDC:  Could not obtain default printer name.\r\n"));
    }

    return hDC;
}   // End of function GetDefaultPrinterDC


LPTSTR CPrePrint::GetDefaultPrinterName(void)
{
    // Local variables
    LPTSTR     lpszDefaultPrinter = NULL;

    if(IS_WIN95)
    {

        lpszDefaultPrinter = GetRegistryString(HKEY_CURRENT_CONFIG,
                                               __TEXT("SYSTEM\\CurrentControlSet\\Control\\Print\\Printers"),
                                               __TEXT("Default"));
    }
    else if (IS_NT)
    {
        TCHAR szTemp[MAX_PATH];
        LPTSTR lpszTemp;

        // Get Default printer name.
        GetProfileString(__TEXT("windows"), __TEXT("device"), __TEXT(""),
                         szTemp, sizeof(szTemp));

        if (lstrlen(szTemp) == 0)
        {
            // INVARIANT:  no default printer.
            return(NULL);
        }

        // Terminate at first comma, just want printer name.
        lpszTemp = _tcschr(szTemp, ',');
        if (lpszTemp != NULL)
        {
            *lpszTemp = '\x0';
        }
        lpszDefaultPrinter = CopyString((LPTSTR)szTemp);
    }
    return(lpszDefaultPrinter);
}   // End of function GetDefaultPrinterName


/*
BOOL CPrePrint::GetPrinterDevice(LPTSTR pszPrinterName, HGLOBAL* phDevNames, HGLOBAL* phDevMode)
{
    // if NULL is passed, then assume we are setting app object's
    // devmode and devnames
    if (phDevMode == NULL || phDevNames == NULL)
        return FALSE;

    // Open printer
    HANDLE hPrinter;
    if (OpenPrinter(pszPrinterName, &hPrinter, NULL) == FALSE)
        return FALSE;

    // obtain PRINTER_INFO_2 structure and close printer
    DWORD dwBytesReturned, dwBytesNeeded;
    GetPrinter(hPrinter, 2, NULL, 0, &dwBytesNeeded);
    PRINTER_INFO_2* p2 = (PRINTER_INFO_2*)GlobalAlloc(GPTR,
        dwBytesNeeded);
    if (GetPrinter(hPrinter, 2, (LPBYTE)p2, dwBytesNeeded,
       &dwBytesReturned) == 0) {
       GlobalFree(p2);
       ClosePrinter(hPrinter);
       return FALSE;
    }
    ClosePrinter(hPrinter);

    // Allocate a global handle for DEVMODE
    HGLOBAL  hDevMode = GlobalAlloc(GHND, sizeof(*p2->pDevMode) +
       p2->pDevMode->dmDriverExtra);
    ASSERT(hDevMode);
    DEVMODE* pDevMode = (DEVMODE*)GlobalLock(hDevMode);
    ASSERT(pDevMode);

    // copy DEVMODE data from PRINTER_INFO_2::pDevMode
    memcpy(pDevMode, p2->pDevMode, sizeof(*p2->pDevMode) +
       p2->pDevMode->dmDriverExtra);
    GlobalUnlock(hDevMode);

    // Compute size of DEVNAMES structure from PRINTER_INFO_2's data
    DWORD drvNameLen = lstrlen(p2->pDriverName)+1;  // driver name
    DWORD ptrNameLen = lstrlen(p2->pPrinterName)+1; // printer name
    DWORD porNameLen = lstrlen(p2->pPortName)+1;    // port name

    // Allocate a global handle big enough to hold DEVNAMES.
    HGLOBAL hDevNames = GlobalAlloc(GHND,
        sizeof(DEVNAMES) +
        (drvNameLen + ptrNameLen + porNameLen)*sizeof(TCHAR));
    ASSERT(hDevNames);
    DEVNAMES* pDevNames = (DEVNAMES*)GlobalLock(hDevNames);
    ASSERT(pDevNames);

    // Copy the DEVNAMES information from PRINTER_INFO_2
    // tcOffset = TCHAR Offset into structure
    int tcOffset = sizeof(DEVNAMES)/sizeof(TCHAR);
    ASSERT(sizeof(DEVNAMES) == tcOffset*sizeof(TCHAR));

    pDevNames->wDriverOffset = tcOffset;
    memcpy((LPTSTR)pDevNames + tcOffset, p2->pDriverName,
        drvNameLen*sizeof(TCHAR));
    tcOffset += drvNameLen;

    pDevNames->wDeviceOffset = tcOffset;
    memcpy((LPTSTR)pDevNames + tcOffset, p2->pPrinterName,
        ptrNameLen*sizeof(TCHAR));
    tcOffset += ptrNameLen;

    pDevNames->wOutputOffset = tcOffset;
    memcpy((LPTSTR)pDevNames + tcOffset, p2->pPortName,
        porNameLen*sizeof(TCHAR));
    pDevNames->wDefault = 0;

    GlobalUnlock(hDevNames);
    GlobalFree(p2);   // free PRINTER_INFO_2

    // set the new hDevMode and hDevNames
    *phDevMode = hDevMode;
    *phDevNames = hDevNames;
    return TRUE;
}
*/
//是否有效打印机
BOOL CPrePrint::IsPrinter(LPTSTR pszPrinterName)
{
    HANDLE hPrinter;
    if (OpenPrinter(pszPrinterName, &hPrinter, NULL) == FALSE)
        return FALSE;
    ClosePrinter(hPrinter);
	return TRUE;
}

⌨️ 快捷键说明

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