VC6.0中改变控件、对话框字体(大小)

来源:互联网 发布:许巍 完美生活 知乎 编辑:程序博客网 时间:2024/05/16 09:28


1 VC的对话框字体设置对所有控件都有效,你不能单独地改变某个静态文本的字体。需要首先用CreateFont来建立一个字体对象,然后调用控件的SetFont,就可以了。

    例子:

    1、改静态文体的ID,如:IDC_STATIC1

    2、添加一个Edit控件,建立一个关联的控件m_editControl。

3、在OnInitDialog中添加如下代码:

 

     CFont* cFont= new CFont;     cFont->CreatePointFont(g_iFontSize,g_sFontStyle);//创建字体     GetDlgItem(IDC_SFONTEXAMPLE)->SetFont(cFont);  


需要注意的是,这里我们使用的是CFont指针,而不是普通的CFont局部变量, 在非MFC程序,首先用CreateFont来建立一个字体句柄,然后再用SendMessage发给控件WM_SETFONT消息,将建立的字体句柄赋值过去,就可以了。


2 但是整个对话框或窗口的字体的大小,使用对话框或窗口的SetFont()函数却没有任何的作用.可以在初始化时遍历每个控件分别设置来处理,但这里说另一种使用回调函数的简单方法:
   :调用系统的API:::EnumChildWindows(). ,传入回调函数和重新定义的字体.(第一个参数不用管啊,本来就有啊)
)
   1)在文档视图结构中CMainFrame::OnCreate().中调用::EnumChildWindows(). 实现所有窗口和子窗口字体改变
   2) 在对话框的OnInitDialog(). 中调用::EnumChildWindows(). 改变对话窗上的所有控件.

回调函数如下:

BOOL __stdcall SetChildFont(HWND hwnd, LPARAM lparam){
  CFont *pFont = (CFont*)lparam;  CWnd *pWnd = CWnd::FromHandle(hwnd);  pWnd->SetFont(pFont);  return TRUE;}


使用1(CDialog):

BOOL CAboutDlg::OnInitDialog() {    CDialog::OnInitDialog();        // TODO: Add extra initialization here    //***********************改变窗口各控件的字体大小、类型**************************    CFont* cFont= new CFont;    cFont->CreatePointFont(g_iFontSize,g_sFontStyle);//创建字体    ::EnumChildWindows(m_hWnd, ::SetChildFont, (LPARAM)cFont);    //*******************************************************************************        return TRUE;  // return TRUE unless you set the focus to a control                  // EXCEPTION: OCX Property Pages should return FALSE}


使用2(CMainFrame):
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct){    if (CFrameWnd::OnCreate(lpCreateStruct) == -1)        return -1;        if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP        | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||        !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))     {        TRACE0("Failed to create toolbar\n");        return -1;      // fail to create    }    if (!m_wndStatusBar.Create(this) ||        !m_wndStatusBar.SetIndicators(indicators,          sizeof(indicators)/sizeof(UINT)))     {        TRACE0("Failed to create status bar\n");        return -1;      // fail to create    }    m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);    EnableDocking(CBRS_ALIGN_ANY);    DockControlBar(&m_wndToolBar);   <pre name="code" class="cpp">    //***********************改变窗口各控件的字体大小、类型**************************    CFont* cFont= new CFont;    cFont->CreatePointFont(g_iFontSize,g_sFontStyle);//创建字体    ::EnumChildWindows(m_hWnd, ::SetChildFont, (LPARAM)cFont);    //*******************************************************************************
return 0;}

3 如何在mfc中实现,当系统的字体变大的时候,对话框上面的字体也相应的变大?(非常感谢)

//IconFont    LOGFONT logFont;    int  size = sizeof(LOGFONT);    bool isGood = SystemParametersInfo(SPI_GETICONTITLELOGFONT,size,&logFont,0);    if(isGood == true)     {        CFont * f;        f = new CFont;             const LOGFONT* pFont = new LOGFONT(logFont);        f->CreateFontIndirectW(pFont);        //::EnumChildWindows(m_hWnd, ::SetChildFont, (LPARAM)f);    }    //other Font    NONCLIENTMETRICS ncm = new NONCLIENTMETRICS();                    bool isGood = SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), ref ncm, 0);    if (isGood == true)     {        LOGFONT logFont2;        //logFont2=ncm.lfntCaptionFont);//CaptionFont        //logFont2 =ncm.lfntSMCaptionFont;//CaptionFont_Small        //logFont2 = ncm.lfntMenuFont;//MenuFont        //logFont2 = ncm.lfntStatusFont;//StatusFont        logFont2 = ncm.lfntMessageFont;//MessageFont        CFont * f;        f = new CFont;             const LOGFONT* pFont = new LOGFONT(logFont2);        f->CreateFontIndirectW(pFont);        //::EnumChildWindows(m_hWnd, ::SetChildFont, (LPARAM)f);    }

以上是取得系统字体的大小,然后再调用上面的第二种方法!
窗体上的所有字体都会跟着系统字体的大小改变!





0 0
原创粉丝点击