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

📄 playlistview.cpp

📁 一个简单漂亮的C++编写的mp3播放器
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/*____________________________________________________________________________

        FreeAmp - The Free MP3 Player

        Portions Copyright (C) 1999 EMusic.com

        This program is free software; you can redistribute it and/or modify
        it under the terms of the GNU General Public License as published by
        the Free Software Foundation; either version 2 of the License, or
        (at your option) any later version.

        This program is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU General Public License for more details.

        You should have received a copy of the GNU General Public License
        along with this program; if not, write to the Free Software
        Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

        $Id: PlaylistView.cpp,v 1.48 2000/01/14 09:16:21 elrod Exp $
____________________________________________________________________________*/

#define STRICT
#include <windows.h>
#include <windowsx.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <direct.h>

#include "config.h"
#include "utility.h"
#include "resource.h"
#include "Win32MusicBrowser.h"
#include "DropSource.h"
#include "DropObject.h"

#define kPrePadding 5

BOOL MusicBrowserUI::DrawItem(int32 controlId, DRAWITEMSTRUCT* dis)
{
    BOOL result = TRUE;

    switch(controlId)
    {
        /*case IDC_STATUS:
        {
            RECT rcClip;
            char* statusText = (char*)dis->itemData;

            rcClip = dis->rcItem;

            UINT oldAlign;

            //oldAlign = SetTextAlign(dis->hDC, TA_RIGHT | TA_TOP );
            
            ExtTextOut( dis->hDC, 
                        rcClip.left, rcClip.top + 1,
                        ETO_CLIPPED | ETO_OPAQUE,
                        &rcClip, 
                        statusText,
                        strlen(statusText),
                        NULL);

            //SetTextAlign(dis->hDC, oldAlign);

            break;
        }*/

        case IDC_PLAYLISTBOX:
        {
            uint32 uiFlags = ILD_TRANSPARENT;
            RECT rcClip;
            HIMAGELIST himl;
            HFONT boldFont = NULL, oldFont = NULL;
            int32 cxImage = 0, cyImage = 0;

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

            rcClip = dis->rcItem;
        
            HWND hwndList = GetDlgItem(m_hWnd, IDC_PLAYLISTBOX);
            PlaylistItem* item;
            LV_ITEM lv_item;
            string displayString;

            lv_item.mask = LVIF_PARAM;
            lv_item.iItem = dis->itemID;
            lv_item.iSubItem = 0;
            lv_item.lParam = NULL;

            ListView_GetItem(hwndList, &lv_item);

            //item = (PlaylistItem*) lv_item.lParam;
            item = m_oPlm->ItemAt(dis->itemID);

            if(item == NULL)
                return FALSE;

            // is this the current index? if so make it bold ...
            // btw, we only do this if it is the primary browser
            if(dis->itemID == m_oPlm->GetCurrentIndex() && !m_pParent)
            {
                LOGFONT lf;

                GetObject(GetWindowFont(hwndList), sizeof(LOGFONT), &lf);

                lf.lfWeight = FW_BOLD;

                boldFont = CreateFontIndirect(&lf);

                oldFont = (HFONT)SelectObject(dis->hDC, boldFont);
            }

            // Item index
            char buf[64];
            sprintf(buf, "%d", dis->itemID + 1);
            displayString = buf;
            
            CalcStringEllipsis(dis->hDC, 
                               displayString, 
                               ListView_GetColumnWidth(hwndList, 0) /*- (cxImage + 1)*/);

            UINT oldAlign;

            oldAlign = SetTextAlign(dis->hDC, TA_CENTER | TA_TOP );

            RECT indexRect = rcClip;

            indexRect.right = indexRect.left + ListView_GetColumnWidth(hwndList, 0) - 1;

            UINT left = indexRect.left + (ListView_GetColumnWidth(hwndList, 0)/2);

            COLORREF scrollColor = GetSysColor(COLOR_SCROLLBAR);
            COLORREF winColor = GetSysColor(COLOR_WINDOW);

            if(scrollColor == winColor)
            {
                int r = GetRValue(scrollColor);
                int g = GetGValue(scrollColor);
                int b = GetBValue(scrollColor);

                if(( r + g + b)/3 < 128)
                {
                    r -= 25;
                    g -= 25;
                    b -= 25;

                    if(r < 0)
                        r = 0;
                    if(g < 0)
                        g = 0;
                    if(b < 0)
                        b = 0;
                }
                else
                {
                    r += 25;
                    g += 25;
                    b += 25;

                    if(r > 255)
                        r = 255;
                    if(g > 255)
                        g = 255;
                    if(b > 255)
                        b = 255;
                }

                SetBkColor(dis->hDC, RGB(r, g, b));
            }
            else
            {
                SetBkColor(dis->hDC, GetSysColor(COLOR_SCROLLBAR)); //COLOR_INFOBK ));
            }
            
            SetTextColor(dis->hDC, GetSysColor(COLOR_WINDOWTEXT)); //COLOR_INFOTEXT));
            

            ExtTextOut( dis->hDC, 
                        left, indexRect.top + 1,
                        ETO_CLIPPED | ETO_OPAQUE,
                        &indexRect, 
                        displayString.c_str(),
                        displayString.size(),
                        NULL);

            SetTextAlign(dis->hDC, oldAlign);

            
            // Move over to the next column
            rcClip.left = indexRect.right; //ListView_GetColumnWidth(hwndList, 0);

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

		        // Also add the ILD_BLEND50 so the images come out selected
		        //uiFlags |= ILD_BLEND50;
            }
            else
            {
                // Set the text background and foreground colors to the
                // standard window colors
                SetTextColor(dis->hDC, GetSysColor(COLOR_WINDOWTEXT));
                SetBkColor(dis->hDC, GetSysColor(COLOR_WINDOW));
            }


            // Title
            //ListView_GetItemText(hwndList, dis->itemID, 1, buf, 1024);
            displayString = item->GetMetaData().Title();

            CalcStringEllipsis(dis->hDC, 
                               displayString, 
                               ListView_GetColumnWidth(hwndList, 1) - kPrePadding);

            ExtTextOut( dis->hDC, 
                        rcClip.left + kPrePadding, rcClip.top + 1, 
                        ETO_CLIPPED | ETO_OPAQUE,
                        &rcClip, 
                        displayString.c_str(),
                        displayString.size(),
                        NULL);            

            // Move over to the next column
            rcClip.left += ListView_GetColumnWidth(hwndList, 1);

            // Artist
            //ListView_GetItemText(hwndList, dis->itemID, 2, buf, 1024);
            displayString = item->GetMetaData().Artist();

            CalcStringEllipsis(dis->hDC, 
                               displayString, 
                               ListView_GetColumnWidth(hwndList, 2) - kPrePadding);

            ExtTextOut( dis->hDC, 
                        rcClip.left + kPrePadding, rcClip.top + 1, 
                        ETO_CLIPPED | ETO_OPAQUE,
                        &rcClip, 
                        displayString.c_str(),
                        displayString.size(),
                        NULL);

            // Move over to the next column
            rcClip.left += ListView_GetColumnWidth(hwndList, 2);

            // Album
            //ListView_GetItemText(hwndList, dis->itemID, 3, buf, 1024);
            displayString = item->GetMetaData().Album();

            CalcStringEllipsis(dis->hDC, 
                               displayString, 
                               ListView_GetColumnWidth(hwndList, 3) - kPrePadding);

            ExtTextOut( dis->hDC, 
                        rcClip.left + kPrePadding, rcClip.top + 1, 
                        ETO_CLIPPED | ETO_OPAQUE,
                        &rcClip, 
                        displayString.c_str(),
                        displayString.size(),
                        NULL);

            // Move over to the next column
            rcClip.left += ListView_GetColumnWidth(hwndList, 3);

            // Length
            //ListView_GetItemText(hwndList, dis->itemID, 4, buf, 1024);

            if(item->GetMetaData().Time() != 0)
            {
                int32 seconds = item->GetMetaData().Time();
                int32 hours = seconds / 3600;
		        int32 minutes = seconds / 60 - hours * 60;
                seconds = seconds - minutes * 60 - hours * 3600;

                if(hours)
                    sprintf(buf, "%d:%02d:%02d", hours, minutes, seconds);
                else
                    sprintf(buf, "%d:%02d", minutes, seconds);

                displayString = buf;
            }
            else    
                displayString = "Unknown";

            CalcStringEllipsis(dis->hDC, 
                               displayString, 
                               ListView_GetColumnWidth(hwndList, 4) - kPrePadding);

            ExtTextOut( dis->hDC, 
                        rcClip.left + kPrePadding, rcClip.top + 1, 
                        ETO_CLIPPED | ETO_OPAQUE,
                        &rcClip, 
                        displayString.c_str(),
                        displayString.size(),
                        NULL);

            // Move over to the next column
            rcClip.left += ListView_GetColumnWidth(hwndList, 4);

            // If we changed font undo it
            if(dis->itemID == m_oPlm->GetCurrentIndex() && !m_pParent)
            {
                SelectObject(dis->hDC, oldFont);
                DeleteObject(boldFont);
            }

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

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

            break;
        }


    }

    return result;
}

#define LENGTH_COLUMN_WIDTH 60
#define INDEX_COLUMN_WIDTH 25
#define FIXED_COLUMN_WIDTH (LENGTH_COLUMN_WIDTH + INDEX_COLUMN_WIDTH)

void MusicBrowserUI::InitList(void)
{
    LV_COLUMN lvc;
    RECT      sRect;
    
    ListView_DeleteAllItems(GetDlgItem(m_hWnd, IDC_PLAYLISTBOX));
    GetClientRect(GetDlgItem(m_hWnd, IDC_PLAYLISTBOX), &sRect);

    lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT;
    lvc.fmt = LVCFMT_LEFT; // left align column

    lvc.pszText = "#";
    lvc.cchTextMax = strlen(lvc.pszText);
    lvc.iSubItem = 0;
    lvc.cx = INDEX_COLUMN_WIDTH; // width of column in pixels
    ListView_InsertColumn(m_hPlaylistView, 0, &lvc);

    int32 remainder = (sRect.right-sRect.left - FIXED_COLUMN_WIDTH)%3;

    lvc.pszText = "Title";
    lvc.cchTextMax = strlen(lvc.pszText);
    lvc.iSubItem = 1;
    lvc.cx = (sRect.right-sRect.left - FIXED_COLUMN_WIDTH)/3; // width of column in pixels
    ListView_InsertColumn(m_hPlaylistView, 1, &lvc);
    
    lvc.pszText = "Artist";
    lvc.cchTextMax = strlen(lvc.pszText);
    lvc.iSubItem = 2;
    ListView_InsertColumn(m_hPlaylistView, 2, &lvc);

    lvc.pszText = "Album";
    lvc.cchTextMax = strlen(lvc.pszText);
    lvc.iSubItem = 3;
    ListView_InsertColumn(m_hPlaylistView, 3, &lvc);

    
    lvc.pszText = "Length";
    lvc.cx = LENGTH_COLUMN_WIDTH + remainder;//((sRect.right-sRect.left)/4) - 3; // width of column in pixels
    lvc.cchTextMax = strlen(lvc.pszText);
    lvc.iSubItem = 4;
    ListView_InsertColumn(m_hPlaylistView, 4, &lvc);

    if(m_itemsAddedBeforeWeWereCreated)
    {
        for(uint32 i = 0; i < m_itemsAddedBeforeWeWereCreated; i++)
        {
            LV_ITEM lv_item;

            lv_item.mask = 0;
            lv_item.iSubItem = 0;
            lv_item.iItem = 0;

            ListView_InsertItem(m_hPlaylistView, &lv_item);
        }

        m_itemsAddedBeforeWeWereCreated = 0;
    }

    //HMENU menu = GetSubMenu(GetMenu(m_hWnd), 1);

    //EnableMenuItem(menu, ID_EDIT_UNDO_ACTION, (m_oPlm->CanUndo() ? MF_ENABLED : MF_GRAYED));
    //EnableMenuItem(menu, ID_EDIT_REDO_ACTION, (m_oPlm->CanRedo() ? MF_ENABLED : MF_GRAYED));
}

void MusicBrowserUI::PlaylistListItemMoved(const PlaylistItem* item, 
                                           uint32 oldIndex, 
                                           uint32 newIndex)
{
    HWND    hwnd = GetDlgItem(m_hWnd, IDC_PLAYLISTBOX);
    uint32  index = m_oPlm->IndexOf(item);

    if(index != kInvalidIndex)
    {
        m_bListChanged = true;
        UpdateButtonMenuStates();

        //char buf[256];
        //sprintf(buf, "oldIndex: %d\tnewIndex: %d\r\n", oldIndex, newIndex);

        //OutputDebugString(buf);

        //LV_ITEM lv_item;

        uint32 state = ListView_GetItemState(m_hPlaylistView, 
                                             oldIndex, 
                                             LVIS_SELECTED|LVIS_FOCUSED);

        /*lv_item.mask = LVIF_IMAGE | LVIF_PARAM | LVIF_STATE;
        lv_item.iSubItem = 0;
        lv_item.iItem = index;
        lv_item.lParam = (LPARAM)item;
        lv_item.iImage = 0;
        lv_item.stateMask = LVIS_FOCUSED|LVIS_SELECTED;
        lv_item.state = state;*/

        ListView_SetItemState(m_hPlaylistView, 
                              oldIndex, 
                              0,
                              LVIS_SELECTED|LVIS_FOCUSED);

        ListView_SetItemState(m_hPlaylistView, 
                              newIndex, 
                              state,

⌨️ 快捷键说明

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