看了一下你的代码。你的m_cfont不能声明为局部变量,而应该声明为类的成员变量。 你可以在OnInitDialog之前初始化m_cfont,然后使用m_cfont设置控件的字体。注意设置了新的字体之后控件的大小不会变化,你可能需要改变控件的大小以适应新的字体。CFont的析构函数会自动帮你做清除工作。下面是WM_SETFONT的说明 Dialog boxes do not support DS_SETFONT. Applications can set the font for their controls manually. The WM_SETFONT message applies to all controls, not just those in dialog boxes. The best time for the owner of a dialog box control to set the font of the control is when it receives the WM_INITDIALOG message. The application should call the DeleteObject function to delete the font when it is no longer needed; for example, after it destroys the control. The size of the control does not change as a result of receiving this message. To avoid clipping text that does not fit within the boundaries of the control, the application should correct the size of the control window before it sets the font. When a dialog box uses the DS_SETFONT style to set the text in its controls, the system sends the WM_SETFONT message to the dialog box procedure before it creates the controls. An application can create a dialog box that contains the DS_SETFONT style by calling any of the following functions:
:)
CFont textFont;
textFont.CreateFont(100,100,0,0,700,FALSE,FALSE,0,
/*ANSI_CHARSET*/1,OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,
DEFAULT_PITCH|FF_SWISS,"楷体_GB2312");
然后把你需要设置字体的那个控件的字体设置为此字体:
GetDlgItem(IDC_YOUREDIT)->SetFont(&textFont);
或者m_message.SetFont(&textFont);
CFont textFont;
OnInitDialog()函数中
假如你的文本编辑框是IDC_TEXT(一定不要设成IDC_STATIC)
加入以下语句:textFont.CreateFont(100,100,0,0,700,FALSE,FALSE,0,
/*ANSI_CHARSET*/1,OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,
DEFAULT_PITCH¦FF_SWISS,"楷体_GB2312");
//然后把你需要设置字体的那个控件的字体设置为此字体:
GetDlgItem(IDC_TEXT)->SetFont(&textFont);
在Dialog的OnDestroy函数中释放:
textFont.DeleteObject();
你可以在OnInitDialog之前初始化m_cfont,然后使用m_cfont设置控件的字体。注意设置了新的字体之后控件的大小不会变化,你可能需要改变控件的大小以适应新的字体。CFont的析构函数会自动帮你做清除工作。下面是WM_SETFONT的说明
Dialog boxes do not support DS_SETFONT. Applications can set the font for their controls manually. The WM_SETFONT message applies to all controls, not just those in dialog boxes. The best time for the owner of a dialog box control to set the font of the control is when it receives the WM_INITDIALOG message. The application should call the DeleteObject function to delete the font when it is no longer needed; for example, after it destroys the control. The size of the control does not change as a result of receiving this message. To avoid clipping text that does not fit within the boundaries of the control, the application should correct the size of the control window before it sets the font. When a dialog box uses the DS_SETFONT style to set the text in its controls, the system sends the WM_SETFONT message to the dialog box procedure before it creates the controls. An application can create a dialog box that contains the DS_SETFONT style by calling any of the following functions:
把你的FormView的构造函数中的SetFont去掉就可以了。
修改过的代码已经运行成功。
m_cfont.CreateFont(
30, // nHeight
15, // nWidth
0, // nEscapement
0, // nOrientation
FW_NORMAL, // nWeight
FALSE, // bItalic
FALSE, // bUnderline
0, // cStrikeOut
ANSI_CHARSET, // DEFAULT_CHARSET, // nCharSet
OUT_DEFAULT_PRECIS, // nOutPrecision
CLIP_DEFAULT_PRECIS, // nClipPrecision
DEFAULT_QUALITY, // nQuality
DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily
"Arial"); // lpszFacename这段代码摆在哪里?是不是我的CBTreeView类中的OnInitUpdate()函数还是CBTreeView类中的的构造函数?
m_yearbox.SetFont (&m_cfont);
m_monthbox.SetFont (&m_cfont);
m_daybox.SetFont (&m_cfont);
m_nyearbox.SetFont (&m_cfont);
m_nmonthbox.SetFont (&m_cfont);
m_ndaybox.SetFont (&m_cfont);
这段代码摆在哪里?是OnInitUpdate()函数?
//设置当前日期
CTime t = CTime::GetCurrentTime();
m_year=t.GetYear();
m_month=t.GetMonth();
m_day=t.GetDay();
//设置还书日期
if(m_month==12)
{
m_nyear=m_year+1;
m_nmonth=1;
}//else
else {
m_nmonth=m_month+1;
m_nyear=m_year;
}//else
m_nday=m_day;
这段代码摆在哪里?是我的CBTreeView类中的OnInitUpdate()函数还是CBTreeView类中的的构造函数?我希望设置字体后,打开我的btree.dat不会出错,蒋大哥试过了吗?我试过设置字体成功的,但就是遇到打开btree.dat文件出错的问题。蒋大哥如果调试成功了,请把工程发回给我好吗?顺便指出改动的地方和原因,谢谢!
在OnInitUpdate中:
if(HFONT(m_cfont)){
//初始化m_cfont
}
if(!HFONT(m_cfont)){
OnInitUpdate可能调用多次(在每次打开文档的时候)”,我又学会了一点知识。很抱歉没有分加给蒋大哥了,下次补上!
同时也感谢其他给位朋友的帮助!