Determining char width

This is a discussion on Determining char width within the Windows Programming forums, part of the Platform Specific Boards category; Hi there! In my Notepad program, to determine char widths I use the following code. Code: pDC->GetTextMetrics (&textmetrics); Size = ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    42

    Determining char width

    Hi there!
    In my Notepad program, to determine char widths I use the following code.
    Code:
    pDC->GetTextMetrics (&textmetrics);
    Size = pDC->GetTextExtent(pDoc->m_text);		
    	HideCaret();
    pDoc->m_CarPos.x = Size.cx -  textmetrics.tmAveCharWidth;
    pDoc->m_text.Empty();
    This doesn't seem to be very effective. As sometimes I actually get it less than the actual width of the character (its the average, so its natural).
    Is there some other method to get the width of the last character in a CString, other than GetTextMetrics();
    Thanks.

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    GetTextMetrics() returns information about the font in general rather than any particular string of characters. GetTextExtentPoint32() works with strings rendered in a particular font, pass it a single character string.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    169
    Look up DT_CALCRECT.
    Code:
    char strText[] = "Your line of text";
    RECT rectDummy = {0,0,0,0};
    int nHeight = pDC->DrawText(strText, -1, &rectDummy, DT_TOP | DT_SINGLELINE | DT_CALCRECT);
    int nWidth  = rectDummy.right - rectDummy.left;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 08-11-2008, 11:02 PM
  2. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  3. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21