courier font width 
Author Message
 courier font width

Hi;
I created an editbox dynamically an dits lentgh must be according to the
data in it that will be shown.
My font is courier  9 pt.
How can I get this fonts width and draw an editbox that will be just 6 chars
courier 9 length ;?

?

thanks



Thu, 30 Dec 2004 02:00:31 GMT  
 courier font width
You have a couple of prospects: CDC::GetTextMetrics(...) and
CDC::GetTextExtent(...).

Normally I'd suggest the GetTextExtent approach to measure the actual text
you want to place.  But since Courier is a fixed point font you should be
able to get away with GetTextMetrics.

Keep in mind that both return their results in logical units.  You can use
CDC::LPtoDP to make the conversion the device points which is required for
the MoveWindow() or SetWindowPos() you might use to size the edit box window
to it's desired size.

TFM3

Note: Spam-resistant e-mail address


Quote:
> Hi;
> I created an editbox dynamically an dits lentgh must be according to the
> data in it that will be shown.
> My font is courier  9 pt.
> How can I get this fonts width and draw an editbox that will be just 6
chars
> courier 9 length ;?

> ?

> thanks



Thu, 30 Dec 2004 02:37:16 GMT  
 courier font width

Quote:

> Hi;
> I created an editbox dynamically an dits lentgh must be according to the
> data in it that will be shown.
> My font is courier  9 pt.
> How can I get this fonts width and draw an editbox that will be just 6 chars
> courier 9 length ;?

> ?

> thanks

FWIW, here's my take on it...

    CONST INT BORDERX = GetSystemMetrics(SM_CXBORDER);
    enum {
        STYLE = WS_CHILD|WS_VISIBLE|WS_TABSTOP,
        DTFLAGS = DT_CALCRECT|DT_SINGLELINE,
        EXSTYLE = WS_EX_CLIENTEDGE,
        OFFSETX = 10, OFFSETY = 10,
        POINTSIZE = 9, COUNT = 6
    };

    if (m_editFont.CreatePointFont((10*POINTSIZE), _T("Courier New")))
    {
        try
        {
            CString str('\x20', COUNT);
            CWindowDC dc(this);

            if (CFont* pOld = dc.SelectObject(&m_editFont))
            {
                CRect rc(0,0,0,0);
                if (dc.DrawText(str, -1, &rc, DTFLAGS))
                {
                    if (::AdjustWindowRectEx(&rc, STYLE, FALSE, EXSTYLE) &&
                         ::InflateRect(&rc, BORDERX, 0))
                    {
                        if (HWND hWnd = CreateWindowEx(
                            EXSTYLE, _T("EDIT"), _T(""), STYLE,
                            OFFSETX, OFFSETY, rc.Width(), rc.Height(),
                            m_hWnd, (HMENU)1012, GetModuleHandle(NULL), NULL))
                        {
                            ::SendMessage(hWnd,
                                WM_SETFONT, (WPARAM)(HFONT)m_editFont, FALSE);
                            ::SendMessage(hWnd,
                                EM_LIMITTEXT, COUNT, 0);
                        }
                    }
                }
                dc.SelectObject(pOld);
            }
        }
        catch (CMemoryException* e)
        {
            e->Delete();
        }
        catch (CResourceException* e)
        {
            e->Delete();
        }
    }

--
Jeff Partch [MVP]



Thu, 30 Dec 2004 22:27:54 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. courier font width

2. courier font width

3. determining char width of courier font

4. CFont: Problem to use Courier or Courier New for any printer

5. Problem with Courier New font

6. Cyrillic Courier font doesn't load

7. Problem with Courier New Font

8. How to get the width of a font

9. How to get width of a font

10. Fonts all characters the same width - how to?

11. calculating width of unicode characters for a Japanese font

12. Problem getting the char width of a font

 

 
Powered by phpBB® Forum Software