MFC打印相关代码

// MySetPrinter
// 
// Demonstrates how to use the SetPrinter API.  This particular function changes the orienation
// for the printer specified in pPrinterName to the orientation specified in dmOrientation.
// 
// Valid values for dmOrientation are:
// DMORIENT_PORTRAIT (1)
// or
// DMORIENT_LANDSCAPE (2)
BOOL CBaseView::MySetPrinter(CString csPrinterName, short dmOrientation)
{
	HANDLE hPrinter = NULL;
	DWORD dwNeeded = 0;
	PRINTER_INFO_2 *pi2 = NULL;
	DEVMODE *pDevMode = NULL;
	PRINTER_DEFAULTS pd;
	BOOL bFlag;
	LONG lFlag;

	// Open printer handle (on Windows NT, you need full-access because you
	// will eventually use SetPrinter)...
	LPSTR	pPrinterName = (char*)(LPCTSTR)csPrinterName;
	ZeroMemory(&pd, sizeof(pd));
	pd.DesiredAccess = PRINTER_ALL_ACCESS;
	bFlag = OpenPrinter(pPrinterName, &hPrinter, &pd);
	if (!bFlag || (hPrinter == NULL))
		return FALSE;

	// The first GetPrinter tells you how big the buffer should be in 
	// order to hold all of PRINTER_INFO_2. Note that this should fail with 
	// ERROR_INSUFFICIENT_BUFFER.  If GetPrinter fails for any other reason 
	// or dwNeeded isn't set for some reason, then there is a problem...
	SetLastError(0);
	bFlag = GetPrinter(hPrinter, 2, 0, 0, &dwNeeded);
         if ((!bFlag) && (GetLastError() != ERROR_INSUFFICIENT_BUFFER) || 
         (dwNeeded == 0))
	{
		ClosePrinter(hPrinter);
		return FALSE;
	}

	// Allocate enough space for PRINTER_INFO_2...
	pi2 = (PRINTER_INFO_2 *)GlobalAlloc(GPTR, dwNeeded);
	if (pi2 == NULL)
	{
		ClosePrinter(hPrinter);
		return FALSE;
	}

	// The second GetPrinter fills in all the current settings, so all you
	// need to do is modify what you're interested in...
	bFlag = GetPrinter(hPrinter, 2, (LPBYTE)pi2, dwNeeded, &dwNeeded);
	if (!bFlag)
	{
		GlobalFree(pi2);
		ClosePrinter(hPrinter);
		return FALSE;
	}

	// If GetPrinter didn't fill in the DEVMODE, try to get it by calling
	// DocumentProperties...
	if (pi2->pDevMode == NULL)
	{
		dwNeeded = DocumentProperties(NULL, hPrinter,
						pPrinterName,
						NULL, NULL, 0);
		if (dwNeeded <= 0)
		{
			GlobalFree(pi2);
			ClosePrinter(hPrinter);
			return FALSE;
		}

		pDevMode = (DEVMODE *)GlobalAlloc(GPTR, dwNeeded);
		if (pDevMode == NULL)
		{
			GlobalFree(pi2);
			ClosePrinter(hPrinter);
			return FALSE;
		}

		lFlag = DocumentProperties(NULL, hPrinter,
						pPrinterName,
						pDevMode, NULL,
						DM_OUT_BUFFER);
		if (lFlag != IDOK || pDevMode == NULL)
		{
			GlobalFree(pDevMode);
			GlobalFree(pi2);
			ClosePrinter(hPrinter);
			return FALSE;
		}

		pi2->pDevMode = pDevMode;
	}

	// Driver is reporting that it doesn't support this change...
	if (!(pi2->pDevMode->dmFields & DM_ORIENTATION))
	{
		GlobalFree(pi2);
		ClosePrinter(hPrinter);
		if (pDevMode)
			GlobalFree(pDevMode);
		return FALSE;
	}

	// Specify exactly what we are attempting to change...
// 	pi2->pDevMode->dmFields = DM_ORIENTATION;
// 	pi2->pDevMode->dmOrientation = dmOrientation;

 	pi2->pDevMode->dmFields = pi2->pDevMode ->dmFields | DM_PAPERSIZE | DM_PAPERLENGTH | DM_PAPERWIDTH |DM_ORIENTATION; 
 	pi2->pDevMode->dmPaperSize = 0;//DMPAPER_USER;  //自定义纸张 绘图仪打印用0!!!!!!!!!
 	
 	pi2->pDevMode->dmPaperWidth = (short)6210; //纸宽多少
 	pi2->pDevMode->dmPaperLength = (short)(m_nPrintHours*6210/5); //纸chang多少  2个小时30厘米 最大设为2730
 	
   	pi2->pDevMode->dmOrientation = DMORIENT_PORTRAIT;//顺便设定纸张走纸方向,横向还是纵向

	// Do not attempt to set security descriptor...
	pi2->pSecurityDescriptor = NULL;

	// Make sure the driver-dependent part of devmode is updated...
	lFlag = DocumentProperties(NULL, hPrinter,
		  pPrinterName,
		  pi2->pDevMode, pi2->pDevMode,
		  DM_IN_BUFFER | DM_OUT_BUFFER);
	if (lFlag != IDOK)
	{
		GlobalFree(pi2);
		ClosePrinter(hPrinter);
		if (pDevMode)
			GlobalFree(pDevMode);
		return FALSE;
	}

	// Update printer information...
	bFlag = SetPrinter(hPrinter, 2, (LPBYTE)pi2, 0);
	if (!bFlag)
	// The driver doesn't support, or it is unable to make the change...
	{
		GlobalFree(pi2);
		ClosePrinter(hPrinter);
		if (pDevMode)
			GlobalFree(pDevMode);
		return FALSE;
	}

	// Tell other apps that there was a change...
	SendMessageTimeout(HWND_BROADCAST, WM_DEVMODECHANGE, 0L,
			  (LPARAM)(LPCSTR)pPrinterName,
			  SMTO_NORMAL, 1000, NULL);

	// Clean up...
	if (pi2)
		GlobalFree(pi2);
	if (hPrinter)
		ClosePrinter(hPrinter);
	if (pDevMode)
		GlobalFree(pDevMode);

	return TRUE;
}


BOOL CBaseView::GetPrinterDevice(CString strPrinterName, 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;
	LPSTR	pszPrinterName = (char*)(LPCTSTR)strPrinterName;
    // 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);
	
	if (!OpenPrinter(pszPrinterName, &hPrinter, NULL))
		return FALSE ;
	int nSize = DocumentProperties(NULL, hPrinter, pszPrinterName, NULL, NULL, 0);
	ASSERT(nSize >= 0);
	HGLOBAL local_hDevMode = ::GlobalAlloc(GHND, nSize) ;	// allocate on heap
	LPDEVMODE lpDevMode = (LPDEVMODE)::GlobalLock(local_hDevMode);// lock it
	//LPDEVMODE lpDevMode = (DEVMODE *)GlobalAlloc(GPTR, nSize);
	if (DocumentProperties(NULL, hPrinter, pszPrinterName, lpDevMode, NULL, DM_OUT_BUFFER) != IDOK)
	{
		ASSERT(::GlobalFlags(local_hDevMode) != GMEM_INVALID_HANDLE);
		UINT nCount = ::GlobalFlags(local_hDevMode) & GMEM_LOCKCOUNT;
		while (nCount--)
			::GlobalUnlock(local_hDevMode);
		::GlobalFree(local_hDevMode);
		local_hDevMode = NULL;
		GlobalFree(p2);
		ClosePrinter(hPrinter);
		return FALSE ;
	}
	
	//p2->pDevMode->dmPaperSize = DMPAPER_USER;
// 	p2->pDevMode->dmFields = p2->pDevMode ->dmFields | DM_PAPERSIZE | DM_PAPERLENGTH | DM_PAPERWIDTH |DM_ORIENTATION; 
// 	p2->pDevMode->dmPaperSize = 0;//DMPAPER_USER;  //自定义纸张
// 	 	
// 	p2->pDevMode->dmPaperWidth = (short)6210; //纸宽多少
// 	p2->pDevMode->dmPaperLength = (short)(m_nPrintHours*6210/5); //纸chang多少  2个小时30厘米 最大设为2730
//  		
//   	p2->pDevMode->dmOrientation = DMORIENT_PORTRAIT;//顺便设定纸张走纸方向,横向还是纵向
//  	if(DocumentProperties(NULL, hPrinter,pszPrinterName,p2->pDevMode, p2->pDevMode,DM_IN_BUFFER | DM_OUT_BUFFER)!= IDOK)
//  	{
//  		ASSERT(::GlobalFlags(local_hDevMode) != GMEM_INVALID_HANDLE);
// 		UINT nCount = ::GlobalFlags(local_hDevMode) & GMEM_LOCKCOUNT;
//  		while (nCount--)
//  			::GlobalUnlock(local_hDevMode);
//  		::GlobalFree(local_hDevMode);
//  		local_hDevMode = NULL;
//  		GlobalFree(p2);
//  		ClosePrinter(hPrinter);
//  		hPrinter = NULL;
//  		return FALSE;
//  	}
//  	BOOL bFlag = SetPrinter(hPrinter, 2, (LPBYTE)p2, 0);
		 
	::GlobalUnlock(local_hDevMode) ;
	ClosePrinter(hPrinter);
	
    // 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 = local_hDevMode;
    *phDevNames = hDevNames;

    return TRUE;
} 

void CBaseView::ChangeDocumentArea(int nWidth ,int nHeight )
{
	double fWidthMulti,fHeightMulti;
	double g_basic_total_page_width = 3600.0;
	double g_basic_total_page_height = 2050.0;
	fWidthMulti = (double)nWidth / g_basic_total_page_width;  
	fHeightMulti = (double)nHeight / g_basic_total_page_height;
	m_nPageWidth = g_basic_total_page_width* fWidthMulti;     //打印页码的总宽度
	m_nPageHeight = g_basic_total_page_height* fHeightMulti;  //总高度
}

bool CBaseView::GetOffsetAndSize(int type, int& x_offset, int& y_offset, int& height, int& width)
{
	double offset_x = 1, offset_y = 1, height_r = 1, width_r = 1;
	switch(type)
	{
	case AREASCHED:
		{
			offset_x = 13;
			offset_y = 18+4;
			height_r = 70;
			width_r  = 70;
		}
		break;
	case AREATITLE:
		{
			offset_x = 50;
			offset_y = 1.5;
			height_r = 1;
		}
		break;
	case AREAFOOT:
		{
			offset_x = 50;
			offset_y = 99.7;
			height_r = 1;
		}
		break;
	default:
		return false;
	}

	x_offset = (int)((offset_x/100.0)*m_nPageWidth);
	y_offset = (int)((offset_y/100.0)*m_nPageHeight);

	height   = (height_r/100.0)*m_nPageHeight;
	width    = (width_r/100.0)*m_nPageWidth;
	return true;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值