我的程序初步如下,由于的API知识很有限,所以不足之处很多,如果有高手请为我修改一下:(主要目标是能够将几个菜单继承在mainmenu中,目前以我的能力无法办到)<br>program Menu2005;<br><br><br>uses<br> Windows,Messages,mmsystem,SysUtils,shellapi;<br><br>var<br> WinClass: TWndClassA;<br> Inst, Handle: Integer;<br> Msg: TMsg;<br> play,cdplay:boolean;<br> trueid,lockid,command,songpos,songsum:integer;<br> f:textfile;<br> mainmenu,urlmenu,myfilemenu,mediamenu,progmenu:hmenu;<br> aatom:atom;<br> cddrive:string;<br><br>{$R *.RES}<br>const extstr='.mp3.mpg.mpeg.mpga.mid.wav.wma';<br>maincount=6;<br>mainstr:array[0..maincount] of string=('我的菜单','快捷访问','网站','程序','多媒体','-',<br>'退出');<br><br>mediacount=3;<br>mediastr:array[0..mediacount]of string=('返回菜单','播放','停止','AudioCD');<br><br>function isfile(substr,str:string):boolean;<br>var i:integer;<br>begin<br>result:=false;<br>i:=pos(substr,str);<br>if i>0 then result:=true;<br>end;<br><br>procedure closetimer;<br>begin<br>lockid:=Settimer(handle,1000,200,nil);<br>end;<br><br>procedure stopsong;<br>begin<br>play:=false;<br>//cdplay:=false;<br>Killtimer(handle,trueid);<br>mciSendString ('stop myAudio', nil, 0, 0);<br>mciSendString ('close myAudio', nil, 0, 0);<br>end;<br><br>function SendMCIString(cmd
Char) : string;<br>var x: hwnd;<br> a: array [0..20] of char;<br>begin<br> x:=0;<br> mciSendString(cmd, a, 20, x);<br> SendMCIString := a;<br>end;<br><br>function gettime(track:integer):integer;<br>var min,sec,mci:string;<br>begin<br>mci:= 'seek myAudio to ' +inttostr(track+1);<br>SendMCIString(PChar(mci));<br>mci := SendMCIString('status myAudio media present');<br>if mci = 'true' then begin<br>mci := 'status myAudio length track ' + IntToStr(track+1);<br>mci:= SendMCIString(PChar(mci));<br>sec:= Copy(mci, 4, 2);<br>min:= Copy(mci, 0, 2);<br>end;<br>result:=strtoint(min)*60+strtoint(sec);<br>end;<br><br>procedure playcdtrack(track:integer);<br>begin<br>SendMCIString('open myAudio wait shareable');<br>mciSendString('play myAudio notify', nil, 0, 0);<br>mciSendString ('play myAudio', nil, 0, 0);<br>SendMCIString(PChar('play myAudio from ' + IntToStr(track+1)));<br>end;<br><br>procedure playsong(track:integer);<br>var i:integer;<br> f:textfile;<br>Buffer : array [0..255] of char;<br>ext:string;<br>begin<br>AssignFile(f,'song.txt');<br>Reset(f);<br>for i:=0 to track do<br>Readln(f,cddrive);<br>Closefile(f);<br>GetShortPathName(pchar(cddrive), @Buffer, sizeof(Buffer));<br>ext:=extractfileext(cddrive);<br>ext:=ansilowercase(ext);<br>if not isfile(ext,extstr) then begin<br>if songpos=songsum-1 then begin stopsong; exit;end;<br>songpos:=songpos+1;<br>play:=false;<br>playsong(songpos);<br>closetimer;<br>exit;<br>end;<br>mciSendString ('close myAudio', nil, 0, 0);<br>mciSendString (pChar('open ' + buffer + ' alias myAudio'), nil, 0, 0);<br>mciSendString('play myAudio notify', nil, 0, handle);<br>end;<br><br><br><br>procedure nextsong;<br>begin<br>if songpos=songsum-1 then begin stopsong; exit;end;<br>songpos:=songpos+1;<br>play:=false;<br>playsong(songpos);<br>closetimer;<br>end;<br><br><br>function getcdtrack(drive:string):integer;<br>var tracks:integer;<br>Buffer : array [0..255] of char;<br>begin<br>GetShortPathName(pchar( drive+'Track01.cda' ), @Buffer, sizeof(Buffer));<br>mciSendString ('stop myAudio', nil, 0, 0);<br>mciSendString ('close myAudio', nil, 0, 0);<br>mciSendString (pChar('open ' + buffer + ' alias myAudio'), nil, 0, 0);<br>mciSendString('play myAudio notify', nil, 0, handle);<br>mciSendString ('play myAudio', nil, 0, 0);<br><br>SendMCIString('open myAudio wait shareable');<br>SendMCIString('stop myAudio');<br>SendMCIString('set myAudio time format tmsf wait');<br>tracks:=strtoint(SendMCIString('status myAudio number of tracks'));<br>result:=tracks;<br>end;<br><br><br><br>function DiskInDrive(Drive: Char): Boolean;<br>var ErrorMode: word;<br>begin<br>{ make it upper case }<br>if Drive in ['a'..'z'] then Dec(Drive, $20);<br>{ make sure it's a letter }<br>if not (Drive in ['A'..'Z']) then<br>raise EConvertError.Create('Not a valid drive ID');<br>{ turn off critical errors }<br>ErrorMode := SetErrorMode(SEM_FailCriticalErrors);<br>try<br>{ drive 1 = a, 2 = b, 3 = c, etc. }<br>if DiskSize(Ord(Drive) - $40) = -1 then<br>Result := False<br><br>else<br>Result := True;<br>finally<br>{ restore old error mode }<br>SetErrorMode(ErrorMode);<br>end;<br>end;<br><br><br>function IsAudioCD(Drive : char):boolean;<br>var<br> DrivePath : string;<br> MaximumComponentLength : DWORD;<br> FileSystemFlags : DWORD;<br> VolumeName : string;<br> DriveType: UINT;<br> begin<br> Result := false;<br> DrivePath := Drive + ':/';<br> DriveType := GetDriveType(Pchar(DrivePath));<br> if DriveType <> DRIVE_CDROM then<br> exit;<br> SetLength(VolumeName, 64);<br> GetVolumeInformation(PChar(DrivePath),<br> PChar(VolumeName),<br> Length(VolumeName),<br> nil,<br> MaximumComponentLength,<br> FileSystemFlags,<br> nil,<br> 0);<br> if lStrCmp(PChar(VolumeName),'Audio CD') = 0 then result := true;<br> end;<br><br>Function FindCDROMDrive: Char;<br>Var<br>drivemap, mask: DWORD;<br>i: Integer;<br>root: String;<br>Begin<br>Result := #0;<br>root := 'A:/';<br>drivemap := GetLogicalDrives;<br>mask := 1;<br>For i:= 1 To 32 Do Begin<br>If (mask and drivemap) <> 0 Then<br>If GetDriveType( PChar(root) ) = DRIVE_CDROM Then Begin<br>Result := root[1];<br>if diskindrive(result)and isaudiocd(result) then begin<br>cddrive:=result+':/';<br>end else begin<br>setwindowtext(handle,pchar('No AudioCD'));<br>cddrive:='';<br>end;<br>end;<br>mask := mask shl 1;<br>Inc( root[1] );<br>end;<br>End;<br><br>procedure playcd;<br>var i:integer;<br>begin<br>if not cdplay then begin<br>FindCDROMDrive;<br>if cddrive='' then exit;<br>songsum:=getcdtrack(cddrive);<br><br>AssignFile(f,'audio.txt');<br>ReWrite(f);<br><br>AppendMenu (mediamenu, MF_STRING, 4, '-');<br><br>for i:=0 to songsum-1 do begin<br>Writeln(f,inttostr(gettime(i)));<br>AppendMenu (mediamenu, MF_STRING, i+5, pchar('Track'+inttostr(i+1)));<br>end;<br>Closefile(f);<br>playcdtrack(0);<br>cdplay:=true;<br>end;<br><br>end;<br><br><br><br><br>procedure myfilemenucreate;<br>var i,p:integer;<br>str:string;<br>pt:tpoint;<br>begin<br>AssignFile(f,'myfile.txt');<br>Reset(f);<br>i:=0;<br>myfilemenu:=CreatePopupMenu;<br>appendmenu(myfilemenu, MF_popup, 0, '返回菜单');<br>repeat<br>i:=i+1;<br>Readln(f,str);<br>p:=pos('|',str);<br>str:=copy(str,1,p-1);<br>appendmenu(myfilemenu, MF_popup, i, pchar(str));<br>until eof(f);<br>getcursorpos(pt);<br>TrackPopupMenu (myfilemenu, tpm_BottomAlign or tpm_RightAlign,<br>pt.x+150,pt.y-10, 0, handle, Nil);<br><br>end;<br><br>procedure urlmenucreate;<br>var i,p:integer;<br>str:string;<br>pt:tpoint;<br>begin<br>AssignFile(f,'url.txt');<br>Reset(f);<br>i:=0;<br>urlmenu:=CreatePopupMenu;<br>appendmenu(urlmenu, MF_popup, 0, '返回菜单');<br>repeat<br>i:=i+1;<br>Readln(f,str);<br>p:=pos('|',str);<br>str:=copy(str,1,p-1);<br>appendmenu(urlmenu, MF_popup, i, pchar(str));<br>until eof(f);<br>getcursorpos(pt);<br>TrackPopupMenu (urlmenu, tpm_BottomAlign or tpm_RightAlign,<br>pt.x+150,pt.y-10, 0, handle, Nil);<br>end;<br><br>procedure progcreate;<br>var i,p:integer;<br>str:string;<br>pt:tpoint;<br>begin<br>AssignFile(f,'program.txt');<br>Reset(f);<br>i:=0;<br>progmenu:=CreatePopupMenu;<br>appendmenu(progmenu, MF_popup, 0, '返回菜单');<br>repeat<br>i:=i+1;<br>Readln(f,str);<br>p:=pos('|',str);<br>str:=copy(str,1,p-1);<br>appendmenu(progmenu, MF_popup, i, pchar(str));<br>until eof(f);<br>getcursorpos(pt);<br>TrackPopupMenu (progmenu, tpm_BottomAlign or tpm_RightAlign,<br>pt.x+150,pt.y-10, 0, handle, Nil);<br>end;<br><br><br>procedure mediacreate;<br>var i:integer;<br>begin<br>mediamenu:=createpopupmenu;<br>for i:=0 to mediacount do<br>appendmenu(mediamenu, MF_popup, i, pchar(mediastr
));<br>end;<br><br><br>procedure mainmenuclick(cmd:word);<br>var <br>i,p:integer;<br>str:string;<br>begin<br><br>case command of<br><br>//myfile<br>1:begin<br>if cmd=0 then begin<br>command:=0;<br>exit;<br>end;<br><br>AssignFile(f,'myfile.txt');<br>Reset(f);<br>for i:=0 to cmd-1 do<br>Readln(f,str);<br>p:=pos('|',str);<br>delete(str,1,p);<br>ShellExecute(handle,nil,pchar(str),nil,nil,sw_shownormal);<br>end;<br>//net<br>2:begin<br><br>if cmd=0 then begin<br>command:=0;<br>exit;<br>end;<br>AssignFile(f,'url.txt');<br>Reset(f);<br>for i:=0 to cmd-1 do<br>Readln(f,str);<br>p:=pos('|',str);<br>delete(str,1,p);<br>ShellExecute(handle,nil,pchar(str),nil,nil,sw_shownormal);<br>end;<br>//program<br>3:begin<br>if cmd=0 then begin<br>command:=0;<br>exit;<br>end;<br>AssignFile(f,'program.txt');<br>Reset(f);<br>for i:=0 to cmd-1 do<br>Readln(f,str);<br>p:=pos('|',str);<br>delete(str,1,p);<br>ShellExecute(handle,nil,pchar(str),nil,nil,sw_shownormal);<br><br><br>end;<br><br>//mediaplay<br>4:begin<br><br>case cmd of<br>0:begin<br>command:=0;<br>exit;<br>end;<br>//playsong;<br>1:begin<br>AppendMenu (mediamenu, MF_STRING, 4, '-');<br>AssignFile(f,'song.txt');<br>Reset(f);<br>i:=0;<br>repeat<br>i:=i+1;<br>Readln(f,str);<br>str:=extractfilename(str);<br>AppendMenu (mediamenu, MF_STRING, i+4, pchar(str));<br>until eof(f);<br>songsum:=i;<br>Closefile(f);<br>songpos:=0;<br>playsong(0);<br>cdplay:=false;<br>end;<br>//stopsong;<br>2:begin<br>stopsong;<br>cdplay:=false;<br>end;<br><br>//play AudioCD<br>3:begin<br>stopsong;<br>playcd;<br><br>end;<br>//plyasong(i);<br>5..1000:begin<br>songpos:=cmd-5;<br>if cdplay then playcdtrack(songpos) else<br>begin<br>stopsong;<br>playsong(songpos);<br>end;<br>end;<br><br><br>end;<br><br><br><br><br>end;<br><br><br><br>0:begin<br><br>case cmd of<br><br>1:begin<br>myfilemenucreate;<br>command:=1;<br>end;<br><br>2:begin<br>urlmenucreate;<br>command:=2;<br>end;<br><br>3:begin<br>progcreate;<br>command:=3;<br><br>end;<br><br>4:begin<br>mediacreate;<br>command:=4;<br><br>end;<br><br>maincountostMessage (handle, wm_Close, 0, 0);<br>end;<br><br><br><br>end;<br><br>end;<br>end;<br><br><br>procedure creatmainmenu;<br>var i:integer;<br>begin<br>mainmenu:=CreatePopupMenu;<br>for i:=0 to maincount do<br>appendmenu(mainmenu, MF_string, i, pchar(mainstr));<br><br>aatom:=globaladdatom('hot key');<br>RegisterHotKey(handle,aatom,0,VK_F2);<br><br>end;<br><br><br><br><br>{ Custom WindowProc function }<br>function WindowProc(hWnd, uMsg, wParam, lParam: Integer): Integer; stdcall;<br>var pt: TPoint;<br>begin<br><br>Result := DefWindowProc(hWnd, uMsg, wParam, lParam);<br><br>case uMsg of<br><br>wm_hotkey:begin<br>GetCursorPos (pt);<br>case command of<br><br>0:TrackPopupMenu (mainmenu, tpm_BottomAlign or tpm_RightAlign,<br>pt.x,pt.y, 0, handle, Nil);<br><br>1:TrackPopupMenu (myfilemenu, tpm_BottomAlign or tpm_RightAlign,<br>pt.x,pt.y, 0, handle, Nil);<br><br>2:TrackPopupMenu (urlmenu, tpm_BottomAlign or tpm_RightAlign,<br>pt.x,pt.y, 0, handle, Nil);<br><br>3:TrackPopupMenu (progmenu, tpm_BottomAlign or tpm_RightAlign,<br>pt.x,pt.y, 0, handle, Nil);<br><br><br>4:TrackPopupMenu (mediamenu, tpm_BottomAlign or tpm_RightAlign,<br>pt.x,pt.y, 0, handle, Nil);<br>end;<br><br>closetimer;<br>end;<br><br>wm_timer:begin<br>if command=4 then play:=true;<br>Killtimer(handle,lockid);<br>end;<br><br>MM_MCINOTIFY:<br>if play then nextsong;<br><br>WM_COMMAND:begin<br>mainmenuclick(wparam);<br>end;<br><br>wm_create:begin<br><br>end;<br><br>wm_close:begin<br><br>end;<br><br>WM_DESTROY:begin<br>stopsong;<br>UnRegisterClass('mymenu',Inst);<br>ExitProcess(Inst);<br>halt;<br>end;<br>end;<br><br><br>end;<br><br><br>begin<br><br> { ** Register Custom WndClass ** }<br> Inst := hInstance;<br><br> with WinClass do<br> begin<br> style := CS_CLASSDC or CS_PARENTDC;<br> hIcon := LoadIcon(Inst,'MAINICON');<br> lpfnWndProc := @WindowProc;<br> hInstance := Inst;<br> hbrBackground := color_btnface + 1;<br> lpszClassname := 'mymenu';<br> hCursor := LoadCursor(0, IDC_ARROW);<br> end; { with }<br> RegisterClass(WinClass);<br><br> { ** Create Main Window ** }<br> Handle := CreateWindowEx(WS_EX_TOOLWINDOW , 'mymenu', 'Mymenu 1.0',<br> WS_CAPTION or WS_SYSMENU,<br> 150, 150,205, 48, 0, 0, Inst, nil);<br><br>UpdateWindow(Handle);<br>creatmainmenu;<br>command:=0;<br> { ** Message Loop ** }<br>while(GetMessage(Msg, Handle, 0, 0)) do<br>begin<br>TranslateMessage(msg);<br>DispatchMessage(msg);<br>end; { with }<br><br><br>end.