Lua 任意调用WindowAPI 之 编写简单的音乐播放器
-- 主窗口模板
WMP_Tpl = {
style = BIT_OR(DS_SETFONT, DS_CENTER, WS_POPUP, WS_BORDER, WS_SYSMENU, DS_MODALFRAME, WS_CAPTION, WS_MINIMIZEBOX),
x = 0,
y = 0,
cx = 232,
cy = 170,
title = "Lua Music Player",
fontsize = 9,
fontface = "宋体",
-- 控件列表
{style = WS_VISIBLE, x = 7, y = 6, cx = 218, cy = 42, id = "wmp", class = "{6BF52A52-394A-11D3-B153-00C04F79FAA6}", title = "wmp"},
{style = WS_VISIBLE, x = 7, y = 50, cx = 30, cy = 14, id = "Play", class = CONTROL_TYPE_BUTTON, title = "播放"},
{style = WS_VISIBLE, x = 40, y = 50, cx = 50, cy = 14, id = "AddLocal", class = CONTROL_TYPE_BUTTON, title = "增加本地"},
{style = WS_VISIBLE, x = 93, y = 50, cx = 50, cy = 14, id = "AddUrl", class = CONTROL_TYPE_BUTTON, title = "增加网络"},
{style = BIT_OR(WS_VISIBLE, BS_AUTOCHECKBOX), x = 150, y = 50, cx = 30, cy = 14, id = "AlwaysOnTop", class = CONTROL_TYPE_BUTTON, title = "最前"},
{style = BIT_OR(WS_VISIBLE, BS_AUTOCHECKBOX), x = 185, y = 50, cx = 30, cy = 14, id = "Repeat", class = CONTROL_TYPE_BUTTON, title = "循环"},
{style = BIT_OR(WS_BORDER, WS_VISIBLE), x = 7, y = 65, cx = 218, cy = 100, id = "musicList", class = CONTROL_TYPE_LISTBOX, title = ""},
};
-- http://yaajee.com/music/2009/05/21/62500200.mp3
-- http://colortone.gd.chinamobile.com/ColorTone/music/03/02/01/c0302010064.mp3
-- 添加网络音乐窗口模板
OpenUrl_Tpl = {
style = BIT_OR(DS_SETFONT, DS_CENTER, WS_POPUP, WS_BORDER, WS_SYSMENU, DS_MODALFRAME, WS_CAPTION, WS_MINIMIZEBOX),
x = 0,
y = 0,
cx = 247,
cy = 43,
title = "添加网络音乐",
fontsize = 9,
fontface = "宋体",
-- 控件列表
{style = WS_VISIBLE, x = 7, y = 7, cx = 49, cy = 8, id = "static", class = CONTROL_TYPE_STATIC, title = "请输入网址:"},
{style = BIT_OR(WS_BORDER, WS_VISIBLE), x = 59, y = 3, cx = 181, cy = 14, id = "musicURL", class = CONTROL_TYPE_EDIT, title = "http://yaajee.com/music/2009/05/21/62500200.mp3"},
{style = WS_VISIBLE, x = 94, y = 22, cx = 50, cy = 14, id = "Add", class = CONTROL_TYPE_BUTTON, title = "添加"},
};
-- 添加网络音乐窗口类
OpenUrl_Dialog = {
URL = "",
ShowModal = function (self, template, parent)
return TfDialog.showModal(self, template, parent);
end,
Add_Click = function (self)
self.URL = self.musicURL.Text;
TfDialog.destroyDialog(self, 1);
end,
};
-- 音乐播放器窗口类
LuaMusicPlayer_Dialog = {
curIndex = 0,
repeatPlay = false,
-- 显示模态对话框
ShowModal = function (self, template, parent)
return TfDialog.showModal(self, template, parent);
end,
-- 窗口处理函数
OnMessage = function (self, message, wParam, lParam)
if message == 0x10000 then
self:PlayMusic(self.curIndex + 1);
return 1;
end
return 0;
end,
-- 对话框初始化
OnInitDialog = function (self)
end,
-- 点击播放
Play_Click = function (self)
local selIndex = USER32.SendMessageA(self.musicList.hWnd, 0x0188, 0, 0);
if selIndex < 0 then
selIndex = 0;
end
self:PlayMusic(selIndex);
end,
-- 点击总是最前
AlwaysOnTop_Click = function (self)
local isChecked = USER32.IsDlgButtonChecked(self.hWnd, self.AlwaysOnTop.id);
if isChecked == TRUE then
USER32.SetWindowPos(self.hWnd, HWND_TOPMOST, 0, 0, 0, 0, BIT_OR(SWP_NOSIZE, SWP_NOMOVE));
else
USER32.SetWindowPos(self.hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, BIT_OR(SWP_NOSIZE, SWP_NOMOVE));
end
end,
-- 点击重复播放
Repeat_Click = function (self)
local isChecked = USER32.IsDlgButtonChecked(self.hWnd, self.Repeat.id);
if isChecked == TRUE then
self.repeatPlay = true;
else
self.repeatPlay = false;
end
end,
-- 播放音乐
PlayMusic = function (self, index)
local count = USER32.SendMessageA(self.musicList.hWnd, 0x018B, 0, 0);
if index >= count and self.repeatPlay then
index = 0;
end
if index >= 0 and index < count then
local strPath = TfString.New(256);
local len = USER32.SendMessageA(self.musicList.hWnd, 0x0189, index, strPath.__udata);
self.curIndex = index;
self.wmp.URL = strPath.value;
end
end,
-- 点击添加本地音乐
AddLocal_Click = function (self)
COMDLG32 = INIT_DLL("Comdlg32", "Comdlg32.dll");
local strPath = TfString.New(256);
local ofn = OPENFILENAMEA:New();
ofn.lStructSize = sizeof(ofn);
ofn.lpstrFile = strPath.__udata;
ofn.nMaxFile = strPath.size;
if (COMDLG32.GetOpenFileNameA(ofn.__udata) == TRUE) then
self:AddToList(strPath.value);
end
end,
-- 点击添加网络音乐
AddUrl_Click = function (self)
if OpenUrl_Dialog:ShowModal(OpenUrl_Tpl, self) == 1 then
self:AddToList(OpenUrl_Dialog.URL);
end
end,
-- 添加到音乐列表
AddToList = function (self, location)
local strUrl = TfString.New(location);
USER32.SendMessageA(self.musicList.hWnd, 0x0180, 0, strUrl.__udata);
end,
-- wmp COM 事件,播放状态改变
wmp_PlayStateChange = function (self, newState)
if self.wmp.currentMedia then
self.Text = self.wmp.currentMedia.name;
end
if newState == 1 then
USER32.PostMessageA(self.hWnd, 0x10000, 0, 0);
end
end,
-- wmp COM 事件,打开状态改变
wmp_OpenStateChange = function (self, newState)
end,
-- wmp COM 事件,改变位置
wmp_PositionChange = function (self, oldPosition, newPosition)
end,
};
-- 显示主窗口
LuaMusicPlayer_Dialog:ShowModal(WMP_Tpl);
- 点赞
- 收藏
- 分享
- 文章举报
- 通过编译luaSQL调用ODBC连接Access以及给OpenResty(LuaJIT)编写简单的C扩展示例
- Cocos2d-x中使用Lua让CCSprite变灰(Gray)的简单办法,支持递归调用
- 简单调用自己编写的dll
- Lua中调用的dll文件的编写方法
- 如何编写简单的lua小脚本@@
- lua 调用C++ DLL函数简单实例
- lua调用c++dll 简单案例
- Lua 中调用 cocos2d-x 的24种简单特效
- VB.NET中的DLL编写和调用的最简单示例
- VC++6.0配置LUA环境和C++调用LUA的简单示例
- 编写一个完整的包含输入和输出的简单程序。 (1)密码错误,显示“对不起,密码错误!”。 (2)密码正确,显示“欢迎你!”,同时内置函数求解2x2+4x+5的值,x为整数 4000 ,并用主函数调用该函数。 (3)
- 简单 Demo:C++编写、调用动态链接库
- C#调用脚本语言(三)-- IronJS 与 IronLua 简单方法性能比较
- cocos3.x 编写C++控件在lua上面调用
- Python调用Windows API函数编写录音机和音乐播放器
- Lua的系统学习(杂)_通过Lua调用C#方法(热更新最直观的原理理解)_简单的随机数实现
- 最简单的C++中调用LUA函数源代码
- lua学习系列:c++调用lua脚本的简单示例
- Vc编写和调用Dll的简单例子
- 编写WebService如此简单 - lua/python/c++