PropertiesWnd.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. // 这段 MFC 示例源代码演示如何使用 MFC Microsoft Office Fluent 用户界面
  2. // (“Fluent UI”)。该示例仅供参考,
  3. // 用以补充《Microsoft 基础类参考》和
  4. // MFC C++ 库软件随附的相关电子文档。
  5. // 复制、使用或分发 Fluent UI 的许可条款是单独提供的。
  6. // 若要了解有关 Fluent UI 许可计划的详细信息,请访问
  7. // https://go.microsoft.com/fwlink/?LinkId=238214.
  8. //
  9. // 版权所有(C) Microsoft Corporation
  10. // 保留所有权利。
  11. #include "stdafx.h"
  12. #include "PropertiesWnd.h"
  13. #include "Resource.h"
  14. #include "MainFrm.h"
  15. #include "IDE.h"
  16. #ifdef _DEBUG
  17. #undef THIS_FILE
  18. static char THIS_FILE[]=__FILE__;
  19. #define new DEBUG_NEW
  20. #endif
  21. /////////////////////////////////////////////////////////////////////////////
  22. // CResourceViewBar
  23. CPropertiesWnd::CPropertiesWnd() noexcept
  24. {
  25. m_nComboHeight = 0;
  26. }
  27. CPropertiesWnd::~CPropertiesWnd()
  28. {
  29. }
  30. BEGIN_MESSAGE_MAP(CPropertiesWnd, CDockablePane)
  31. ON_WM_CREATE()
  32. ON_WM_SIZE()
  33. ON_COMMAND(ID_EXPAND_ALL, OnExpandAllProperties)
  34. ON_UPDATE_COMMAND_UI(ID_EXPAND_ALL, OnUpdateExpandAllProperties)
  35. ON_COMMAND(ID_SORTPROPERTIES, OnSortProperties)
  36. ON_UPDATE_COMMAND_UI(ID_SORTPROPERTIES, OnUpdateSortProperties)
  37. ON_COMMAND(ID_PROPERTIES1, OnProperties1)
  38. ON_UPDATE_COMMAND_UI(ID_PROPERTIES1, OnUpdateProperties1)
  39. ON_COMMAND(ID_PROPERTIES2, OnProperties2)
  40. ON_UPDATE_COMMAND_UI(ID_PROPERTIES2, OnUpdateProperties2)
  41. ON_WM_SETFOCUS()
  42. ON_WM_SETTINGCHANGE()
  43. END_MESSAGE_MAP()
  44. /////////////////////////////////////////////////////////////////////////////
  45. // CResourceViewBar 消息处理程序
  46. void CPropertiesWnd::AdjustLayout()
  47. {
  48. if (GetSafeHwnd () == nullptr || (AfxGetMainWnd() != nullptr && AfxGetMainWnd()->IsIconic()))
  49. {
  50. return;
  51. }
  52. CRect rectClient;
  53. GetClientRect(rectClient);
  54. int cyTlb = m_wndToolBar.CalcFixedLayout(FALSE, TRUE).cy;
  55. m_wndObjectCombo.SetWindowPos(nullptr, rectClient.left, rectClient.top, rectClient.Width(), m_nComboHeight, SWP_NOACTIVATE | SWP_NOZORDER);
  56. m_wndToolBar.SetWindowPos(nullptr, rectClient.left, rectClient.top + m_nComboHeight, rectClient.Width(), cyTlb, SWP_NOACTIVATE | SWP_NOZORDER);
  57. m_wndPropList.SetWindowPos(nullptr, rectClient.left, rectClient.top + m_nComboHeight + cyTlb, rectClient.Width(), rectClient.Height() -(m_nComboHeight+cyTlb), SWP_NOACTIVATE | SWP_NOZORDER);
  58. }
  59. int CPropertiesWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
  60. {
  61. if (CDockablePane::OnCreate(lpCreateStruct) == -1)
  62. return -1;
  63. CRect rectDummy;
  64. rectDummy.SetRectEmpty();
  65. // 创建组合:
  66. const DWORD dwViewStyle = WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST | WS_BORDER | CBS_SORT | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
  67. if (!m_wndObjectCombo.Create(dwViewStyle, rectDummy, this, 1))
  68. {
  69. TRACE0("未能创建属性组合 \n");
  70. return -1; // 未能创建
  71. }
  72. m_wndObjectCombo.AddString(_T("应用程序"));
  73. m_wndObjectCombo.AddString(_T("属性窗口"));
  74. m_wndObjectCombo.SetCurSel(0);
  75. CRect rectCombo;
  76. m_wndObjectCombo.GetClientRect (&rectCombo);
  77. m_nComboHeight = rectCombo.Height();
  78. if (!m_wndPropList.Create(WS_VISIBLE | WS_CHILD, rectDummy, this, 2))
  79. {
  80. TRACE0("未能创建属性网格\n");
  81. return -1; // 未能创建
  82. }
  83. InitPropList();
  84. m_wndToolBar.Create(this, AFX_DEFAULT_TOOLBAR_STYLE, IDR_PROPERTIES);
  85. m_wndToolBar.LoadToolBar(IDR_PROPERTIES, 0, 0, TRUE /* 已锁定*/);
  86. m_wndToolBar.CleanUpLockedImages();
  87. m_wndToolBar.LoadBitmap(theApp.m_bHiColorIcons ? IDB_PROPERTIES_HC : IDR_PROPERTIES, 0, 0, TRUE /* 锁定*/);
  88. m_wndToolBar.SetPaneStyle(m_wndToolBar.GetPaneStyle() | CBRS_TOOLTIPS | CBRS_FLYBY);
  89. m_wndToolBar.SetPaneStyle(m_wndToolBar.GetPaneStyle() & ~(CBRS_GRIPPER | CBRS_SIZE_DYNAMIC | CBRS_BORDER_TOP | CBRS_BORDER_BOTTOM | CBRS_BORDER_LEFT | CBRS_BORDER_RIGHT));
  90. m_wndToolBar.SetOwner(this);
  91. // 所有命令将通过此控件路由,而不是通过主框架路由:
  92. m_wndToolBar.SetRouteCommandsViaFrame(FALSE);
  93. AdjustLayout();
  94. return 0;
  95. }
  96. void CPropertiesWnd::OnSize(UINT nType, int cx, int cy)
  97. {
  98. CDockablePane::OnSize(nType, cx, cy);
  99. AdjustLayout();
  100. }
  101. void CPropertiesWnd::OnExpandAllProperties()
  102. {
  103. m_wndPropList.ExpandAll();
  104. }
  105. void CPropertiesWnd::OnUpdateExpandAllProperties(CCmdUI* /* pCmdUI */)
  106. {
  107. }
  108. void CPropertiesWnd::OnSortProperties()
  109. {
  110. m_wndPropList.SetAlphabeticMode(!m_wndPropList.IsAlphabeticMode());
  111. }
  112. void CPropertiesWnd::OnUpdateSortProperties(CCmdUI* pCmdUI)
  113. {
  114. pCmdUI->SetCheck(m_wndPropList.IsAlphabeticMode());
  115. }
  116. void CPropertiesWnd::OnProperties1()
  117. {
  118. // TODO: 在此处添加命令处理程序代码
  119. }
  120. void CPropertiesWnd::OnUpdateProperties1(CCmdUI* /*pCmdUI*/)
  121. {
  122. // TODO: 在此处添加命令更新 UI 处理程序代码
  123. }
  124. void CPropertiesWnd::OnProperties2()
  125. {
  126. // TODO: 在此处添加命令处理程序代码
  127. }
  128. void CPropertiesWnd::OnUpdateProperties2(CCmdUI* /*pCmdUI*/)
  129. {
  130. // TODO: 在此处添加命令更新 UI 处理程序代码
  131. }
  132. void CPropertiesWnd::InitPropList()
  133. {
  134. SetPropListFont();
  135. m_wndPropList.EnableHeaderCtrl(FALSE);
  136. m_wndPropList.EnableDescriptionArea();
  137. m_wndPropList.SetVSDotNetLook();
  138. m_wndPropList.MarkModifiedProperties();
  139. CMFCPropertyGridProperty* pGroup1 = new CMFCPropertyGridProperty(_T("外观"));
  140. pGroup1->AddSubItem(new CMFCPropertyGridProperty(_T("三维外观"), (_variant_t) false, _T("指定窗口的字体不使用粗体,并且控件将使用三维边框")));
  141. CMFCPropertyGridProperty* pProp = new CMFCPropertyGridProperty(_T("边框"), _T("对话框外框"), _T("其中之一: “无”、“细”、“可调整大小”或“对话框外框”"));
  142. pProp->AddOption(_T("无"));
  143. pProp->AddOption(_T("细"));
  144. pProp->AddOption(_T("可调整大小"));
  145. pProp->AddOption(_T("对话框外框"));
  146. pProp->AllowEdit(FALSE);
  147. pGroup1->AddSubItem(pProp);
  148. pGroup1->AddSubItem(new CMFCPropertyGridProperty(_T("标题"), (_variant_t) _T("关于"), _T("指定窗口标题栏中显示的文本")));
  149. m_wndPropList.AddProperty(pGroup1);
  150. CMFCPropertyGridProperty* pSize = new CMFCPropertyGridProperty(_T("窗口大小"), 0, TRUE);
  151. pProp = new CMFCPropertyGridProperty(_T("高度"), (_variant_t) 250l, _T("指定窗口的高度"));
  152. pProp->EnableSpinControl(TRUE, 50, 300);
  153. pSize->AddSubItem(pProp);
  154. pProp = new CMFCPropertyGridProperty( _T("宽度"), (_variant_t) 150l, _T("指定窗口的宽度"));
  155. pProp->EnableSpinControl(TRUE, 50, 200);
  156. pSize->AddSubItem(pProp);
  157. m_wndPropList.AddProperty(pSize);
  158. CMFCPropertyGridProperty* pGroup2 = new CMFCPropertyGridProperty(_T("字体"));
  159. LOGFONT lf;
  160. CFont* font = CFont::FromHandle((HFONT) GetStockObject(DEFAULT_GUI_FONT));
  161. font->GetLogFont(&lf);
  162. _tcscpy_s(lf.lfFaceName, _T("宋体, Arial"));
  163. pGroup2->AddSubItem(new CMFCPropertyGridFontProperty(_T("字体"), lf, CF_EFFECTS | CF_SCREENFONTS, _T("指定窗口的默认字体")));
  164. pGroup2->AddSubItem(new CMFCPropertyGridProperty(_T("使用系统字体"), (_variant_t) true, _T("指定窗口使用“MS Shell Dlg”字体")));
  165. m_wndPropList.AddProperty(pGroup2);
  166. CMFCPropertyGridProperty* pGroup3 = new CMFCPropertyGridProperty(_T("杂项"));
  167. pProp = new CMFCPropertyGridProperty(_T("(名称)"), _T("应用程序"));
  168. pProp->Enable(FALSE);
  169. pGroup3->AddSubItem(pProp);
  170. CMFCPropertyGridColorProperty* pColorProp = new CMFCPropertyGridColorProperty(_T("窗口颜色"), RGB(210, 192, 254), nullptr, _T("指定默认的窗口颜色"));
  171. pColorProp->EnableOtherButton(_T("其他..."));
  172. pColorProp->EnableAutomaticButton(_T("默认"), ::GetSysColor(COLOR_3DFACE));
  173. pGroup3->AddSubItem(pColorProp);
  174. static const TCHAR szFilter[] = _T("图标文件(*.ico)|*.ico|所有文件(*.*)|*.*||");
  175. pGroup3->AddSubItem(new CMFCPropertyGridFileProperty(_T("图标"), TRUE, _T(""), _T("ico"), 0, szFilter, _T("指定窗口图标")));
  176. pGroup3->AddSubItem(new CMFCPropertyGridFileProperty(_T("文件夹"), _T("c:\\")));
  177. m_wndPropList.AddProperty(pGroup3);
  178. CMFCPropertyGridProperty* pGroup4 = new CMFCPropertyGridProperty(_T("层次结构"));
  179. CMFCPropertyGridProperty* pGroup41 = new CMFCPropertyGridProperty(_T("第一个子级"));
  180. pGroup4->AddSubItem(pGroup41);
  181. CMFCPropertyGridProperty* pGroup411 = new CMFCPropertyGridProperty(_T("第二个子级"));
  182. pGroup41->AddSubItem(pGroup411);
  183. pGroup411->AddSubItem(new CMFCPropertyGridProperty(_T("项 1"), (_variant_t) _T("值 1"), _T("此为说明")));
  184. pGroup411->AddSubItem(new CMFCPropertyGridProperty(_T("项 2"), (_variant_t) _T("值 2"), _T("此为说明")));
  185. pGroup411->AddSubItem(new CMFCPropertyGridProperty(_T("项 3"), (_variant_t) _T("值 3"), _T("此为说明")));
  186. pGroup4->Expand(FALSE);
  187. m_wndPropList.AddProperty(pGroup4);
  188. }
  189. void CPropertiesWnd::OnSetFocus(CWnd* pOldWnd)
  190. {
  191. CDockablePane::OnSetFocus(pOldWnd);
  192. m_wndPropList.SetFocus();
  193. }
  194. void CPropertiesWnd::OnSettingChange(UINT uFlags, LPCTSTR lpszSection)
  195. {
  196. CDockablePane::OnSettingChange(uFlags, lpszSection);
  197. SetPropListFont();
  198. }
  199. void CPropertiesWnd::SetPropListFont()
  200. {
  201. ::DeleteObject(m_fntPropList.Detach());
  202. LOGFONT lf;
  203. afxGlobalData.fontRegular.GetLogFont(&lf);
  204. NONCLIENTMETRICS info;
  205. info.cbSize = sizeof(info);
  206. afxGlobalData.GetNonClientMetrics(info);
  207. lf.lfHeight = info.lfMenuFont.lfHeight;
  208. lf.lfWeight = info.lfMenuFont.lfWeight;
  209. lf.lfItalic = info.lfMenuFont.lfItalic;
  210. m_fntPropList.CreateFontIndirect(&lf);
  211. m_wndPropList.SetFont(&m_fntPropList);
  212. m_wndObjectCombo.SetFont(&m_fntPropList);
  213. }