Printing using WIN 32 API/GDI

Dec 1, 2011 at 7:25pm
I'm using visual studio 2008...and I'm trying to link my code to printer in order to print out the name that user key in...so, anyone can teach me how am i able to print out the string name by using textout?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <windows.h>
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int WINAPI WinMain( HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, int nCmdShow ){
	PRINTDLG pd;
	memset( &pd, 0, sizeof( pd ) );
	pd.lStructSize = sizeof( pd );
	pd.Flags = PD_RETURNDEFAULT | PD_RETURNDC;

	if( !PrintDlg( &pd ) ){
		MessageBox( NULL, L"PrintDlg( &pd ) failed!", L"Fatal Error", MB_OK | MB_ICONERROR );
		return -1;
	}

	DOCINFO di;
	HDC hPrinter = pd.hDC;

	memset( &di, 0, sizeof( di ) );
	di.cbSize = sizeof( di );
	StartDoc( hPrinter, &di );

		StartPage( hPrinter );
		//print text to printer
		TextOut( hPrinter, 100, 100, L"Hello, World!", 13 );

		EndPage( hPrinter );

	EndDoc( hPrinter );
	DeleteDC( hPrinter );
	return 0;
}

int main(){
	string name;
	cout << "Enter Your Name:";
	getline(cin,name);
	cout << "Please enter you choice <1 = Print , 2 = Exit> :";
	int choice;
	cin >> choice;
	if (choice == 1){}
	if(choice == 2){
		cout<<"Please select 1....because this system is trying to print !!"<<endl;
		return 0;
	}
}
Dec 1, 2011 at 7:27pm
please help me rectify the line 26 and line 42...
line 26 need to print the string name that user key in....
line 42 need to use the print function...but i dunno what should i do..
Dec 2, 2011 at 2:03pm
Why do you have your print routing inside a WinMain() function. WinMain() is the GUI equivalent of main(); it is the entry point for a Windows application. Just rename WinMain() to something else, like bool Print(const string &theNameToPrint) and call that function from your line 42.

Remember to change line 26 to actually print the given name: TextOutA(hPrinter, 100, 100, theNameToPrint.c_str(), -1);. Notice how I changed TextOut to TextOutA. You MUST read my response here http://www.cplusplus.com/forum/general/56526/.
Dec 4, 2011 at 5:44am
thanksss~~
Topic archived. No new replies allowed.