使用Windows消息控制Winamp(Delphi)
--------------------------------------------------------------------------------
第一部分:得到Winamp的窗口
Winamp是一个32位的Windows应用程序。也就是说,我们可以使用32位的Windows编程的一些基础技术来实现控
制Winamp,即使用Windows的消息系统。在你给Winamp发送消息之前,你必须得到Winamp窗口的句柄。这里有
个方法,使用外部应用程序(例如你自己编写的Delphi应用)就可以达到要求。
var
hwndWinamp:HWND;
hwndWinamp := FindWindow('Winamp v1.x',0); //得到Winamp的句柄
大家可能会注意到一点,为什么要使用类名"Winamp v1.x"来FindWindow呢?因为Winamp 1.x 和 2.x 的所有
版本都是用"Winamp v1.x"这个class name的。
第二部分:使用消息
Winamp相应的三个消息:WM_USER,WM_COMMAND,和 WM_COPYDATA。WM_USER 和 WM_COPYDATA 允许你可以对
Winamp进行一些高级的控制。而WM_COMMAND则可以做一些简单的操作,如暂停,播放,和跳到下一首音乐。
1.WM_COMMAND 消息:
Previous track button 40044
Next track button 40048
Play button 40045
Pause/Unpause button 40046
Stop button 40047
Fadeout and stop 40147
Stop after current track 40157
Fast-forward 5 seconds 40148
Fast-rewind 5 seconds 40144
Start of playlist 40154
Go to end of playlist 40158
Open file dialog 40029
Open URL dialog 40155
Open file info box 40188
Set time display mode to elapsed 40037
Set time display mode to remaining 40038
Toggle preferences screen 40012
Open visualization options 40190
Open visualization plug-in options 40191
Execute current visualization plug-in 40192
Toggle about box 40041
Toggle title Autoscrolling 40189
Toggle always on top 40019
Toggle Windowshade 40064
Toggle Playlist Windowshade 40266
Toggle doublesize mode 40165
Toggle EQ 40036
Toggle playlist editor 40040
Toggle main window visible 40258
Toggle minibrowser 40298
Toggle easymove 40186
Raise volume by 1% 40058
Lower volume by 1% 40059
Toggle repeat 40022
Toggle shuffle 40023
Open jump to time dialog 40193
Open jump to file dialog 40194
Open skin selector 40219
Configure current visualization plug-in 40221
Reload the current skin 40291
Close Winamp 40001
Moves back 10 tracks in playlist 40197
Show the edit bookmarks 40320
Adds current track as a bookmark 40321
Play audio CD 40323
Load a preset from EQ 40253
Save a preset to EQF 40254
Opens load presets dialog 40172
Opens auto-load presets dialog 40173
Load default preset 40174
Opens save preset dialog 40175
Opens auto-load save preset 40176
Opens delete preset dialog 40178
Opens delete an auto load preset dialog 40180
例程:控制Winamp暂停,播放,和跳到下一首音乐
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
Const IPC_WINAMP_NextTrackButton:integer = 40048; //跳到下一首音乐
Const IPC_WINAMP_PlayButton:integer = 40045; //播放
Const IPC_WINAMP_PauseUnpauseButton:integer = 40046; //暂停
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
hwndWinamp:HWND;//Winamp窗口的句柄
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
hwndWinamp := FindWindow('Winamp v1.x',0); //得到Winamp的句柄
If hwndWinamp = 0 Then //判断Winamp是否运行
begin
ShowMessage('请启动Winamp先!');
Application.Terminate ;
end
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
//让Winamp跳到下一首音乐
SendMessage(hwndWinamp, WM_COMMAND, IPC_WINAMP_NextTrackButton,0);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
//让Winamp播放当前Mp3
SendMessage(hwndWinamp, WM_COMMAND, IPC_WINAMP_PlayButton,0);
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
//让Winamp暂停当前Mp3
SendMessage(hwndWinamp, WM_COMMAND, IPC_WINAMP_PauseUnpauseButton,0);
end;
end.
2.WM_USER 消息:
例程:得到Winamp的版本
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,StrUtils;//使用StrUtils才能用LeftStr,RightStr,MidStr等函数
Const WA_GETVERSION:integer = 0;//Winamp的版本
type
TForm1 = class(TForm)
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
hwndWinamp:HWND;//Winamp窗口的句柄
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
hwndWinamp := FindWindow('Winamp v1.x',0); //得到Winamp的句柄
If hwndWinamp = 0 Then //判断Winamp是否运行
begin
ShowMessage('请启动Winamp先!');
Application.Terminate ;
end
end;
procedure TForm1.Button1Click(Sender: TObject);
var
VersionNum : integer;
ReturnVersion : String;
begin
VersionNum := SendMessage(hwndWinamp, WM_USER, 0, WA_GETVERSION);
If Length(IntToHex(VersionNum,4)) > 3 Then
begin
ReturnVersion := LeftStr(IntToHex(VersionNum,4), 1) + '.' ;
ReturnVersion := ReturnVersion + MidStr(IntToHex(VersionNum,4), 2, 1);
ReturnVersion := ReturnVersion + RightStr(IntToHex(VersionNum,4),
Length(IntToHex(VersionNum,4)) - 3);
end
Else
ReturnVersion := 'UNKNOWN';
ShowMessage(ReturnVersion);
end;
end.
3.WM_COPYDATA 消息:
例程:Winamp的运行时刻在播放列表中加入一首Mp3的歌曲
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
Const IPC_PLAYFILE:integer = 100 ;//Winamp中加入文件
type
TForm1 = class(TForm)
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
hwndWinamp:HWND;//Winamp窗口的句柄
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
hwndWinamp := FindWindow('Winamp v1.x',0); //得到Winamp的句柄
If hwndWinamp = 0 Then //判断Winamp是否运行
begin
ShowMessage('请启动Winamp先!');
Application.Terminate ;
end
end;
procedure TForm1.Button1Click(Sender: TObject);
var
CDS:COPYDATASTRUCT;
SongName:String;
begin
SongName:='I:\Mp3\周杰伦-简单爱.mp3';
CDS.dwData := IPC_PLAYFILE;
CDS.cbData :=Length(SongName)+1;
CDS.lpData := PChar(SongName);
SendMessage(hwndWinamp, WM_COPYDATA, 0, integer(@CDS));
end;
end.
所有例程在 Win2000 + Delphi6 中调试通过