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

Posted on 2000-03-26
Medium Priority
Last Modified: 2013-12-03
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?
Question by:emitchell
  • 2
  • 2
LVL 10

Accepted Solution

RONSLOW earned 300 total points
ID: 2659386
Get GetFont() to get the font.

Author Comment

ID: 2664349
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.

LVL 10

Expert Comment

ID: 2667394
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.

Author Comment

ID: 2696680
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!

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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.

Join & Write a Comment

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question