123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941 |
- // 这段 MFC 示例源代码演示如何使用 MFC Microsoft Office Fluent 用户界面
- // ("Fluent UI"),该示例仅作为参考资料提供,
- // 用以补充《Microsoft 基础类参考》和
- // MFC C++ 库软件随附的相关电子文档。
- // 复制、使用或分发 Fluent UI 的许可条款是单独提供的。
- // 若要了解有关 Fluent UI 许可计划的详细信息,请访问
- // http://msdn.microsoft.com/officeui。
- //
- // 版权所有 (C) Microsoft Corporation
- // 保留所有权利。
- #include "stdafx.h"
- #include "PropertiesWnd.h"
- #include "Resource.h"
- #include "MainFrm.h"
- #include "FieldTestTool.h"
- #include "filehelper.h"
- #define MINIMUM_WINDOWS_X 395
- #define MINIMUM_WINDOWS_Y 535
- #define MAX_SWITCH_LEN 16
- #define MAX_SENDDATA 512
- #define TMSG_QUITE_THREAD 9
- #define TMSG_START_LEARN 10
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #define new DEBUG_NEW
- #endif
- unsigned char TwoHexChar2Char(char ch1, char ch2)
- {
- char Numb1;
- char Numb2;
- if (ch1 >= 'A')
- Numb1 = (toupper(ch1) - '0' - 7) * 16;
- else
- Numb1 = (ch1 - '0') * 16;
- if (ch2 >= 'A')
- Numb2 = (toupper(ch2) - '0' - 7);
- else
- Numb2 = (ch2 - '0');
- return (Numb1 + Numb2);
- }
- COLORREF HexColor2RGB(std::string strHexColor)
- {
- DWORD r = TwoHexChar2Char(strHexColor[0], strHexColor[1]);
- DWORD g = TwoHexChar2Char(strHexColor[2], strHexColor[3]);
- DWORD b = TwoHexChar2Char(strHexColor[4], strHexColor[5]);
- return RGB(r, g, b);
- }
- std::string RGBColor2Hex(DWORD dwRGB)
- {
- DWORD r = GetRValue(dwRGB);
- DWORD g = GetGValue(dwRGB);
- DWORD b = GetBValue(dwRGB);
- TCHAR szHex[8] = { 0 };
- _stprintf_s(szHex, _T("%02X%02X%02X"), r, g, b);
- return std::string(szHex);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CResourceViewBar
- KeyInfo* CPropertiesWnd::m_pKeyInfo = NULL;
- CPropertiesWnd::CPropertiesWnd()
- {
- m_dwThreadId = 0;
- }
- CPropertiesWnd::~CPropertiesWnd()
- {
- }
- BEGIN_MESSAGE_MAP(CPropertiesWnd, CDockablePane)
- ON_WM_CREATE()
- ON_WM_SIZE()
- ON_WM_SETFOCUS()
- ON_WM_SETTINGCHANGE()
- ON_WM_PAINT()
- // 信号按钮;
- ON_COMMAND_RANGE(BTNID, BTNID + 100, OnSignalBtnClick)
- ON_UPDATE_COMMAND_UI_RANGE(BTNID, BTNID + 100, OnUpdateSignalBtnClick)
- // 下拉框事件;
- ON_CBN_SELCHANGE(1, &CPropertiesWnd::OnCbnSelchangeSingals)
- ON_WM_DESTROY()
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CResourceViewBar 消息处理程序
- void CPropertiesWnd::AdjustLayout()
- {
- if (GetSafeHwnd() == NULL)
- {
- return;
- }
- CRect rectClient;
- GetClientRect(rectClient);
- m_wndSignalCombo.GetWindowRect(&m_rcCombobox);
- ScreenToClient(m_rcCombobox);
- int cyCmb = m_rcCombobox.Size().cy;
- m_wndSignalCombo.SetWindowPos(NULL, rectClient.left, rectClient.top, rectClient.Width(), 200, SWP_NOACTIVATE | SWP_NOZORDER);
- TRACE2(_T("更新后的大小:%d,%d\n"), rectClient.Width(), rectClient.Height());
- int sx = 0, sy = m_rcCombobox.bottom + m_nTopMargin;
- int i = 0, id = 10000;
- int nWidth = (rectClient.Width() - 25) / m_nCols;
- DWORD dwStyle = WS_CHILD | WS_VISIBLE;
- for (std::vector<KeyInfo>::iterator it = m_vtKeyInfo.begin(); it != m_vtKeyInfo.end(); it++)
- {
- if (it->pbtnInfo->pBTN == NULL)
- continue;
- if (i != 0 && i % m_nCols == 0)
- {//换行;
- sy += it->height + m_nRowSpacing;
- }
- //sx = m_nLeftMargin + (i % m_nCols) * nWidth + (i % m_nCols + 1) * m_nColSpacing;
- sx = m_nLeftMargin + (i % m_nCols) * (nWidth + m_nColSpacing);
- it->pbtnInfo->pBTN->SetWindowPos(NULL, sx, sy, nWidth, it->height, SWP_NOACTIVATE | SWP_NOZORDER);
- i++;
- }
- }
- int CPropertiesWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CDockablePane::OnCreate(lpCreateStruct) == -1)
- return -1;
- CRect rectDummy;
- rectDummy.SetRectEmpty();
- // 创建组合:
- const DWORD dwViewStyle = WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST | WS_BORDER | CBS_SORT | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
- if (!m_wndSignalCombo.Create(dwViewStyle | WS_VSCROLL | WS_HSCROLL, rectDummy, this, 1))
- {
- TRACE0("未能创建属性组合 \n");
- return -1; // 未能创建
- }
- InitPropList();
- // 设置最小窗体;
- SetMinSize((530, 260));
- LoadSignals();
- m_wndSignalCombo.SelectString(0, Global::g_Config.twUseSignal.c_str());
- m_strXMLPath = Global::g_Config.twSignaldir + _T("\\") + Global::g_Config.twUseSignal + _T(".xml");
- LoadKeyInfo(m_vtKeyInfo, m_strXMLPath);
- AdjustLayout();
- return 0;
- }
- void CPropertiesWnd::OnSize(UINT nType, int cx, int cy)
- {
- CDockablePane::OnSize(nType, cx, cy);
- AdjustLayout();
- }
- void CPropertiesWnd::InitPropList()
- {
- SetPropListFont();
- }
- void CPropertiesWnd::OnSetFocus(CWnd* pOldWnd)
- {
- CDockablePane::OnSetFocus(pOldWnd);
- }
- void CPropertiesWnd::OnSettingChange(UINT uFlags, LPCTSTR lpszSection)
- {
- CDockablePane::OnSettingChange(uFlags, lpszSection);
- SetPropListFont();
- }
- void CPropertiesWnd::OnSignalBtnClick(UINT uId) // 发送遥控信号;
- {
- CWnd* pWnd = GetFocus();
- int id = pWnd->GetDlgCtrlID();
- if (id >= BTNID && id <= BTNID + 100)
- {
- CString text;
- pWnd->GetWindowText(text);
- KeyInfo* pKey = m_map_key.find(id)->second;
- // 发送红外信号或学习;
- Global::g_bLearn ? LearnInfraedSignal(uId, pKey) : SendInfraedSignal(pKey);
- }
- }
- void CPropertiesWnd::OnUpdateSignalBtnClick(CCmdUI* pCmdUI)
- {
- pCmdUI->Enable(m_map_key.find(pCmdUI->m_nID)->second->bUse);
- }
- void CPropertiesWnd::OnCbnSelchangeSingals()
- {
- // TODO: 在此添加控件通知处理程序代码
- CString strSignal;
- int nCurSel = m_wndSignalCombo.GetCurSel();
- if (nCurSel != CB_ERR)
- {
- std::string signalpath;
- m_wndSignalCombo.GetLBText(nCurSel, strSignal);
- // 保存到配置文件中;
- Global::g_Config.twUseSignal = strSignal.GetString();
- WritePrivateProfileString("TestWizard", "useSignal", strSignal.GetString(), Global::g_szConfig);
- SetAllBTNUnUsed();
- m_strXMLPath = Global::g_Config.twSignaldir + _T("\\") + Global::g_Config.twUseSignal + _T(".xml");
- LoadKeyInfo(m_vtKeyInfo, m_strXMLPath);
- }
- }
- void CPropertiesWnd::SetPropListFont()
- {
- ::DeleteObject(m_fntPropList.Detach());
- LOGFONT lf;
- afxGlobalData.fontRegular.GetLogFont(&lf);
- NONCLIENTMETRICS info;
- info.cbSize = sizeof(info);
- afxGlobalData.GetNonClientMetrics(info);
- lf.lfHeight = info.lfMenuFont.lfHeight;
- lf.lfWeight = info.lfMenuFont.lfWeight;
- lf.lfItalic = info.lfMenuFont.lfItalic;
- m_fntPropList.CreateFontIndirect(&lf);
- }
- void CPropertiesWnd::LoadSignals()
- {
- filehelper fh;
- m_vtTWSignal.clear();
- fh.getfiles_findout_subfolder(Global::g_Config.twSignaldir.c_str(), _T("*.xml"), &m_vtTWSignal);
- m_wndSignalCombo.ResetContent();
- for (std::vector<TString>::iterator it = m_vtTWSignal.begin(); it != m_vtTWSignal.end(); it++)
- {
- m_wndSignalCombo.AddString(fh.getfilename(it->c_str()).c_str());
- }
- //AutoSetDroppedWidth(&m_cbSignals);
- }
- void CPropertiesWnd::LoadKeyInfo(std::vector<KeyInfo>& vtKeyInfo, TString path)
- {
- // 解析xml;
- tinyxml2::XMLDocument doc;
- if (tinyxml2::XML_SUCCESS != doc.LoadFile(path.c_str()))
- {
- return;
- }
- vtKeyInfo.clear();
- std::string data;
- tinyxml2::XMLElement* pXmlRoot = NULL;
- if ((pXmlRoot = doc.RootElement()) != NULL)
- {
- if (_tcsicmp(pXmlRoot->Value(), "KeyList") == 0)
- {
- //////////////////////////////////////////////////////////////////////////
- // 获取属性;
- m_curKeyLayout.cols = pXmlRoot->Attribute("cols") ? pXmlRoot->Attribute("cols") : "3";
- m_curKeyLayout.with = pXmlRoot->Attribute("with") ? pXmlRoot->Attribute("with") : "58";
- m_curKeyLayout.height = pXmlRoot->Attribute("height") ? pXmlRoot->Attribute("height") : "25";
- m_curKeyLayout.rowSpacing = pXmlRoot->Attribute("rowSpacing") ? pXmlRoot->Attribute("rowSpacing") : "5";
- m_curKeyLayout.colSpacing = pXmlRoot->Attribute("colSpacing") ? pXmlRoot->Attribute("colSpacing") : "5";
- m_curKeyLayout.leftMargin = pXmlRoot->Attribute("leftMargin") ? pXmlRoot->Attribute("leftMargin") : "8";
- m_curKeyLayout.topMargin = pXmlRoot->Attribute("topMargin") ? pXmlRoot->Attribute("topMargin") : "8";
- m_curKeyLayout.ver = pXmlRoot->Attribute("ver") ? pXmlRoot->Attribute("ver") : "1.0.0.1";
- m_curKeyLayout.desc = pXmlRoot->Attribute("desc") ? pXmlRoot->Attribute("desc") : "key info";
- m_nCols = _tstoi(m_curKeyLayout.cols.c_str());
- m_nRowSpacing = _tstoi(m_curKeyLayout.rowSpacing.c_str());
- m_nColSpacing = _tstoi(m_curKeyLayout.colSpacing.c_str());
- m_nTopMargin = _tstoi(m_curKeyLayout.topMargin.c_str());
- m_nLeftMargin = _tstoi(m_curKeyLayout.leftMargin.c_str());
- int nWidth = _tstoi(m_curKeyLayout.with.c_str());
- int nHeight = _tstoi(m_curKeyLayout.height.c_str());
- //////////////////////////////////////////////////////////////////////////
- // 子项;
- tinyxml2::XMLElement* pXmlElent = pXmlRoot->FirstChildElement();
- while (pXmlElent)
- {
- if (_tcsicmp(pXmlElent->Value(), _T("ITEM")) == 0)
- {
- KeyInfo keyInfo;
- // 获取属性值;
- keyInfo.width = pXmlElent->Attribute("with") ? _ttol(pXmlElent->Attribute("with")) : nWidth;
- keyInfo.height = pXmlElent->Attribute("height") ? _ttol(pXmlElent->Attribute("height")) : nHeight;
- keyInfo.color = HexColor2RGB(pXmlElent->Attribute("color") ? pXmlElent->Attribute("color") : "87CEFA");
- tinyxml2::XMLElement* pItem = pXmlElent->FirstChildElement();
- while (pItem)
- {
- if (_tcsicmp(pItem->Value(), _T("NAME")) == 0)
- {
- keyInfo.strName = pItem->GetText();
- }
- else if (_tcsicmp(pItem->Value(), _T("USE")) == 0)
- {
- keyInfo.bUse = pItem->BoolText(true);
- }
- else if (_tcsicmp(pItem->Value(), _T("DESC")) == 0)
- {
- keyInfo.strDesc = pItem->GetText();
- }
- else if (_tcsicmp(pItem->Value(), _T("TYPE")) == 0)
- {
- keyInfo.strType = pItem->GetText();
- }
- else if (_tcsicmp(pItem->Value(), _T("VALUE")) == 0)
- {
- keyInfo.strValue = pItem->GetText();
- }
- else if (_tcsicmp(pItem->Value(), _T("VALUE2")) == 0)
- {
- keyInfo.strValue2 = pItem->GetText();
- }
- else if (_tcsicmp(pItem->Value(), _T("VALUE3")) == 0)
- {
- keyInfo.strValue3 = pItem->GetText();
- }
- else if (_tcsicmp(pItem->Value(), _T("VALUE4")) == 0)
- {
- keyInfo.strValue4 = pItem->GetText();
- }
- pItem = pItem->NextSiblingElement();
- }
- keyInfo.pbtnInfo = GetUnUseBTN();
- if (keyInfo.pbtnInfo == NULL)
- {
- BTNInfo* pbtnInfo = new BTNInfo();
- pbtnInfo->pBTN = new CMFCButton();
- m_vtBTNInfo.push_back(pbtnInfo);
- keyInfo.pbtnInfo = pbtnInfo;
- }
- SetBTNStatus(keyInfo.pbtnInfo, TRUE);
- vtKeyInfo.push_back(keyInfo);
- }
- pXmlElent = pXmlElent->NextSiblingElement();
- }
- }
- }
- int sx = 0, sy = m_rcCombobox.bottom + m_nTopMargin;
- int i = 0, id = 10000;
- DWORD dwStyle = WS_CHILD | WS_VISIBLE;
- int maxX = 0, maxY = 0;
- for (std::vector<KeyInfo>::iterator it = vtKeyInfo.begin(); it != vtKeyInfo.end(); it++)
- {
- if (i != 0 && i % m_nCols == 0)
- {//换行;
- sy += it->height + m_nRowSpacing;
- }
- sx = (i % m_nCols) * it->width + (i % m_nCols + 1) * m_nColSpacing;
- //m_nDefaultWidth = m_nLeftMargin + m_nCols * it->width + m_nCols * m_nColSpacing;
- if (!it->pbtnInfo->bCreated)
- {
- it->pbtnInfo->pBTN->Create(it->strName.c_str(), dwStyle, CRect(sx, sy, sx + it->width, sy + it->height), this, BTNID + i);
- it->pbtnInfo->bCreated = true;
- // 设置透明false以显示背景色
- it->pbtnInfo->pBTN->m_bTransparent = FALSE;
- it->pbtnInfo->pBTN->m_bDontUseWinXPTheme = TRUE;
- // 消除单击时黑色内圈;
- it->pbtnInfo->pBTN->m_bDrawFocus = FALSE;
- // 设置按钮扁平样式;
- it->pbtnInfo->pBTN->m_nFlatStyle = CMFCButton::FlatStyle::BUTTONSTYLE_FLAT;
- // 设置鼠标形状;
- it->pbtnInfo->pBTN->SetMouseCursorHand();
- }
- else
- {
- it->pbtnInfo->pBTN->SetWindowText(it->strName.c_str());
- }
- // 设置背景色;
- it->pbtnInfo->pBTN->SetFaceColor(it->color, TRUE);
- // 显示窗口;
- it->pbtnInfo->pBTN->ShowWindow(SW_SHOW);
- // 是否禁用;
- it->pbtnInfo->pBTN->EnableWindow(it->bUse);
- // 存入map中;
- m_map_key.insert(std::pair<UINT, KeyInfo*>(BTNID + i, &*it));
- i++;
- }
- std::vector<KeyInfo>::iterator it = vtKeyInfo.begin();
- m_nDefaultHeight = m_rcCombobox.bottom + 15 + (vtKeyInfo.size() / m_nCols + (vtKeyInfo.size() % m_nCols > 0 ? 1 : 0)) * (it->height + m_nRowSpacing);
- m_nDefaultHeight = m_nDefaultHeight < MINIMUM_WINDOWS_Y ? MINIMUM_WINDOWS_Y : m_nDefaultHeight;
- m_nDefaultWidth = m_nLeftMargin * 2 + m_nCols * it->width + m_nCols * m_nColSpacing;
- m_nDefaultWidth = m_nDefaultWidth < MINIMUM_WINDOWS_X ? MINIMUM_WINDOWS_X : m_nDefaultWidth;
- // 获取标题栏高度;
- int nCaptionHeight = 39; //GetSystemMetrics(SM_CYCAPTION);不准 GetWindowRect - GetClientRect
- AdjustLayout();
- }
- void CPropertiesWnd::SaveKeyInfo()
- {
- tinyxml2::XMLDocument doc;
- tinyxml2::XMLElement* pXmlRoot = NULL;
- // 创建新的xml文件;
- const char* declaration = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
- doc.Parse(declaration); // 覆盖之前的xml;
- pXmlRoot = doc.NewElement("KeyList");
- if (pXmlRoot)
- {
- pXmlRoot->SetAttribute(_T("cols"), m_curKeyLayout.cols.c_str());
- pXmlRoot->SetAttribute(_T("with"), m_curKeyLayout.with.c_str());
- pXmlRoot->SetAttribute(_T("height"), m_curKeyLayout.height.c_str());
- pXmlRoot->SetAttribute(_T("rowSpacing"), m_curKeyLayout.rowSpacing.c_str());
- pXmlRoot->SetAttribute(_T("colSpacing"), m_curKeyLayout.colSpacing.c_str());
- pXmlRoot->SetAttribute(_T("leftMargin"), m_curKeyLayout.leftMargin.c_str());
- pXmlRoot->SetAttribute(_T("topMargin"), m_curKeyLayout.topMargin.c_str());
- pXmlRoot->SetAttribute(_T("ver"), m_curKeyLayout.ver.c_str());
- pXmlRoot->SetAttribute(_T("desc"), m_curKeyLayout.desc.c_str());
- doc.InsertEndChild(pXmlRoot);
- for (std::vector<KeyInfo>::iterator it = m_vtKeyInfo.begin(); it != m_vtKeyInfo.end(); it++)
- {
- tinyxml2::XMLElement* pXmlElent = doc.NewElement("ITEM");
- if (pXmlElent)
- {
- pXmlElent->SetAttribute(_T("color"), RGBColor2Hex(it->color).c_str());
- // 赋值各元素节点;
- tinyxml2::XMLElement* pUse = doc.NewElement("USE");
- pUse->SetText(1);
- pXmlElent->InsertEndChild(pUse);
- tinyxml2::XMLElement* pName = doc.NewElement("NAME");
- pName->SetText(it->strName.c_str());
- pXmlElent->InsertEndChild(pName);
- tinyxml2::XMLElement* pDesc = doc.NewElement("DESC");
- pDesc->SetText(it->strDesc.c_str());
- pXmlElent->InsertEndChild(pDesc);
- tinyxml2::XMLElement* pType = doc.NewElement("TYPE");
- pType->SetText(it->strType.c_str());
- pXmlElent->InsertEndChild(pType);
- tinyxml2::XMLElement* pValue = doc.NewElement("VALUE");
- pValue->SetText(it->strValue.c_str());
- pXmlElent->InsertEndChild(pValue);
- tinyxml2::XMLElement* pValue2 = doc.NewElement("VALUE2");
- pValue2->SetText(it->strValue2.c_str());
- pXmlElent->InsertEndChild(pValue2);
- tinyxml2::XMLElement* pValue3 = doc.NewElement("VALUE3");
- pValue3->SetText(it->strValue3.c_str());
- pXmlElent->InsertEndChild(pValue3);
- tinyxml2::XMLElement* pValue4 = doc.NewElement("VALUE4");
- pValue4->SetText(it->strValue4.c_str());
- pXmlElent->InsertEndChild(pValue4);
- pXmlRoot->InsertEndChild(pXmlElent);
- }
- }
- }
- doc.SaveFile(m_strXMLPath.c_str());
- }
- void CPropertiesWnd::ConvertOldSignalsToNew(std::string strOldSignal, std::string strNewSignal)
- {
- typedef struct
- {
- std::string name;
- std::string protocol;
- std::string key;
- //std::string color;
- }OldXML, * pOldXML;
- std::vector<OldXML> vtOldXML;
- tinyxml2::XMLDocument doc;
- if (tinyxml2::XML_SUCCESS != doc.LoadFile(strOldSignal.c_str()))
- {
- return;
- }
- tinyxml2::XMLElement* pXmlRoot = NULL;
- if ((pXmlRoot = doc.RootElement()) != NULL)
- {
- if (_tcsicmp(pXmlRoot->Value(), "KeyList") == 0)
- {
- // 子项;
- tinyxml2::XMLElement* pXmlElent = pXmlRoot->FirstChildElement();
- while (pXmlElent)
- {
- if (_tcsicmp(pXmlElent->Value(), _T("ITEM")) == 0)
- {
- OldXML oldxml;
- tinyxml2::XMLElement* pItem = pXmlElent->FirstChildElement();
- while (pItem)
- {
- if (_tcsicmp(pItem->Value(), _T("KeyName")) == 0)
- {
- oldxml.name = pItem->GetText();
- }
- else if (_tcsicmp(pItem->Value(), _T("KeyType")) == 0)
- {
- oldxml.protocol = pItem->GetText();
- }
- else if (_tcsicmp(pItem->Value(), _T("KeyCode")) == 0)
- {
- oldxml.key = pItem->GetText();
- }
- else if (_tcsicmp(pItem->Value(), _T("HexKey")) == 0)
- {
- oldxml.key = pItem->GetText();
- }
- pItem = pItem->NextSiblingElement();
- }
- vtOldXML.push_back(oldxml);
- }
- pXmlElent = pXmlElent->NextSiblingElement();
- }
- }
- }
- if (vtOldXML.size() == 0)
- return;
- // 创建新的xml文件;
- const char* declaration = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
- doc.Parse(declaration); // 覆盖之前的xml;
- pXmlRoot = doc.NewElement("KeyList");
- if (pXmlRoot)
- {
- pXmlRoot->SetAttribute(_T("cols"), _T("4"));
- pXmlRoot->SetAttribute(_T("with"), _T("60"));
- pXmlRoot->SetAttribute(_T("height"), _T("25"));
- pXmlRoot->SetAttribute(_T("rowSpacing"), _T("5"));
- pXmlRoot->SetAttribute(_T("colSpacing"), _T("3"));
- pXmlRoot->SetAttribute(_T("leftMargin"), _T("8"));
- pXmlRoot->SetAttribute(_T("topMargin"), _T("8"));
- pXmlRoot->SetAttribute(_T("ver"), _T("1.0.0.1"));
- pXmlRoot->SetAttribute(_T("desc"), _T("key info"));
- doc.InsertEndChild(pXmlRoot);
- for (std::vector<OldXML>::iterator it = vtOldXML.begin(); it != vtOldXML.end(); it++)
- {
- tinyxml2::XMLElement* pXmlElent = doc.NewElement("ITEM");
- if (pXmlElent)
- {
- pXmlElent->SetAttribute(_T("color"), _T("87CEFA"));
- // 赋值各元素节点;
- tinyxml2::XMLElement* pUse = doc.NewElement("USE");
- pUse->SetText(1);
- pXmlElent->InsertEndChild(pUse);
- tinyxml2::XMLElement* pName = doc.NewElement("NAME");
- pName->SetText(it->name.c_str());
- pXmlElent->InsertEndChild(pName);
- tinyxml2::XMLElement* pDesc = doc.NewElement("DESC");
- pDesc->SetText(_T(""));
- pXmlElent->InsertEndChild(pDesc);
- tinyxml2::XMLElement* pType = doc.NewElement("TYPE");
- pType->SetText(it->protocol.c_str());
- pXmlElent->InsertEndChild(pType);
- tinyxml2::XMLElement* pValue = doc.NewElement("VALUE");
- pValue->SetText(it->key.c_str());
- pXmlElent->InsertEndChild(pValue);
- pXmlRoot->InsertEndChild(pXmlElent);
- }
- }
- }
- doc.SaveFile(strNewSignal.c_str());
- }
- BTNInfo* CPropertiesWnd::GetUnUseBTN()
- {
- for (std::vector<BTNInfo*>::iterator it = m_vtBTNInfo.begin(); it != m_vtBTNInfo.end(); it++)
- {
- if (!(*it)->bUsed)
- return *it;
- }
- return NULL;
- }
- void CPropertiesWnd::SetBTNStatus(BTNInfo* pbtnInfo, BOOL bStatus /* = TRUE */)
- {
- if (pbtnInfo)
- {
- pbtnInfo->bUsed = bStatus;
- }
- }
- void CPropertiesWnd::SetAllBTNUnUsed()
- {
- BTNInfo* pbtnInfo = NULL;
- for (std::vector<BTNInfo*>::iterator it = m_vtBTNInfo.begin(); it != m_vtBTNInfo.end(); it++)
- {
- pbtnInfo = (*it);
- if (pbtnInfo)
- {
- pbtnInfo->bUsed = false;
- if (pbtnInfo->bCreated)
- {
- pbtnInfo->pBTN->ShowWindow(SW_HIDE);
- }
- }
- }
- }
- void CPropertiesWnd::ClearAllBTNInfo()
- {
- BTNInfo* pbtnInfo = NULL;
- for (std::vector<BTNInfo*>::iterator it = m_vtBTNInfo.begin(); it != m_vtBTNInfo.end(); it++)
- {
- pbtnInfo = (*it);
- if (pbtnInfo)
- {
- pbtnInfo->pBTN->DestroyWindow();
- delete pbtnInfo->pBTN;
- pbtnInfo->pBTN = NULL;
- }
- delete pbtnInfo;
- pbtnInfo = NULL;
- }
- m_vtBTNInfo.clear();
- }
- void CPropertiesWnd::AutoSetDroppedWidth(CComboBox* pCombobox)
- {
- if (!pCombobox)
- return;
- // Find the longest string in the combo box.
- CString str;
- CSize sz;
- int dx = 0;
- TEXTMETRIC tm = { 0 };
- CDC* pDC = pCombobox->GetDC();
- CFont* pFont = pCombobox->GetFont();
- // Select the listbox font, save the old font
- CFont* pOldFont = pDC->SelectObject(pFont);
- // Get the text metrics for avg char width
- pDC->GetTextMetrics(&tm);
- for (int i = 0; i < pCombobox->GetCount(); i++)
- {
- pCombobox->GetLBText(i, str);
- sz = pDC->GetTextExtent(str);
- // Add the avg width to prevent clipping
- sz.cx += tm.tmAveCharWidth;
- if (sz.cx > dx)
- dx = sz.cx;
- }
- // Select the old font back into the DC
- pDC->SelectObject(pOldFont);
- pCombobox->ReleaseDC(pDC);
- // Set the horizontal extent so every character of all strings can
- // be scrolled to.
- pCombobox->SetHorizontalExtent(dx);
- }
- bool CPropertiesWnd::SendInfraedSignal(const KeyInfo* pKey)
- {
- CMainFrame* pMainWnd = (CMainFrame*)AfxGetApp()->m_pMainWnd;
- if (!pMainWnd)
- return false;
- if (!pMainWnd->m_SerialTW.IsOpen())
- {
- MessageBox(_T("The serial port is not open."), _T("Warnning"), MB_OK | MB_ICONWARNING);
- return false;
- }
- DWORD dwSendLen = 0;
- byte szSendData[MAX_SENDDATA] = { 0 };
- // 根据不同的版本号来发送;
- if (Global::g_nVersion == 1)
- {
- _stprintf_s((TCHAR*)szSendData, MAX_SENDDATA, _T("*INPUT %s %s\r"), pKey->strType.c_str(), pKey->strValue.c_str());
- dwSendLen = _tcslen((TCHAR*)szSendData);
- }
- else if (Global::g_nVersion == 2)
- {
- // 将十六进制的字符串转成字节流;
- std::string strBytes = utils::HexStringToBytes(pKey->strValue2, 2);
- memcpy_s(szSendData, MAX_SENDDATA, strBytes.c_str(), strBytes.size());
- dwSendLen = strBytes.size();
- }
- // 发送单信号;
- if (pMainWnd->m_SerialTW.WriteSync(szSendData, dwSendLen))
- {
- memset(szSendData, 0, MAX_SENDDATA);
- pMainWnd->m_SerialTW.ReadSync(szSendData, MAX_SENDDATA);
- TRACE("发送单信号成功\n");
- return true;
- }
- else
- {
- TRACE("发送单信号失败\n");
- }
- return false;
- }
- void CPropertiesWnd::LearnInfraedSignal(UINT uId, KeyInfo* pKey)
- {
- m_pKeyInfo = pKey;
- CMainFrame* pMainWnd = (CMainFrame*)AfxGetApp()->m_pMainWnd;
- if (!pMainWnd)
- return;
- if (!pMainWnd->m_SerialTW.IsOpen())
- {
- MessageBox(_T("The serial port is not open."), _T("Warnning"), MB_OK | MB_ICONWARNING);
- return;
- }
- DWORD dwSendLen = 0;
- byte szSendData[MAX_SENDDATA] = { 0 };
- // 根据不同的版本号来发送;
- if (Global::g_nVersion == 1)
- {
- }
- else if (Global::g_nVersion == 2)
- {
- // 将十六进制的字符串转成字节流;
- std::string strBytes = utils::HexStringToBytes(_T("FD FD F1 F2 DF"), 3);
- memcpy_s(szSendData, MAX_SENDDATA, strBytes.c_str(), strBytes.size());
- dwSendLen = strBytes.size();
- // 发送学习红外信号;
- if (pMainWnd->m_SerialTW.WriteSync(szSendData, dwSendLen))
- {
- // 自动化的红外不返回,如果没有收到任务红外,会等待10多秒后自动结束学习状态;
- TRACE("发送单信号成功\n");
- }
- else
- {
- TRACE("发送单信号失败\n");
- }
- }
- if (m_dwThreadId)
- {
- PostThreadMessage(m_dwThreadId, TMSG_START_LEARN, uId, 0);
- }
- }
- DWORD WINAPI CPropertiesWnd::ThreadLearn(LPVOID lpParam)
- {
- CPropertiesWnd* pthis = (CPropertiesWnd*)lpParam;
- CMainFrame* pMainWnd = (CMainFrame*)AfxGetApp()->m_pMainWnd;
- if (!pthis || !pMainWnd)
- return 0;
- DWORD dwSendLen = 0;
- byte szSendData[MAX_SENDDATA] = { 0 };
- while (Global::g_bLearn)
- {
- MSG msg;
- if (GetMessage(&msg, NULL, 0, 0))
- {
- // 当接收到退出消息,退出线程;
- if (msg.message == TMSG_QUITE_THREAD)
- {
- // 退出并保存学习数据;
- pthis->SaveKeyInfo();
- break;
- }
- else if (msg.message == TMSG_START_LEARN)
- {
- DWORD dwTickCount = ::GetTickCount64();
- while (GetTickCount64() - dwTickCount < 60000)
- {
- // 等待学习的红外信号发出;
- DWORD dwRealRead = 0;
- memset((char*)szSendData, 0, MAX_SENDDATA);
- if (dwRealRead = pMainWnd->m_SerialTW.ReadSync(szSendData, MAX_SENDDATA))
- {
- if (m_pKeyInfo)
- {
- if (Global::g_nVersion == 1)
- m_pKeyInfo->strValue = (TCHAR*)szSendData;
- else if (Global::g_nVersion == 2)
- {
- std::string val = utils::BytesToHexString(szSendData, dwRealRead, ' ');
- // 超时没有学习,该设备会自动返回数据,其中开头16字节如下.
- //if (_tcsncmp("fd fd 30 03 88 99 aa 34 ff ff ff ff ff ff ff ff", val.c_str(), 16))
- if (_tcsncmp("FD FD 30 03 88 99 AA 34 FF FF FF FF FF FF FF FF", val.c_str(), 16))
- {
- m_pKeyInfo->strValue2 = val;
- }
- else
- {
- break;
- }
- }
- // 学习成功的,将按钮颜色改成白色以区分未学习的.
- m_pKeyInfo->pbtnInfo->pBTN->SetFaceColor(RGB(255, 255, 255), TRUE);
- }
- break;
- }
- Sleep(200);
- }
- }
- }
- }
- OutputDebugString(_T("线程结束"));
- pthis->m_dwThreadId = 0;
- return 0;
- }
- // 如果CDockablePane无控件,改变大小时背景会刷新失败;
- // 所以要重载onpaint;
- void CPropertiesWnd::OnPaint()
- {
- CPaintDC dc(this); // device context for painting
- // TODO: 在此处添加消息处理程序代码
- // 不为绘图消息调用 CDockablePane::OnPaint()
- CRect rc;
- GetClientRect(rc);
- CBrush brush;
- // 获取office风格主题颜色;
- #if _MSC_VER <=1500
- // CMFCVisualManagerOffice2007继承于CMFCVisualManager,使用CMFCVisualManager的GetInstance获取父对象指针后转换为CMFCVisualManagerOffice2007对象指针;
- brush.CreateSolidBrush(((CMFCVisualManagerOffice2007*)CMFCVisualManagerOffice2007::GetInstance())->GetRibbonEditBackgroundColor(FALSE, FALSE));
- #else // VS2010之后,方法有变化;
- CMainFrame* pMainFrame = (CMainFrame*)AfxGetApp()->GetMainWnd();
- brush.CreateSolidBrush(((CMFCVisualManagerOffice2007*)CMFCVisualManagerOffice2007::GetInstance())->GetRibbonEditBackgroundColor((CMFCRibbonRichEditCtrl*)pMainFrame->GetRibbonStatusBar(), FALSE, FALSE, FALSE));
- #endif
- dc.FillRect(&rc, &brush);
- }
- void CPropertiesWnd::OnDestroy()
- {
- CDockablePane::OnDestroy();
- // TODO: 在此处添加消息处理程序代码
- ClearAllBTNInfo();
- }
- BOOL CPropertiesWnd::PreTranslateMessage(MSG* pMsg)
- {
- // TODO: 在此添加专用代码和/或调用基类
- if (pMsg->message == WM_KEYDOWN)
- {
- if (pMsg->wParam == VK_F5)
- {
- Global::g_bLearn = !Global::g_bLearn;
- if (m_dwThreadId == 0 && Global::g_bLearn)
- CreateThread(NULL, 0, ThreadLearn, this, 0, &m_dwThreadId);
- // 关闭线程;
- if (!Global::g_bLearn)
- PostThreadMessage(m_dwThreadId, TMSG_QUITE_THREAD, 0, 0);
- MessageBox(Global::g_bLearn ? _T("开启学习红外") : _T("关闭红外学习"), _T("提示"), MB_OK);
- return TRUE;
- }
- else if ( pMsg->wParam == VK_F2 )
- {
- // 查找目录文件;
- filehelper fh;
- std::vector<std::string> vtSignals;
- fh.getfiles_findout_subfolder(Global::g_Config.twOldSignaldir.c_str(), _T("*.xml"), &vtSignals);
- // 逐个文件转换;
- for ( std::vector<std::string>::iterator it = vtSignals.begin(); it != vtSignals.end(); it++ )
- {
- std::string newfile = Global::g_Config.twSignaldir + _T("\\") + filehelper::getfilename(it->c_str()) + _T(".xml");
- ConvertOldSignalsToNew(*it, newfile);
- }
- MessageBox(_T("Convert old signals ok!"), _T("Convert"), MB_OK);
- LoadSignals();
- return TRUE;
- }
- }
- return CDockablePane::PreTranslateMessage(pMsg);
- }
|