...
"Joseph M. Newcomer" wrote:
On Mon, 21 May 2007 13:42:02 -0700, Saul775 <Saul775@discussions.microsoft.com> wrote:
Hello, everyone. I'm wondering if you could assist me in my endeavor to
print the contents of a list box.
I have my list box with about twenty or so items in it. I would send it a
simple WM_PRINT, but the number of items VISIBLE is less than the actual
number of items in the list box, so a scrollbar is inserted into the control;
therefore, when printing, items will be missing.
When I print the contents of the list box, I want it to encompass the ENTIRE
printable area of the sheet of paper; I do not care if it's ANISOTROPIC or
ISOTROPIC. I've tried many ways to print the contents, but all my methods
seems to fail. What's plaguing me is the scaling; I'm having problems with
scaling the image -- sometimes its too small and other times it's too large,
truncating the image. Any help would be much appreciated.
I will now iterate the steps for my code...
To draw the image I call GetListBoxItems
void CMyView::GetListBoxItems(CDC &dc, CBitmap &bm)
{
CDC memDC;
memDC.CreateCompatibleDC(&dc);
// Let's get the size of the approximate rectangle that will encompass all
items
CSize listRect(m_ListBox.ApproximateViewRect());
// Now get the maximum printing area of the printer
CSize printerLP(dc.GetDeviceCaps(HORZRES), dc.GetDeviceCaps(VERTRES));
// Now adjust the memDC
memDC.SetMapMode(MM_ISOTROPIC);
memDC.SetWindowExt(printerLP);
memDC.SetViewportExt(listRec);
// Create the bitmap now with the correct dimensions
bm.CreateComaptibleBitmap(&dc, listRect.cx, listRect.cy);
CBitmap *pOrigBM = memDC.SelectObject(&bm);
// Now all I do is iterate through each of the items, using LVITEM...
// For that new item, I memDC.TextOut the item's text
*****
Seems remarkably clumsy, and why use a bitmap? Why not just do
int x = ... compute left margin here;
int y = ... compute top margin
TEXTMETRIC tm;
dc.GetTextMetrics(&tm);
int LineHeight = tm.tmHeight + tm.tmInternalLeading;
for(int i = 0; i < c_ctl.GetCount(); i++)
{ /* print contents */
CString s;
c_ctl.GetLBText(i, s);
dc.TextOut(x, y, s);
y += LineHeight;
} /* print contents */
}
I see no reason here to consider using some intermediate bitmap along the way. It will
only produce ugly results.
To fit it to the maximum size, you could get each line and compute
CSize sz = dc.GetTextExtent(s);
width = max(width, sz.cx);
then compute font such that the longest line fits across the page, e.g.,
int page = dc.GetDevCaps(SZHORZ);
double ratio = (double) page / (double)width;
CFont * f = dc.GetCurrentFont();
LOGFONT lf;
f->GetLogFont(&lf);
lf.lfHeight = (int) ((double)lf.lfHeight * ratio);
CFont newfont;
newfont.CreateFontIndirect(&lf);
dc.SelectObject(&newfont);
(or something very close to this; I'm typing this largely from memory)
Do not use a bitmap for printing unless there is some overwhelmingly compelling reason for
doing so, and such reason does not exist in this case.
Note that if you want to fit all the contents on one page, you may have to compute the
ratio as the min of the ratio for full width and the ratio for full height.
joe
******
Now, let me comment on my code. I know I do not need to create a memory DC,
but I do it for now, for later I will be appending other bitmaps to the
printer DC, but for now I want my list box to encompass the entire page.
Again, the problem is scaling. I believe the issue is stemming from the
mapping mode and setting the extents of the viewport and the window. Again,
I thank you all for perusing my code and assisting me in my predicament.
Should you need more code, I will be more than happy to post it, but I did
not want to intimidate anyone with the amount of code.
Thank you,
Saul775
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm