Skip to content

Commit

Permalink
日期时间控件支持显示文字自定义
Browse files Browse the repository at this point in the history
  • Loading branch information
qdtroy committed Jul 28, 2022
1 parent 99e2f20 commit 64108ba
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
34 changes: 16 additions & 18 deletions DuiLib/Control/UIDateTime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,8 @@ namespace DuiLib
::GetLocalTime(&m_pOwner->m_sysTime);
}
// 显示格式
if(m_pOwner->IsShowTime()) {
::SendMessage(m_hWnd, DTM_SETFORMAT, 0, LPARAM(_T("yyyy-MM-dd HH:mm:ss")));
}
else {
::SendMessage(m_hWnd, DTM_SETFORMAT, 0, LPARAM(_T("yyyy-MM-dd")));
}
CDuiString sTimeFormat = m_pOwner->GetTimeFormat();
::SendMessage(m_hWnd, DTM_SETFORMAT, 0, LPARAM(sTimeFormat.GetData()));
memcpy(&m_oldSysTime, &m_pOwner->m_sysTime, sizeof(SYSTEMTIME));
::SendMessage(m_hWnd, DTM_SETSYSTEMTIME, 0, (LPARAM)&m_pOwner->m_sysTime);
::ShowWindow(m_hWnd, SW_SHOWNOACTIVATE);
Expand Down Expand Up @@ -161,8 +157,9 @@ namespace DuiLib
{
::GetLocalTime(&m_sysTime);
m_bReadOnly = false;
m_bShowTime = true;
m_sTimeFormat = _T("yyyy-MM-dd HH:mm:ss");
m_pWindow = NULL;

m_nDTUpdateFlag=DT_UPDATE;
UpdateText();
m_nDTUpdateFlag = DT_NONE;
Expand Down Expand Up @@ -204,17 +201,15 @@ namespace DuiLib
return m_bReadOnly;
}

void CDateTimeUI::SetShowTime(bool bShowTime)
void CDateTimeUI::SetTimeFormat(LPCTSTR pstrFormat)
{
if(bShowTime != m_bShowTime) {
m_bShowTime = bShowTime;
Invalidate();
}
m_sTimeFormat = pstrFormat;
Invalidate();
}

bool CDateTimeUI::IsShowTime() const
CDuiString CDateTimeUI::GetTimeFormat() const
{
return m_bShowTime;
return m_sTimeFormat;
}

void CDateTimeUI::UpdateText()
Expand All @@ -224,11 +219,14 @@ namespace DuiLib
}
else if (m_nDTUpdateFlag == DT_UPDATE) {
CDuiString sText;
if(IsShowTime()) {
sText.SmallFormat(_T("%4d-%02d-%02d %02d:%02d:%02d"), m_sysTime.wYear, m_sysTime.wMonth, m_sysTime.wDay, m_sysTime.wHour, m_sysTime.wMinute, m_sysTime.wSecond);
if(m_sTimeFormat.CompareNoCase(_T("yyyy-MM-dd")) == 0) {
sText.SmallFormat(_T("%4d-%02d-%02d"), m_sysTime.wYear, m_sysTime.wMonth, m_sysTime.wDay);
}
else if(m_sTimeFormat.CompareNoCase(_T("HH:mm:ss")) == 0) {
sText.SmallFormat(_T("%02d:%02d:%02d"), m_sysTime.wHour, m_sysTime.wMinute, m_sysTime.wSecond);
}
else {
sText.SmallFormat(_T("%4d-%02d-%02d"), m_sysTime.wYear, m_sysTime.wMonth, m_sysTime.wDay);
sText.SmallFormat(_T("%4d-%02d-%02d %02d:%02d:%02d"), m_sysTime.wYear, m_sysTime.wMonth, m_sysTime.wDay, m_sysTime.wHour, m_sysTime.wMinute, m_sysTime.wSecond);
}
SetText(sText);
}
Expand Down Expand Up @@ -315,7 +313,7 @@ namespace DuiLib

void CDateTimeUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
{
if(lstrcmpi(pstrName, _T("showtime")) == 0) SetShowTime(lstrcmpi(pstrValue, _T("true")) == 0);
if(lstrcmpi(pstrName, _T("timeformat")) == 0) SetTimeFormat(pstrValue);
else if(lstrcmpi(pstrName, _T("readonly")) == 0) SetReadOny(lstrcmpi(pstrValue, _T("true")) == 0);
else return CLabelUI::SetAttribute(pstrName, pstrValue);
}
Expand Down
7 changes: 4 additions & 3 deletions DuiLib/Control/UIDateTime.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ namespace DuiLib

void SetReadOny(bool bReadOnly);
bool IsReadOnly() const;
void SetShowTime(bool bShowTime);
bool IsShowTime() const;

void SetTimeFormat(LPCTSTR pstrFormat = _T("yyyy-MM-dd HH:mm:ss"));
CDuiString GetTimeFormat() const;

void UpdateText();

Expand All @@ -37,7 +38,7 @@ namespace DuiLib
SYSTEMTIME m_sysTime;
int m_nDTUpdateFlag;
bool m_bReadOnly;
bool m_bShowTime;
CDuiString m_sTimeFormat;
CDateTimeWnd* m_pWindow;
};
}
Expand Down

0 comments on commit 64108ba

Please sign in to comment.