什么都没打印到纸上 [英] Nothing is printing to the paper

查看:90
本文介绍了什么都没打印到纸上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我是C ++的新手,正在从事我的学位项目,
我已运行以下代码以向打印机打印问候世界,但没有打印内容,只有空白页.
代码有什么问题?

Hi everyone,
I am new to C++ and working on my degree project,
I have run the following code to print hello world to the printer, but nothing is printing, only a blank page.
Whats wrong with the code?

int WINAPI WinMain( HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, int nCmdShow )
{
	PRINTDLG pd;

	memset( &pd, 0, sizeof( pd ) );

	pd.lStructSize = sizeof( pd );

	/* 
	 * get rid of PD_RETURNDEFAULT on the line below if you'd like to
	 * see the "Printer Settings" dialog!
	 *
	 */
	pd.Flags = PD_RETURNDEFAULT | PD_RETURNDC;

	// try to retrieve the printer DC
	if( !PrintDlg( &pd ) )
	{
		MessageBox( NULL, "PrintDlg( &pd ) failed!", "Fatal Error", MB_OK | MB_ICONERROR );

		return -1;
	}

	DOCINFO di;
	HDC hPrinter = pd.hDC;

	// initialization of the printing!
	memset( &di, 0, sizeof( di ) );
	di.cbSize = sizeof( di );
	StartDoc( hPrinter, &di );

		// start the first and only page
		StartPage( hPrinter );

		// uncomment the following line to print in colour! :)
		 SetTextColor( hPrinter, 0x0000FF );

		// write "Hello, World!" to the printer's DC
		TextOut( hPrinter, 100, 100, "Hello, World!", 13 );

		// finish the first page
		EndPage( hPrinter );

	// end this document and release the printer's DC
	EndDoc( hPrinter );
	DeleteDC( hPrinter );

	return 0;
}

推荐答案

我刚刚使用PDF打印机尝试了您的代码,它可以正常工作.

StartDoc [起始页 [
I just tried your code with a PDF printer and it worked correctly.

StartDoc[^] and StartPage[^] both have a return value that specifies success or failure.
You might want to check the returns values to see if they return success. Either code to check the return value or debug the program to see what is returned.

I would also skip the color printing for now by commenting out the line:
SetTextColor( hPrinter, 0x0000FF );


如果您要在页面的空白处打印,还可能需要增加打印坐标:


You might also want to increase the print coordinates in case you are printing outside the margins of the page:

TextOut( hPrinter, 1000, 1000, "Hello, World!", 13 );


这篇关于什么都没打印到纸上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆