为对话框的控件设置字体不是什么难事c;根据MSDN的介绍c;设置字体最好放在对话框接收到WM_INITDIALOG的时候c;所以可以在OnInitDialog中调用::EnumChildWindows(m_hWnd, ::SetChildFont, (LPARAM)m_font)枚举控件c;并为每个控件发送WM_SETFONT消息。其中回调函数可以简单得定义如下:
但是当这个对话框是一个继承于CFileDialog的自定义类CMyFileDialog时c;会有什么不同吗?首先你会意识到属于CFileDialog的控件都没有得到更新。通过SPY查看窗口句柄c;可以快速得发现问题所在。原来c;CFileDialog模板上的控件并不属于CMyFileDialogc;它们都是CFileDialog的子窗口c;解决方法很简单c;将EnumChildWindows改为::EnumChildWindows(GetParent()->m_hWnd, ::SetChildFont, (LPARAM)m_font)。重新运行来查看效果c;会发现一个有趣的现象:
c="http://hi.csdn.net/attachment/201108/24/0_1314172567QQH9.gif" alt="" />
color:rgb(51,51,51); font-size:14px; line-height:26px">
大多数控件都如我们预期改变了字体c;但用于文件浏览的class="tags" href="/tags/CLASS.html" title=class>class="tags" href="/tags/LISTVIEW.html" title=listview>listview却没有。难道在执行OnInitDialog的时候c;该窗口还没有创建?由于可以明确的是所有的窗口最终都将通过CreateWindowEx进行创建c;故对该API进行跟踪。
1c;首先在VS中填入.SRV*c:/symbol*http://msdl.microsoft.com/download/symbols为class="tags" href="/tags/USER.html" title=user>user32.dll获得调试PDB(关于如何获得和使用符号c;查看这里)
2c;为OnInitDialog设置函数断点并打印在输出窗口
3c;添加函数断点{,,class="tags" href="/tags/USER.html" title=user>user32.dll}_CreateWindowExW@48
4c;设置该断点触发时继续执行c;并将CreateWindowEx:{*(void**)(@ESP+8)}打印到输出窗口中。其中ESP+8是传入CreateWindowEx的第二个参数lpClassName
通过调试c;打印结果如下:
CreateWindowEx:0x771817ec class="tags" href="/tags/CLASS.html" title=class>class="tags" href="/tags/STRING.html" title=string>string L"ComboLBox"
CreateWindowEx:0x77181800 class="tags" href="/tags/CLASS.html" title=class>class="tags" href="/tags/STRING.html" title=string>string L"ComboBox"
CreateWindowEx:0x771817ec class="tags" href="/tags/CLASS.html" title=class>class="tags" href="/tags/STRING.html" title=string>string L"ComboLBox"
CreateWindowEx:0x77181824 class="tags" href="/tags/CLASS.html" title=class>class="tags" href="/tags/STRING.html" title=string>string L"Edit"
CreateWindowEx:0x771817ec class="tags" href="/tags/CLASS.html" title=class>class="tags" href="/tags/STRING.html" title=string>string L"ComboLBox"
CreateWindowEx:0x0000c038
CreateWindowEx:0x77182ad8 _c_szToolbarClass
MyFileDialog::OnInitDialog(void)
CreateWindowEx:0x75efa3e0 class="tags" href="/tags/CLASS.html" title=class>class="tags" href="/tags/STRING.html" title=string>string L"Auto-Suggest Dropdow"...
CreateWindowEx:0x75f0096c class="tags" href="/tags/CLASS.html" title=class>class="tags" href="/tags/STRING.html" title=string>string L"SysListView32"
CreateWindowEx:0x77182a58 _c_szHeaderClass
CreateWindowEx:0x77181a38 class="tags" href="/tags/CLASS.html" title=class>class="tags" href="/tags/STRING.html" title=string>string L"tooltips_class="tags" href="/tags/CLASS.html" title=class>class32"
CreateWindowEx:0x75f0093c class="tags" href="/tags/CLASS.html" title=class>class="tags" href="/tags/STRING.html" title=string>string L"ScrollBar"
CreateWindowEx:0x75f0093c class="tags" href="/tags/CLASS.html" title=class>class="tags" href="/tags/STRING.html" title=string>string L"ScrollBar"
CreateWindowEx:0x7d5a22d0 class="tags" href="/tags/CLASS.html" title=class>class="tags" href="/tags/STRING.html" title=string>string L"SHELLDLL_DefView"
CreateWindowEx:0x7d59aef8 class="tags" href="/tags/CLASS.html" title=class>class="tags" href="/tags/STRING.html" title=string>string L"SysListView32"
CreateWindowEx:0x77181a38 class="tags" href="/tags/CLASS.html" title=class>class="tags" href="/tags/STRING.html" title=string>string L"tooltips_class="tags" href="/tags/CLASS.html" title=class>class32"
CreateWindowEx:0x7d59fa64 class="tags" href="/tags/CLASS.html" title=class>class="tags" href="/tags/STRING.html" title=string>string L"tooltips_class="tags" href="/tags/CLASS.html" title=class>class32"
CreateWindowEx:0x76991b20
CreateWindowEx:0x77182ab4 _c_szSToolTipsClass
CreateWindowEx:0x77182ab4 _c_szSToolTipsClass
结果证实了推测c;作为文件浏览窗口的SHELLDLL_DefViewc;它的创建将在OnInitDialog执行完之后进行。尽管我们将没有办法在OnInitDialog中直接完成设置字体的class="tags" href="/tags/GongZuo.html" title=工作>工作c;但代码仍然可以放置在OnInitDialog中。我们可以使用PostMessage发送自定义消息c;消息的目标函数执行设置字体的class="tags" href="/tags/GongZuo.html" title=工作>工作c;这样消息函数将在所有的窗体创建完毕后得以触发。关于如何处理自定义消息c;这里不在详细描述。