Need font used by CListView in report mode so can print columns with same size

I am trying to print information that is held in a CListView in report mode.  I have about 12 - 15 columns that have been sized to the header and text in the column below with a LVSCW_AUTOSIZE_USEHEADER and then the user may have changed the widths of any colunn by moving the header separators.  I want to replicate this column information (not necessarily in the same columns) in fields on the printed output.

I can get the width of each column in pixels from the LV_COLUMN struct but I'm not sure how to get the average number of characters that the column is sized for.  If I could get the font that the list control is using then I could ask text metrics for the average character width and from that get the number of average chacters in each column.  Since I know the printer font, I can size the printer fields to match the list control fields.

Basic question is: How can I ask the system for the font that a list control is using?

Can the user change a list control font?

Do different machines have different list control fonts?
emitchellAsked:
Who is Participating?
 
RONSLOWConnect With a Mentor Commented:
Get GetFont() to get the font.
0
 
emitchellAuthor Commented:
I tried using GetFont() in the list control.  The sequence was as follows:

    // pick up the actual list control associated with this view
    CListCtrl& rlc = GetListCtrl();

    // get the font for list control window
    CFont* pFont = rlc.GetFont();
    LOGFONT lf;
    int ret = pFont->GetLogFont(&lf);

The LOGFONT struct here has -10 for the lfHeight member and 0 for the lfWidth which is supposed to be the average character width of the font. ie.

      lfHeight      -10
      lfWidth      0
        ...
+      lfFaceName      0x006eeae8 "MS Sans Serif"

I got the above text by highlighting the line in the QuickWatch window and doing Copy/Paste into this doc.

I need the average width so that I can convert the cx from the CListCtrl in pixels to an average number of characters.

0
 
RONSLOWCommented:
get a DC (GetDC) for the list control and then call GetTextMetrics on in and get the average character width from there.

Or call GetTextExtent with a large string (eg "ABCD...XYZabcd...xyz") and work out average size from that.
0
 
emitchellAuthor Commented:
The DC for the ListCtrl does the trick.  The average character width from this works fine.

Thanks to the input.  I'm sorry I have been so long in responding but I was travelling back to the cold North from Florida!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.