PropertiesWnd.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. // 这段 MFC 示例源代码演示如何使用 MFC Microsoft Office Fluent 用户界面
  2. // ("Fluent UI"),该示例仅作为参考资料提供,
  3. // 用以补充《Microsoft 基础类参考》和
  4. // MFC C++ 库软件随附的相关电子文档。
  5. // 复制、使用或分发 Fluent UI 的许可条款是单独提供的。
  6. // 若要了解有关 Fluent UI 许可计划的详细信息,请访问
  7. // http://msdn.microsoft.com/officeui。
  8. //
  9. // 版权所有 (C) Microsoft Corporation
  10. // 保留所有权利。
  11. #include "stdafx.h"
  12. #include "PropertiesWnd.h"
  13. #include "Resource.h"
  14. #include "MainFrm.h"
  15. #include "FieldTestTool.h"
  16. #include "filehelper.h"
  17. #define MINIMUM_WINDOWS_X 395
  18. #define MINIMUM_WINDOWS_Y 535
  19. #define MAX_SWITCH_LEN 16
  20. #ifdef _DEBUG
  21. #undef THIS_FILE
  22. static char THIS_FILE[]=__FILE__;
  23. #define new DEBUG_NEW
  24. #endif
  25. unsigned char TwoHexChar2Char(char ch1, char ch2)
  26. {
  27. char Numb1;
  28. char Numb2;
  29. if (ch1 >= 'A')
  30. Numb1 = (toupper(ch1) - '0' - 7) * 16;
  31. else
  32. Numb1 = (ch1 - '0') * 16;
  33. if (ch2 >= 'A')
  34. Numb2 = (toupper(ch2) - '0' - 7);
  35. else
  36. Numb2 = (ch2 - '0');
  37. return (Numb1 + Numb2);
  38. }
  39. COLORREF HexColor2RGB(std::string strHexColor)
  40. {
  41. DWORD r = TwoHexChar2Char(strHexColor[0], strHexColor[1]);
  42. DWORD g = TwoHexChar2Char(strHexColor[2], strHexColor[3]);
  43. DWORD b = TwoHexChar2Char(strHexColor[4], strHexColor[5]);
  44. return RGB(r, g, b);
  45. }
  46. /////////////////////////////////////////////////////////////////////////////
  47. // CResourceViewBar
  48. CPropertiesWnd::CPropertiesWnd()
  49. {
  50. }
  51. CPropertiesWnd::~CPropertiesWnd()
  52. {
  53. }
  54. BEGIN_MESSAGE_MAP(CPropertiesWnd, CDockablePane)
  55. ON_WM_CREATE()
  56. ON_WM_SIZE()
  57. ON_WM_SETFOCUS()
  58. ON_WM_SETTINGCHANGE()
  59. ON_WM_PAINT()
  60. // 信号按钮;
  61. ON_COMMAND_RANGE(BTNID, BTNID + 100, OnSignalBtnClick)
  62. ON_UPDATE_COMMAND_UI_RANGE(BTNID, BTNID + 100, OnUpdateSignalBtnClick)
  63. // 下拉框事件;
  64. ON_CBN_SELCHANGE(1, &CPropertiesWnd::OnCbnSelchangeSingals)
  65. END_MESSAGE_MAP()
  66. /////////////////////////////////////////////////////////////////////////////
  67. // CResourceViewBar 消息处理程序
  68. void CPropertiesWnd::AdjustLayout()
  69. {
  70. if (GetSafeHwnd() == NULL)
  71. {
  72. return;
  73. }
  74. CRect rectClient;
  75. GetClientRect(rectClient);
  76. m_wndSignalCombo.GetWindowRect(&m_rcCombobox);
  77. ScreenToClient(m_rcCombobox);
  78. int cyCmb = m_rcCombobox.Size().cy;
  79. m_wndSignalCombo.SetWindowPos(NULL, rectClient.left, rectClient.top, rectClient.Width(), 200, SWP_NOACTIVATE | SWP_NOZORDER);
  80. TRACE2(_T("更新后的大小:%d,%d\n"), rectClient.Width(), rectClient.Height());
  81. int sx = 0, sy = m_rcCombobox.bottom + m_nTopMargin;
  82. int i = 0, id = 10000;
  83. int nWidth = (rectClient.Width() - 25) / m_nCols;
  84. DWORD dwStyle = WS_CHILD | WS_VISIBLE;
  85. for (std::vector<KeyInfo>::iterator it = m_vtKeyInfo.begin(); it != m_vtKeyInfo.end(); it++)
  86. {
  87. if (it->pbtnInfo->pBTN == NULL)
  88. continue;
  89. if (i != 0 && i % m_nCols == 0)
  90. {//换行;
  91. sy += it->height + m_nRowSpacing;
  92. }
  93. //sx = m_nLeftMargin + (i % m_nCols) * nWidth + (i % m_nCols + 1) * m_nColSpacing;
  94. sx = m_nLeftMargin + (i % m_nCols) * (nWidth + m_nColSpacing);
  95. it->pbtnInfo->pBTN->SetWindowPos(NULL, sx, sy, nWidth, it->height, SWP_NOACTIVATE | SWP_NOZORDER);
  96. i++;
  97. }
  98. }
  99. int CPropertiesWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
  100. {
  101. if (CDockablePane::OnCreate(lpCreateStruct) == -1)
  102. return -1;
  103. CRect rectDummy;
  104. rectDummy.SetRectEmpty();
  105. // 创建组合:
  106. const DWORD dwViewStyle = WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST | WS_BORDER | CBS_SORT | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
  107. if (!m_wndSignalCombo.Create(dwViewStyle|WS_VSCROLL | WS_HSCROLL, rectDummy, this, 1))
  108. {
  109. TRACE0("未能创建属性组合 \n");
  110. return -1; // 未能创建
  111. }
  112. InitPropList();
  113. // 设置最小窗体;
  114. SetMinSize((530, 260));
  115. LoadSignals();
  116. m_wndSignalCombo.SelectString(0, Global::g_Config.twUseSignal.c_str());
  117. LoadKeyInfo(m_vtKeyInfo, Global::g_Config.twSignaldir + _T("\\") + Global::g_Config.twUseSignal + _T(".xml"));
  118. AdjustLayout();
  119. return 0;
  120. }
  121. void CPropertiesWnd::OnSize(UINT nType, int cx, int cy)
  122. {
  123. CDockablePane::OnSize(nType, cx, cy);
  124. AdjustLayout();
  125. }
  126. void CPropertiesWnd::InitPropList()
  127. {
  128. SetPropListFont();
  129. }
  130. void CPropertiesWnd::OnSetFocus(CWnd* pOldWnd)
  131. {
  132. CDockablePane::OnSetFocus(pOldWnd);
  133. }
  134. void CPropertiesWnd::OnSettingChange(UINT uFlags, LPCTSTR lpszSection)
  135. {
  136. CDockablePane::OnSettingChange(uFlags, lpszSection);
  137. SetPropListFont();
  138. }
  139. void CPropertiesWnd::OnSignalBtnClick(UINT uId) // 发送遥控信号;
  140. {
  141. CWnd* pWnd = GetFocus();
  142. int id = pWnd->GetDlgCtrlID();
  143. if (id >= BTNID && id <= BTNID + 100)
  144. {
  145. CString text;
  146. pWnd->GetWindowText(text);
  147. KeyInfo *pKey = m_map_key.find(id)->second;
  148. // 从父窗口获取对象;
  149. CMainFrame *pMainWnd = (CMainFrame*)AfxGetApp()->m_pMainWnd;
  150. if ( pMainWnd )
  151. {
  152. if ( pMainWnd->m_SerialTW.IsOpen() ){
  153. pMainWnd->m_SerialTW.SetTimeouts();
  154. // 封装发送包;
  155. byte szSendData[MAX_PATH] = {0};
  156. _stprintf_s((TCHAR*)szSendData, MAX_PATH, _T("*INPUT %s %s\r"), pKey->strType.c_str(), pKey->strValue.c_str());
  157. // 发送单信号;
  158. if ( pMainWnd->m_SerialTW.WriteSync(szSendData, _tcslen((TCHAR*)szSendData)) )
  159. {
  160. memset(szSendData, 0, MAX_PATH);
  161. pMainWnd->m_SerialTW.ReadSync(szSendData, MAX_PATH);
  162. TRACE("发送单信号成功\n");
  163. }
  164. else
  165. {
  166. TRACE("发送单信号失败\n");
  167. }
  168. }
  169. else
  170. {
  171. MessageBox(_T("The serial port is not open."), _T("Warnning"), MB_OK|MB_ICONWARNING);
  172. }
  173. }
  174. }
  175. }
  176. void CPropertiesWnd::OnUpdateSignalBtnClick(CCmdUI* pCmdUI)
  177. {
  178. pCmdUI->Enable(m_map_key.find(pCmdUI->m_nID)->second->bUse);
  179. }
  180. void CPropertiesWnd::OnCbnSelchangeSingals()
  181. {
  182. // TODO: 在此添加控件通知处理程序代码
  183. CString strSignal;
  184. int nCurSel = m_wndSignalCombo.GetCurSel();
  185. if (nCurSel != CB_ERR)
  186. {
  187. std::string signalpath;
  188. m_wndSignalCombo.GetLBText(nCurSel, strSignal);
  189. // 保存到配置文件中;
  190. Global::g_Config.twUseSignal = strSignal.GetString();
  191. WritePrivateProfileString("TestWizard", "useSignal", strSignal.GetString(), Global::g_szConfig);
  192. SetAllBTNUnUsed();
  193. LoadKeyInfo(m_vtKeyInfo, Global::g_Config.twSignaldir + _T("\\") + Global::g_Config.twUseSignal + _T(".xml"));
  194. }
  195. }
  196. void CPropertiesWnd::SetPropListFont()
  197. {
  198. ::DeleteObject(m_fntPropList.Detach());
  199. LOGFONT lf;
  200. afxGlobalData.fontRegular.GetLogFont(&lf);
  201. NONCLIENTMETRICS info;
  202. info.cbSize = sizeof(info);
  203. afxGlobalData.GetNonClientMetrics(info);
  204. lf.lfHeight = info.lfMenuFont.lfHeight;
  205. lf.lfWeight = info.lfMenuFont.lfWeight;
  206. lf.lfItalic = info.lfMenuFont.lfItalic;
  207. m_fntPropList.CreateFontIndirect(&lf);
  208. }
  209. void CPropertiesWnd::LoadSignals()
  210. {
  211. filehelper fh;
  212. m_vtTWSignal.clear();
  213. fh.getfiles_findout_subfolder(Global::g_Config.twSignaldir.c_str(), _T("*.xml"), &m_vtTWSignal);
  214. m_wndSignalCombo.ResetContent();
  215. for (std::vector<TString>::iterator it = m_vtTWSignal.begin(); it != m_vtTWSignal.end(); it++ )
  216. {
  217. m_wndSignalCombo.AddString(fh.getfilename(it->c_str()).c_str());
  218. }
  219. //AutoSetDroppedWidth(&m_cbSignals);
  220. }
  221. void CPropertiesWnd::LoadKeyInfo(std::vector<KeyInfo> &vtKeyInfo, TString path)
  222. {
  223. // 解析xml;
  224. tinyxml2::XMLDocument doc;
  225. if (tinyxml2::XML_SUCCESS != doc.LoadFile(path.c_str()))
  226. {
  227. return;
  228. }
  229. vtKeyInfo.clear();
  230. std::string data;
  231. tinyxml2::XMLElement* pXmlRoot = NULL;
  232. if ((pXmlRoot = doc.RootElement()) != NULL)
  233. {
  234. if (_tcsicmp(pXmlRoot->Value(), "KeyList") == 0)
  235. {
  236. //////////////////////////////////////////////////////////////////////////
  237. // 获取属性;
  238. m_nCols = _tstoi(pXmlRoot->Attribute("cols") ? pXmlRoot->Attribute("cols") : "3");
  239. m_nRowSpacing = _tstoi(pXmlRoot->Attribute("rowSpacing") ? pXmlRoot->Attribute("rowSpacing") : "5");
  240. m_nColSpacing = _tstoi(pXmlRoot->Attribute("colSpacing") ? pXmlRoot->Attribute("colSpacing") : "5");
  241. m_nTopMargin = _tstoi(pXmlRoot->Attribute("topMargin") ? pXmlRoot->Attribute("topMargin") : "8");
  242. m_nLeftMargin = _tstoi(pXmlRoot->Attribute("leftMargin") ? pXmlRoot->Attribute("leftMargin") : "8");
  243. int nWidth = _tstoi(pXmlRoot->Attribute("with") ? pXmlRoot->Attribute("with") : "58");
  244. int nHeight = _tstoi(pXmlRoot->Attribute("height") ? pXmlRoot->Attribute("height") : "25");
  245. //////////////////////////////////////////////////////////////////////////
  246. // 子项;
  247. tinyxml2::XMLElement* pXmlElent = pXmlRoot->FirstChildElement();
  248. while (pXmlElent)
  249. {
  250. if (_tcsicmp(pXmlElent->Value(), _T("ITEM")) == 0)
  251. {
  252. KeyInfo keyInfo;
  253. // 获取属性值;
  254. keyInfo.width = pXmlElent->Attribute("with") ? _ttol(pXmlElent->Attribute("with")) : nWidth;
  255. keyInfo.height = pXmlElent->Attribute("height") ? _ttol(pXmlElent->Attribute("height")) : nHeight;
  256. keyInfo.color = HexColor2RGB(pXmlElent->Attribute("color") ? pXmlElent->Attribute("color") : "87CEFA");
  257. tinyxml2::XMLElement* pItem = pXmlElent->FirstChildElement();
  258. while (pItem)
  259. {
  260. if (_tcsicmp(pItem->Value(), _T("NAME")) == 0)
  261. {
  262. keyInfo.strName = pItem->GetText();
  263. }
  264. else if (_tcsicmp(pItem->Value(), _T("USE")) == 0)
  265. {
  266. keyInfo.bUse = pItem->BoolText(true);
  267. }
  268. else if (_tcsicmp(pItem->Value(), _T("DESC")) == 0)
  269. {
  270. keyInfo.strDesc = pItem->GetText();
  271. }
  272. else if (_tcsicmp(pItem->Value(), _T("TYPE")) == 0)
  273. {
  274. keyInfo.strType = pItem->GetText();
  275. }
  276. else if (_tcsicmp(pItem->Value(), _T("VALUE")) == 0)
  277. {
  278. keyInfo.strValue = pItem->GetText();
  279. }
  280. pItem = pItem->NextSiblingElement();
  281. }
  282. keyInfo.pbtnInfo = GetUnUseBTN();
  283. if ( keyInfo.pbtnInfo == NULL)
  284. {
  285. BTNInfo *pbtnInfo = new BTNInfo();
  286. pbtnInfo->pBTN = new CMFCButton();
  287. m_vtBTNInfo.push_back(pbtnInfo);
  288. keyInfo.pbtnInfo = pbtnInfo;
  289. }
  290. SetBTNStatus(keyInfo.pbtnInfo, TRUE);
  291. vtKeyInfo.push_back(keyInfo);
  292. }
  293. pXmlElent = pXmlElent->NextSiblingElement();
  294. }
  295. }
  296. }
  297. int sx = 0, sy = m_rcCombobox.bottom + m_nTopMargin;
  298. int i = 0, id = 10000;
  299. DWORD dwStyle = WS_CHILD | WS_VISIBLE;
  300. int maxX = 0, maxY = 0;
  301. for (std::vector<KeyInfo>::iterator it = vtKeyInfo.begin(); it != vtKeyInfo.end(); it++)
  302. {
  303. if (i != 0 && i % m_nCols == 0)
  304. {//换行;
  305. sy += it->height + m_nRowSpacing;
  306. }
  307. sx = (i % m_nCols) * it->width + (i % m_nCols + 1) * m_nColSpacing;
  308. //m_nDefaultWidth = m_nLeftMargin + m_nCols * it->width + m_nCols * m_nColSpacing;
  309. if ( !it->pbtnInfo->bCreated )
  310. {
  311. it->pbtnInfo->pBTN->Create(it->strName.c_str(), dwStyle, CRect(sx, sy, sx + it->width, sy + it->height), this, BTNID + i);
  312. it->pbtnInfo->bCreated = true;
  313. // 设置透明false以显示背景色
  314. it->pbtnInfo->pBTN->m_bTransparent = FALSE;
  315. it->pbtnInfo->pBTN->m_bDontUseWinXPTheme = TRUE;
  316. // 消除单击时黑色内圈;
  317. it->pbtnInfo->pBTN->m_bDrawFocus = FALSE;
  318. // 设置按钮扁平样式;
  319. it->pbtnInfo->pBTN->m_nFlatStyle = CMFCButton::FlatStyle::BUTTONSTYLE_FLAT;
  320. // 设置鼠标形状;
  321. it->pbtnInfo->pBTN->SetMouseCursorHand();
  322. }
  323. else
  324. {
  325. it->pbtnInfo->pBTN->SetWindowText(it->strName.c_str());
  326. }
  327. // 设置背景色;
  328. it->pbtnInfo->pBTN->SetFaceColor(it->color, TRUE);
  329. // 显示窗口;
  330. it->pbtnInfo->pBTN->ShowWindow(SW_SHOW);
  331. // 是否禁用;
  332. it->pbtnInfo->pBTN->EnableWindow(it->bUse);
  333. // 存入map中;
  334. m_map_key.insert(std::pair<UINT, KeyInfo*>(BTNID + i, &*it));
  335. i++;
  336. }
  337. std::vector<KeyInfo>::iterator it = vtKeyInfo.begin();
  338. m_nDefaultHeight = m_rcCombobox.bottom + 15 + (vtKeyInfo.size()/m_nCols + (vtKeyInfo.size()%m_nCols > 0 ? 1 : 0)) * (it->height + m_nRowSpacing);
  339. m_nDefaultHeight = m_nDefaultHeight < MINIMUM_WINDOWS_Y ? MINIMUM_WINDOWS_Y : m_nDefaultHeight;
  340. m_nDefaultWidth = m_nLeftMargin*2 + m_nCols * it->width + m_nCols * m_nColSpacing;
  341. m_nDefaultWidth = m_nDefaultWidth < MINIMUM_WINDOWS_X ? MINIMUM_WINDOWS_X : m_nDefaultWidth;
  342. // 获取标题栏高度;
  343. int nCaptionHeight = 39; //GetSystemMetrics(SM_CYCAPTION);不准 GetWindowRect - GetClientRect
  344. AdjustLayout();
  345. }
  346. void CPropertiesWnd::ConvertOldSignalsToNew(std::string strOldSignal, std::string strNewSignal)
  347. {
  348. typedef struct
  349. {
  350. std::string name;
  351. std::string protocol;
  352. std::string key;
  353. //std::string color;
  354. }OldXML, *pOldXML;
  355. std::vector<OldXML> vtOldXML;
  356. tinyxml2::XMLDocument doc;
  357. if (tinyxml2::XML_SUCCESS != doc.LoadFile(strOldSignal.c_str()))
  358. {
  359. return;
  360. }
  361. tinyxml2::XMLElement* pXmlRoot = NULL;
  362. if ((pXmlRoot = doc.RootElement()) != NULL)
  363. {
  364. if (_tcsicmp(pXmlRoot->Value(), "KeyList") == 0)
  365. {
  366. // 子项;
  367. tinyxml2::XMLElement* pXmlElent = pXmlRoot->FirstChildElement();
  368. while (pXmlElent)
  369. {
  370. if (_tcsicmp(pXmlElent->Value(), _T("ITEM")) == 0)
  371. {
  372. OldXML oldxml;
  373. tinyxml2::XMLElement* pItem = pXmlElent->FirstChildElement();
  374. while (pItem)
  375. {
  376. if (_tcsicmp(pItem->Value(), _T("KeyName")) == 0)
  377. {
  378. oldxml.name = pItem->GetText();
  379. }
  380. else if (_tcsicmp(pItem->Value(), _T("KeyType")) == 0)
  381. {
  382. oldxml.protocol = pItem->GetText();
  383. }
  384. else if (_tcsicmp(pItem->Value(), _T("KeyCode")) == 0)
  385. {
  386. oldxml.key = pItem->GetText();
  387. }
  388. else if (_tcsicmp(pItem->Value(), _T("KeyCode")) == 0)
  389. {
  390. oldxml.key = pItem->GetText();
  391. }
  392. pItem = pItem->NextSiblingElement();
  393. }
  394. vtOldXML.push_back(oldxml);
  395. }
  396. pXmlElent = pXmlElent->NextSiblingElement();
  397. }
  398. }
  399. }
  400. if ( vtOldXML.size() == 0 )
  401. return;
  402. // 创建新的xml文件;
  403. const char* declaration = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
  404. doc.Parse(declaration); // 覆盖之前的xml;
  405. pXmlRoot = doc.NewElement("KeyList");
  406. if ( pXmlRoot )
  407. {
  408. pXmlRoot->SetAttribute(_T("cols"), _T("4"));
  409. pXmlRoot->SetAttribute(_T("with"), _T("60"));
  410. pXmlRoot->SetAttribute(_T("height"), _T("25"));
  411. pXmlRoot->SetAttribute(_T("rowSpacing"), _T("5"));
  412. pXmlRoot->SetAttribute(_T("colSpacing"), _T("3"));
  413. pXmlRoot->SetAttribute(_T("leftMargin"), _T("8"));
  414. pXmlRoot->SetAttribute(_T("topMargin"), _T("8"));
  415. pXmlRoot->SetAttribute(_T("ver"), _T("1.0.0.1"));
  416. pXmlRoot->SetAttribute(_T("desc"), _T("key info"));
  417. doc.InsertEndChild(pXmlRoot);
  418. for (std::vector<OldXML>::iterator it = vtOldXML.begin(); it != vtOldXML.end(); it++ )
  419. {
  420. tinyxml2::XMLElement* pXmlElent = doc.NewElement("ITEM");
  421. if ( pXmlElent )
  422. {
  423. pXmlElent->SetAttribute(_T("color"), _T("87CEFA"));
  424. // 赋值各元素节点;
  425. tinyxml2::XMLElement *pUse = doc.NewElement("USE");
  426. pUse->SetText(1);
  427. pXmlElent->InsertEndChild(pUse);
  428. tinyxml2::XMLElement *pName = doc.NewElement("NAME");
  429. pName->SetText(it->name.c_str());
  430. pXmlElent->InsertEndChild(pName);
  431. tinyxml2::XMLElement *pDesc = doc.NewElement("DESC");
  432. pDesc->SetText(_T(""));
  433. pXmlElent->InsertEndChild(pDesc);
  434. tinyxml2::XMLElement *pType = doc.NewElement("TYPE");
  435. pType->SetText(it->protocol.c_str());
  436. pXmlElent->InsertEndChild(pType);
  437. tinyxml2::XMLElement *pValue = doc.NewElement("VALUE");
  438. pValue->SetText(it->key.c_str());
  439. pXmlElent->InsertEndChild(pValue);
  440. pXmlRoot->InsertEndChild(pXmlElent);
  441. }
  442. }
  443. }
  444. doc.SaveFile(strNewSignal.c_str());
  445. }
  446. BTNInfo *CPropertiesWnd::GetUnUseBTN()
  447. {
  448. for (std::vector<BTNInfo*>::iterator it = m_vtBTNInfo.begin(); it != m_vtBTNInfo.end(); it++ )
  449. {
  450. if ( !(*it)->bUsed )
  451. return *it;
  452. }
  453. return NULL;
  454. }
  455. void CPropertiesWnd::SetBTNStatus(BTNInfo *pbtnInfo, BOOL bStatus /* = TRUE */)
  456. {
  457. if ( pbtnInfo )
  458. {
  459. pbtnInfo->bUsed = bStatus;
  460. }
  461. }
  462. void CPropertiesWnd::SetAllBTNUnUsed()
  463. {
  464. BTNInfo *pbtnInfo = NULL;
  465. for (std::vector<BTNInfo*>::iterator it = m_vtBTNInfo.begin(); it != m_vtBTNInfo.end(); it++ )
  466. {
  467. pbtnInfo = (*it);
  468. if ( pbtnInfo )
  469. {
  470. pbtnInfo->bUsed = false;
  471. if ( pbtnInfo->bCreated )
  472. {
  473. pbtnInfo->pBTN->ShowWindow(SW_HIDE);
  474. }
  475. }
  476. }
  477. }
  478. void CPropertiesWnd::ClearAllBTNInfo()
  479. {
  480. BTNInfo *pbtnInfo = NULL;
  481. for (std::vector<BTNInfo*>::iterator it = m_vtBTNInfo.begin(); it != m_vtBTNInfo.end(); it++ )
  482. {
  483. pbtnInfo = (*it);
  484. if ( pbtnInfo )
  485. {
  486. pbtnInfo->pBTN->DestroyWindow();
  487. delete pbtnInfo->pBTN;
  488. pbtnInfo->pBTN = NULL;
  489. }
  490. delete pbtnInfo;
  491. pbtnInfo = NULL;
  492. }
  493. m_vtBTNInfo.clear();
  494. }
  495. void CPropertiesWnd::AutoSetDroppedWidth(CComboBox* pCombobox)
  496. {
  497. if (!pCombobox)
  498. return;
  499. // Find the longest string in the combo box.
  500. CString str;
  501. CSize sz;
  502. int dx = 0;
  503. TEXTMETRIC tm = { 0 };
  504. CDC* pDC = pCombobox->GetDC();
  505. CFont* pFont = pCombobox->GetFont();
  506. // Select the listbox font, save the old font
  507. CFont* pOldFont = pDC->SelectObject(pFont);
  508. // Get the text metrics for avg char width
  509. pDC->GetTextMetrics(&tm);
  510. for (int i = 0; i < pCombobox->GetCount(); i++)
  511. {
  512. pCombobox->GetLBText(i, str);
  513. sz = pDC->GetTextExtent(str);
  514. // Add the avg width to prevent clipping
  515. sz.cx += tm.tmAveCharWidth;
  516. if (sz.cx > dx)
  517. dx = sz.cx;
  518. }
  519. // Select the old font back into the DC
  520. pDC->SelectObject(pOldFont);
  521. pCombobox->ReleaseDC(pDC);
  522. // Set the horizontal extent so every character of all strings can
  523. // be scrolled to.
  524. pCombobox->SetHorizontalExtent(dx);
  525. }
  526. // 如果CDockablePane无控件,改变大小时背景会刷新失败;
  527. // 所以要重载onpaint;
  528. void CPropertiesWnd::OnPaint()
  529. {
  530. CPaintDC dc(this); // device context for painting
  531. // TODO: 在此处添加消息处理程序代码
  532. // 不为绘图消息调用 CDockablePane::OnPaint()
  533. CRect rc;
  534. GetClientRect(rc);
  535. CBrush brush;
  536. // 获取office风格主题颜色;
  537. // CMFCVisualManagerOffice2007继承于CMFCVisualManager,使用CMFCVisualManager的GetInstance获取父对象指针后转换为CMFCVisualManagerOffice2007对象指针;
  538. brush.CreateSolidBrush(((CMFCVisualManagerOffice2007*)CMFCVisualManagerOffice2007::GetInstance())->GetRibbonEditBackgroundColor(FALSE,FALSE));
  539. dc.FillRect(&rc, &brush);
  540. }