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

📄 playlistdlg.cpp

📁 media player 控件源码 用EVC编译可以进行对WINCE下media player控制
💻 CPP
📖 第 1 页 / 共 4 页
字号:
                mii.dwItemData = (DWORD)pMenuStruct;

                if (!InsertMenu(hmenuPopup, cItems, 
                        MF_BYPOSITION | MF_OWNERDRAW, 
                        ID_PLAYLIST1_HISTORY + iMRU, NULL))
                {
                    ASSERT(FALSE);
                    break;
                }

                SetMenuItemInfo(hmenuPopup, cItems, TRUE, &mii);
            }

            //
            // Remove the separator if there are no MRU playlists
            //

            if (0 == pManager->MRUPlaylistCount())
            {
                RemoveMenu(hmenuPopup, cItems, MF_BYPOSITION);
            }
        }

        //
        // Display the menu
        //

        POINT ptPopup = { 0, APPBAR_HEIGHT };

        ClientToScreen(m_hwndDlg, &ptPopup);

        cmd = TrackPopupMenuEx(hmenuPopup,
                               TPM_LEFTALIGN | TPM_TOPALIGN | TPM_NONOTIFY | TPM_RETURNCMD,
                               ptPopup.x, ptPopup.y,
                               m_hwndDlg,
                               NULL);

        if (0 != cmd && ID_FAVORITES != cmd)
        {
            fRestoreText = FALSE;
        }

        //
        // Now re-post the selected command, if any
        //
        if (0 != cmd)
        {
            PostMessage(m_hwndDlg, WM_COMMAND, cmd, 0);
        }
    } while (FALSE);

    if (fRestoreText)
    {
        UpdateInfo();
    }

    if (NULL != hmenuPopup)
    {
        for (i = 0; i < sizeof (rgIDs) / sizeof (rgIDs[0]); i++)
        {
            mii.fMask = MIIM_DATA;
            mii.dwItemData = NULL;

            GetMenuItemInfo(hmenuPopup, rgIDs[i], FALSE, &mii);

            if (NULL != mii.dwItemData)
            {
                delete (DrawMenuStruct*)mii.dwItemData;
            }
        }
    }

    if (NULL != hmenu)
    {
        DestroyMenu(hmenu);
    }
}

void CPlaylistDialog::SetDropdownText(LPTSTR szText, int iImage)
{
    // first make sure the text fits in the control
    SIZE sizText, sizEllipsis;
    TBBUTTONINFO tbbi;
    BOOL fRet, fTrunc = FALSE;
    LPTSTR szDDText;
    INT nMax, nFit, nLen = _tcslen(szText);
    LPINT rgExtent = new INT[nLen];

    if (rgExtent)
        memset(rgExtent, 0, sizeof(rgExtent));

    HDC hdc = ::GetDC(m_hwndDropdown);
    ASSERT(hdc);

    RECT rcDlg;
    fRet = GetWindowRect(m_hwndDlg, &rcDlg);
    ASSERT(fRet);

    nMax = rcDlg.right - rcDlg.left - 60;

    fRet = GetTextExtentExPoint(hdc, szText, nLen,
                nMax, &nFit, rgExtent, &sizText);
    ASSERT(fRet);

    if (nFit && rgExtent && nFit < nLen)
    {
        fRet = GetTextExtentExPoint(hdc, TEXT("\x2026"),
                    1, 0, NULL, NULL, &sizEllipsis);
        ASSERT(fRet);

        nMax -= sizEllipsis.cx;

        nFit++;
        while (rgExtent[--nFit - 1] > nMax);

        szDDText = new TCHAR[nFit + 1];
        if (szDDText)
        {
            _tcsncpy(szDDText, szText, nFit - 1);
            szDDText[nFit - 1] = TEXT('\x2026');
            szDDText[nFit] = 0;
            fTrunc = TRUE;
        }
        else ASSERT(FALSE);
    }
    else
    {
        szDDText = szText;
    }

    ReleaseDC(m_hwndDropdown, hdc);
    if (rgExtent) delete [] rgExtent;

    // now fill the control info
    tbbi.cbSize = sizeof (TBBUTTONINFO);
    tbbi.dwMask = TBIF_TEXT | TBIF_IMAGE;
    tbbi.pszText = szDDText;
    tbbi.iImage  = iImage;

    SendMessage(m_hwndDropdown, TB_SETBUTTONINFO,
        IDC_PLAYLIST_DROPDOWN, (LPARAM)&tbbi);

    if (fTrunc) delete [] szDDText;
}

void CPlaylistDialog::UpdateInfo()
{
   LVITEM item;
   SIZE   size;
   HDC    hdc;
   LONG   cx = 0;
   CPlaylist             * pPlaylist = NULL;
   CPlaylist::playlist_t * pList     = NULL;
   LPCTSTR                 pszName   = NULL;
   int iImage;
   int i      = 0;
   int cxText = 0;

   CPlaylistMgr * pManager = CPlaylistMgr::GetInstance();

   hdc = ::GetDC(m_hwndDlg);

   pPlaylist = m_pPlaylist;

   if (NULL != pPlaylist)
   {
       iImage  = I_PLAYLIST;
       pszName = pPlaylist->GetName();
       pList   = pPlaylist->GetFirst();
   }
   else if (m_bOrganizeFavorites && NULL != pManager)
   {
       iImage    = I_ALL_MY_MUSIC;
       pszName   = TEXT("Favorites");
       pPlaylist = pManager->GetFavorites();
       if (pPlaylist) pList = pPlaylist->GetFirst();
   }
   else if (NULL != pManager)
   {
       iImage    = I_ALL_MY_MUSIC;
       pszName   = TEXT("Local Content");
       pManager->UpdateLocalContent();
       pPlaylist = pManager->LocalContent();
       if (pPlaylist) pList = pPlaylist->GetFirst();
   }
   else
   {
       iImage    = I_ALL_MY_MUSIC;
       pszName   = TEXT("Local Content");
       pList     = NULL;
   }

   // This causes a WM_NOTIFY, for which we call OnNotify.  OnNotify calls EnableMenuBarIcons, which can scan for content
   ListView_DeleteAllItems(m_hwndTrackList);

   while (pList)
   {
       CMediaClip * pClip = pList->pClip;

       memset(&item, 0, sizeof (item));

       item.mask    = LVIF_TEXT | LVIF_DI_SETITEM | LVIF_IMAGE;
       item.iItem   = i;
       item.iImage  = (int)pClip->GetLocation();
       item.pszText = (LPTSTR)pClip->GetTitle();
       ListView_InsertItem(m_hwndTrackList, &item);

       if (GetTextExtentExPoint(hdc, item.pszText, _tcslen(item.pszText), 0, NULL, NULL, &size))
       {
           if (size.cx > cx)
           {
               cx = size.cx;
           }
       }

       pList = pList->pNext;
       i++;
   }

   if (hdc)
   {
       ::ReleaseDC(m_hwndTrackList, hdc);
   }

   //
   // Clean up the list view
   //
   RECT rc;

   GetWindowRect(m_hwndTrackList, &rc);

   if (cx < rc.right - rc.left)
   {
       cx = rc.right - rc.left;
   }

   if (ListView_GetCountPerPage(m_hwndTrackList) < ListView_GetItemCount(m_hwndTrackList))
   {
       cx -= GetSystemMetrics(SM_CXVSCROLL);
   }

   ListView_SetColumnWidth(m_hwndTrackList, 0, cx);

   SetDropdownText((LPTSTR)pszName, iImage);

   SendMessage(m_hwndTrackList, WM_SETREDRAW, TRUE, 0);

   InvalidateRect(m_hwndTrackList, NULL, TRUE);
}

void CPlaylistDialog::OnMeasureItem(MEASUREITEMSTRUCT * pmis)
{
    DrawMenuStruct *pMenuStruct = NULL;

    pMenuStruct = (DrawMenuStruct *)pmis->itemData;

    // we have fixed height menu items
    pmis->itemHeight = MENU_ITEM_HEIGHT;

    // calculate the required width for this item's text
    HDC hdc = GetDC(m_hwndDropdown);

    LPTSTR szMenuItem = NULL;

    if (NULL != pMenuStruct)
    {
        szMenuItem = pMenuStruct->szText;
    }
    else
    {
        szMenuItem = (LPTSTR)pmis->itemData;
    }

    SIZE size;

    if (szMenuItem)
    {
        GetTextExtentPoint32(hdc, szMenuItem, _tcslen(szMenuItem), &size);
    }
    else
    {
        size.cx = 0;
    }

    ReleaseDC(m_hwndDropdown, hdc);

    // the item width includes text, and icon, and spacing
    pmis->itemWidth = size.cx + MENU_ICON_WIDTH + 3*MENU_ITEM_HORZ_MARGIN;
    pmis->itemWidth = min(pmis->itemWidth, MAX_MENU_ITEM_WIDTH);
}

void CPlaylistDialog::OnDrawItem(DRAWITEMSTRUCT *pdis)
{
    HDC hdc = pdis->hDC;
    RECT rcItem = pdis->rcItem;
    DrawMenuStruct *pMenuStruct = NULL;

    pMenuStruct = (DrawMenuStruct *)pdis->itemData;

    // Draw menu item background
    if ((ODA_SELECT == pdis->itemAction) || (ODA_DRAWENTIRE == pdis->itemAction))
    {
        if (0 != (pdis->itemState & ODS_SELECTED))
        {
            SetTextColor(hdc, GetSysColor(COLOR_HIGHLIGHTTEXT));

            FillRect(hdc, &rcItem, GetSysColorBrush(COLOR_HIGHLIGHT));
        }
        else
        {
            SetTextColor(hdc, GetSysColor(COLOR_MENUTEXT));

            FillRect(hdc, &rcItem, GetSysColorBrush(COLOR_MENU));
        }
    }

    // Draw the appropriate image
    int iImage = I_PLAYLIST;

    if (pMenuStruct)
    {
        iImage = pMenuStruct->iImage;
    }

    ImageList_DrawEx(
        m_himgPlaylist,
        iImage,
        hdc,
        rcItem.left + MENU_ITEM_HORZ_MARGIN,
        rcItem.top +1,
        0,
        0,
        CLR_NONE,
        CLR_NONE,
        ILD_TRANSPARENT);

    // Draw the menu item text
    RECT rcText = rcItem;

    rcText.left += MENU_ICON_WIDTH + (2 * MENU_ITEM_HORZ_MARGIN);

    LPTSTR szMenuItem = NULL;
    if (NULL != pMenuStruct)
    {
        szMenuItem = pMenuStruct->szText;
    }
    else
    {
        szMenuItem = (LPTSTR) pdis->itemData;
    }

    int cchMenuItem = 0;

    if (szMenuItem)
    {
        cchMenuItem = _tcslen(szMenuItem);
    }
    else
    {
        szMenuItem = TEXT("");
    }

    int iOldMode = SetBkMode(hdc, TRANSPARENT);

    DrawEllipsisText(
        hdc,
        szMenuItem,
        cchMenuItem,
        &rcText,
        DT_LEFT | DT_SINGLELINE | DT_VCENTER | DT_NOPREFIX | DT_NOCLIP);

   // clean up
   if (0 != iOldMode) SetBkMode(hdc, iOldMode);
}

void CPlaylistDialog::OnContextMenu(POINT ptHold)
{
    // The user has pushed and help, so give them a context menu
    HMENU hmenu = LoadMenu(g_hInst, MAKEINTRESOURCE(IDM_TRACKS_CONTEXT));

    if (NULL == hmenu)
    {
       return;
    }

    HMENU hmenuPopup = GetSubMenu(hmenu, 0);

    if (NULL != hmenuPopup)
    {
        DWORD dwSelected = ListView_GetSelectedCount(m_hwndTrackList);
        DWORD dwSelMark  = ListView_GetSelectionMark(m_hwndTrackList);
        DWORD dwIndex =    ListView_GetNextItem(m_hwndTrackList, -1, LVNI_SELECTED);
 
        DWORD dwType = 0;

        // only enable the play icon when one item is selected and it exists
        BOOL fEnablePlay = (1 == dwSelected);

        if (fEnablePlay)
        {
            int iTrack = ListView_GetNextItem(m_hwndTrackList, -1, LVNI_SELECTED);
            // check if it exists
        }

        if (!fEnablePlay)
        {
            EnableMenuItem(hmenuPopup, ID_PLAY_SONG, MF_BYCOMMAND | MF_GRAYED);
        }

        if( !m_pPlaylist )
        {
            EnableMenuItem(hmenuPopup, ID_REORDER_UP, MF_BYCOMMAND | MF_GRAYED );
            EnableMenuItem(hmenuPopup, ID_REORDER_DOWN, MF_BYCOMMAND | MF_GRAYED );
        }

        // Let's look at the selection. If more than one item is selection, disable Delete
        if( dwSelected != 1 )
        {
            EnableMenuItem(hmenuPopup, ID_DELETE_TRACKS, MF_BYCOMMAND | MF_GRAYED);
        }

        if(( dwIndex == 0 ) || ( dwSelected != 1 ))
        {
            EnableMenuItem(hmenuPopup, ID_REORDER_UP, MF_BYCOMMAND | MF_GRAYED);
        }

        if(!m_pPlaylist || (-1 == dwIndex ) || ( dwIndex == m_pPlaylist->GetCount() -1 ) || ( dwSelected != 1 ))
        {
            EnableMenuItem(hmenuPopup, ID_REORDER_DOWN, MF_BYCOMMAND | MF_GRAYED);
        }


        // optionally disable various menu items

        TrackPopupMenuEx(hmenuPopup,
                         TPM_LEFTALIGN | TPM_TOPALIGN,
                         ptHold.x, ptHold.y,
                         m_hwndDlg,
                         NULL);
    }

    DestroyMenu(hmenu);
}

⌨️ 快捷键说明

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