VC++中static text字體改變
窗口都有2個(gè)和字體有關(guān)的函數(shù):
CWnd::GetFont()和SetFont(CFont*, BOOL);
1)CFont* pFont = m_static.GetFont();
2)LOGFONT LogFont;
pFont->GetLogFont(&LogFont);
3)對(duì)LogFont直接操縱修改里面的字體選項(xiàng)//如LogFont.lfUnderline = 1;設(shè)置下劃線
LogFont.lfHeight=30; //字體大小設(shè)置
strcpy(LogFont.lfFaceName, "楷體_GB2312"); //字體設(shè)置
4)pFont->Detach();
第四步的目的是將pFont里裝有的HFONT解除關(guān)聯(lián),否則pFont無法調(diào)用緊接的Create函數(shù)。
5)pFont->CreateFontIndirect(&LogFont);
m_static.SetFont(pFont);
6)pFont->Detach();
必須再一次解除在pFont里裝載的HFONT,原因是第5步已經(jīng)將HFONT賦給了m_static。pFont的任務(wù)已完成,不應(yīng)該持有HFONT資源,它也不能去銷毀HFONT,因?yàn)閙_static在使用這個(gè)HFONT,所以必須是Detach()來解除關(guān)聯(lián)。
VC++中字體顏色的改變
在OnCtlColor函數(shù)中如下代碼:
HBRUSH CDlg_SignIn::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: Change any attributes of the DC here
if(nCtlColor == CTLCOLOR_STATIC)
{
if(pWnd->GetDlgCtrlID()== IDC_REGARD)
{
pDC->SetTextColor(RGB(255,0,0));
pDC->SetBkColor(RGB(251, 247, 200));//設(shè)置文本背景色
pDC->SetBkMode(TRANSPARENT);//設(shè)置背景透明
}
}
// TODO: Return a different brush if the default is not desired
return hbr;
}
其他控件的宏定義為:
CTLCOLOR_BTN 按鈕控件
CTLCOLOR_DLG 對(duì)話框
CTLCOLOR_EDIT 編輯框
CTLCOLOR_LISTBOX 列表控件
CTLCOLOR_MSGBOX 消息控件
CTLCOLOR_SCROLLBAR 滾動(dòng)條控件
CTLCOLOR_STATIC 靜態(tài)控件
VC中動(dòng)態(tài)改變控件和對(duì)話框字體.
1 VC的對(duì)話框字體設(shè)置對(duì)所有控件都有效,你不能單獨(dú)地改變某個(gè)靜態(tài)文本的字體。對(duì)于你的問題,需要首先用CreateFont來建立一個(gè)字體對(duì)象,然后調(diào)用控件的SetFont,就可以了。
例子:
1、改靜態(tài)文體的ID,如:IDC_STATIC1
2、添加一個(gè)Edit控件,建立一個(gè)關(guān)聯(lián)的控件m_editControl。
3、在OnInitDialog中添加如下代碼:
CFont * f;
f = new CFont;
f->CreateFont(16, // nHeight
0, // nWidth
0, // nEscapement
0, // nOrientation
FW_BOLD, // nWeight
TRUE, // bItalic
FALSE, // bUnderline
0, // cStrikeOut
ANSI_CHARSET, // nCharSet
OUT_DEFAULT_PRECIS, // nOutPrecision
CLIP_DEFAULT_PRECIS, // nClipPrecision
DEFAULT_QUALITY, // nQuality
DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily
_T("Arial")); // lpszFac
GetDlgItem(IDC_STATIC1)->SetFont(f);
CWnd *cWnd = GetDlgItem(IDC_STATIC1);
cWnd->SetFont(&font);
cWnd->SetWindowTextW(L"設(shè)置需要的內(nèi)容");
需要注意的是,這里我們使用的是CFont指針,而不是普通的CFont局部變量, 在非MFC程序,首先用CreateFont來建立一個(gè)字體句柄,然后再用SendMessage發(fā)給控件WM_SETFONT消息,將建立的字體句柄賦值過去,就可以了。
2 但是整個(gè)對(duì)話框或窗口的字體的大小,使用對(duì)話框或窗口的SetFont()函數(shù)卻沒有任何的作用.可以在初始化時(shí)遍歷每個(gè)控件分別設(shè)置來處理,但這里說另一種使用回調(diào)函數(shù)的簡(jiǎn)單方法:
:調(diào)用系統(tǒng)的API:::EnumChildWindows(). ,傳入回調(diào)函數(shù)和重新定義的字體.(第一個(gè)參數(shù)不用管啊,本來就有啊)
)
1)在文檔視圖結(jié)構(gòu)中CMainFrame::OnCreate().中調(diào)用::EnumChildWindows(). 實(shí)現(xiàn)所有窗口和子窗口字體改變
2) 在對(duì)話框的OnInitDialog(). 中調(diào)用::EnumChildWindows(). 改變對(duì)話窗上的所有控件.
回調(diào)函數(shù)如下:
/ lParam is a pointer to CFont object
BOOL __stdcall SetChildFont(HWND hwnd, LPARAM lparam)
{
CFont *pFont = (CFont*)lparam;
CWnd *pWnd = CWnd::FromHandle(hwnd);
pWnd->SetFont(pFont);
return TRUE;
}
使用1:
BOOL CAboutDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
::EnumChildWindows(m_hWnd, ::SetChildFont, (LPARAM)g_Font.GetFont());
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
使用2:
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);
::EnumChildWindows(m_hWnd, ::SetChildFont, (LPARAM)g_Font.GetFont());
return 0;
}
(很好用,不像mfc中的那個(gè)垃圾setfont(),設(shè)置了對(duì)話框的沒有一點(diǎn)反應(yīng)!)
3 如何在mfc中實(shí)現(xiàn),當(dāng)系統(tǒng)的字體變大的時(shí)候,對(duì)話框上面的字體也相應(yīng)的變大?(非常感謝)
//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);
}
以上是取得系統(tǒng)字體的大小,然后再調(diào)用上面的第二種方法。
窗體上的所有字體都會(huì)跟著系統(tǒng)字體的大小改變。
聯(lián)系客服