;==============================================================================
;	    Test Department's WINDOWS 32 BIT x86 ASSEMBLY TUTORIAL'S	211
;==============================================================================

;==============================================================================
; ==> Part 211 : enhanced audio cd player
;------------------------------------------------------------------------------
; Ahoy, the shortest (?) Audio CD Player around welcomes you.
; Like other features I missed a TRACK DISPLAY in the last ( Part 210 )
; win32asm example.
; This is just a small example how to enhance the program.
; If you carefully read the tut's before you will be able to program your own
; Audio CD Player with much more features, like graphics and so on ...
; In the Window procedure reacting to WM_CREATE message, we init a Timer.
; We proceed a WM_TIMER message.
; Here we check the MCI_STATUS, we also program a short routine to convert a
; BYTE value into a STRING, for example ( 1A HEX ==> 26 DECIMAL ).
; Now we are ready to call API= "SetWindowTextA" to update the Title Bar.
; All "buttons" are menu bar items defined in the resource file ( rsrc.rc ) !!!
; Any question ? Refere to the tutorials before or email me ...
; "lpfnWndProc" is a pointer to the subroutine label "WindowProc" ( WP1 ) where
; all the action code for this main window resist.
; "lpfnWndProc" is part of WndClassEx structure used by API RegisterClassEx.

;==============================================================================
; Assembler directives
;------------------------------------------------------------------------------
.386			; specifies the processor our program want run on
.Model Flat ,StdCall	; always the same for Win95 (32 Bit)
option casemap:none	; case sensitive !!!

;==============================================================================
; Include all files where API functions resist you want use, set correct path !
;------------------------------------------------------------------------------
include \masm32\include\windows.inc
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib
includelib \masm32\lib\winmm.lib

;==============================================================================
; Declaration of used API functions,take a look into WIN32.HLP and *.inc files
;------------------------------------------------------------------------------
GetModuleHandleA	PROTO :DWORD
LoadIconA		PROTO :DWORD,:DWORD
LoadCursorA		PROTO :DWORD,:DWORD
RegisterClassExA	PROTO :DWORD
CreateWindowExA		PROTO :DWORD,:DWORD,:DWORD,:DWORD,:DWORD,:DWORD,:DWORD,
			      :DWORD,:DWORD,:DWORD,:DWORD,:DWORD
ShowWindow		PROTO :DWORD,:DWORD
UpdateWindow		PROTO :DWORD
GetMessageA 		PROTO :DWORD,:DWORD,:DWORD,:DWORD
TranslateMessage 	PROTO :DWORD
DispatchMessageA 	PROTO :DWORD
PostQuitMessage 	PROTO :DWORD
DefWindowProcA 		PROTO :DWORD,:DWORD,:DWORD,:DWORD
ExitProcess		PROTO :DWORD
MessageBoxA		PROTO :DWORD,:DWORD,:DWORD,:DWORD
DestroyWindow 		PROTO :DWORD
SendMessageA 		PROTO :DWORD,:DWORD,:DWORD,:DWORD
mciSendCommandA   	PROTO :DWORD,:DWORD,:DWORD,:DWORD
mciGetErrorStringA 	PROTO :DWORD,:DWORD,:DWORD
SetTimer		PROTO :DWORD,:DWORD,:DWORD,:DWORD
SetWindowTextA		PROTO :DWORD,:DWORD

;==============================================================================
; .const 	= the constants area starts here,constants are defined & fixed
;------------------------------------------------------------------------------
.const

;==============================================================================
; .Data		= the data area starts here, datas are defined but not fixed
;------------------------------------------------------------------------------
.Data
IconName		db "TDIcon",0		;icon name in rc file
MenuName		db "TDMenu",0		;menu name in rc file
ClassName		db "TDWinClass",0	;name of windows class
WindowName		db "Test Department - Track 01",0;window name titel bar
MB2Titel 		db "Last Status",0	;message box name
MB2Text			db 255 dup(0)		;message box text
mci_status_flag		dd 0h			;
mci_pause_flag		dd 0h			;

; - MCI_OPEN_PARMS Structure ( API=mciSendCommand ) -
open_dwCallback		dd 0h		;
open_wDeviceID		dd 0h		;
open_lpstrDeviceType	dd 0h		;
open_lpstrElementName	dd 0h		;
open_lpstrAlias		dd 0h		;

; - MCI_SET_PARMS Structure ( API=mciSendCommand ) -
set_dwCallback		dd 0h		;  
set_dwTimeFormat	dd 0h		; 
set_dwAudio		dd 0h		; 

; - MCI_PLAY_PARMS Structure ( API=mciSendCommand ) -
play_dwCallback		dd 0h		;   
play_dwFrom		dd 1h		; 
play_dwTo		dd 0h		; 

; - MCI_GENERIC_PARMS Structure ( API=mciSendCommand ) -
generic_dwCallback	dd 0h		;

; - MCI_STATUS_PARMS Structure ( API=mciSendCommand ) -
status_dwCallback	dd 0h		;
status_dwReturn		dd 0h		;
status_dwItem		dd 0h		;
status_dwTrack		dd 0h		;

; - WndClassEx Structure ( API=RegisterClassExA ) -
cbSize			dd 0h		;size in bytes of this structure
style			dd 0h		;window style
lpfnWndProc		dd 0h		;address of user proc function
cbclsExtra		dd 0h		;extra bytes to allocate set to 0
cbWndExtra		dd 0h		;extra bytes class directive, rc file
hInstance		dd 0h		;program handle(API=GetModuleHandleA)
hIcon			dd 0h		;handle of icon (API=LoadIconA)
hcursor			dd 0h		;handle of cursor (API=LoadCursor)
hbrBackground		dd 0h		;background color, 0=transparent
lpszMenuName 		dd 0h		;name of menu class in resource file
lpszClassName		dd 0h		;name of windows this window class
hIconSm			dd 0h		;iconhandle 0=search in resource file

; - Push Parameter done by lpfnWndProc Function ( API=RegisterClassExA )
WP1_CallBack		dd 0h		;return address of calling routine
WP1_hWnd		dd 0h		;handle of window who receives message
WP1_uMsg		dd 0h		;the message number 
WP1_wParam		dd 0h		;extra info about the message 
WP1_lParam		dd 0h		;extra info about the message 

; - Msg Structure ( API=GetMessageA ) - member POINT = POINT structure
hWnd			dd 0h		;handle of window who receives message
message			dd 0h		;the message number
wParam			dd 0h		;extra info about the message
lParam			dd 0h		;extra info about the message 
time			dd 0h		;time the message was posted 
xpt			dd 0h		;cursor x-position, POINT struc
ypt			dd 0h		;cursor x-position, POINT struc 

;==============================================================================
; .Data?	= the data? area starts here, not defined and not fixed
;------------------------------------------------------------------------------
.data?

;==============================================================================
; .CODE	= our code area starts here	Main = label of our program code
;------------------------------------------------------------------------------
.Code
Main:

;==============================================================================
; Always get your program ID first (API=GetModuleHandleA)
;------------------------------------------------------------------------------
push	0h				;lpModuleHandle, 0=get program handle
call 	GetModuleHandleA		;- API Function -
mov	hInstance,eax			;return value in eax=handle of program

;==============================================================================
; The API function "RegisterClassExA" registers a window class
; This API needs a "WNDCLASSEX" structure so we fill it with correct values
;------------------------------------------------------------------------------
mov	cbSize,30h			;size in bytes of WNDCLASSEX structure
mov	style,3h			;window style
mov	lpfnWndProc,OFFSET WP1		;address of user lpfnWndProc function
mov	cbclsExtra,0h			;extra bytes to allocate set to 0
mov	cbWndExtra,0h			;class directive in rc file
mov	hbrBackground,2h		;background,1=background(parameter+1)
mov	lpszMenuName,OFFSET MenuName	;menu name in resource file
mov	lpszClassName,OFFSET ClassName	;name of windows class
mov	hIconSm,0h			;iconhandle 0=search in rc file
;------------------------------------------------------------------------------
; API "LoadIconA" loads an icon defined in the resource file and store the
; handle in the "WNDCLASSEX" structure
;------------------------------------------------------------------------------
push	OFFSET IconName 		;icon-string or icon resource id
push	hInstance			;our program handle
call 	LoadIconA			;- API Function -
mov	hIcon,eax			;handle of newly loaded icon
;------------------------------------------------------------------------------
; API "LoadCursorA" loads a default system cursor, in this case we must set
; hInstance to 0 and lpCursorName to a default system cursor value, here 32512
; Then we store the cursor handle in the "WNDCLASSEX" structure
;------------------------------------------------------------------------------
push	32512				;lpCursorName,default value in dezimal
push	0h				;hInstance, 0=default system cursor
call	LoadCursorA			;- API Function -
mov	hcursor,eax			;handle of the cursor
;------------------------------------------------------------------------------
; Now, after filled the "WNDCLASSEX" structure we call API "RegisterClassEx"
;------------------------------------------------------------------------------
push	OFFSET cbSize			;pointer to WNDCLASSEX structure
call 	RegisterClassExA		;- API Function -

;==============================================================================
; API "CreateWindowExA" creates an overlapped, pop-up, or child window with an
; extended style. The return value in EAX is the handle of the new window.
; This API sends a WM_CREATE message to the window procedure (WP1).
;------------------------------------------------------------------------------
push	0h				;lpParam, extra pointer data 0=no data
push	hInstance			;hInstance, handle of our program
push 	0h				;hMenu, handle window menu 0=class menu
push	0h				;hWndParent, handle parent window 0=no
push	00000062h			;intnHeight, window height pixel
push	0100h    			;intnWidth, window width pixel
push	00000040h			;inty, vertical position window
push	00000140h			;intx, horizontal position window
push	04CA0000h			;dwStyle, 0=no sysmenu/close buttons
push	OFFSET WindowName		;lpWindowName, pointer to window name
push	OFFSET ClassName		;lpClassName, pointer to class name
push	0300h				;dwExStyle, extra window style 0=no
call	CreateWindowExA			;- API Function -
mov	hWnd,eax			;hwnd,return value=handle of window

;==============================================================================
; API "ShowWindow" function sets the specified window's show state. 
;------------------------------------------------------------------------------
push 	1h				;nCmdShow, show state 1=SW_SHOWNORMAL
push 	hWnd				;hwnd, handle of window
call 	ShowWindow			;- API Function -

;==============================================================================
; API "UpdateWindow" updates the area of the specified window by sending a
; WM_PAINT message to the window if the window's update region is not empty.
;------------------------------------------------------------------------------
push 	hWnd				;hwnd, handle of window
call	UpdateWindow			;- API Function -

;==============================================================================
; API "GetMessageA" retrieves a message & places it in the specified structure.
;------------------------------------------------------------------------------
LoopGetMessage:
push	0h				;wMsgFilterMax, highest message value
push	0h				;wMsgFilterMin, lowest message value
push	0h				;hWnd, handle of window who gets msg.
push	OFFSET hWnd			;lpMsg, pointer to MSG structure
call	GetMessageA			;- API Function -
cmp	eax,0h				;check if return value=0 (exit)
je	ExitPrg				;if return value is 0 goto LABEL

;==============================================================================
; API "TranslateMessage" translates key code into ASCII character messages
;------------------------------------------------------------------------------
push	OFFSET hWnd			;lpMSG, pointer to msg structure
call	TranslateMessage		;- API Function - keyboard code

;==============================================================================
; API "DispatchMessageA" function dispatches a message to a window procedure.
;------------------------------------------------------------------------------
push	OFFSET hWnd			;lpMSG, pointer to msg structure
call	DispatchMessageA		;- API Function -
jmp	LoopGetMessage			;check for message again, goto LABEL

;==============================================================================
; Next we terminate our program (API=ExitProcess)
;------------------------------------------------------------------------------
ExitPrg:
push	hInstance			;push our programm handle to exit
call	ExitProcess			;- API Function -

;##############################################################################
;==============================================================================
; This is the Window Procedure lpfnWndProc (API=RegisterClassExA) for this
; registered window.
; The WindowProc function is an application-defined callback function that
; processes messages sent to a window. 
; Here our code for checking the receiving messages resist.
; In the future it is the main work for us to react to the recieved messages.
; First we POP all parameter given by Windows from the stack and store them in
; variables for future use.
; !!!!! Next we PUSH the WP1_CallBack return address back to the stack !!!!!
; !!! WP1_CallBack must be avaible on the stack in this window procedure !!!
; It is also a good idea to PUSHAD all register, because than we are free to
; use all register in this window procedure.
; Before we leave this subroutine we must POPAD them back. 
;------------------------------------------------------------------------------
WP1:
pop 	WP1_CallBack			;POP return address lpfnWndProc
pop	WP1_hWnd			;handle of window who receives message
pop	WP1_uMsg			;the message number
pop	WP1_wParam			;extra info about the message
pop	WP1_lParam			;extra info about the message
push	WP1_CallBack			;PUSH return address lpfnWndProc !!!!!
pushad					;push all register to the stack

mov	eax,WP1_uMsg			;move the message number to eax
;==============================================================================
; WM_CREATE (value=01h) message received ?, posted by API - CREATEWINDOWEX
; Here we open the device and set the audio and time format.
; We also init a windows timer, which will send a WM_TIMER message.
;------------------------------------------------------------------------------
WP1_uMsg_01h:
cmp	eax,1h				;check if WM_CREATE message recieved
jne	WP1_uMsg_02h			;if not goto label
;------------------------------------------------------------------------------
; API "mciSendCommandA" here opens the device
;------------------------------------------------------------------------------
mov	open_lpstrDeviceType,516	;fill MCI_OPEN_PARMS structure
					;MCI_DEVTYPE_CD_AUDIO	= 516
push	OFFSET open_dwCallback        	;dwParam, address MCI_OPEN_PARMS struc.
push	3100h                         	;fdwCommand,
					;MCI_OPEN_TYPE		= 2000h
					;MCI_OPEN_TYPE_ID	= 1000h
					;MCI_OPEN_SHAREABLE	= 100h
push	0803h                      	;uMsg, command message, MCI_OPEN
push	0h                            	;IDDevice, not used with MCI_OPEN
call	mciSendCommandA               	;- API Function -
call	My_mciErrorString               ;- SUBROUTINE -
;------------------------------------------------------------------------------
; API "mciSendCommandA" here sets the time format and audio format
; TMSF = track, minute, second, frame
;------------------------------------------------------------------------------
mov	set_dwTimeFormat,0Ah		;fill MCI_SET_PARMS structure
					;MCI_FORMAT_TMSF	= 0Ah
mov	set_dwAudio,6401h		;fill MCI_SET_PARMS structure
					;MCI_SET_AUDIO_ALL	= 4001h
					;MCI_SET_ON		= 2000h
					;MCI_SET_TIME_FORMAT	= 400h
push	OFFSET set_dwCallback		;dwParam, address MCI_SET_PARMS struc.
push	6401h				;fdwCommand, same as above 
push	080Dh				;uMsg, command message, MCI_SET
push	open_wDeviceID			;IDDevice, given from MCI_OPEN above
call	mciSendCommandA			;- API Function -
call	My_mciErrorString		;- SUBROUTINE -
;------------------------------------------------------------------------------
; API "SetTimer" creates a timer with the specified time-out value
;------------------------------------------------------------------------------
push	0h				;tmprc, address timer procedure,0=this
push	1000				;uTimeout, time-out value milliseconds
push	10h				;idTimer, timer identifier, nonzero
push	WP1_hWnd			;hwnd, handle of window for timer msg.
call	SetTimer			;- API Function -
jmp	WP1_return			;

;==============================================================================
; WM_DESTROY (value=02h) message received ?
;------------------------------------------------------------------------------
WP1_uMsg_02h:
cmp	eax,2h				;check if value=2h (WM_DESTROY)
jne	WP1_uMsg_0Fh			;if not 2h go to LABEL
call	My_mciSendCommandStop		;- SUBROUTINE -
call	My_mciSendCommandClose		;- SUBROUTINE -
;------------------------------------------------------------------------------
; API "PostQuitMessage" indicates to Windows a request to terminate
;------------------------------------------------------------------------------
popad					;pop all register back from stack
push	0h				;nExitCode, exit code=wParam
call 	PostQuitMessage			;- API Function -
xor	eax,eax				;set eax to 0 to exit our program
ret					;return to lpfnWndProc

;==============================================================================
; WM_PAINT (value=0Fh) message, used to repaint the window area
;------------------------------------------------------------------------------
WP1_uMsg_0Fh:
cmp	eax,0Fh				;check if WM_PAINT message recieved
jne	WP1_uMsg_111h			;if not goto label
jmp	WP1_return			;

;==============================================================================
; WM_COMMAND (value=111h) message recieved ?
;------------------------------------------------------------------------------
WP1_uMsg_111h:
cmp	eax,111h			;check if WM_COMMAND message recieved
jne	WP1_uMsg_0113h			;if not goto label

;==============================================================================
; On WM_COMMAND message we check if it is posted by a menu control ( lParam=0 )
; or by a child window control ( lParam not 0 )!
;------------------------------------------------------------------------------
;                 Low word of wParam    High word wParam    lParam
; Menu          - Menu ID               0                   0
; Child window  - Control ID            Notification code   Child Window Handle
;------------------------------------------------------------------------------
mov	eax,WP1_lParam			;check if menu control ( lParam=0 )
jne	WP1_lParam_0F00h		;if not 0 goto label

;==============================================================================
; Check extra message info menu control, "01"-"30" item in menu bar clicked
;------------------------------------------------------------------------------
WP1_wParam_01h:
mov	eax,WP1_wParam			;extra info about the message in ax
cmp	ax,1Eh				;max. (30) ID of item in rc file
ja	WP1_wParam_0032h		;if above 1Eh (30 dezimal) goto LABEL
cwde					;convert word ax into doubleword eax
push	eax				;
call	My_mciSendCommandClose		;- SUBROUTINE -
call	My_mciErrorString		;- SUBROUTINE -
call	My_mciSendCommandStatus_03	;- SUBROUTINE -
pop	ebx				;
cmp	ebx,eax				;
ja	WP1_return			;
mov	mci_pause_flag,0h		;reset pause flag
mov	play_dwFrom,ebx			;store starting track in structure
call	My_mciSendCommandPlay		;- SUBROUTINE -
call	My_mciErrorString		;- SUBROUTINE -
jmp	WP1_return			;

;==============================================================================
; Extra message menu control, "?" item in menu bar,ID=32h clicked
;------------------------------------------------------------------------------
WP1_wParam_0032h:
cmp	ax,0032h			;ID of menu control in rc file
jne	WP1_wParam_0051h		;if not 0032h goto LABEL
call	My_mciErrorTextbox		;- SUBROUTINE -
jmp	WP1_return			;

;==============================================================================
; Check extra message info menu control, "Play" item in menu bar,ID=51h clicked
; If the "Pause Flag" is 1 we continue playing
;------------------------------------------------------------------------------
WP1_wParam_0051h:
cmp	ax,51h				;ID of item in rc file
jne	WP1_wParam_0052h		;if not 51h goto LABEL
call	My_mciSendCommandClose		;- SUBROUTINE -
call	My_mciErrorString		;- SUBROUTINE -
cmp	mci_pause_flag,1h		;check if currently paused playing
jne	play				;if not go to LABEL
mov	mci_pause_flag,0h		;reset pause flag
call	My_mciSendCommandResume		;- SUBROUTINE -
call	My_mciErrorString		;- SUBROUTINE -
jmp	WP1_return			;
play:					;LABEL
call	My_mciSendCommandPlay		;- SUBROUTINE -
call	My_mciErrorString		;- SUBROUTINE -
jmp	WP1_return			;

;==============================================================================
; Check extra message info menu control,"Pause" item in menu bar,ID=52h clicked
; If the "Pause Flag" is 1 we continue playing
;------------------------------------------------------------------------------
WP1_wParam_0052h:
cmp	ax,52h				;ID of item in rc file
jne	WP1_wParam_0053h		;if not 52h goto LABEL
call	My_mciSendCommandStatus_04	;- SUBROUTINE -
cmp	eax,526				;check if currently playing a track
jne	continue			;if not playing goto LABEL
mov	mci_pause_flag,1h		;set pause flag
push	OFFSET generic_dwCallback	;dwParam, MCI_GENERIC_PARMS structure
push	0h				;fdwCommand, 0h=no command
push	0809h				;uMsg, command message, MCI_PAUSE
push	open_wDeviceID			;IDDevice, given from MCI_OPEN
call	mciSendCommandA			;- API Function -
call	My_mciErrorString		;- SUBROUTINE -
jmp  	WP1_return			;
continue:				;
cmp	mci_pause_flag,1h		;check if pause flag is set
jne	WP1_return			;if not nothing to do
mov	mci_pause_flag,0h		;reset pause flag
call	My_mciSendCommandResume		;- SUBROUTINE -
call	My_mciErrorString		;- SUBROUTINE -
jmp	WP1_return			;

;==============================================================================
; Check extra message info menu control,"Stop" item in menu bar,ID=53h clicked
;------------------------------------------------------------------------------
WP1_wParam_0053h:
cmp	ax,53h				;ID of item in rc file
jne	WP1_wParam_0054h		;if not 53h goto LABEL
mov	mci_pause_flag,0h		;reset pause flag
call	My_mciSendCommandStop		;- SUBROUTINE -
call	My_mciErrorString		;- SUBROUTINE -
jmp  	WP1_return			;

;==============================================================================
; Check extra message info menu control, "Open" item in menu bar,ID=54h clicked
;------------------------------------------------------------------------------
WP1_wParam_0054h:
cmp	ax,0054h			;ID of menu control in rc file
jne	WP1_wParam_0055h		;if not 0054h goto LABEL
mov	mci_pause_flag,0h		;reset pause flag
push	OFFSET set_dwCallback		;dwParam, address MCI_SET_PARMS struc.
push	0100h				;fdwCommand, MCI_SET_DOOR_OPEN
push	080Dh				;uMsg, command message, MCI_SET
push	open_wDeviceID			;IDDevice, given from MCI_OPEN
call	mciSendCommandA			;- API Function -
call	My_mciErrorString		;- SUBROUTINE -
mov	play_dwFrom,1h			;reset track
jmp	WP1_return			;

;==============================================================================
; Check extra message info menu control,"Close" item in menu bar,ID=55h clicked
;------------------------------------------------------------------------------
WP1_wParam_0055h:
cmp	ax,0055h			;ID of menu control in rc file
jne	WP1_wParam_0056h		;if not 0055h goto LABEL
mov	mci_pause_flag,0h		;reset pause flag
call	My_mciSendCommandClose		;- SUBROUTINE -
call	My_mciErrorString		;- SUBROUTINE -
mov	play_dwFrom,1h			;reset track
jmp	WP1_return			;

;==============================================================================
; Check extra message info menu control,"<" item in menu bar,ID=56h clicked
;------------------------------------------------------------------------------
WP1_wParam_0056h:
cmp	ax,0056h			;ID of menu control in rc file
jne	WP1_wParam_0057h		;if not 0055h goto LABEL
call	My_mciSendCommandClose		;- SUBROUTINE -
call	My_mciErrorString		;- SUBROUTINE -
call	My_mciSendCommandStatus_03	;- SUBROUTINE -
cmp	play_dwFrom,1h			;check if current track is the first
ja	play_trackdown			;it is not the first so go to LABEL
inc	eax				;inc highest possible track ...
mov	play_dwFrom,eax			;move EAX into play_dwFrom structure
play_trackdown:				;LABEL
dec	play_dwFrom			;dec play_dwFrom
mov	mci_pause_flag,0h		;reset pause flag
call	My_mciSendCommandPlay		;- SUBROUTINE -
call	My_mciErrorString		;- SUBROUTINE -
jmp	WP1_return			;

;==============================================================================
; Check extra message info menu control,">" item in menu bar,ID=57h clicked
;------------------------------------------------------------------------------
WP1_wParam_0057h:
cmp	ax,0057h			;ID of menu control in rc file
jne	WP1_return			;if not 0055h goto LABEL
call	My_mciSendCommandClose		;- SUBROUTINE -
call	My_mciErrorString		;- SUBROUTINE -
call	My_mciSendCommandStatus_03	;- SUBROUTINE -
inc	play_dwFrom			;inc track number
cmp	play_dwFrom,eax			;check if next track is possible
jbe	play_trackup			;track is avaible so play it
mov	play_dwFrom,1h			;it is not avaible so start over
play_trackup:				;LABEL
mov	mci_pause_flag,0h		;reset pause flag
call	My_mciSendCommandPlay		;- SUBROUTINE -
call	My_mciErrorString		;- SUBROUTINE -
jmp	WP1_return			;

;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
; Here we react to any action related to the child windows ( lParam not 0 ).
; On event we send a message to the Window Procedure "lpfnWndProc" !
;==============================================================================
; DUMMY ! For more information take a Look in the tutorials before.
;------------------------------------------------------------------------------
WP1_lParam_0F00h:
jmp	WP1_return			;DUMMY !
;==============================================================================
;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

;==============================================================================
; WM_TIMER (value=113h) message received ?
;------------------------------------------------------------------------------
WP1_uMsg_0113h:
cmp	eax,113h			;check if WM_TIMER message recieved
jne	WP1_return			;if not goto label
;------------------------------------------------------------------------------
; API "mciSendCommandA" here retrieves information about the current track
;------------------------------------------------------------------------------
mov	status_dwItem,08h		;fill struc., MCI_STATUS_CURRENT_TRACK
push	OFFSET status_dwCallback	;dwParam, MCI_STATUS_PARMS structure
push	0108h				;fdwCommand,
					;MCI_STATUS_ITEM	  = 0100h
					;MCI_STATUS_CURRENT_TRACK =   08h
push	0814h				;uMsg, command message, MCI_STATUS
push	open_wDeviceID			;IDDevice, given from MCI_OPEN
call	mciSendCommandA			;- API Function -
;------------------------------------------------------------------------------
; This is my small routine to manage the Track handling ( TMSF format )
; Current track number is in status_dwReturn of MCI_STATUS_PARMS structure !
;------------------------------------------------------------------------------
mov	eax,status_dwReturn		;value from MCI_STATUS_PARMS structure
cmp	eax,mci_status_flag		;check if new track
je	WP1_return			;if not new exit
mov	mci_status_flag,eax		;store new track in variable
mov	play_dwFrom,eax			;don't forget to store it in structure!
mov	cl,al				;move AL into CL, use CL as a counter
mov	al,30h				;mov 30h (ASCII=0) into AL
mov	ah,30h				;mov 30h (ASCII=0) into AH
cmp	cl,0h				;check if track=0, hopefully not
je	update_track			;if track=0 go direct to LABEL
cmp	cl,63h				;check if track above 99 decimal
ja	update_track			;if track above 99 go direct to LABEL
loop_00:				;LABEL for loop
inc	al				;this routine converts HEX into ASCII
cmp	al,3Ah				;
jne	next_00				;
mov	al,30h				;
inc	ah				;
next_00:				;LABEL
dec	cl				;
jne	loop_00				;jump to LABEL if counter CL is not 0
update_track:				;
mov	ebx,OFFSET WindowName		;move pointer WindowName into EBX
mov	[ebx+24],ah			;store current track in the title bar
mov	[ebx+25],al			;
;------------------------------------------------------------------------------
; API "SetWindowTextA" here sets the window title bar text, updating track
;------------------------------------------------------------------------------
push	OFFSET WindowName		;lpsz, address of string
push	WP1_hWnd			;hwnd, handle of window or control
call	SetWindowTextA			;- API Function -
jmp	WP1_return			;

;==============================================================================
; API "DefWindowProcA" calls the window procedure to provide default processing
; for any window messages that an application does not process.
; This function ensures that every message is processed.
; It is called with the same parameters received by the window procedure.
;------------------------------------------------------------------------------
WP1_return:
popad					;pop all register from stack
push	WP1_lParam			;extra info about the message
push	WP1_wParam			;extra info about the message
push	WP1_uMsg			;the message number
push	WP1_hWnd			;handle of window who receives message
call	DefWindowProcA			;- API Function -
ret					;return to lpfnWndProc (multitask os)
;==============================================================================
;##############################################################################

;******************************************************************************
;==============================================================================
; My own subroutine(s) for a compacter code resist here ...
;------------------------------------------------------------------------------
; API "mciGetErrorString" retrieves the last status message
;------------------------------------------------------------------------------
My_mciErrorString:
push	255				;cchErrorText, length of textbuffer
push	OFFSET MB2Text			;lpszErrorText, address of textbuffer
push	eax				;fdwError, error code returned by API
call	mciGetErrorStringA		;- API Function -
ret					;
;------------------------------------------------------------------------------
; API "MessageBoxA" creates a message box, we can only click OK
;------------------------------------------------------------------------------
My_mciErrorTextbox:
push	0h             			;uType, style, 0=MB_OK Button
push	OFFSET MB2Titel			;lpCaption,pointer to title text
push	OFFSET MB2Text      		;lpText,pointer to text message box
push	WP1_hWnd			;handle of owner window 0=no owner
call	MessageBoxA       		;- API Function -
ret					;
;------------------------------------------------------------------------------
; API "mciSendCommandA" here closes the cdrom door if it is open
;------------------------------------------------------------------------------
My_mciSendCommandClose:
call	My_mciSendCommandStatus_04	;- SUBROUTINE -
cmp	eax,530				;check if door is open
jne	goout				;
push	OFFSET set_dwCallback		;dwParam, address MCI_SET_PARMS struc.
push	0200h				;fdwCommand, MCI_SET_DOOR_CLOSED
push	080Dh				;uMsg, command message, MCI_SET
push	open_wDeviceID			;IDDevice, given from MCI_OPEN
call	mciSendCommandA			;- API Function -
goout:					;
ret					;
;------------------------------------------------------------------------------
; API "mciSendCommandA" here stops playing the current track
;------------------------------------------------------------------------------
My_mciSendCommandStop:
push	OFFSET generic_dwCallback	;dwParam, MCI_GENERIC_PARMS structure
push	0h				;fdwCommand, 0h=no command
push	0808h				;uMsg, command message, MCI_STOP
push	open_wDeviceID			;IDDevice, given from MCI_OPEN
call	mciSendCommandA			;- API Function -
ret					;
;------------------------------------------------------------------------------
; API "mciSendCommandA" here resumes playing the current track
; Currently I do not know why RESUME don't work, so I use the PLAY uMsg !!!
;------------------------------------------------------------------------------
My_mciSendCommandResume:
push	OFFSET play_dwCallback		;dwParam, address MCI_PLAY_PARMS struc.
push	0h				;fdwCommand, 0=no command
push	0806h				;uMsg, command message, MCI_PLAY
push	open_wDeviceID			;IDDevice, given from MCI_OPEN
call	mciSendCommandA			;- API Function -
ret					;
;------------------------------------------------------------------------------
; API "mciSendCommandA" here plays from the choosen track to the end of media
; The resource ID is also used in play_dwFrom for the starting track number !
;------------------------------------------------------------------------------
My_mciSendCommandPlay:
push	OFFSET play_dwCallback		;dwParam, address MCI_PLAY_PARMS struc.
push	04h				;fdwCommand, MCI_FROM
push	0806h				;uMsg, command message, MCI_PLAY
push	open_wDeviceID			;IDDevice, given from MCI_OPEN
call	mciSendCommandA			;- API Function -
ret					;
;------------------------------------------------------------------------------
; API "mciSendCommandA" here retrieves information about max. number of tracks
;------------------------------------------------------------------------------
My_mciSendCommandStatus_03:
mov	status_dwItem,03h		;structure, MCI_STATUS_NUMBER_OF_TRACKS
push	OFFSET status_dwCallback	;dwParam, MCI_STATUS_PARMS structure
push	0103h				;fdwCommand,
					;MCI_STATUS_ITEM	     = 0100h
					;MCI_STATUS_NUMBER_OF_TRACKS =   03h
push	0814h				;uMsg, command message, MCI_STATUS
push	open_wDeviceID			;IDDevice, given from MCI_OPEN
call	mciSendCommandA			;- API Function -
mov	eax,status_dwReturn		;value from MCI_STATUS_PARMS structure
ret					;
;------------------------------------------------------------------------------
; API "mciSendCommandA" here retrieves infos about current mode of the device
;------------------------------------------------------------------------------
My_mciSendCommandStatus_04:
mov	status_dwItem,04h		;structure, MCI_STATUS_MODE
push	OFFSET status_dwCallback	;dwParam, MCI_STATUS_PARMS structure
push	0104h				;fdwCommand,
					;MCI_STATUS_ITEM = 0100h
					;MCI_STATUS_MODE =   04h
push	0814h				;uMsg, command message, MCI_STATUS
push	open_wDeviceID			;IDDevice, given from MCI_OPEN
call	mciSendCommandA			;- API Function -
mov	eax,status_dwReturn		;move current mode into EAX
ret					;
;==============================================================================
;******************************************************************************

;==============================================================================
; end Main	= end of our program code
;------------------------------------------------------------------------------
end Main				;end of our program code, entry point

;==============================================================================
; To create the exe file use this commands with your Microsoft Assembler/Linker
;------------------------------------------------------------------------------
; ml.exe /c /coff td__211.asm					;asm command
; rc.exe /v rsrc.rc						;rc command
; cvtres.exe /machine:ix86 rsrc.res
; link.exe /subsystem:windows td__211.obj rsrc.obj		;link command
;==============================================================================