⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 downloadui.cpp

📁 FreeAMP(MP3播放)程序源代码-用来研究MP3解码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
            int32 cxImage = 0, cyImage = 0;
            HDC hDc;
            HBITMAP hBitmap, hSavedBitmap;
            HFONT   hSavedFont;
            
            hDc = CreateCompatibleDC(dis->hDC);
            hBitmap = CreateCompatibleBitmap(dis->hDC, 
                       dis->rcItem.right - dis->rcItem.left,
                       dis->rcItem.bottom - dis->rcItem.top);
            hSavedBitmap = (HBITMAP)SelectObject(hDc, hBitmap);           
            hSavedFont = (HFONT)SelectObject(hDc, (HFONT)GetStockObject(DEFAULT_GUI_FONT));
            
            rcClip.left = 0;
            rcClip.top = 0;
            rcClip.right = dis->rcItem.right - dis->rcItem.left;            
            rcClip.bottom = dis->rcItem.bottom - dis->rcItem.top;            

            // Get Image List
            himl = ListView_GetImageList(dis->hwndItem, LVSIL_SMALL);
            ImageList_GetIconSize(himl, &cxImage, &cyImage);

            // Check to see if this item is selected
            if(dis->itemState & ODS_SELECTED)
            {
                // Set the text background and foreground colors
                SetTextColor(hDc, GetSysColor(COLOR_HIGHLIGHTTEXT));
                SetBkColor(hDc, GetSysColor(COLOR_HIGHLIGHT));

                HBRUSH brush = CreateSolidBrush(GetSysColor(COLOR_HIGHLIGHT));
                FillRect(hDc, &rcClip, brush);
                DeleteObject(brush);
            }
            else
            {
                // Set the text background and foreground colors to the
                // standard window colors
                SetTextColor(hDc, GetSysColor(COLOR_WINDOWTEXT));
                SetBkColor(hDc, GetSysColor(COLOR_WINDOW));
                
                HBRUSH brush = CreateSolidBrush(GetSysColor(COLOR_WINDOW));
                FillRect(hDc, &rcClip, brush);
                DeleteObject(brush);
            }

            //LV_ITEM* item = (LV_ITEM*)dis->itemData;
            DownloadItem* dli = (DownloadItem*)dis->itemData;

            string displayString;

            if(dli->GetMetaData().Title().size())
                displayString = dli->GetMetaData().Title();
            else
                displayString = dli->DestinationFile();

            // Check to see if the string fits in the clip rect.
            // If not, truncate the string and add "...".
            CalcStringEllipsis(hDc, 
                               displayString, 
                               ListView_GetColumnWidth(m_hwndList, 0) - (cxImage + 1));

            ExtTextOut( hDc, 
                        cxImage + 1, 1, 
                        ETO_CLIPPED | ETO_OPAQUE,
                        &rcClip, 
                        displayString.c_str(),
                        displayString.size(),
                        NULL);

            // draw the icon
            if (himl)
            {
                ImageList_Draw( himl, 0, hDc, 0, 0, uiFlags);
            }

            // draw the progress column

            rcClip.left += ListView_GetColumnWidth(m_hwndList, 0);

            int32 progressWidth = 0;

            switch(dli->GetState())
            {
                case kDownloadItemState_Queued:
                {
                    if(!(dis->itemState & ODS_SELECTED))
                        SetTextColor(hDc, RGB(0, 127, 0));

                    ostringstream ost;
                    float total;
                    
                    ost.precision(2);
                    ost.flags(ios_base::fixed);

                    total = dli->GetTotalBytes();

                    if(total >= 1048576)
                    {
                        total /= 1048576;
                        ost << "Queued (" << total << " MB)";
                    }
                    else if(total >= 1024)
                    {
                        total /= 1024;
                        ost << "Queued (" << total << " KB)";
                    }
                    else
                    {
                        ost << "Queued (" << dli->GetTotalBytes() << " Bytes)";
                    }
                   
                    displayString = ost.str();

                    break;
                }

                case kDownloadItemState_Downloading:
                {
                    if(!(dis->itemState & ODS_SELECTED))
                        SetTextColor(hDc, RGB(0, 0, 192));

                    ostringstream ost;
                    float total;
                    float recvd;
                    uint32 percent;
                    
                    ost.precision(2);
                    ost.flags(ios_base::fixed);

                    total = dli->GetTotalBytes();
                    recvd = dli->GetBytesReceived();
                    percent= (uint32)recvd/total*100;

                    if(total >= 1048576)
                    {
                        total /= 1048576;
                        recvd /= 1048576;
                        ost  << percent << "% (" << recvd << " of "<< total << " MB) ";
                    }
                    else if(total >= 1024)
                    {
                        total /= 1024;
                        recvd /= 1024;
                        ost << percent << "% ("<< recvd << " of "<< total << " KB)";
                    }
                    else
                    {
                        ost << percent << "% (" << dli->GetBytesReceived() << " of " << 
                            dli->GetTotalBytes() << " Bytes)";
                    }
                   
                    displayString = ost.str();

                    SIZE stringSize;
                    
                    GetTextExtentPoint32(hDc, displayString.c_str(), 
                                            displayString.size(), &stringSize);
                    
                    int32 leftoverWidth = ListView_GetColumnWidth(m_hwndList, 1) - (stringSize.cx + kTotalPadding);

                    // do we have room to show a progress bar?
                    if(leftoverWidth - kMinProgressWidth > 0)
                    {
                        progressWidth = leftoverWidth; // padding on ends and between elements
                        int32 bmpWidth = (float)(progressWidth - 3) * (float)percent/(float)100;
                        int32 count = bmpWidth/(kProgressWidth);
                        int32 remainder = bmpWidth%(kProgressWidth);

                        //ostringstream debug;
                        //debug << "bmpWidth: " << bmpWidth << endl << "progressWidth: " << progressWidth << endl;
                        //OutputDebugString(debug.str().c_str());

                        HDC memDC = CreateCompatibleDC(hDc);
                        SelectObject(memDC, m_progressBitmap);

                        rcClip.left += kPrePadding;

                        RECT progressRect = rcClip;

                        progressRect.top += ((rcClip.bottom - rcClip.top) - kProgressHeight)/2 - 1;
                        progressRect.bottom = progressRect.top + kProgressHeight + 2;
                        progressRect.right = progressRect.left + progressWidth;

                        if(dis->itemState & ODS_SELECTED)
                        {
                            HBRUSH brush = CreateSolidBrush(GetSysColor(COLOR_WINDOW));

                            FillRect(hDc, &progressRect, brush);
                            DeleteObject(brush);
                        }

                        DrawEdge(hDc, &progressRect, EDGE_SUNKEN, BF_RECT);

                        uint32 i = 0;

                        for(i = 0; i< count; i++)
                        {
                            BitBlt(hDc, progressRect.left + 2 + i*kProgressWidth, progressRect.top + 2, kProgressWidth, kProgressHeight, 
                                   memDC, 0, 0, SRCCOPY);

                        }

                        if(remainder)
                        {
                            BitBlt(hDc, progressRect.left + 2 + i*kProgressWidth, progressRect.top + 2, remainder, kProgressHeight, 
                                   memDC, 0, 0, SRCCOPY);
                        }

                        DeleteDC(memDC);
                    }

                    break;
                }

                case kDownloadItemState_Cancelled:
                {
                    if(!(dis->itemState & ODS_SELECTED))
                        SetTextColor(hDc, RGB(192, 0, 0));

                    ostringstream ost;

                    ost << "Cancelled";

                    displayString = ost.str();
                    break;
                }

                case kDownloadItemState_Paused:
                {
                    if(!(dis->itemState & ODS_SELECTED))
                        SetTextColor(hDc, RGB(0, 128, 128));

                    ostringstream ost;
                    float total;
                    float recvd;
                    uint32 percent;
                    
                    ost.precision(2);
                    ost.flags(ios_base::fixed);

                    total = dli->GetTotalBytes();
                    recvd = dli->GetBytesReceived();
                    percent= (uint32)recvd/total*100;

                    if(total >= 1048576)
                    {
                        total /= 1048576;
                        recvd /= 1048576;
                        ost << "Paused (" << recvd << " of "<< total << " MB - " << percent << "%)";
                    }
                    else if(total >= 1024)
                    {
                        total /= 1024;
                        recvd /= 1024;
                        ost << "Paused (" << recvd << " of "<< total << " KB - " << percent << "%)";
                    }
                    else
                    {
                        ost << "Paused (" << dli->GetBytesReceived() << " of " << 
                            dli->GetTotalBytes() << " Bytes - " << percent << "%)";
                    }
                   
                    displayString = ost.str();

                    break;
                }

                case kDownloadItemState_Error:
                {
                    if(!(dis->itemState & ODS_SELECTED))
                        SetTextColor(hDc, RGB(192, 0, 0));

                    ostringstream ost;
                    int32 index = (int32)dli->GetDownloadError();

                    ost << "Error: " <<  ErrorString[index];

                    displayString = ost.str();
                    break;
                }

                case kDownloadItemState_Done:
                    displayString = string("Download Complete");
                    break;

                default:
                    break;
            }
            uint32 pad = kPrePadding;

            if(progressWidth)
                pad = (progressWidth + kElementPadding);

            CalcStringEllipsis(hDc, 
                               displayString, 
                               ListView_GetColumnWidth(m_hwndList, 1) - pad);

            rcClip.left += pad;

            ExtTextOut( hDc, 
                        rcClip.left, rcClip.top + 1, 
                        ETO_CLIPPED | ETO_OPAQUE,
                        &rcClip, 
                        displayString.c_str(),
                        displayString.size(),
                        NULL);

            // If we changed the colors for the selected item, undo it
            if(dis->itemState & ODS_SELECTED)
            {
                // Set the text background and foreground colors
                SetTextColor(hDc, GetSysColor(COLOR_WINDOWTEXT));
                SetBkColor(hDc, GetSysColor(COLOR_WINDOW));
            }

            // If the item is focused, now draw a focus rect around the entire row
            if(dis->itemState & ODS_FOCUS)
            {
                // Draw the focus rect
                DrawFocusRect(hDc, &dis->rcItem);
            }

            // paint the actual bitmap
            BitBlt(dis->hDC, dis->rcItem.left, dis->rcItem.top,
                             dis->rcItem.right - dis->rcItem.left, 
                             dis->rcItem.bottom - dis->rcItem.top, 
                   hDc, 0, 0, SRCCOPY);          

            SelectObject(hDc, hSavedBitmap);
            DeleteObject(hBitmap);
            SelectObject(hDc, hSavedFont);
            DeleteDC(hDc);

            break;
        }
 
        case IDC_INFO:
        {
            SetTextColor(dis->hDC, RGB(0, 0, 128));
            SetBkColor(dis->hDC, GetSysColor(COLOR_WINDOW));
            string displayString;

            string longestString = string("File Name :");

            SIZE stringSize;
                    
            GetTextExtentPoint32(dis->hDC, longestString.c_str(), 
                                    longestString.size(), &stringSize);

            uint32 dataOffset = stringSize.cx + 15;


            RECT rcClip = dis->rcItem;  

            rcClip.left += 2;

            uint32 itemCount =  ListView_GetItemCount(m_hwndList);
            DownloadItem* dli = NULL;

            //ostringstream debug;
            //debug << "itemCount: " << itemCount << endl;
            //OutputDebugString(debug.str().c_str());

            if(itemCount)
            {
                LV_ITEM item;

                for(uint32 i = 0; i < itemCount; i++)
                {
                    if(ListView_GetItemState(m_hwndList, i, LVIS_FOCUSED) & LVIS_FOCUSED)
                    {
                        item.mask = LVIF_PARAM;
                        item.iItem = i;
                        item.lParam = 0;

                        if(ListView_GetItem(m_hwndList, &item))
                        {
                            dli = (DownloadItem*)item.lParam;
                            break;
                        }
                    }
                }

                if(!dli)
                {
                    item.mask = LVIF_PARAM;
                    item.iItem = 0;
                    item.lParam = 0;

                    ListView_GetItem(m_hwndList, &item);

                    dli = (DownloadItem*)item.lParam;
                }
            }

            switch(dis->itemID)
            {
                case 0:
                {
                    displayString = string("Artist :");
                    ExtTextOut( dis->hDC, 
                        rcClip.left, rcClip.top + 1, 
                        ETO_CLIPPED | ETO_OPAQUE,
                        &rcClip, 
                        displayString.c_str(),
                        displayString.size(),
                        NULL);

                    rcClip.left = dis->rcItem.left + dataOffset;

                    displayString = string("");

                    if(dli)
                        displayString = dli->GetMetaData().Artist();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -