main.pas

来自「mp3 播放器 delphi 源码」· PAS 代码 · 共 1,262 行 · 第 1/5 页

PAS
1,262
字号
      ReadLn(t1,s2); s2:=Trim(s2);
      b1:=false;
      
      If UpperCase(s2)='#EXTM3U' then PlaylistKind:=2 else
      If UpperCase(s2)='[PLAYLIST]' then PlaylistKind:=3 else
      If (Pos('#EXTINF:',UpperCase(s2))=1)and(PlaylistKind=2) then begin
        s3:=Trim(copy(s2,9,Length(s2)-8));        
        i1:=Pos(',',s3);
        If i1>0 then begin s9:=copy(s3,1,i1-1); Val(Trim(s9),i2,i3); If i3=0 then Delete(s3,1,i1); end;
        If Length(s3)>0 then b2:=true;
      end else begin
        i1:=Pos('=',s2);
        If (i1>0)and(Pos('FILE',UpperCase(s2))=1)and(PlaylistKind=3) then begin
          s4:=Trim(copy(s2,i1+1,Length(s2)-i1+1)); If Length(s4)>0 then If s4[1]='=' then begin Delete(s4,1,1); s4:=Trim(s4); end;
          If FileExists(s4) then begin s4:=ExpandFileName(s4); b3:=true; end else
          If FileExists(s1+s4) then begin s4:=s1+s4; b3:=true; end;
        end else
        If (i1>0)and(Pos('TITLE',UpperCase(s2))=1)and(PlaylistKind=3) then begin
          If b3 then begin
            delete(s2,1,i1); s2:=Trim(s2); If Length(s2)>0 then If s2[1]='=' then begin Delete(s2,1,1); s2:=Trim(s2); end;
            AddToSongList(0,s4,(PlayUseID3Tags)and(not b3),s2); b3:=false;
          end;
        end else begin
          If FileExists(s2) then begin s2:=ExpandFileName(s2); b1:=true; end else
          If FileExists(s1+s2) then begin s2:=s1+s2; b1:=true; end;
          If b1 then begin If not b2 then s3:='' else b2:=not PlayUseID3Tags; AddToSongList(0,s2,not b2,s3); b2:=false; end;
        end;        
      end;

      Application.ProcessMessages;
    end;
    GaugePanel.Visible:=false;
    Screen.Cursor:=crDefault;
  end;
  {$I-} CloseFile(t1); {$I+}
end;

{-------------------------------------------------------------------------------
Forumlar
-------------------------------------------------------------------------------}

procedure TMainForm.OnClose(Sender: TObject; var Action: TCloseAction);
var Ini1: TIniFile;
begin
  If (IsNowProtected)and(NeedPwForExit) then begin MsgNotAllowed; Action:=caNone; Exit; end;
  Case mainwindowstate of 0..2: If FileExists(IniFileCom) then begin
    Ini1:=TIniFile.Create(IniFileCom);
    Try
      If (mainwidth=-1)or(flatButtonPanel<>ButtonPanelFlatButton.Down) then Ini1.WriteBool('Main','flatButtonPanel',ButtonPanelFlatButton.Down);
      If (mainwidth=-1)or(flatPanelLeftCaption<>LeftTitleFlatButton.Down) then Ini1.WriteBool('Main','flatPanelLeftCaption',LeftTitleFlatButton.Down);
      If (mainwidth=-1)or(flatPanelRightCaption<>RightTitleFlatButton.Down) then Ini1.WriteBool('Main','flatPanelRightCaption',RightTitleFlatButton.Down);
      If (mainwidth=-1)or(flatItemsPanel<>ItemsPanelFlatButton.Down) then Ini1.WriteBool('Main','flatItemsPanel',ItemsPanelFlatButton.Down);
      If (mainwidth=-1)or(flatmemo<>MemoFlatButton.Down) then Ini1.WriteBool('Main','flatmemo',MemoFlatButton.Down);
      If (mainwidth=-1)or(flatsongpanel<>SongFlatButton.Down) then Ini1.WriteBool('Main','flatsongpanel',SongFlatButton.Down);
      If (mainwidth=-1)or(flatpeakpanel<>PeakFlatButton.Down) then Ini1.WriteBool('Main','flatpeakpanel',PeakFlatButton.Down);
      If (mainwidth=-1)or(flatpospanel<>PosFlatButton.Down) then Ini1.WriteBool('Main','flatpospanel',PosFlatButton.Down);
      If (mainwidth=-1)or(flatplayerpanel<>PlayerFlutButton.Down) then Ini1.WriteBool('Main','flatplayerpanel',PlayerFlutButton.Down);
      If SaveLastOpenPlaylistfile then If FileExists(PlaylistFileCom) then Ini1.WriteString('Playing','AutoopenPlaylist',PlayThisPlaylistfileOnStart);
      If WindowState=wsNormal then begin
        If mainleft<>left then begin mainleft:=left; Ini1.WriteInteger('Main','left',mainleft); end;
        If maintop<>top then begin maintop:=top; Ini1.WriteInteger('Main','top',maintop); end;
        If mainwidth<>width then begin mainwidth:=width; Ini1.WriteInteger('Main','width',mainwidth); end;
        If mainheight<>height then begin mainheight:=height; Ini1.WriteInteger('Main','height',mainheight); end;
      end;
      If PanelRight.Width<>mainsplitter1 then begin mainsplitter1:=PanelRight.Width; Ini1.WriteInteger('Main','splitter1',mainsplitter1); end;
      If MemoPanel.Height<>mainsplitter2 then begin
        If MemoFlatButton.Down then mainsplitter2:=StrToInt(MemoPanel.Hint) else mainsplitter2:=MemoPanel.Height;
        Ini1.WriteInteger('Main','splitter2',mainsplitter2);
      end;
      If PlayRandom<>ButtonRandom.Down then Ini1.WriteBool('Playing','Random',ButtonRandom.Down);
    Finally Ini1.Free; end;
  end; end;
  Try
    If (MPEGPlayer.Mode=plmPlaying)or(MPEGPlayer.Mode=plmPaused) then MPEGPlayer.Stop;
    Try MpgAudio.Free; Except end;
  Finally
    If mainwindowstate=2 then Shell_NotifyIcon(NIM_DELETE, @FNotifyIconData);
    DeallocateHWnd(TrayIconHandle);
  end;
end;

procedure TMainForm.OnCreate(Sender: TObject);
var i1: integer; s1: string; font1: TFont; Ini1: TIniFile;

  function ActivatePreviousInstances: boolean;
  var pc1: pchar; Handle1: THandle; Wind1: HWnd;
  begin
    Handle1:=CreateSemaphore(nil,0,1,'Dipesh-MP3yk - Semaphore');
    Result:=(Handle1<>0)and(GetLastError=ERROR_ALREADY_EXISTS);
    If Result then begin
      CloseHandle(Handle1);
      pc1:=PChar(Application.Title);
      Application.Title:='Dipesh-MP3yk - Wait...';
      Wind1:=FindWindow(nil,pc1);
      Application.Title:=pc1;
      If Wind1<>0 then begin
        PostMessage(Wind1,WM_SYSCOMMAND,SC_RESTORE,0);
        SetForegroundWindow(Wind1);
      end;
    end;
  end;

begin
  Screen.Cursor:=crHourGlass;
  Application.OnMessage:=AppMessage; TrayIconHandle:=AllocateHWnd(WndProc);
  StartDir:=ExtractFilePath(ParamStr(0)); If StartDir[Length(StartDir)]<>'\' then StartDir:=StartDir+'\';
  If not FileExists(StartDir+'Memo.txt') then begin MemoPanel.Visible:=false; Splitter1.Height:=0; end;
  If FileExists(IniFileCom) then IniFileCom:=ExpandFileName(IniFileCom) else IniFileCom:=StartDir+IniFileCom;
  Ini1:=TIniFile.Create(IniFileCom);
  Try
    // -> [Playing]
    Case Ini1.ReadInteger('Playing','Priority',-1) of 0: MPEGPlayer.PlayerPriority:=Idle; 1: MPEGPlayer.PlayerPriority:=Lowest;
      2: MPEGPlayer.PlayerPriority:=BelowNormal; 3: MPEGPlayer.PlayerPriority:=Normal; 4: MPEGPlayer.PlayerPriority:=AboveNormal;
      5: MPEGPlayer.PlayerPriority:=Highest; 6: MPEGPlayer.PlayerPriority:=TimeCritical; end;
    PlayRandom:=Ini1.ReadBool('Playing','Random',PlayRandom); ButtonRandom.Down:=PlayRandom;
    PlayAutoStart:=Ini1.ReadBool('Playing','Autostart',PlayAutoStart);
    PlayUseID3Tags:=Ini1.ReadBool('Playing','use ID3-Tag''s',PlayUseID3Tags);
    PlayAutoRemove:=Ini1.ReadBool('Playing','Autoremove',PlayAutoRemove);
    PlayAutoRandom:=Ini1.ReadBool('Playing','Autorandom',PlayAutoRandom);
    PlayRemoveAfterBack:=Ini1.ReadBool('Playing','RemoveAfterBack',PlayRemoveAfterBack);
    PlayThisPlaylistfileOnStart:=Ini1.ReadString('Playing','AutoopenPlaylist',PlayThisPlaylistfileOnStart);
    SaveLastOpenPlaylistfile:=Ini1.ReadBool('Playing','AutoopenPlaylistSave',SaveLastOpenPlaylistfile);
    // -> [Pw]
    PasswordStr:=Trim(Ini1.ReadString('Pw','Password',''));
    NeedPwForExit:=Ini1.ReadBool('Pw','Exit',NeedPwForExit);
    NeedPwForOpenPlaylist:=Ini1.ReadBool('Pw','Open Playlistfile',NeedPwForOpenPlaylist);
    NeedPwForSavePlaylist:=Ini1.ReadBool('Pw','Save Playlistfile',NeedPwForSavePlaylist);
    NeedPwForAddDir:=Ini1.ReadBool('Pw','Add dir',NeedPwForAddDir);
    MeedPwForViewID3Tag:=Ini1.ReadBool('Pw','view ID3-Tag',MeedPwForViewID3Tag);
    MeedPwForEditID3Tag:=Ini1.ReadBool('Pw','edit ID3-Tag',MeedPwForEditID3Tag);
    NeedPwForAddSel:=Ini1.ReadBool('Pw','AddSel',NeedPwForAddSel);
    NeedPwForAddAll:=Ini1.ReadBool('Pw','AddAll',NeedPwForAddAll);
    NeedPwForRemoveSel:=Ini1.ReadBool('Pw','RemoveSel',NeedPwForRemoveSel);
    NeedPwForRemoveAll:=Ini1.ReadBool('Pw','RemoveAll',NeedPwForRemoveAll);
    NeedPwForPlay:=Ini1.ReadBool('Pw','Play',NeedPwForPlay);
    NeedPwForPause:=Ini1.ReadBool('Pw','Pause',NeedPwForPause);
    NeedPwForStop:=Ini1.ReadBool('Pw','Stop',NeedPwForStop);
    NeedPwForBack:=Ini1.ReadBool('Pw','Back',NeedPwForBack);
    NeedPwForNext:=Ini1.ReadBool('Pw','Next',NeedPwForNext);
    // -> [Main]
    singleinstance:=Ini1.ReadBool('Main','singleinstance',singleinstance);
    mainwindowstate:=Ini1.ReadInteger('Main','state',mainwindowstate);
    mainzoom:=Ini1.ReadBool('Main','zoom',mainzoom);
    maincenter:=Ini1.ReadBool('Main','center',maincenter);
    If Ini1.ReadBool('Main','dragdrop',true) then begin ListViewLeft.DragMode:=dmAutomatic; ListViewRight.DragMode:=dmAutomatic; end;
    ListViewLeft.HotTrack:=Ini1.ReadBool('Main','leftlisthottrack',ListViewLeft.HotTrack);
    ListViewLeft.GridLines:=Ini1.ReadBool('Main','leftlistgridlines',ListViewLeft.GridLines);
    ListViewLeft.ShowColumnHeaders:=Ini1.ReadBool('Main','leftlistheaders',ListViewLeft.ShowColumnHeaders);
    PanelLeftCaption.Visible:=Ini1.ReadBool('Main','leftlisttitles',PanelLeftCaption.Visible);
    ListViewRight.HotTrack:=Ini1.ReadBool('Main','rightlisthottrack',ListViewRight.HotTrack);
    ListViewRight.GridLines:=Ini1.ReadBool('Main','rightlistgridlines',ListViewRight.GridLines);
    ListViewRight.ShowColumnHeaders:=Ini1.ReadBool('Main','rightlistheaders',ListViewRight.ShowColumnHeaders);
    PanelRightCaption.Visible:=Ini1.ReadBool('Main','rightlisttitles',PanelRightCaption.Visible);
    mainleft:=Ini1.ReadInteger('Main','left',60);
    maintop:=Ini1.ReadInteger('Main','top',50);
    mainwidth:=Ini1.ReadInteger('Main','width',Screen.Width-120);
    mainheight:=Ini1.ReadInteger('Main','height',Screen.Height-100);
    mainsplitter1:=Ini1.ReadInteger('Main','splitter1',-1); If mainsplitter1<0 then mainsplitter1:=PanelRight.Width;
    mainsplitter2:=Ini1.ReadInteger('Main','splitter2',82); If mainsplitter2>=0 then MemoPanel.Height:=mainsplitter2; MemoPanel.Hint:=IntToStr(MemoPanel.Height);
    flatButtonPanel:=Ini1.ReadBool('Main','flatButtonPanel',false);
    flatPanelLeftCaption:=Ini1.ReadBool('Main','flatPanelLeftCaption',false);
    flatPanelRightCaption:=Ini1.ReadBool('Main','flatPanelRightCaption',false);
    flatItemsPanel:=Ini1.ReadBool('Main','flatItemsPanel',false);
    flatmemo:=Ini1.ReadBool('Main','flatmemo',false);
    flatsongpanel:=Ini1.ReadBool('Main','flatsongpanel',false);
    flatpeakpanel:=Ini1.ReadBool('Main','flatpeakpanel',true);
    flatpospanel:=Ini1.ReadBool('Main','flatpospanel',false);
    flatplayerpanel:=Ini1.ReadBool('Main','flatplayerpanel',false);
    // -> [Style]
    font1:=TFont.Create; Try
      s1:=Ini1.ReadString('Style','mainfont',''); If StringToFont(s1,font1) then SetFontOrColor(11,font1,-1);
      i1:=Ini1.ReadInteger('Style','mainbackgrd',-1); if i1>=0 then SetFontOrColor(101,nil,i1);
      s1:=Ini1.ReadString('Style','leftlistfont',''); If StringToFont(s1,font1) then SetFontOrColor(12,font1,-1);
      i1:=Ini1.ReadInteger('Style','leftlistbackgrd',-1); if i1>=0 then SetFontOrColor(102,nil,i1);
      s1:=Ini1.ReadString('Style','rightlistfont',''); If StringToFont(s1,font1) then SetFontOrColor(13,font1,-1);
      i1:=Ini1.ReadInteger('Style','rightlistbackgrd',-1); If i1>=0 then SetFontOrColor(103,nil,i1);
      i1:=Ini1.ReadInteger('Style','playerfontcolor',-1); If i1>=0 then SetFontOrColor(104,nil,i1);
    Finally font1.Free; end;
    PosUpdateTime:=Ini1.ReadInteger('Style','scrollandpostime',PosUpdateTime); MPEGPlayer.TimerFreq:=PosUpdateTime;
    PeakLevelUpdateTime:=Ini1.ReadInteger('Style','peakleveltime',PeakLevelUpdateTime); PeakTimer.Interval:=PeakLevelUpdateTime;
  Finally Ini1.Free; end;
  If SingleInstance then If ActivatePreviousInstances then Try Free; Finally Halt; end;
  Case mainwindowstate of 0,1,2,3: begin // first set normal
    If maincenter then begin mainleft:=(Screen.Width-mainwidth) div 2; maintop:=(Screen.Height-mainheight) div 2; end;
    SetBounds(mainleft,maintop,mainwidth,mainheight);
    WindowState:=wsNormal;
    BorderStyle:=bsSizeable;
    If mainsplitter1>=0 then PanelRight.Width:=mainsplitter1 else PanelRight.Width:=(mainwidth-ItemsPanel.Width) div 2;
  end; end;
  Case mainwindowstate of // now set the other states (by default normal)
    1: PostMessage(Handle,WM_SYSCOMMAND,SC_MINIMIZE,0); // minimized
    2: begin // trayicon
         FillChar(FNotifyIconData,SizeOf(TNotifyIconData),0);
         With FNotifyIconData do begin
           cbSize:=SizeOf(TNotifyIconData);
           uID:=1;
           Wnd:=TrayIconHandle;
           uFlags:=uFlags or NIF_MESSAGE or NIF_ICON or NIF_TIP;
           StrPCopy(szTip,PChar(Application.Title));
           uCallbackMessage:=WM_SYSTRAY;
           hIcon:=Application.Icon.Handle;
         end;
         Shell_NotifyIcon(NIM_ADD,@FNotifyIconData);         
       end;
    3: WindowState:=wsMaximized; // maximized
    4: PanelRight.Width:=(Screen.Width-ItemsPanel.Width) div 2; // fullscreen
  end;
  ButtonSelGroup.Glyph:=ButtonSelect.Glyph;
  If flatButtonPanel then ButtonPanelFlatButton_OnClick(nil);
  If flatPanelLeftCaption then LeftTitleFlatButton_OnClick(nil);
  If flatPanelRightCaption then RightTitleFlatButton_OnClick(nil);
  If flatItemsPanel then ItemsPanelFlatButton_OnClick(nil);
  If flatmemo then MemoFlatButton_OnClick(nil);
  If flatsongpanel then SongFlatButton_OnClick(nil);
  If flatpeakpanel then PeakFlatButton_OnClick(nil);
  If flatpospanel then PosFlatButton_OnClick(nil);
  If flatplayerpanel then PlayerFlatButton_OnClick(nil);
end;

procedure TMainForm.OnShow(Sender: TObject);
begin If mainwindowstate=2 then ShowWindow(Application.Handle,SW_HIDE); end;

procedure TMainForm.OnActivate(Sender: TObject);
var i1: integer; s1, s2: string; CmdParamsStr: string;
begin
  If mainwindowstate=2 then ShowWindow(Application.Handle,SW_HIDE);
  If Tag=1 then begin Tag:=2;
    Update;
    LastSongs:=TStringList.Create;
    MPEGPlayer.PathToDll:=StartDir; MPEGPlayer.Init;
    MpgAudio:=TMPEGAudio.Create;
    If MemoPanel.Visible then Memo.Lines.LoadFromFile(StartDir+'Memo.txt');
    If ParamCount>0 then begin
      CmdParamsStr:='';
      For i1:=1 to ParamCount do CmdParamsStr:=CmdParamsStr+' '+ParamStr(i1);
      If ExpandFileCom(CmdParamsStr,s1)=1 then ReadPlaylistfile(CmdParamsStr,false);
    end else begin
      s1:=PlayThisPlaylistfileOnStart;
      If ExpandFileCom(s1,s2)=1 then ReadPlaylistfile(Trim(s1),false);
    end;
    Screen.Cursor:=crDefault;
  end;
end;           

procedure TMainForm.ButtonPanel_OnResize(Sender: TObject);
var i1: integer;
begin
  ButtonPanel1.Width:=ButtonPanel.Width-ButtonPanelFlatButton.Width-5;
  i1:=(ButtonPanel1.Width-4) div 10;
  ButtonExit.Width:=i1;
  ButtonOpen.SetBounds(ButtonExit.Left+i1,1,i1,ButtonExit.Height);
  ButtonAdd.SetBounds(ButtonOpen.Left+i1,1,i1,ButtonExit.Height);
  ButtonAddDir.SetBounds(ButtonAdd.Left+i1,1,i1,ButtonExit.Height);
  ButtonSave.SetBounds(ButtonAddDir.Left+i1,1,i1,ButtonExit.Height);
  ButtonSelect.SetBounds(ButtonSave.Left+i1,1,i1,ButtonExit.Height);

⌨️ 快捷键说明

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