Win7任务栏进度条c++的实现

To compile this code, either use Visual Studio 2010 or download the Windows 7 SDK from MSDN website.

// Declare this as member of the class or global (not recommended)
ITaskbarList3* m_pTaskBarlist;
void CTaskBarSampleDlg::OnBnClickedButtonStart()
{
    m_Progress.SetRange( 0, 10 ); // set the range for the control
    m_Progress.SetPos( 0 ); // Set initial position
    // Initialize the pointer. You can also do this in the constructor.
    // Remember to release after use
    if( NULL == m_pTaskBarlist )
    {
        CoCreateInstance(
            CLSID_TaskbarList, NULL, CLSCTX_ALL,
            IID_ITaskbarList3, (void**)&m_pTaskBarlist);
    }
    m_pTaskBarlist->SetProgressState( m_hWnd, TBPF_INDETERMINATE );
    SetTimer( 0, 500, 0 );
}
// Timer to update the progress
void CTaskBarSampleDlg::OnTimer(UINT_PTR nIDEvent)
{
    int nPos = m_Progress.GetPos();
    int nMin, nMax;
    m_Progress.GetRange( nMin, nMax );
    nPos++;
    // if finished, kill timer and return
    if( nPos > nMax )
    {
        // reset the progress state
        m_pTaskBarlist->SetProgressState( m_hWnd, TBPF_NOPROGRESS );
        // just flash the window to notify user
        FlashWindow( true );
        // Stop the timer
        KillTimer( 0 );
        // You can release the pointer if necessary here. 
                  // But not a good practice
        return;
    }
    // set progress to taskbar overlay
    m_pTaskBarlist->SetProgressValue( m_hWnd, nPos, nMax );
    // set the control's progress state
    m_Progress.SetPos( nPos );
}

 

posted @ 2017-02-06 14:28  秋月的私语  阅读(789)  评论(0)    收藏  举报