读书人

如何实现窗体中的所有字体大小随窗体大

发布时间: 2012-03-18 13:55:38 作者: rapoo

怎么实现窗体中的所有字体大小随窗体大小变化?(控件大小变化已实现)
如题!

[解决办法]
我以前做过一个类似的,是在对话框的OnPaint里边做的.根据窗口的大小改变字体大小.
不过你这里有文本框,组合框,按钮,都重写OnPaint是有点麻烦的,不知道有没有什么办法可以一次性搞定~
代码供参考
其中的m_strCaption就是界面上显示的文字内容

C/C++ code
void CTestDlg::OnPaint(){    CPaintDC dc(this); // device context for painting    // TODO: 在此处添加消息处理程序代码    // 不为绘图消息调用 CDialog::OnPaint()    CRect rcClient;    GetClientRect(&rcClient);    HDC dcMemory = CreateCompatibleDC( dc.m_hDC );    if (NULL == dcMemory)    {        return;    }    HBITMAP hMemoryBitmap = CreateCompatibleBitmap( dc.m_hDC, rcClient.Width(), rcClient.Height() );    if(NULL == hMemoryBitmap)    {        return ;        }    SelectObject( dcMemory, hMemoryBitmap );        SetTextColor( dcMemory, RGB(236,0,0) );    SetBkColor( dcMemory, RGB(51,0,0) );    HBRUSH hBrushBackground = CreateSolidBrush( RGB(51,0,0) );    if( hBrushBackground == NULL )        return ;    HBRUSH    hBrushBorder = CreateSolidBrush( RGB(50,50,255));    if( hBrushBorder == NULL )        return ;    FillRect( dcMemory, &rcClient, hBrushBackground );    FrameRect( dcMemory, &rcClient, hBrushBorder );    int nWidth = GetValidTextWidth(dcMemory, m_strCaption.GetLength()) / 2;    HFONT hFont = CreateFont( rcClient.Height(),nWidth,0,0,FW_NORMAL,0,0,0,DEFAULT_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH, _T("Arial") );    SelectObject( dcMemory, hFont );            SetBkMode( dcMemory, TRANSPARENT );    DrawTextEx(dcMemory, m_strCaption.GetBuffer(), m_strCaption.GetLength(), &rcClient, DT_CENTER | DT_VCENTER | DT_NOCLIP | DT_SINGLELINE, 0 );    BitBlt( dc.m_hDC, 0, 0, rcClient.Width(), rcClient.Height(), dcMemory, 0, 0, SRCCOPY );    DeleteObject( hMemoryBitmap );    DeleteDC( dcMemory );            DeleteObject( hFont );    DeleteObject( hBrushBackground );    DeleteObject( hBrushBorder );    return;} 

读书人网 >VC/MFC

热点推荐