Thread: Printing nightmare

  1. #1
    Former Member
    Join Date
    Oct 2001
    Posts
    955

    Printing nightmare

    Greetings.

    I'm trying to make an app that prints data on the printer with VC 6, however, when I do the TextOut function for the whole text, it prints the whole thing on one line, and \r\n 's are turned into two small unidentified-character boxes.. I've searched through the forums and I've found the idea of taking all the lines, one by one, and print them on a separate line each.

    That would work, but I get the same problem with \t , they are also printed as little boxes and not as tabs. I can't believe there isn't an easy way to print text into the printer! I mean, isn't there anything like the Printer object in Visual Basic? or something to make printing easier? it could take me days to write a whole printing module while I'm thinking that it is a very common task that there must already be something implemented that does it!

    If anybody knows anything, please, let me know

    Osk

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    I've not played around with printing much, Osk, but from what I undertstand printing, like gdi, is via a device context. So fns like TextOut, ExtTextOut, DrawText etc should all work ok. The output may look slightly different ie may not be completely wysiwyg.

    If you are looking to output a chunk of formatted text take a look at DrawText or DrawTextEx.

    Sorry I can't be more helpful than that.

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Unfortunately, as you have noticed, '\r' and '\n' dont work very well in GDI....but '\t' does if you use a specialized API called TabbedTextOut.......it works as TextOut, but allows you to use tabbing.

    Unfortunately, you must specify the size that tabs leave between text and calculate them as intervals accross the horizontal dimention of the page/window.....this can be done with an array....I have also noticed there isnt too good info on this function on the web (I found the API docs a little confusing at first, and neither Petzold or Prosise give a real example - that I could find..)...so it took me a while to Understand it.....

    I thought I'd use this opportunity to make a prog that paid homage to one of my favorite horror movies & books - The Shining!!! (you have to enjoy yourself now and again) - hope it helps you

    Code:
    #include <windows.h>
    #include <commdlg.h>
    #pragma comment(lib,"comdlg32.lib")//for vc++...otherwise add lib
    
    //***********************************************
    
    
    int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hInstPrev,
    	LPSTR szCommand,int nCmdShow)
    {
    	PRINTDLG  pd = {0};//For the PrintDlg API
    	pd.lStructSize = sizeof(PRINTDLG);
    	pd.hwndOwner = HWND_DESKTOP;
    	pd.Flags = PD_RETURNDC;
    	pd.nCopies = 1;
    	DOCINFO di = //This gives info of your job on the printing queue
    	{
    		sizeof(DOCINFO),
    		"Fordy loves the Shining!"//catchy phrase
    	};
    	const char szBuff[] = //Token text to print - note TAB in middle
    		"All work and no play.....\t.....makes Fordy a dull boy!";
    
    	
    	if(!PrintDlg(&pd))//Open the Print Dialog...allows you to choose printer
    		return 0;//if cancelled...bail
    
    	HGDIOBJ hFont = //get current font of printer
    		GetCurrentObject(pd.hDC,OBJ_FONT);
    	if(!hFont)//error check
    	{
    		MessageBox(0,"Unable to get font",0,MB_OK);
    		return 1;
    	}
    
    	LOGFONT lf;//struct to hold font data
    	if(!GetObject(hFont,sizeof(lf),&lf))//get data about font
    	{//error check
    		MessageBox(0,"Unable to get font data",0,MB_OK);
    		return 1;
    	}
    	LONG lpHeight = GetDeviceCaps(pd.hDC,VERTRES),//get height of page
    		 lpWidth = GetDeviceCaps(pd.hDC,HORZRES),//and its width too
    		 lfHeight = lf.lfHeight;//get height of font
    	
    	//Now we want an array for our tab positions
    	const int nTabArraySize = 4;//4 tab positions
    	int nTabArray[nTabArraySize];//array of positions
    	for(int i = 0;i < nTabArraySize;++i)//for each array...
    	{
    		//space it out in quarters along horizontal of page
    		nTabArray[i] = i*(lpWidth/nTabArraySize);
    	}
    
    
    	if(StartDoc(pd.hDC,&di)>0 && StartPage(pd.hDC)>0)//Start doc and page
    	{
    		for(int i = lfHeight;i <= lpHeight;i+=(lfHeight*3))
    		{
    			//Now...print out text
    			//....and move down 3 times the height of font each time
    			TabbedTextOut(pd.hDC,10,i,szBuff,sizeof(szBuff),
    				nTabArraySize,nTabArray,10);
    		}
    		
    		EndPage(pd.hDC);//end page
    		EndDoc(pd.hDC);//end doc
    	}
    	DeleteDC(pd.hDC);//cleanup!
    
    	return 0;
    }

  4. #4
    Former Member
    Join Date
    Oct 2001
    Posts
    955
    This is great: TabbedTextOut() fixes the tabs and DrawText() fixes the line breaks... but it's still not working... Well, but with the DrawText function, I can do some good stuff...

    However, I still can't believe Visual C++ has no single simple text printing routines!!! Every language does!!

    Well, thank God Windows XP still supports

    Code:
    FILE* printer=fopen("LPT1","wt");
    fprintf(printer,"Hello, world");
    fclose(printer);
    doesn't work with networked printers.. I wonder why

    Well, I'll get to wrok on it tonight! how fun!!!

    If you guys have any other ideas, lemme know, preferably by PM@FD

    Osk

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    I use an array of strings

    Code:
    GetTextExtentPoint32( hdcPrinter, sBuffer, lstrlen(sBuffer),  &Size);//find the size of a SPECIFIC line of text
    XStart=iLeftMargin+(int)( ( (iRightMargin - iLeftMargin) -Size.cx )/2); //this will center the text
    TextOut(hdcPrinter,  XStart, YPos, sBuffer, lstrlen(sBuffer));//print it
    YPos+=Size.cy;//increment line count
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# Printing Problem
    By silverlight001 in forum C# Programming
    Replies: 0
    Last Post: 03-23-2009, 01:13 AM
  2. Printing Lines to .txt File
    By Programmer3922 in forum C Programming
    Replies: 2
    Last Post: 08-02-2008, 12:45 PM
  3. generic printing preferences dialog box
    By stanlvw in forum Windows Programming
    Replies: 8
    Last Post: 06-27-2008, 02:20 AM
  4. printing data to a file
    By coralreef in forum C Programming
    Replies: 3
    Last Post: 11-02-2006, 08:10 PM
  5. need help relating printing?
    By omarlodhi in forum Linux Programming
    Replies: 0
    Last Post: 03-03-2006, 04:46 AM