Click to See Complete Forum and Search --> : DrawString() not using specified font


dhanson
January 28th, 2003, 08:17 AM
I'm having a rather difficult time trying to get the DrawString function to print a line of text to a printer using the Font object I created. It picks up the font size, but no mater what Font I hardcode, the printer always uses Lucida. Here's the code that I'm using:

// prepare GDI+
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

// create device context
hdcPrint = CreateDC(NULL, sPrinterName, NULL, NULL);

// Get handle to the destined printer
if(!OpenPrinter(sPrinterName, &hPrinter, NULL)){
fclose(fptr);
return((short) GetLastError());
}

// Fill in the structure with info about this "document"
DOCINFO docInfo;
ZeroMemory(&docInfo, sizeof(DOCINFO));
docInfo.cbSize = sizeof(DOCINFO);
docInfo.lpszDocName = sFile;

// inform spooler we are starting a print document
StartDoc(hdcPrint, &docInfo);

// inform spooler we are starting a page
StartPage(hdcPrint);



// create graphic handling object
Graphics graphics(hdcPrint, hPrinter);

// Initialize printing/GDI+ arguments
wchar_t wFont[25];
GetProfileString("PrtService", "Font", "Courier", sFont, sizeof(sFont));
// convert to wchar_t
mbstowcs(wFont, sFont, 25);
iPointSize = GetProfileInt("PrtService", "PointSize", 10);

// declare graphic objects
Font myFont(wFont, (REAL)iPointSize);
SolidBrush blackBrush(Color(255, 0, 0, 0));
StringFormat format;
format.SetAlignment(StringAlignmentNear); // left alignment

int iMargin;
int iPointSizeOffset;
iMargin = GetProfileInt("PrtService", "MarginOffset", 50);
iPointSizeOffset = GetProfileInt("PrtService", "PointSizeOffset", 50);

/*
* Loop: copying data from the file to printer. Keep on
* copying data till we hit the end of the file.
*/
do{
// read a line of data
if(fgetws(wpData, sizeof(wpData), fptr) == NULL)
break;

// increment the number of lines currently written
iLines ++;

// check to see if we need to start a new page
if(iLines > iLinesPerPage){
EndPage(hdcPrint);
StartPage(hdcPrint);

// reset number of lines written
iLines = 1;
}

if(wcslen(wpData) > 1){
// set line orientation before printing
PointF origin((REAL)iMargin, (REAL)(((iLines - 1) * (iPointSize + 5)) + iPointSizeOffset));

// write the chunk of data to the printer
graphics.DrawString(wpData, wcslen(wpData), &myFont, origin, &format, &blackBrush);
}
}while(true);

// end current page
EndPage(hdcPrint);

(blah, blah, blah...)




The graphics object is writing the string at the correct X and Y coordinates, brush, format, and font size; however, it never uses the font I specify. I've tried Courier, Univers, and other fixed fonts but nothing seems to work.

Can someone point me in the right direction in fixing this?

Thanks!

dhanson
January 28th, 2003, 09:07 AM
Ok, even though I don't know why the original code doesn't work, I did happen to find a workaround. I'm posting it for those who may care:

// create a FontFamily object
FontFamily fontFamily(wFont);
// replaced the original Font object creation with the following:
Font myFont(&fontFamily, (REAL)iPointSize, FontStyleRegular, UnitPoint);

// replaced the original DrawString call with the following:
graphics.DrawString(wpData, -1, &myFont, origin, &blackBrush);

It's now using the Courier font that I originally wanted. Again, I'm not sure why the first code didn't work, but as I always say, "at least it's working now."