-
Printing Text
I'm trying to print the text in a multiline edit box.
The text prints, but only on one line without line breaks and
the font is not correct.
SelectObject(...) seems to set the font, but then the text prints
really small.
Code:
void PrintWindowText(HWND hWnd)
{
DOCINFO di;
PRINTDLG pd;
ZeroMemory(&pd, sizeof(PRINTDLG));
pd.lStructSize = sizeof(PRINTDLG);
pd.hwndOwner = hWnd;
pd.hDevMode = NULL;
pd.hDevNames = NULL;
pd.Flags = PD_RETURNDC | PD_NOPAGENUMS;
if (PrintDlg(&pd))
{
memset( &di, 0, sizeof(DOCINFO));
di.cbSize = sizeof(DOCINFO);
di.lpszOutput = (LPTSTR) NULL;
di.lpszDatatype = (LPTSTR) NULL;
di.fwType = 0;
StartDoc(pd.hDC, &di);
StartPage(pd.hDC);
GetWindowRect(hWnd, &rPrint);
DWORD dwBufferSize = GetWindowTextLength(hWnd)+1;
CHAR* szBuffer = new CHAR[dwBufferSize];
GetWindowText(hWnd, szBuffer, dwBufferSize);
//SelectObject(pd.hDC, (HFONT)SendMessage(hWnd, WM_GETFONT, 0, 0));
TextOut(pd.hDC, 20, 20, szBuffer, dwBufferSize);
EndPage(pd.hDC);
EndDoc(pd.hDC);
DeleteDC(pd.hDC);
delete[] szBuffer;
}
}
-
Fonts sizes are given in device units. This means that a 40 pixel font (quite large on a screen) will end up very tiny on a 300dpi printer.
MSDN contains the code to calculate a point size font for a given device.