📄 filelistdlg.cpp
字号:
// FileListDlg.cpp : implementation file
//
#include "stdafx.h"
#include "AudioPlay.h"
#include "FileListDlg.h"
//#include "FileListSel.h"
#include "Coordinate.h"
#include "XPath.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern CStringList g_StrList;
extern int g_nCurSelItem;
typedef enum {
LIST_CTRL_CLOSE,
LIST_CTRL_OK,
LIST_CTRL_CANCEL,
LIST_CTRL_ADD,
LIST_CTRL_DEL,
LIST_CTRL_COUNT,
}LISTCTRL;
POINT FileBtnPos[LIST_CTRL_COUNT] ={
{CLOSE_BTN_X, CLOSE_BTN_Y}, //close
{FILEOK_BTN_X, FILEOK_BTN_Y}, //ok
{FILECANCEL_BTN_X, FILECANCEL_BTN_Y}, //cancel
{FILEADD_BTN_X, FILEADD_BTN_Y}, //add
{FILEDEL_BTN_X, FILEDEL_BTN_Y}, //del
};
/////////////////////////////////////////////////////////////////////////////
// CFileListDlg dialog
CFileListDlg::CFileListDlg(CWnd* pParent /*=NULL*/)
: CDialog(CFileListDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CFileListDlg)
//}}AFX_DATA_INIT
m_nCurSelItem = g_nCurSelItem;
m_CurType = FILE_AUDIO;
m_bNeedSave = FALSE;
memset(m_chCurFilename,0,sizeof(TCHAR)*MAX_STRING);
CString pathstr = UXPGetModulePath();
wcscpy(m_chCurPath,pathstr.GetBuffer(pathstr.GetLength()));
}
void CFileListDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CFileListDlg)
DDX_Control(pDX, IDC_FILE_LIST, m_FileList);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CFileListDlg, CDialog)
//{{AFX_MSG_MAP(CFileListDlg)
ON_WM_PAINT()
ON_BN_CLICKED(IDC_FILECLOSE, OnFileclose)
ON_BN_CLICKED(IDC_FILEOK, OnFileok)
ON_BN_CLICKED(IDC_FILECANCEL, OnFilecancel)
ON_BN_CLICKED(IDC_FILE_ADD, OnFileAdd)
ON_BN_CLICKED(IDC_FILE_SUBTRACT, OnFileSubtract)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFileListDlg message handlers
BOOL CFileListDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
SetWindowPos(NULL, AUDIO_LEFT, AUDIO_TOP, AUDIO_WIDTH, AUDIO_HEIGHT, SWP_SHOWWINDOW);
#ifdef SCREEN_320X240
m_BMPBG.LoadBitmap(IDB_FILELIST320);
#else
m_BMPBG.LoadBitmap(IDB_FILELIST480);
#endif
LoadAndLocateButtons();
#ifndef WCELISTBOX
m_FileList.InitListScroll();
#endif
//update list style
ListView_SetExtendedListViewStyle(m_FileList.m_hWnd, LVS_EX_FULLROWSELECT);
m_FileList.MoveWindow(LIST_LEFT,LIST_TOP,LIST_WIDTH,LIST_HEIGHT,FALSE);
InitFileList();
//add battery
VERIFY(m_bat.Create(NULL, WS_VISIBLE|WS_CHILD|SS_BITMAP, CRect(410,11,415,21), this));
RegisterBatteryWndToMainUI(m_bat.GetSafeHwnd());
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CFileListDlg::LoadAndLocateButtons()
{
CRect btnRect;
VERIFY(mBtnFileClose.AutoLoad(IDC_FILECLOSE, this));
mBtnFileClose.GetClientRect(&btnRect);
mBtnFileClose.MoveWindow(FileBtnPos[LIST_CTRL_CLOSE].x, FileBtnPos[LIST_CTRL_CLOSE].y, btnRect.Width(), btnRect.Height(), FALSE);
VERIFY(mBtnFileOK.AutoLoad(IDC_FILEOK, this));
mBtnFileOK.GetClientRect(&btnRect);
mBtnFileOK.MoveWindow(FileBtnPos[LIST_CTRL_OK].x, FileBtnPos[LIST_CTRL_OK].y, btnRect.Width(), btnRect.Height(), FALSE);
VERIFY(mBtnFileCancel.AutoLoad(IDC_FILECANCEL, this));
mBtnFileCancel.GetClientRect(&btnRect);
mBtnFileCancel.MoveWindow(FileBtnPos[LIST_CTRL_CANCEL].x, FileBtnPos[LIST_CTRL_CANCEL].y, btnRect.Width(), btnRect.Height(), FALSE);
VERIFY(mBtnFileAdd.AutoLoad(IDC_FILE_ADD, this));
mBtnFileAdd.GetClientRect(&btnRect);
mBtnFileAdd.MoveWindow(FileBtnPos[LIST_CTRL_ADD].x, FileBtnPos[LIST_CTRL_ADD].y, btnRect.Width(), btnRect.Height(), FALSE);
VERIFY(mBtnFileSubtract.AutoLoad(IDC_FILE_SUBTRACT, this));
mBtnFileSubtract.GetClientRect(&btnRect);
mBtnFileSubtract.MoveWindow(FileBtnPos[LIST_CTRL_DEL].x, FileBtnPos[LIST_CTRL_DEL].y, btnRect.Width(), btnRect.Height(), FALSE);
}
void CFileListDlg::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
RECT rc;
GetClientRect(&rc);
CXTMemDC m_Memdc(&dc,rc);
//draw bg
CDC MemDes;
MemDes.CreateCompatibleDC(&dc);
MemDes.SelectObject(&m_BMPBG);
m_Memdc.BitBlt(AUDIO_LEFT,AUDIO_TOP,AUDIO_WIDTH,AUDIO_HEIGHT,&MemDes,0,0,SRCCOPY);
MemDes.DeleteDC();
// Do not call CDialog::OnPaint() for painting messages
}
void CFileListDlg::OnFileclose()
{
// TODO: Add your control notification handler code here
if(m_bNeedSave) //save audio play list
SavePlayList();
//del battery
UnregisterBatteryWndToMainUI(m_bat.GetSafeHwnd());
m_bat.DestroyWindow();
DeleteObject(m_BMPBG);
ReleaseMemery();
g_nCurSelItem = m_nCurSelItem;
CDialog::OnOK();
}
void CFileListDlg::OnFilecancel()
{
// TODO: Add your control notification handler code here
OnFileclose();
}
void CFileListDlg::InitFileList()
{
LVCOLUMN lColumn;
lColumn.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
lColumn.fmt = LVCFMT_LEFT;
lColumn.iSubItem = 0; //subitem 0 is icon
lColumn.pszText = NULL;
lColumn.cx = 10; //18
m_FileList.InsertColumn(0,&lColumn);
lColumn.iSubItem = 1; //subitem 1 is test
lColumn.pszText = NULL;
lColumn.cx = LIST_WIDTH-30; //25
m_FileList.InsertColumn(1,&lColumn);
m_FileList.SetRedraw(FALSE);
if(IsFileExist(GetCurrentListName()))
LoadFilePlayList();
else
{
m_bNeedSave = TRUE;
LoadFileList();
}
m_FileList.SetRedraw(TRUE);
}
void CFileListDlg::LoadFileList()
{
HANDLE hFindData;
WIN32_FIND_DATA lpFindFileData;
int nItem = 0;
LPARAM lparam;
TCHAR chTmp[MAX_STRING];
ReleaseMemery();
m_FileList.DeleteAllItems();
wcscpy(chTmp,m_chCurPath);
wcscat(chTmp,L"\\*.*");
//open file system
hFindData = FindFirstFile(chTmp, &lpFindFileData);
//Audio dir
if(hFindData != INVALID_HANDLE_VALUE)
{
if(CheckAudio(lpFindFileData.cFileName))//
{
wcscpy(chTmp,m_chCurPath);
wcscat(chTmp,L"\\");
wcscat(chTmp,lpFindFileData.cFileName);
g_StrList.AddTail(chTmp);
lparam = GetAttribution(chTmp,FILE_AUDIO);
AddItemToListView(lpFindFileData.cFileName, nItem, lparam);
nItem++;
}
while(FindNextFile(hFindData, &lpFindFileData))
{
if(CheckAudio(lpFindFileData.cFileName))//
{
wcscpy(chTmp,m_chCurPath);
wcscat(chTmp,L"\\");
wcscat(chTmp,lpFindFileData.cFileName);
g_StrList.AddTail(chTmp);
lparam = GetAttribution(chTmp,FILE_AUDIO);
AddItemToListView(lpFindFileData.cFileName, nItem, lparam);
nItem++;
}
}
}
//close it
FindClose(hFindData);
//select it
m_FileList.SetItemState(m_nCurSelItem,LVIS_SELECTED|LVIS_FOCUSED,LVIS_SELECTED|LVIS_FOCUSED);
}
void CFileListDlg::LoadFilePlayList()
{
int nItem = 0;
TCHAR chStr[MAX_STRING];
LPARAM lparam;
CString Str;
//first delete all space
ReleaseMemery();
m_FileList.DeleteAllItems();
for(nItem = 0;nItem<g_StrList.GetCount();nItem++)
{
Str = g_StrList.GetAt(g_StrList.FindIndex(nItem));
wcscpy(chStr,Str);
lparam = GetAttribution(chStr,m_CurType);
AddItemToListView(GetFileName(chStr), nItem, lparam);
}
//select it
m_FileList.SetItemState(m_nCurSelItem,LVIS_SELECTED|LVIS_FOCUSED,LVIS_SELECTED|LVIS_FOCUSED);
}
void CFileListDlg::SavePlayList()
{
CFile file;
char szByte[MAX_STRING];
LVITEM lvItem;
PLVNodeData plParamData;
int nItem = m_FileList.GetItemCount();
g_StrList.RemoveAll();
//open file, save full file name
if (file.Open(GetCurrentListName(), CFile::modeCreate|CFile::modeWrite))
{
for(int i = 0;i< nItem;i++)
{
lvItem.mask = LVIF_PARAM;
lvItem.iItem = i;
lvItem.iSubItem = 0;
m_FileList.GetItem(&lvItem);
plParamData = (PLVNodeData)lvItem.lParam;
//save filename to string list
g_StrList.AddTail(plParamData->szFileName);
//from param get full file name,save to txtfile
WCharToMByte(plParamData->szFileName,szByte,MAX_STRING);
strcat(szByte,"\r");
file.Write(szByte, strlen(szByte));
}
file.Close();
}
}
LPARAM CFileListDlg::GetAttribution(TCHAR* chFileName,FileType nType)
{
PLVNodeData pData = new LVNodeData;
//chFileName is full name
pData->nFileType = nType;
wcscpy(pData->szFileName, chFileName);
return (LPARAM)pData;
}
void CFileListDlg::AddItemToListView(TCHAR* FileName, INT nItem, LPARAM Param)
{
//when add item to list view, Param is meaning full file name or not
LVITEM lItem;
lItem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
lItem.iItem = nItem;
lItem.iSubItem = 0;
lItem.pszText = NULL;
lItem.iImage = 0;
lItem.lParam = Param;
int pos = m_FileList.InsertItem(&lItem);
lItem.mask = LVIF_TEXT;
lItem.iItem = pos;
lItem.iSubItem = 1;
lItem.cchTextMax= 100;
lItem.pszText = FileName;
m_FileList.SetItem(&lItem);
}
BOOL CFileListDlg::CheckAudio(TCHAR *chFileName)
{
if(wcsstr(chFileName,L".mp3") || wcsstr(chFileName,L".MP3"))
return true;
else if(wcsstr(chFileName,L".wma") || wcsstr(chFileName,L".WMA"))
return true;
else if(wcsstr(chFileName,L".wav") || wcsstr(chFileName,L".WAV"))
return true;
else
return false;
}
BOOL CFileListDlg::CheckVideo(TCHAR *chFileName)
{
if(wcsstr(chFileName,L".avi") || wcsstr(chFileName,L".AVI"))
return true;
else if(wcsstr(chFileName,L".wmv") || wcsstr(chFileName,L".WMV"))
return true;
else if(wcsstr(chFileName,L".mpeg") || wcsstr(chFileName,L".MPEG"))
return true;
else
return false;
}
void CFileListDlg::GetUpFolder()
{
TCHAR chFileName[MAX_STRING];
int nCount = wcslen(m_chCurPath);
wcscpy(chFileName, m_chCurPath);
for( ; nCount >= 0; nCount--)
{
if('\\' == chFileName[nCount])
{
*(chFileName + nCount) = 0;
break;
}
}
wcscpy(m_chCurPath, chFileName);
}
void CFileListDlg::OnFileok()
{
// TODO: Add your control notification handler code here
LVITEM lvItem;
PLVNodeData plParamData;
m_nCurSelItem = m_FileList.GetSelectionMark();
if (m_nCurSelItem < 0)
{
OnFileclose();
return;
}
//get current file type
lvItem.mask = LVIF_PARAM;
lvItem.iItem = m_nCurSelItem;
lvItem.iSubItem = 0;
m_FileList.GetItem(&lvItem);
plParamData = (PLVNodeData)lvItem.lParam;
switch(plParamData->nFileType)
{
case FILE_AUDIO:
wcscpy(m_chCurFilename, plParamData->szFileName);
OnFileclose();
break;
default:
OnFileclose();
break;
}
}
TCHAR* CFileListDlg::GetFileName(TCHAR* chFullFileName)
{
if (chFullFileName == NULL || chFullFileName[0] == 0)
return NULL;
int siLen = wcslen(chFullFileName);
for(int i = siLen - 1; i >= 0; --i)
{
if(chFullFileName[i] == '\\')
break;
}
return chFullFileName + i + 1;
}
BOOL CFileListDlg::ReleaseMemery()
{
int nItem = 0,i = 0;
LVITEM lvItem;
BOOL bl;
nItem = m_FileList.GetItemCount();
//release all PLVNodeData
for(i = 0; i < nItem; i++)
{
lvItem.mask = LVIF_PARAM;
lvItem.iItem = i;
lvItem.iSubItem = 0;
bl = m_FileList.GetItem(&lvItem);
if(!bl)
return false;
delete (PLVNodeData)lvItem.lParam;
}
return true;
}
void CFileListDlg::OnFileAdd()
{
// TODO: Add your control notification handler code here
m_bNeedSave = TRUE;
#ifdef WCELISTBOX
CString fileFilter = _T("Audio files|*.wma;*.mp3|All files|*.*||");
CString chcurFile;
CString pathstr = UXPGetModulePath();
CFileDialog nFileDialog(TRUE,
_T("mp3"),
NULL,
OFN_HIDEREADONLY,
fileFilter,
NULL);
nFileDialog.m_ofn.lpstrInitialDir = pathstr.GetBuffer(pathstr.GetLength());
if(nFileDialog.DoModal() == IDOK)
{
chcurFile=nFileDialog.GetPathName();
g_StrList.AddTail(chcurFile);
}
#else
CFileListSel dlg(this);
m_FileSelList = &dlg;
dlg.DoModal();
#endif
TCHAR Name[MAX_STRING];
CString Str;
int nTotal = m_FileList.GetItemCount();
PLVNodeData plParam;
//in filelistsel.cpp, adding to g_StrList,then add to m_FileList
//compare g_StrList count and m_FileList count
while(0 < (g_StrList.GetCount()-nTotal))
{
Str = g_StrList.GetAt(g_StrList.FindIndex(nTotal));
wcscpy(Name, Str);
plParam = (PLVNodeData)GetAttribution(Name,m_CurType);
AddItemToListView(GetFileName(Name), nTotal, (LPARAM)plParam);
nTotal++;
}
//select it
if(m_nCurSelItem>nTotal)
m_nCurSelItem = 0;
m_FileList.SetItemState(m_nCurSelItem,LVIS_SELECTED|LVIS_FOCUSED,LVIS_SELECTED|LVIS_FOCUSED);
}
void CFileListDlg::OnFileSubtract()
{
// TODO: Add your control notification handler code here
//get select item count
int nTemp, nSelRows = m_FileList.GetSelectedCount();
if(!nSelRows)
return;
m_bNeedSave = TRUE;
//malloc array to save current select item index
int* pnArrayOfSelRows = new int[nSelRows];
if(!pnArrayOfSelRows)
return;
nTemp = nSelRows; //save temp variable
//get position
POSITION pos = m_FileList.GetFirstSelectedItemPosition();
//get index
int i = m_FileList.GetNextSelectedItem(pos);
while (i != -1)
{
//calculate correct index
nTemp = nTemp-1;
pnArrayOfSelRows[nTemp] = i;
i = m_FileList.GetNextSelectedItem(pos);
}
for(int j=0; j < nSelRows; j++)
{
//delete item
g_StrList.RemoveAt(g_StrList.FindIndex(pnArrayOfSelRows[j]));
//current delete file < current select file, current select item --
if(pnArrayOfSelRows[j] < m_nCurSelItem)
m_nCurSelItem--;
m_FileList.DeleteItem(pnArrayOfSelRows[j]);
}
//delete Array
delete(pnArrayOfSelRows);
pnArrayOfSelRows = NULL;
m_FileList.SetItemState(0,LVIS_SELECTED|LVIS_FOCUSED,LVIS_SELECTED|LVIS_FOCUSED);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -