+ -
当前位置:首页 → 问答吧 → 如何使编辑框的字体大小与编辑框一样大??

如何使编辑框的字体大小与编辑框一样大??

时间:2011-12-11

来源:互联网

我用GetWindowRect(rect2)得到编辑框大小后,怎么转为字体高度?用ScreenToClient(rect2)不可行;求解?
我的代码如下:
void CMillimarView::OnSize(UINT nType, int cx, int cy) 
{
CFormView::OnSize(nType, cx, cy);

// TODO: Add your message handler code here
int eHeight,eWidth,sHeight,sWidth;
CRect rect1;
this->GetWindowRect(rect1);

if (m_EDVal.m_hWnd!=NULL)
{

float hstatic;
hstatic=0.5f;
eHeight=rect1.Height()/5;
eWidth=rect1.Width();
sHeight=eHeight*hstatic;

m_static1.MoveWindow(0,0,eWidth,sHeight); //设置静态文本框和编辑框的大小和位置
m_EDVal.MoveWindow(0,sHeight,eWidth,eHeight);

CRect rect2;

int hfont;
m_EDVal.GetWindowRect(rect2); //获得编辑框的客户区大小
//hfont=(rect2.Height()-10)*7;
this->ClientToScreen(rect2); //没有效果,用ScreenToClient()也一样;
hfont=(rect2.Height())*6.5; //设置字体高度;

if (&m_edFont==NULL)
{
m_edFont.CreatePointFont(hfont, _T("Arial"));
m_stFont.CreatePointFont(hfont*hstatic, _T("Arial"));
//这里也有个问题,如果我把*m_edFont定义为指针型成员变量,那运行时老是报错???????????不知道为什么

}
else
{
m_edFont.DeleteObject();
m_edFont.CreatePointFont(hfont, _T("Arial"));
m_stFont.DeleteObject();
m_stFont.CreatePointFont(hfont*hstatic, _T("Arial"));

}


// VERIFY(font1->CreatePointFont(hfont, _T("Arial")));


//pFont->CreatePointFontIndirect(&lf);

// pFont->CreatePointFontIndirect(180,_T("楷体_GB2312"));
m_EDVal.SetFont(&m_edFont,true);
m_edvals="3.1415926 mm";
m_static1.SetFont(&m_stFont,true);
m_stTips="设备状态:123";
m_EDVal.SetSel(-1);
UpdateData(false);


}


}

作者: lfxiansheng   发布时间: 2011-12-11

CreatePointFont函数中nPointSize参数定义如下:
Requested font height in tenths of a point. (For instance, pass 120 to request a 12-point font.)

显然与CRect::Height 函数返回值不匹配。或者转换,或者改用CreateFont函数创建字体。

作者: fight_in_dl   发布时间: 2011-12-11