#INCLUDE "windows.bi"
#INCLUDE "win/commdlg.bi"
#Include "win/mmsystem.bi"
Type f
	msg As MSG
	wc As WNDCLASSEX
	hbr as  HBRUSH
	bmp(1 To 6) As HBITMAP
	hwndbut(0 To 8) As HWND
	hm As HMENU
	i As Integer
	NmAs As Integer
	ofn  As OPENFILENAME
	fi As ZString * 8192
	Hinst As HINSTANCE = GetModuleHandle(0)
	PATHMP3(300) As ZString*512
	mp3_string As String
	NameClass As String = "ClassWINDOW"
End Type

Dim Shared As f full
Sub PlayRecourse(Nsongle As Integer)
	mciSendString("CLOSE MP3","",0,0)
	mciSendString("OPEN "& Chr(34) & full.PATHMP3(Nsongle)& Chr(34) & " TYPE MpegVideo ALIAS MP3","",0,0)
	mciSendString("PLAY MP3 from 0","",0,0)
End Sub
sub MP3LengthAndPos()
	Dim st As String*256
	Dim As String*9 b
	Dim As Integer v,m,s,v1,m1,s1,F1,F2
	mciSendString("status MP3 length",st,256,0)
	F1=Val(st)
	v=Val(st)\1000
	s= v Mod 60
	v/=60
	m=v Mod 60
	v/=60
	mciSendString("status MP3 position",st,256,0)
	F2=Val(st)
	v1=Val(st)\1000
	s1= v1 Mod 60
	v1/=60
	m1=v1 Mod 60
	v1/=60
	wsprintf(b,"       %02d:%02d:%02d  ---  %02d:%02d:%02d",v,m,s,v1,m1,s1)
	SendMessage(full.hwndbut(8), 1025 ,0, Cast(LPARAM,@b))
	If F2>=F1 And F1<>0 Then
		If full.NmAs<full.i-1 Then
			full.NmAs+=1
		Else 
			full.NmAs=1						
		EndIf
		PlayRecourse(full.NmAs)
	EndIf
End Sub

Function OpenFileRequester(ByVal Title As String,ByVal curentdir As String, ByVal Pattern As String,ByVal flag As Integer=0) As String
	Dim fbguiTemp As Integer
	With full.ofn
		.hwndOwner       = 0
		.lStructSize     = SizeOf(OPENFILENAME)
		.lpstrFilter     = @Pattern
		.lpstrFile       = @full.fi
		.nFileOffset     = 0
		.nMaxFile        = SizeOf(full.fi)
		.lpstrFileTitle  = @Title
		.nMaxFileTitle   = SizeOf(Title)
		.lpstrInitialDir = @curentdir
		.lpstrTitle      = @Title
		.Flags           = OFN_EXPLORER Or OFN_FILEMUSTEXIST Or OFN_OVERWRITEPROMPT Or flag
	End With

	if( GetOpenFileName( @full.ofn ) = 0 ) then
		Return ""
	Else
		Return full.fi
	end If
End Function

Function NextSelectedFilename Alias "NextSelectedFilename"() As String
	Static e As Byte
	If full.ofn.nFileOffset< Len(full.fi) Then
		If e=0 Then
			e=1
			Return full.fi
		Else
			e=0
			full.fi=""
			Return ""
		EndIf
	Else
		Static a As Integer
		Static b As Byte
		Static c As Byte
		If b=0  Then
			If c=0 Then
				a=full.ofn.nFileOffset
				a=Cast(Integer,@full.fi) + a
			EndIf
			dim aa As string=Peek(a)
			a=a+Len(aa)+1
			If Peek(a) = 0 Then b=1
			c=1
			If Right(full.fi,1)<>"\" Then
				Return full.fi & "\" & aa
			EndIf
			Return full.fi  & aa
		Else
			a=0
			b=0
			c=0
			full.fi=""
			Return ""
		endif
	EndIf
End Function
Function wndproc(hwnd As HWND, msg As Uinteger,_
	wparam As WPARAM, lparam As LPARAM) As Integer
	Select Case msg
		Case WM_CREATE
			full.hm=CreatePopupMenu()
			AppendMenu( full.hm, 0, 1, "Exit")
			For n As Integer=1 To 6
				full.hwndbut(N)=CreateWindowEx(0,"BUTTON","",WS_VISIBLE Or WS_CHILD Or BS_BITMAP,n*25-20,30,20,20,hwnd,Cast(HMENU,n),0,0)
				full.bmp(n)=LoadImage(0,"but/" & n & ".bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE)
				SendMessage(full.hwndbut(N),BM_SETIMAGE,image_bitmap,Cast(LPARAM,full.bmp(n)))
			Next
			full.hwndbut(7)=CreateWindowEx(0,"Static","SimpleAudioPlayer",WS_VISIBLE Or WS_CHILD or SS_CENTER,0,0,156,15,hwnd,Cast(HMENU,7),0,0)
			full.hwndbut(8) = CreateWindowEx(0,"msctls_statusbar32","",WS_CHILD Or WS_VISIBLE,0,0,0,0,hwnd,NULL,0,NULL)
		Case WM_RBUTTONDOWN
			Dim pt As Point
			GetCursorPos(@pt)
			TrackPopupMenuEx(full.hm,0,pt.x,pt.y,full.hwndbut(0),0)
		Case WM_LBUTTONDOWN
			ReleaseCapture()
			SendMessage(full.hwndbut(0), WM_NCLBUTTONDOWN, HTCAPTION, NULL)
		Case WM_COMMAND
			If lParam=0 Then
				If LoWord(wparam)=1 Then
					mciSendString("CLOSE MP3","",0,0)
					PostQuitMessage(0)
				EndIf
			Else
				Select Case LoWord(wparam)
					Case 1
						full.i=0
						Var ddd= OpenFileRequester("","C:\","Sound files (*.mp3, *.wav, *.wma)"_
						+Chr(0)+"*.mp3;*.wav;*.wma"+Chr(0),OFN_ALLOWMULTISELECT)
						If ddd<>"" Then
							While ddd<>""
								full.i+=1
								ddd= NextSelectedFilename()
								full.PATHMP3(full.i)=ddd
							Wend
							full.NmAs=1
							PlayRecourse(1)
							SetTimer(full.hwndbut(0),1,1000,Cast(TimerProc,@MP3LengthAndPos))
						EndIf
					Case 2
						mciSendString("PLAY MP3 from 0","",0,0)
					Case 3
						Dim a As String*20
						mciSendString("status MP3 mode",a,20,0)
						If a="paused" Then
							mciSendString("RESUME MP3","",0,0)
						Else
							mciSendString("PAUSE MP3","",0,0)
						EndIf
					Case 4
						mciSendString("STOP MP3","",0,0)
					Case 5
						If full.NmAs>1 Then
							full.NmAs-=1
						Else
							full.NmAs=full.i-1
						EndIf
						PlayRecourse(full.NmAs)
					Case 6
						If full.NmAs<full.i-1 Then
							full.NmAs+=1
						Else
							full.NmAs=1
						EndIf
						PlayRecourse(full.NmAs)
				End Select
			EndIf
	End Select
	Return DefWindowProc(hwnd,msg,wparam,lparam)
End Function

With full.wc
	.cbSize=SizeOf(WNDCLASSEX)
	.style=CS_HREDRAW Or CS_VREDRAW
	.lpfnWndProc=@WndProc
	.hInstance=full.Hinst
	.hIcon=LoadIcon(0,IDI_QUESTION)
	.hCursor=LoadCursor(0,IDC_ARROW)
	.hbrBackground=Cast(HBRUSH,COLOR_WINDOWFRAME)
	.lpszClassName=StrPtr(full.NameClass)
	.hIconSm=.hIcon
End With

If RegisterClassEx(@full.wc)=0 Then
	Print "Register error, press any key"
	Sleep
	End
Endif

full.hwndbut(0)=CreateWindowEx(0,full.NameClass,"MP3-WAV-WMA Player",_
WS_VISIBLE Or WS_POPUP,10,10,154,80,0,0,full.Hinst,0)
full.hbr=CreateSolidBrush(Cast(COLORREF,&h5F5F5F))
SetClassLong(full.hwndbut(0),GCL_HBRBACKGROUND, Cast(Long,full.hbr))
InvalidateRect (full.hwndbut(0),NULL,TRUE)

While GetMessage(@full.msg,0,0,0)
	TranslateMessage(@full.msg)
	DispatchMessage(@full.msg)
Wend
