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

📄 downloadui.cpp

📁 一个简单漂亮的C++编写的mp3播放器
💻 CPP
📖 第 1 页 / 共 4 页
字号:
/*____________________________________________________________________________
	
	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: downloadui.cpp,v 1.17 2000/01/21 10:01:19 elrod Exp $
____________________________________________________________________________*/

/* system headers */
#define STRICT
#include <windows.h>
#include <commctrl.h>
#include <stdio.h>
#include <math.h>

#include <string>
#include <iostream>
#include <sstream>

using namespace std;

/* project headers */
#include "config.h"
#include "thread.h"
#include "downloadui.h"
#include "event.h"
#include "eventdata.h"
#include "playlist.h"
#include "errors.h"
#include "help.h"
#include "resource.h"
#include "debug.h"

static const int32 kProgressHeight = 8;
static const int32 kProgressWidth = 7;

static const int32 kPrePadding = 4;
static const int32 kElementPadding = 5;
static const int32 kPostPadding = 5;
static const int32 kMinProgressWidth = 3;
static const int32 kTotalPadding = kPrePadding + kElementPadding + kPostPadding;


HINSTANCE g_hInstance = NULL;

BOOL CALLBACK MainProc(	HWND hwnd, 
						UINT msg, 
						WPARAM wParam, 
						LPARAM lParam ); 

LRESULT WINAPI 
ProgressWndProc(HWND hwnd, 
                UINT msg, 
                WPARAM wParam, 
                LPARAM lParam);

LRESULT WINAPI 
FreeTracksWndProc(HWND hwnd, 
                  UINT msg, 
                  WPARAM wParam, 
                  LPARAM lParam);

extern "C" DownloadUI *Initialize(FAContext *context)
{
    return new DownloadUI(context);
}

INT WINAPI DllMain (HINSTANCE hInst,
                    ULONG ul_reason_being_called,
	                LPVOID lpReserved)
{
	switch (ul_reason_being_called)
	{
		case DLL_PROCESS_ATTACH:
			g_hInstance = hInst;
	    	break;

		case DLL_THREAD_ATTACH:
		    break;

		case DLL_THREAD_DETACH:
		    break;

		case DLL_PROCESS_DETACH:
		    break;
	}

    return 1;                 
}


DownloadUI::DownloadUI(FAContext *context):UserInterface()
{
    m_context = context;
    m_prefs = context->prefs;
    m_plm = m_context->plm;
    m_target = m_context->target;
    m_propManager = m_context->props;
    m_dlm = m_context->downloadManager;
    m_overURL = false;

    m_totalItems = 0;
    m_doneItems = 0;
    m_totalBytes = 0;
    m_doneBytes = 0;

    m_uiSemaphore = new Semaphore();

    m_uiThread = Thread::CreateThread();
    m_uiThread->Create(UIThreadFunc,this);

    m_progressBitmap = NULL;

    m_uiSemaphore->Wait();
}

DownloadUI::~DownloadUI()
{
    if(m_progressBitmap)
        DeleteObject(m_progressBitmap);

    if(m_handCursor)
        DeleteObject(m_handCursor);

    delete m_uiSemaphore;
    delete m_uiThread;


}

int32 DownloadUI::AcceptEvent(Event* event)
{
    int32 result = 255;

    if(event) 
    {
        switch(event->Type()) 
        {
	        case CMD_Cleanup: 
            {
                LV_ITEM item;
                
                if (m_hwndList)
                {
                    uint32 itemCount = ListView_GetItemCount(m_hwndList);
                    for(uint32 i = 0; i < itemCount; i++)
                    {
                        item.mask = LVIF_PARAM;
                        item.iItem = i;
                        item.lParam = 0;

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

                            if(dli->GetState() == kDownloadItemState_Downloading)
                            {
                                m_dlm->CancelDownload(dli, true);  
                            }
                        }
                    }
                }

                PostMessage(m_hwnd, WM_QUIT, 0, 0);
                m_uiThread->Join();
                
	            m_target->AcceptEvent(new Event(INFO_ReadyToDieUI));
	            break; 
            }

            case CMD_ToggleDownloadUI:
            {
                BOOL visible = IsWindowVisible(m_hwnd);
                ShowWindow(m_hwnd, (visible ? SW_HIDE: SW_SHOW));

                if(!visible)
                    SetForegroundWindow(m_hwnd);
                
                break;
            }
            
            case INFO_DownloadItemAdded: 
            {
                DownloadItemAddedEvent* dliae = (DownloadItemAddedEvent*)event;
                
                LV_ITEM item;
               
                item.mask = LVIF_PARAM | LVIF_STATE;
                item.state = 0;
                item.stateMask = 0;
                item.iItem = ListView_GetItemCount(m_hwndList);
                item.iSubItem = 0;
                item.lParam = (LPARAM)dliae->Item();

                ListView_InsertItem(m_hwndList, &item);

                UpdateOverallProgress();

                // bring window into view
                ShowWindow(m_hwnd, SW_SHOW);
                SetForegroundWindow(m_hwnd);

                if (m_dlm->IsPaused())
                   m_dlm->ResumeDownloads();
               
	            break; 
            }

            case INFO_DownloadItemRemoved: 
            {
	            DownloadItemRemovedEvent* dlire = (DownloadItemRemovedEvent*)event;

                uint32 itemCount = ListView_GetItemCount(m_hwndList);

                if(itemCount)
                {
                    LV_ITEM item;

                    for(uint32 i = 0; i < itemCount; i++)
                    {
                        item.mask = LVIF_PARAM;
                        item.iItem = i;
                        item.lParam = 0;

                        if(ListView_GetItem(m_hwndList, &item))
                        {
                            if(dlire->Item() == (DownloadItem*)item.lParam)
                            {
                                ListView_RedrawItems(m_hwndList, i, i);
                                UpdateWindow(m_hwndList);
                                break;
                            }
                        }
                    }
                }

                UpdateOverallProgress();

	            break; 
            }

            case INFO_DownloadItemNewState: 
            {
	            DownloadItemNewStateEvent* dlinse = (DownloadItemNewStateEvent*)event;

                uint32 itemCount = ListView_GetItemCount(m_hwndList);

                if(itemCount)
                {
                    LV_ITEM item;

                    for(uint32 i = 0; i < itemCount; i++)
                    {
                        item.mask = LVIF_PARAM;
                        item.iItem = i;
                        item.lParam = 0;

                        if(ListView_GetItem(m_hwndList, &item))
                        {
                            if(dlinse->Item() == (DownloadItem*)item.lParam)
                            {
                                if (dlinse->Item()->GetState() == 
                                    kDownloadItemState_Downloading)
                                {    
                                    ListView_SetItemState(m_hwndList, i, 
                                        LVIS_SELECTED | LVIS_FOCUSED, 
                                        LVIS_SELECTED | LVIS_FOCUSED);
                                    ListView_EnsureVisible(m_hwndList, i, false);
                                }    
                            
                                ListView_RedrawItems(m_hwndList, i, i);
                                UpdateWindow(m_hwndList);
                                break;
                            }
                        }
                    }
                }

                UpdateOverallProgress();

	            break; 
            }

            case INFO_DownloadItemProgress: 
            {
	            DownloadItemProgressEvent* dlipe = (DownloadItemProgressEvent*)event;

                uint32 itemCount = ListView_GetItemCount(m_hwndList);

                if(itemCount)
                {
                    LV_ITEM item;

                    for(uint32 i = 0; i < itemCount; i++)
                    {
                        item.mask = LVIF_PARAM;
                        item.iItem = i;
                        item.lParam = 0;

                        if(ListView_GetItem(m_hwndList, &item))
                        {
                            if(dlipe->Item() == (DownloadItem*)item.lParam)
                            {
                                ListView_RedrawItems(m_hwndList, i, i);
                                UpdateWindow(m_hwndList);
                                break;
                            }
                        }
                    }
                }

                UpdateOverallProgress();

	            break; 
            }
            
	        default:
	            break;
	    }

	    result = 0;

    } 

    return result;
}

void DownloadUI::ParseArgs(int32 argc, char** argv)
{
    /*
    char *arg = NULL;
    int32 count = 0;

    for(int32 i = 1;i < argc; i++) 
    {
	    arg = argv[i];

	    if(arg[0] == '-') 
        {
	        switch(arg[1]) 
            {
		        
            }
        }
       
    }
    */
}

void DownloadUI::CreateUI()
{
    InitCommonControls();

    HWND hwnd;
    MSG msg;

	hwnd = CreateDialogParam( g_hInstance, 
                    MAKEINTRESOURCE(IDD_DIALOG),
                    NULL,
                    MainProc, 
                    (LPARAM)this);

    while(GetMessage(&msg,NULL,0,0))
    {
        if(hwnd)
        {
            IsDialogMessage(hwnd, &msg);
        }
    }
    ImageList_Destroy(m_noteImage);
    DestroyWindow(hwnd);
    m_hwnd = NULL;
    m_hwndList = NULL;

    m_target->AcceptEvent(new Event(CMD_QuitPlayer));
}

void DownloadUI::UIThreadFunc(void* arg)
{
    DownloadUI* ui = (DownloadUI*)arg;

    ui->CreateUI();
}

Error DownloadUI::Init(int32 startup_type) 
{ 
    ParseArgs(m_context->argc, m_context->argv);

    if(startup_type == PRIMARY_UI)
    {
        ShowWindow(m_hwnd, SW_SHOWNORMAL);
    }

    return kError_NoErr;
}


BOOL DownloadUI::InitDialog()
{
    // get hwnds for all my controls
    m_hwndList = GetDlgItem(m_hwnd, IDC_LIST);
    m_hwndInfo = GetDlgItem(m_hwnd, IDC_INFO);
    m_hwndPause = GetDlgItem(m_hwnd, IDC_PAUSE);
    m_hwndCancel = GetDlgItem(m_hwnd, IDC_CANCEL);
    m_hwndResume = GetDlgItem(m_hwnd, IDC_RESUME);
    m_hwndClose = GetDlgItem(m_hwnd, IDC_CLOSE);
    m_hwndProgress = GetDlgItem(m_hwnd, IDC_OVERALL_PROGRESS);
    m_hwndText = GetDlgItem(m_hwnd, IDC_FREETRACKS);
    
    // Set the proc address as a property 
	// of the window so it can get it
	SetProp(m_hwndText, 
			"oldproc",
			(HANDLE)GetWindowLong(m_hwndText, GWL_WNDPROC));

    SetProp(m_hwndText, 
			"ui",
			(HANDLE)this);

    // Subclass the window so we can draw it
	SetWindowLong(	m_hwndText, 
					GWL_WNDPROC, 
                    (DWORD)::FreeTracksWndProc ); 


    HINSTANCE hinst = (HINSTANCE)GetWindowLong(m_hwnd, GWL_HINSTANCE);
    HICON appIcon = LoadIcon(hinst, MAKEINTRESOURCE(IDI_EXE_ICON));

    SetClassLong(m_hwnd, GCL_HICON, (LONG)appIcon);

    // initialize controls
    // first let's add our columns
    RECT rect;
    GetClientRect(m_hwndList, &rect);

    LV_COLUMN lvc;

    lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
    lvc.fmt = LVCFMT_LEFT; // left align column
    lvc.cx = (rect.right-rect.left)/2; // width of column in pixels
    lvc.pszText = "Song Title";
    lvc.iSubItem = 0;

    ListView_InsertColumn(m_hwndList, 0, &lvc);

    if((rect.right-rect.left)%2) // squeeze out every last pixel :)
        lvc.cx += 1;

    lvc.pszText = "Status";
    lvc.iSubItem = 1;

    ListView_InsertColumn(m_hwndList, 1, &lvc);

    lvc.pszText = "";
    lvc.cx = (rect.right-rect.left); // width of column in pixels
    lvc.iSubItem = 0;

    ListView_InsertColumn(m_hwndInfo, 0, &lvc);

    // init the images    
    m_noteImage = ImageList_Create(16, 16, ILC_MASK, 1, 0);

⌨️ 快捷键说明

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