您的位置:首页 > 编程语言 > Lua

Lua 任意调用WindowAPI 之 编写简单的音乐播放器

2020-03-05 02:23 627 查看

 

-- 主窗口模板
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);

 

  • 点赞
  • 收藏
  • 分享
  • 文章举报
traymoon 发布了2 篇原创文章 · 获赞 0 · 访问量 3631 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: