Bootstrap

如何用win32api修改纸张大小

Author:  kagula

Last update data: 2015-10-09

为了代码不依赖MFC,用Win32API 实现了一遍。


		HGLOBAL hDevNames = NULL;
		HGLOBAL hDevMode = NULL;
		if (FALSE == GetPrinterDevice(const_cast<LPTSTR>(StringHelper::s2ws(body.GetPrinterName()).c_str()),
			&hDevNames, &hDevMode))
		{
			::MessageBox(NULL,L"打印机无法使用,请重新选择打印机!",L"Error",MB_OK);
			return;
		} //else//AfxGetApp()->SelectPrinter(hDevNames, hDevMode); 

		//set paper width and height
		LPDEVMODE lpDevMode=(LPDEVMODE)GlobalLock(hDevMode); 
		if(lpDevMode)
		{ 
			lpDevMode->dmPaperSize = DMPAPER_USER;  //自定义纸张    A4 = 9; A5 = 11; B5 = 13;
			lpDevMode->dmFields |= DM_PAPERSIZE | DM_PAPERLENGTH | DM_PAPERWIDTH | lpDevMode->dmFields;
			lpDevMode->dmPaperWidth = body.GetBarcode_paper_width();//新的纸张宽度
			lpDevMode->dmPaperLength = body.GetBarcode_paper_heig
;