中年人学C语言Windows程序设计,35 WIN32图形界面音乐播放器实例代码2
接上一篇内容,上一篇没有播放列表,只能单曲播放,现在将代码升级为播放列表的形式,可以进行列表播放
通过listbox加入播放列表,点击列表可以切换歌曲
新增listbox代码
hListBox = CreateWindow(TEXT("LISTBOX"), TEXT("我是个BOX"),
WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL | LBS_HASSTRINGS | LBS_NOTIFY,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
hWnd, (HMENU)4,
((LPCREATESTRUCT)lParam)->hInstance, NULL);
SendMessageW(hListBox, WM_SETFONT, (WPARAM)hFont, (LPARAM)NULL);
设置listbox的位置
MoveWindow(//拖动大小,改变条位置
hListBox,
0, 2*cHeighChar,
cxClient, cyClient,
TRUE);
单击和双击LISTBOX时的动作
左键点击listbox的内容则切换filename,让后点击播放音乐
如果双击listbox的内容,直接播放双击的歌曲
case 4:
switch (HIWORD(wParam))
{
case LBN_SELCHANGE:
{
// Get current selection.
iIndex = SendMessage(hListBox, LB_GETCURSEL, 0, 0);//选中ID
iLength = SendMessage(hListBox, LB_GETTEXTLEN, iIndex, 0) + 1;//选中文本长度
pVarName = calloc(iLength, sizeof(TCHAR));//开辟内存
//memset(pVarName, 0, sizeof(pVarName));
SendMessage(hListBox, LB_GETTEXT, iIndex, (LPARAM)pVarName);//获取当前选中
lstrcpy(fileName, pVarName);
flagChange = 1;
free(pVarName);//释放内存
}
break;
case LBN_DBLCLK://双击列表
{
// Get current selection.
iIndex = SendMessage(hListBox, LB_GETCURSEL, 0, 0);//选中ID
iLength = SendMessage(hListBox, LB_GETTEXTLEN, iIndex, 0) + 1;//选中文本长度
pVarName = calloc(iLength, sizeof(TCHAR));//开辟内存
//memset(pVarName, 0, sizeof(pVarName));
SendMessage(hListBox, LB_GETTEXT, iIndex, (LPARAM)pVarName);//获取当前选中
//MessageBox(hWnd, pVarName, TEXT("提示信息"), MB_ICONINFORMATION);
lstrcpy(fileName, pVarName);
flagChange = 1;
free(pVarName);//释放内存
//SendMessage(hWnd, IDM_PLAY, 0, 0);
SendMessage(hWnd, WM_COMMAND, IDM_PLAY, 0);//播放音乐
}
break;
}
break;