DuiLib使用MFC的时间控件CDateTimeCtrl出现第二个控件无法获取焦点的问题
我在Duilib里用了两个MFC的CDateTimeCtrl控件,第一个控件可以随便编辑,第二个鼠标点击根本就获取不到焦点,所以无法编辑,这是什么原因啊,搞了好长时间,百思不得其解。。。
控件现实效果和XML表达已经代码实现如下:
![](https://images2018.cnblogs.com/blog/1386917/201808/1386917-20180818192009133-1146242686.png)
![](https://images2018.cnblogs.com/blog/1386917/201808/1386917-20180818192012158-2069120841.png)
![](https://images2018.cnblogs.com/blog/1386917/201808/1386917-20180818192059823-847462473.png)
--------------------------解决问题:
需要定义CStatic作为MFC控件的父类,所以增加两个CStatic对象
CStatic m_parentBegin; CStatic m_parentEnd; CDateTimeCtrl m_ctrTmBegin; //MFC时间控件 CDateTimeCtrl m_ctrTmEnd;
CControlUI* CAnalyzeTimeSpanWnd::CreateControl(LPCTSTR pstrClass) { if (lstrcmp(pstrClass, _T("MfcTimeStart")) == 0) { //AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0); CWndUIPos *pWnd = new CWndUIPos; CRect rc(CPoint(0, 0), CSize(85, 22)); m_parentBegin.Create(_T(""), WS_CHILD | WS_VISIBLE, rc, CWnd::FromHandle(m_hWnd)); assert(m_parentBegin); if (m_parentBegin.GetSafeHwnd()) { m_ctrTmBegin.Create(WS_CHILD | WS_VISIBLE | DTS_UPDOWN, rc, &m_parentBegin, 0); m_ctrTmBegin.SetFormat(_T("HH:mm:ss")); pWnd->Attach(m_parentBegin.GetSafeHwnd()); pWnd->SetName(_T("MFCTimeStart")); return pWnd; } else { delete pWnd; return NULL; } } else if (lstrcmp(pstrClass, _T("MfcTimeEnd")) == 0) { //AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0); CWndUIPos *pWnd = new CWndUIPos; CRect rc(CPoint(0, 0), CSize(85, 22)); m_parentEnd.Create(_T(""), WS_CHILD | WS_VISIBLE, rc, CWnd::FromHandle(m_hWnd)); assert(m_parentEnd); if (m_parentEnd.GetSafeHwnd()) { m_ctrTmEnd.Create(WS_CHILD | WS_VISIBLE | DTS_UPDOWN, rc, &m_parentEnd, 0); m_ctrTmEnd.SetFormat(_T("HH:mm:ss")); pWnd->Attach(m_parentEnd.GetSafeHwnd()); pWnd->SetName(_T("MFCTimeEnd")); return pWnd; } else { delete pWnd; return NULL; } } return NULL; }
这样就可以正常操作这两个MFC控件对象了,问题解决。
没有坚守就没有事业,没有执着就没有未来!