CEdit及其使用的字体. [英] CEdit and the font that it uses.

查看:60
本文介绍了CEdit及其使用的字体.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我试图在运行时确定多行CEdit控件中一行可以容纳多少个字符.
这是代码:

Hi,

I am trying to determine at run time how many characters I can fit on a line in a multi-line CEdit control.
Here is the code:

BOOL CSegmentsPage::OnInitDialog( void )
{
    CPropertyPage::OnInitDialog();
    BOOL bRet = m_font.CreatePointFont( 100, _T("Courier New") );
    
    m_editSegments.SetFont( &m_font );
    // calculate the number of characters per line.
    CRect r;
    CDC* pDC = m_editSegments.GetDC();
    
    CFont* pFont = pDC->GetCurrentFont();
    LOGFONT logFont;
    pFont->GetLogFont( &logFont );
    //logFont.lfFaceName == _T("System")

    m_editSegments.GetClientRect( &r );
    CSize sizeChar = pDC->GetTextExtent( CString(_T("1")) );
    m_nCharsPerLine = r.Width() / sizeChar.cx;

...



问题在于CDC使用的字体与我在CEdit控件(Courier New)上设置的字体不同,而是使用系统"字体.有人知道如何解决此问题吗?

谢谢-约翰



The problem is that the font the CDC is using is not the same as the font i set on the CEdit control(Courier New), it is using "System" font instead. Does anyone know how to fix this?

Thanks - John

推荐答案

我会使用
I would use CClientDC[^] instead of calling GetDC() from edit control.
Also try to manually select the created font in your client DC like this:
// Here you create the font and call SetFont for your edit control...
//...
// Now create a client DC for the edit control and select the created font
CClientDC dc(&m_editSegments);
CFont* pOldFont = dc.SelectObject(&m_font);
CSize size = dc.GetTextExtent("....");
dc.SelectObject(pOldFont);



我以前使用过类似的东西,对我有用.

注意:如果您需要所选字体的平均字符宽度,可以使用:



I''ve used something similar before and it worked for me.

NOTE: In case you need the average char width of the selected font you could use:

// Get text metrics
TEXTMETRIC txtMetric;
memset(&txtMetric, 0, sizeof(txtMetric));
dc.GetTextMetrics(&txtMetric);
// Now "txtMetric.tmAveCharWidth" contains the average char width



希望对您有所帮助:)



I hope this helps :)


那太好了,效果很好.

谢谢Nuri!
Thats great, worked just fine.

Thanks Nuri!


这篇关于CEdit及其使用的字体.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆