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;
}