ilovehellworld 2024-09-29 09:26 采纳率: 52.9%
浏览 18
已结题

vs2010修改时间编辑控件

大家好!这个自己开发的时间编辑控件,现在时,分,秒是选中2个数字,毫秒是选中3个数字,需要改成只选中一个数字进行编辑,谢谢!VS2010开发,请加我微信 yyzsoftcom 发工程文件,帮我修改好再加钱,谢谢!

img

#include "StdAfx.h"
#include "TimeWnd.h"
 
CTimeWnd::CTimeWnd(void)
{
    for (int i=0; i<valCount; i++)
    {
        m_nValue[i] = 0;
        m_Rect[i].SetRectEmpty();
    }
    m_nValueMax[valHours] = 24;
    m_nValueMax[valMinutes] = 60;
    m_nValueMax[valSeconds] = 60;
    m_nValueMax[valMilliseconds] = 1000;
 
    m_dPower[valHours] = 3600;
    m_dPower[valMinutes] = 60;
    m_dPower[valSeconds] = 1;
    m_dPower[valMilliseconds] = 0.001000;
    m_nFirst = 0;
            
    m_nCurSel = -1;
    m_nOldSel = 0;
    m_pFont = NULL;
    m_dMinTime = 0;
    m_dMaxTime = 90000;
    m_bFocus = false;

    m_nFontColor = RGB(0,0,0);
    m_nBackColor = RGB(255,255,255);
}
 
CTimeWnd::~CTimeWnd(void)
{
}
 
BEGIN_MESSAGE_MAP(CTimeWnd, CWnd)
 
ON_WM_PAINT()
    ON_WM_CREATE()
    ON_WM_LBUTTONDOWN()
    ON_WM_KEYDOWN()
    ON_NOTIFY(UDN_DELTAPOS, 1051, &CTimeWnd::OnDeltaposSpin1)
    ON_WM_KILLFOCUS()
    ON_WM_SIZE()
    ON_WM_CAPTURECHANGED()
    ON_WM_ENABLE()
END_MESSAGE_MAP()
 
void CTimeWnd::OnDeltaposSpin1(NMHDR *pNMHDR, LRESULT *pResult)
{
    LPNMUPDOWN pNMUpDown = reinterpret_cast<LPNMUPDOWN>(pNMHDR);
    *pResult = 0;
    SetFocus();
    ChangeSpin(pNMUpDown->iDelta*(-1));
}
 
void CTimeWnd::ChangeSpin(int nDelta)
{
    if(m_nCurSel == -1)
    {
        m_nCurSel = m_nOldSel;
    }
    if (nDelta > 0)
    {
        m_nValue[m_nCurSel] = (m_nValue[m_nCurSel]+nDelta)%m_nValueMax[m_nCurSel];
    }
    else
    {
        m_nValue[m_nCurSel] = (m_nValue[m_nCurSel]+nDelta+m_nValueMax[m_nCurSel])%m_nValueMax[m_nCurSel];
    }
    
    VarifyRange();
    ReDraw(true);
    GetParent()->PostMessage(WM_TIMECHNGE, (WPARAM)GetDlgCtrlID(), (LPARAM)GetTime());
}
 
void CTimeWnd::OnPaint()
{
    CPaintDC dc(this); // device context for painting
    CRect rcBounds;   
    GetClientRect(&rcBounds);   
    CPen penWhite;   
    penWhite.CreatePen(PS_SOLID, 1, RGB(127,157,185));   
    CPen *pOldPen = dc.SelectObject(&penWhite); 
    dc.FillSolidRect(rcBounds, m_nBackColor);
    CString    str;
    int xoffset = 5;
    CRect rc;
    rc.SetRectEmpty();
    for(int i=0; i<valCount; i++)
    {
        str = GetString(i);
        rc = rcBounds;
        DrawSingleString(&dc,str,rc,&xoffset,(i == m_nCurSel));
        m_Rect[i] = rc;
        switch (i)
        {
        case 0:
        case 1:
            str = _T(":");
            rc = rcBounds;
            DrawSingleString(&dc, str, rc, &xoffset);
            break;
        case 2:
            str = _T(".");
            rc = rcBounds;
            DrawSingleString(&dc, str, rc, &xoffset);
            break;
        }
    }
    dc.SelectObject(pOldPen); 
}
 
void CTimeWnd::PreSubclassWindow()
{
    CWnd::PreSubclassWindow();
}
 
int CTimeWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CWnd::OnCreate(lpCreateStruct) == -1)
        return -1;
 
    m_Spin.Create(UDS_ALIGNRIGHT|UDS_AUTOBUDDY|WS_VISIBLE, CRect(0,0,20,19), this, 1051);
    m_Spin.SetBuddy(this);
 
    return 0;
}
 
void CTimeWnd::OnLButtonDown(UINT nFlags, CPoint point)
{
    for (int i=0; i<valCount; i++)
    {
        if (m_Rect[i].PtInRect(point))
        {
            if (m_nCurSel == i)break;
            m_nFirst = 0;
            ReDraw(false);
            m_nCurSel = i;
            ReDraw(true);
            break;
        }
    }
    SetFocus();
    m_bFocus = true;
    CWnd::OnLButtonDown(nFlags, point);
}
 
void CTimeWnd::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
     if (m_nCurSel == -1)
    {
        m_nCurSel = m_nOldSel;
    }
   // if ((nChar>='0') && (nChar<='9')) // 2024/9/4
  //  {
     //   ChangeChar(nChar, '0');
   // }
    else if ((nChar>=VK_NUMPAD0) && (nChar<=VK_NUMPAD9))
    {
        ChangeChar(nChar, VK_NUMPAD0);
    }
    ReDraw(true);
    CWnd::OnKeyDown(nChar, nRepCnt, nFlags);
}
 
CString CTimeWnd::GetString(int nType)
{
    CString str = NULL;
    int nCount = 0;
    int nZero = 0;
    nCount = GetNumberCount(m_nValueMax[nType]-1);
    nZero = nCount - GetNumberCount(m_nValue[nType]) ;
    m_nValue[nType] %= m_nValueMax[nType];
    str.Format(_T("%d"), m_nValue[nType]);
    while (nZero--)
    {
        str = _T("0") + str;
    }
    return str;
}

void CTimeWnd::SetRange( double dMinTime, double dMaxTime )
{
    m_dMinTime = dMinTime;
    m_dMaxTime = dMaxTime;
    VarifyRange();
}

void CTimeWnd::SetRangeMax( int nHour, int nMin, int nSecond, int nMilliSecond )
{
    double dMaxTime = 0;
    dMaxTime += nHour * m_dPower[valHours];
    dMaxTime += nMin * m_dPower[valMinutes];
    dMaxTime += nSecond * m_dPower[valSeconds];
    dMaxTime += nMilliSecond * m_dPower[valMilliseconds];
    m_dMaxTime = dMaxTime;
    VarifyRange();
}
void CTimeWnd::SetRangeMin( int nHour, int nMin, int nSecond, int nMilliSecond )
{
    double dMinTime = 0;
    dMinTime += nHour * m_dPower[valHours];
    dMinTime += nMin * m_dPower[valMinutes];
    dMinTime += nSecond * m_dPower[valSeconds];
    dMinTime += nMilliSecond * m_dPower[valMilliseconds];
    m_dMinTime = dMinTime;
    VarifyRange();
}
bool CTimeWnd::VarifyRange()
{
    bool bRet = false;
    double dCur = GetTime();
    if(    m_dMinTime > dCur )
    {
        dCur = m_dMinTime;
        bRet = true;
    }
    if(    m_dMaxTime < dCur )
    {
        dCur = m_dMaxTime;
        bRet = true;
    }
    if(bRet)
    {
        double nTemVal = 0;
        for (int i=0; i<valCount; i++)
        {
            m_nValue[i] = (int)((dCur+0.0005 - nTemVal) / m_dPower[i]);
            nTemVal += m_nValue[i] * m_dPower[i];
            m_nValue[i] %= m_nValueMax[i]; 
        }
        CRect   rcBounds;   
        GetClientRect(&rcBounds);
        rcBounds.right -= 20;
        RedrawWindow( &rcBounds );
    }
    return bRet;
}
void CTimeWnd::DrawSingleString(CDC *pDC, CString &str, CRect &rc, int *pOffset/*= -1*/, bool bSelect/* = false*/)
{
    int nHeight = rc.Height();
    int offset = 0;
    if (pOffset == NULL)
    {
        offset = rc.left;
    }
    else
    {
        offset = *pOffset;
    }
    CFont *pOldFont = NULL;
    if (m_pFont)
    {
        pOldFont = pDC->SelectObject(m_pFont);
    }
    pDC->DrawText(str, rc, DT_CALCRECT);
    rc.MoveToXY(offset, (nHeight - rc.Height())/2);
    COLORREF clr;
    int nMode = 0;
    BOOL bEnable = IsWindowEnabled();
    if (bSelect && bEnable)
    {
        pDC->FillSolidRect(rc, RGB(49,106,197));
        clr = pDC->SetTextColor(RGB(255,255,255));
        nMode = pDC->SetBkMode(TRANSPARENT);
    }
    else
    {
        pDC->FillSolidRect(rc, m_nBackColor);
        clr = pDC->SetTextColor(m_nFontColor);
    }
    if (bEnable)
         pDC->DrawState(CPoint(rc.left, rc.top), CSize(rc.Width(), rc.Height()), str, DSS_NORMAL, TRUE, 0, (HBRUSH)NULL);
    else
        pDC->DrawState(CPoint(rc.left, rc.top), CSize(rc.Width(), rc.Height()), str, DSS_NORMAL, TRUE, 0, (HBRUSH)NULL);
    offset += rc.Width();
    offset = offset + 1;  // 2023.3.25 by yeyizhou
    if (bSelect)
    {
        pDC->SetTextColor(clr);
        pDC->SetBkMode(nMode);
    }
    if (pOffset) *pOffset = offset;
    if (pOldFont) pDC->SelectObject(pOldFont);
}
 
void CTimeWnd::OnKillFocus(CWnd *pNewWnd)
{
    CWnd::OnKillFocus(pNewWnd);
    ReDraw(false);
    m_nOldSel = m_nCurSel;
    m_nCurSel = -1;
    m_bFocus = false; 
}
 
void CTimeWnd::ReDraw(bool bNeedBk/*= true*/)
{
    if (m_nCurSel == -1)
    {
        return;
    }
    CDC *pDC = GetDC();
    CString str = GetString(m_nCurSel);
    CRect   rcBounds;   
    GetClientRect(&rcBounds);   
    rcBounds.left = m_Rect[m_nCurSel].left;
    rcBounds.right = m_Rect[m_nCurSel].right;
    DrawSingleString(pDC, str, rcBounds, NULL, bNeedBk);
    ReleaseDC(pDC);
}
 
void CTimeWnd::SetFont(CFont *pFont, BOOL bRedraw/*=false*/)
{
    m_pFont = pFont;
    if (bRedraw)
    {
        Invalidate();
    }
}
 
CFont *CTimeWnd::GetFont()
{
    return m_pFont;
}
 
void CTimeWnd::ChangeChar(UINT nChar, int refer)
{
    if (!IsCountValid(m_nCurSel)) return;
    int iCharValue = nChar - refer;
    int iTemVal = m_nValue[m_nCurSel] * 10 + iCharValue;
    //m_nValue[m_nCurSel] = (iTemVal >= m_nValueMax[m_nCurSel]) ? iCharValue : iTemVal;
    m_nValue[m_nCurSel] = (m_nFirst<=0) ? iCharValue : (iTemVal%m_nValueMax[m_nCurSel]);
    if(m_nCurSel==3)
    {
        if(++m_nFirst>2 )
            m_nFirst = 0;
    }
    else
        m_nFirst = !m_nFirst;
    VarifyRange();
    GetParent()->PostMessage(WM_TIMECHNGE, (WPARAM)GetDlgCtrlID(), (LPARAM)GetTime());
}
 
 
BOOL CTimeWnd::PreTranslateMessage(MSG *pMsg)
{
    if (pMsg->message == WM_KEYDOWN)
    {
        if (pMsg->wParam == VK_UP)
        {
            ChangeSpin(1);
            return true;
        }
        if (pMsg->wParam == VK_DOWN)
        {
            ChangeSpin(-1);
            return true;
        }
        if (pMsg->wParam == VK_LEFT)
        {
            ReDraw(false);
            m_nCurSel = (m_nCurSel + valCount - 1) % valCount;
            ReDraw(true);
            return true;
        }
        if (pMsg->wParam == VK_RIGHT)
        {
            ReDraw(false);
            m_nCurSel = (m_nCurSel + 1) % valCount;
            ReDraw(true);
            return true;
        }
        if (pMsg->wParam == VK_END)
        {        
            m_nValue[m_nCurSel] = m_nValueMax[m_nCurSel];
        }
        if (pMsg->wParam == VK_HOME)
        {
            m_nValue[m_nCurSel] = 0;
        }
    }

    
    else if (pMsg->message == WM_CHAR)  // 2024/9/4
    {
        if ((pMsg->wParam >= '0' && pMsg->wParam <= '9'))
        {
            ChangeChar(pMsg->wParam, '0');
            ReDraw(true);
            return TRUE;
        }
    }
    return CWnd::PreTranslateMessage(pMsg);
}
 
double CTimeWnd::GetTime()
{
    double dTime = 0;
    for (int i=0; i<valCount; i++)
    {
        dTime += m_nValue[i] * m_dPower[i];
    }
    return dTime;
}
 
int CTimeWnd::GetValue(int nType)
{
    if (!IsCountValid(nType)) 
        return 0;
    else 
        return m_nValue[nType];
}
 
void CTimeWnd::SetValue(int nType, int nVal)
{
    if ((!IsCountValid(nType)) || nVal<0) return;
    else m_nValue[nType] = nVal % m_nValueMax[nType];
    
    VarifyRange();
}
 
void CTimeWnd::SetTime(double dTime)
{
    double nTemVal = 0;
    for (int i=0; i<valCount; i++)
    {
        m_nValue[i] = (int)((dTime+0.0005 - nTemVal) / m_dPower[i]);
        nTemVal += m_nValue[i] * m_dPower[i];
        m_nValue[i] %= m_nValueMax[i]; 
    }
    VarifyRange();
}
 
bool CTimeWnd::IsFocus()
{
    return m_bFocus;
}
 
void CTimeWnd::OnSize(UINT nType, int cx, int cy)
{
    CWnd::OnSize(nType, cx, cy);
    m_Spin.MoveWindow(cx-19, 1, 18, cy-2);
    
    MoveWindow(x, y, width, height); // 2024/3/4
    Invalidate();
}
 
void CTimeWnd::OnCaptureChanged(CWnd *pWnd)
{
    CWnd::OnCaptureChanged(pWnd);
}
 
void CTimeWnd::OnEnable(BOOL bEnable)
{
    CWnd::OnEnable(bEnable);
    Invalidate();
    m_Spin.EnableWindow(bEnable);
}
 
int CTimeWnd::GetNumberCount(int num)
{
    int i = 0;
    if (num == 0)
    {
        return 1;
    }
    while (num)
    {
        num /= 10;    
        i++;
    };


    return i;
}
 
bool CTimeWnd::IsCountValid(int nCount)
{
    if ((nCount>=0) && (nCount<valCount))
    {
        return true;
    }
    return false;
}

void CTimeWnd::SetColorStyle(COLORREF nFont, COLORREF nBack, COLORREF nFrame)
{
    m_nFontColor = nFont;
    m_nBackColor = nBack;
    m_Spin.SetColorStyle(m_nFontColor,m_nBackColor,nFrame);
    m_oBackBrush.CreateSolidBrush(nBack);
}


  • 写回答

22条回答 默认 最新

  • Minuw 2024-10-04 19:56
    关注
    
    #include "StdAfx.h"
    #include "TimeWnd.h"
    #include "resource.h"
     
    CTimeWnd::CTimeWnd(void)
    {
        for (int i=0; i<valCount; i++)
        {
            m_nValue[i] = 0;
            m_Rect[i].SetRectEmpty();
        }
        m_nValueMax[valHours] = 3;
        m_nValueMax[valHours1] = 10;
        m_nValueMax[valMinutes] = 6;
        m_nValueMax[valMinutes1] = 10;
        m_nValueMax[valSeconds] = 6;
        m_nValueMax[valSeconds1] = 10;
        m_nValueMax[valMilliseconds] = 10;
        m_nValueMax[valMilliseconds1] = 10;
        m_nValueMax[valMilliseconds2] = 10;
     
        m_dPower[valHours] = 36000;
        m_dPower[valHours1] = 3600;
        m_dPower[valMinutes] = 600;
        m_dPower[valMinutes1] = 60;
        m_dPower[valSeconds] = 10;
        m_dPower[valSeconds1] = 1;
        m_dPower[valMilliseconds] = 0.100000;
        m_dPower[valMilliseconds1] = 0.010000;
        m_dPower[valMilliseconds2] = 0.001000;
        m_nFirst = 0;
                
        m_nCurSel = -1;
        m_nOldSel = 0;
        m_pFont = NULL;
        m_dMinTime = 0;
        m_dMaxTime = 90000;
        m_bFocus = false;
    
        m_nFontColor = RGB(0,0,0);
        m_nBackColor = RGB(255,255,255);
    }
     
    CTimeWnd::~CTimeWnd(void)
    {
    }
     
    BEGIN_MESSAGE_MAP(CTimeWnd, CWnd)
     
    ON_WM_PAINT()
        ON_WM_CREATE()
        ON_WM_LBUTTONDOWN()
        ON_WM_KEYDOWN()
        ON_NOTIFY(UDN_DELTAPOS, 1051, &CTimeWnd::OnDeltaposSpin1)
        ON_WM_KILLFOCUS()
        ON_WM_SIZE()
        ON_WM_CAPTURECHANGED()
        
        ON_WM_ENABLE()
    END_MESSAGE_MAP()
     
    //点击上下按键
    void CTimeWnd::OnDeltaposSpin1(NMHDR *pNMHDR, LRESULT *pResult)
    {
        LPNMUPDOWN pNMUpDown = reinterpret_cast<LPNMUPDOWN>(pNMHDR);
        *pResult = 0;
        SetFocus();
        ChangeSpin(pNMUpDown->iDelta*(-1));
    }
     
    void CTimeWnd::ChangeSpin(int nDelta)
    {
        if(m_nCurSel == -1)
        {
            m_nCurSel = m_nOldSel;
        }
        if (nDelta > 0)
        {
            m_nValue[m_nCurSel] = (m_nValue[m_nCurSel]+nDelta)%m_nValueMax[m_nCurSel];
        }
        else
        {
            m_nValue[m_nCurSel] = (m_nValue[m_nCurSel]+nDelta+m_nValueMax[m_nCurSel])%m_nValueMax[m_nCurSel];
        }
        
        VarifyRange();
        ReDraw(true);
        GetParent()->PostMessage(WM_TIMECHNGE, (WPARAM)GetDlgCtrlID(), (LPARAM)GetTime());
    }
     
    void CTimeWnd::OnPaint()
    {
        CPaintDC dc(this); // device context for painting
        CRect rcBounds;   
        GetClientRect(&rcBounds);   
        CPen penWhite;   
        penWhite.CreatePen(PS_SOLID, 1, RGB(127,157,185));   
        CPen *pOldPen = dc.SelectObject(&penWhite); 
        dc.FillSolidRect(rcBounds, m_nBackColor);
        CString    str;
        int xoffset = 5;
        CRect rc;
        rc.SetRectEmpty();
        for(int i=0; i<valCount; i++)
        {
            str = GetString(i);
            rc = rcBounds;
            DrawSingleString(&dc,str,rc,&xoffset,(i == m_nCurSel));
            m_Rect[i] = rc;
            switch (i)
            {
            //case 0:
            case 1:
            case 3:
                str = _T(":");
                rc = rcBounds;
                DrawSingleString(&dc, str, rc, &xoffset);
                break;
            //case 2:
            case 5:
                str = _T(".");
                rc = rcBounds;
                DrawSingleString(&dc, str, rc, &xoffset);
                break;
            }
        }
        dc.SelectObject(pOldPen); 
    }
     
    void CTimeWnd::PreSubclassWindow()
    {
        CWnd::PreSubclassWindow();
    }
     
    int CTimeWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
    {
        if (CWnd::OnCreate(lpCreateStruct) == -1)
            return -1;
     
        m_Spin.Create(UDS_ALIGNRIGHT|UDS_AUTOBUDDY|WS_VISIBLE, CRect(0,0,20,19), this, 1051);
        m_Spin.SetBuddy(this);
     
        return 0;
    }
     
    void CTimeWnd::OnLButtonDown(UINT nFlags, CPoint point)
    {
        //::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_IBEAM));
    
        for (int i=0; i<valCount; i++)
        {
            if (m_Rect[i].PtInRect(point))
            {
                if (m_nCurSel == i)break;
                m_nFirst = 0;
                ReDraw(false);
                m_nCurSel = i;
                ReDraw(true);
                break;
            }
        }
        SetFocus();
        m_bFocus = true;
        CWnd::OnLButtonDown(nFlags, point);
    }
     
    
    //键盘按下事件
    void CTimeWnd::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
    {
        if (m_nCurSel == -1)
        {
            m_nCurSel = m_nOldSel;
        }
        else if ((nChar >= VK_NUMPAD0) && (nChar <= VK_NUMPAD9))
        {
            ChangeChar(nChar, VK_NUMPAD0);
        }
        
         // if ((nChar>='0') && (nChar<='9')) // 2024/9/4
    //  {
       //   ChangeChar(nChar, '0');
     // }
    
        ReDraw(true);
    
        CWnd::OnKeyDown(nChar, nRepCnt, nFlags);
    
    }
    
    /**
     * @brief   选中下一个数值位
     * @param   当前数值位索引
     * @retval  无
     * @time    2024.10.1
     */
    void CTimeWnd::MoveToNext(int nIndex)
    {
        if (nIndex!=8)
        {
            ReDraw(false);
            m_nCurSel = (nIndex + 1) % valCount;
            ReDraw(true);
        }
        else
        {
            ReDraw(false);
            m_nCurSel = -1;
            ReDraw(true);
        }
        
    }
     
    CString CTimeWnd::GetString(int nType)
    {
        CString str = NULL;
        int nCount = 0;
        int nZero = 0;
        nCount = GetNumberCount(m_nValueMax[nType]-1);
        nZero = nCount - GetNumberCount(m_nValue[nType]) ;
        m_nValue[nType] %= m_nValueMax[nType];
        str.Format(_T("%d"), m_nValue[nType]);
        while (nZero--)
        {
            str = _T("0") + str;
        }
        return str;
    }
    
    void CTimeWnd::SetRange( double dMinTime, double dMaxTime )
    {
        m_dMinTime = dMinTime;
        m_dMaxTime = dMaxTime;
        VarifyRange();
    }
    
    void CTimeWnd::SetRangeMax(int nHour, int nMin, int nSecond, int nMilliSecond)
    {
        int H1, H2, M1, M2, S1, S2, Mill1, Mill2, Mill3;
        // 拆分小时  
        if (nHour >= 10) 
        {
            H1 = nHour / 10;    // 十位  
            H2 = nHour % 10;     // 个位  
        }
        else
        {
            H1 = 0;
            H2 = nHour;
        }
        // 拆分分钟  
        if (nMin >= 10) 
        {
            M1 = nMin / 10;      // 十位  
            M2 = nMin % 10;       // 个位  
        }
        else
        {
            M1 = 0;
            M2 = nMin;
        }
    
        // 拆分秒  
        if (nSecond >= 10) 
        {
            S1 = nSecond / 10; // 十位  
            S2 = nSecond % 10;  // 个位  
        }
        else
        {
            S1 = 0;
            S2 = nSecond;
        }
    
        // 拆分毫秒  
        if (nMilliSecond >= 100)
        {
            Mill1 = nMilliSecond / 100; // 百位 
            nMilliSecond = nMilliSecond % 100;
            Mill2 = nMilliSecond / 10;  // 十位
            Mill3 = nMilliSecond % 10;  // 个位
        }
        else if (nMilliSecond >= 10) 
        {
            Mill1 = 0;   
            Mill2 = nMilliSecond / 10; // 十位  
            Mill3 = nMilliSecond % 10;  // 个位  
        }
        else
        {
            Mill1 = 0;
            Mill2 = 0; 
            Mill3 = nMilliSecond;   
        }
    
    
        double dMaxTime = 0;
        dMaxTime += H1 * m_dPower[valHours];
        dMaxTime += H2 * m_dPower[valHours1];
        dMaxTime += M1 * m_dPower[valMinutes];
        dMaxTime += M2 * m_dPower[valMinutes1];
        dMaxTime += S1 * m_dPower[valSeconds];
        dMaxTime += S2 * m_dPower[valSeconds1];
        dMaxTime += Mill1 * m_dPower[valMilliseconds];
        dMaxTime += Mill2 * m_dPower[valMilliseconds1];
        dMaxTime += Mill3 * m_dPower[valMilliseconds2];
        m_dMaxTime = dMaxTime;
        VarifyRange();
    }
    void CTimeWnd::SetRangeMin(int nHour, int nMin, int nSecond, int nMilliSecond)
    {
        double dMinTime = 0;
        dMinTime += nHour * m_dPower[valHours];
        dMinTime += nHour * m_dPower[valHours1];
        dMinTime += nMin * m_dPower[valMinutes];
        dMinTime += nMin * m_dPower[valMinutes1];
        dMinTime += nSecond * m_dPower[valSeconds];
        dMinTime += nSecond * m_dPower[valSeconds1];
        dMinTime += nMilliSecond * m_dPower[valMilliseconds];
        dMinTime += nMilliSecond * m_dPower[valMilliseconds1];
        dMinTime += nMilliSecond * m_dPower[valMilliseconds2];
        m_dMinTime = dMinTime;
        VarifyRange();
    }
    bool CTimeWnd::VarifyRange()
    {
        bool bRet = false;
        double dCur = GetTime();
        if(    m_dMinTime > dCur )
        {
            dCur = m_dMinTime;
            bRet = true;
        }
        if(    m_dMaxTime < dCur )
        {
            dCur = m_dMaxTime;
            bRet = true;
        }
        if(bRet)
        {
            double nTemVal = 0;
            for (int i=0; i<valCount; i++)
            {
                m_nValue[i] = (int)((dCur+0.0005 - nTemVal) / m_dPower[i]);
                nTemVal += m_nValue[i] * m_dPower[i];
                m_nValue[i] %= m_nValueMax[i]; 
            }
            CRect   rcBounds;   
            GetClientRect(&rcBounds);
            rcBounds.right -= 20;
            RedrawWindow( &rcBounds );
        }
        return bRet;
    }
    void CTimeWnd::DrawSingleString(CDC *pDC, CString &str, CRect &rc, int *pOffset/*= -1*/, bool bSelect/* = false*/)
    {
        int nHeight = rc.Height();
        int offset = 0;
        if (pOffset == NULL)
        {
            offset = rc.left;
        }
        else
        {
            offset = *pOffset;
        }
        CFont *pOldFont = NULL;
        if (m_pFont)
        {
            pOldFont = pDC->SelectObject(m_pFont);
        }
        pDC->DrawText(str, rc, DT_CALCRECT);
        rc.MoveToXY(offset, (nHeight - rc.Height())/2);
        COLORREF clr;
        int nMode = 0;
        BOOL bEnable = IsWindowEnabled();
        if (bSelect && bEnable)
        {
            pDC->FillSolidRect(rc, RGB(49,106,197));
            clr = pDC->SetTextColor(RGB(255,255,255));
            nMode = pDC->SetBkMode(TRANSPARENT);
        }
        else
        {
            pDC->FillSolidRect(rc, m_nBackColor);
            clr = pDC->SetTextColor(m_nFontColor);
        }
        if (bEnable)
             pDC->DrawState(CPoint(rc.left, rc.top), CSize(rc.Width(), rc.Height()), str, DSS_NORMAL, TRUE, 0, (HBRUSH)NULL);
        else
            pDC->DrawState(CPoint(rc.left, rc.top), CSize(rc.Width(), rc.Height()), str, DSS_NORMAL, TRUE, 0, (HBRUSH)NULL);
        offset += rc.Width();
        offset = offset + 1;  // 2023.3.25 by yeyizhou
        if (bSelect)
        {
            pDC->SetTextColor(clr);
            pDC->SetBkMode(nMode);
        }
        if (pOffset) *pOffset = offset;
        if (pOldFont) pDC->SelectObject(pOldFont);
    }
     
    void CTimeWnd::OnKillFocus(CWnd *pNewWnd)
    {
        CWnd::OnKillFocus(pNewWnd);
        ReDraw(false);
        m_nOldSel = m_nCurSel;
        m_nCurSel = -1;
        m_bFocus = false; 
    }
     
    void CTimeWnd::ReDraw(bool bNeedBk/*= true*/)
    {
        if (m_nCurSel == -1)
        {
            return;
        }
        CDC *pDC = GetDC();
        CString str = GetString(m_nCurSel);
        CRect   rcBounds;   
        GetClientRect(&rcBounds);   
        rcBounds.left = m_Rect[m_nCurSel].left;
        rcBounds.right = m_Rect[m_nCurSel].right;
        DrawSingleString(pDC, str, rcBounds, NULL, bNeedBk);
        ReleaseDC(pDC);
    }
     
    void CTimeWnd::SetFont(CFont *pFont, BOOL bRedraw/*=false*/)
    {
        m_pFont = pFont;
        if (bRedraw)
        {
            Invalidate();
        }
    }
     
    CFont *CTimeWnd::GetFont()
    {
        return m_pFont;
    }
     
    //设置每位的数值
    void CTimeWnd::ChangeChar(UINT nChar, int refer)
    {
        if (!IsCountValid(m_nCurSel)) return;
        int iCharValue = nChar - refer;
        //int iTemVal = m_nValue[m_nCurSel] * 10 + iCharValue;
        int iTemVal = iCharValue;
        //m_nValue[m_nCurSel] = (iTemVal >= m_nValueMax[m_nCurSel]) ? iCharValue : iTemVal;
        if (m_nCurSel == 0)
        {
            if (m_nValue[1] > 4)
            {
                m_nValue[m_nCurSel] = 1;
            }
            else
            {
                if (iTemVal > 2)
                {
                    m_nValue[m_nCurSel] == 2;
                }
                else
                {
                    m_nValue[m_nCurSel] = iTemVal;
                }
            }
        }
        else if (m_nCurSel == 2 || m_nCurSel == 4)
        {
            if (iTemVal >5)
            {
                m_nValue[m_nCurSel] == 5;
            }
            else
            {
                m_nValue[m_nCurSel] = (m_nFirst <= 0) ? iCharValue : (iTemVal % m_nValueMax[m_nCurSel]);
            }
        }
        else
        {
            m_nValue[m_nCurSel] = (m_nFirst <= 0) ? iCharValue : (iTemVal % m_nValueMax[m_nCurSel]);
        }
    
        //if(m_nCurSel==3)
        if(m_nCurSel==6)
        {
            if(++m_nFirst>2 )
                m_nFirst = 0;
        }
        else
            m_nFirst = !m_nFirst;
    
        
    
        VarifyRange();
        GetParent()->PostMessage(WM_TIMECHNGE, (WPARAM)GetDlgCtrlID(), (LPARAM)GetTime());
    
        //设置完当前数值后自动跳转到下一位
        MoveToNext(m_nCurSel);
    }
     
     
    BOOL CTimeWnd::PreTranslateMessage(MSG *pMsg)
    {
        if (pMsg->message == WM_KEYDOWN)
        {
            if (pMsg->wParam == VK_UP)
            {
                ChangeSpin(1);
                return true;
            }
            if (pMsg->wParam == VK_DOWN)
            {
                ChangeSpin(-1);
                return true;
            }
            if (pMsg->wParam == VK_LEFT)
            {
                ReDraw(false);
                m_nCurSel = (m_nCurSel + valCount - 1) % valCount;
                ReDraw(true);
                return true;
            }
            if (pMsg->wParam == VK_RIGHT)
            {
                ReDraw(false);
                m_nCurSel = (m_nCurSel + 1) % valCount;
                ReDraw(true);
                return true;
            }
            if (pMsg->wParam == VK_END)
            {        
                m_nValue[m_nCurSel] = m_nValueMax[m_nCurSel];
            }
            if (pMsg->wParam == VK_HOME)
            {
                m_nValue[m_nCurSel] = 0;
            }
        }
    
        
        else if (pMsg->message == WM_CHAR)  // 2024/9/4
        {
            if ((pMsg->wParam >= '0' && pMsg->wParam <= '9'))
            {
                ChangeChar(pMsg->wParam, '0');
                ReDraw(true);
                return TRUE;
            }
        }
        return CWnd::PreTranslateMessage(pMsg);
    }
     
    double CTimeWnd::GetTime()
    {
        double dTime = 0;
        for (int i=0; i<valCount; i++)
        {
            dTime += m_nValue[i] * m_dPower[i];
        }
        return dTime;
    }
     
    int CTimeWnd::GetValue(int nType)
    {
        if (!IsCountValid(nType)) 
            return 0;
        else
        {
            if (nType == 0)
            {
                return m_nValue[0]*10+ m_nValue[1];
            }
            else if (nType == 1)
            {
                return m_nValue[2] * 10 + m_nValue[3];
            }
            else if (nType == 2)
            {
                return m_nValue[4] * 10 + m_nValue[5];
            }
            else
            {
                return m_nValue[6] * 100 + m_nValue[7] * 10 + m_nValue[8];
            }
        }
    
    }
     
    void CTimeWnd::SetValue(int nType, int nVal)
    {
        if ((!IsCountValid(nType)) || nVal<0) return;
        else
        {
            int N1 = 0, N2 = 0, N3 = 0;
            if (nType == CTimeWnd::valHours)
            {
                if (nVal >= 10)
                {
                    N1 = nVal / 10;    // 十位  
                    N2 = nVal % 10;     // 个位  
                    m_nValue[valHours] = N1 % m_nValueMax[valHours];
                    m_nValue[valHours1] = N2 % m_nValueMax[valHours1];
                }
                else
                {
                    m_nValue[valHours] = 0;
                    m_nValue[valHours1] = nVal;
                }
            }
            else if (nType == CTimeWnd::valMinutes)
            {  
                if (nVal >= 10)
                {
                    N1 = nVal / 10;    // 十位  
                    N2 = nVal % 10;     // 个位  
                    m_nValue[valMinutes] = N1 % m_nValueMax[valMinutes];
                    m_nValue[valMinutes1] = N2 % m_nValueMax[valMinutes1];
                }
                else
                {
                    m_nValue[valMinutes] = 0;
                    m_nValue[valMinutes1] = nVal;
                }
            }
            else if (nType == CTimeWnd::valSeconds)
            {
                if (nVal >= 10)
                {
                    N1 = nVal / 10;    // 十位  
                    N2 = nVal % 10;     // 个位  
                    m_nValue[valSeconds] = N1 % m_nValueMax[valSeconds];
                    m_nValue[valSeconds1] = N2 % m_nValueMax[valSeconds1];
                }
                else
                {
                    m_nValue[valSeconds] = 0;
                    m_nValue[valSeconds1] = nVal;
                }
            }
            else if (nType == CTimeWnd::valMilliseconds)
            {
                // 拆分毫秒  
                if (nVal >= 100)
                {
                    N1 = nVal / 100; // 百位 
                    nVal = nVal % 100;
                    N2 = nVal / 10;  // 十位
                    N3 = nVal % 10;  // 个位
    
                    m_nValue[valMilliseconds] = N1 % m_nValueMax[valMilliseconds];
                    m_nValue[valMilliseconds1] = N2 % m_nValueMax[valMilliseconds1];
                    m_nValue[valMilliseconds2] = N3 % m_nValueMax[valMilliseconds2];
                }
                else if (nVal >= 10)
                {
                    N1 = 0;
                    N2 = nVal / 10; // 十位  
                    N3 = nVal % 10;  // 个位  
    
                    m_nValue[valMilliseconds] = N1;
                    m_nValue[valMilliseconds1] = N2 % m_nValueMax[valMilliseconds1];
                    m_nValue[valMilliseconds2] = N3 % m_nValueMax[valMilliseconds2];
                }
                else
                {
                    N1 = 0;
                    N2 = 0;
                    N3 = nVal;
    
                    m_nValue[valMilliseconds] = N1;
                    m_nValue[valMilliseconds1] = N2;
                    m_nValue[valMilliseconds2] = N3 % m_nValueMax[valMilliseconds2];
                }
            }
    
        }
    
        VarifyRange();
    }
     
    void CTimeWnd::SetTime(double dTime)
    {
        double nTemVal = 0;
        for (int i=0; i<valCount; i++)
        {
            m_nValue[i] = (int)((dTime+0.0005 - nTemVal) / m_dPower[i]);
            nTemVal += m_nValue[i] * m_dPower[i];
            m_nValue[i] %= m_nValueMax[i]; 
        }
        VarifyRange();
    }
     
    bool CTimeWnd::IsFocus()
    {
        return m_bFocus;
    }
     
    void CTimeWnd::OnSize(UINT nType, int cx, int cy)
    {
        CWnd::OnSize(nType, cx, cy);
        m_Spin.MoveWindow(cx-19, 1, 18, cy-2);
        
        MoveWindow(x, y, width, height); // 2024/3/4
        Invalidate();
    }
     
    void CTimeWnd::OnCaptureChanged(CWnd *pWnd)
    {
        CWnd::OnCaptureChanged(pWnd);
    }
     
    void CTimeWnd::OnEnable(BOOL bEnable)
    {
        CWnd::OnEnable(bEnable);
        Invalidate();
        m_Spin.EnableWindow(bEnable);
    }
     
    int CTimeWnd::GetNumberCount(int num)
    {
        int i = 0;
        if (num == 0)
        {
            return 1;
        }
        while (num)
        {
            num /= 10;    
            i++;
        };
        return i;
    }
     
    bool CTimeWnd::IsCountValid(int nCount)
    {
        if ((nCount>=0) && (nCount<valCount))
        {
            return true;
        }
        return false;
    }
    
    void CTimeWnd::SetColorStyle(COLORREF nFont, COLORREF nBack, COLORREF nFrame)
    {
        m_nFontColor = nFont;
        m_nBackColor = nBack;
        m_Spin.SetColorStyle(m_nFontColor,m_nBackColor,nFrame);
        m_oBackBrush.CreateSolidBrush(nBack);
    }
    
    
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(21条)

报告相同问题?

问题事件

  • 系统已结题 10月12日
  • 已采纳回答 10月4日
  • 修改了问题 9月29日
  • 创建了问题 9月29日