使用Windows訊息控制Winamp(VB) (轉)
第一部分:得到的視窗
Winamp是一個32位的應用。也就是說,我們可以使用32位的Windows的一些基礎技術來實現控制Winamp,即使用Windows的訊息。在你給Winamp傳送訊息之前,你必須得到Winamp視窗的控制程式碼。這裡有個方法,使用外部應用程式(例如你自己編寫的VB應用)就可以達到要求。
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Dim hwndWinamp as long
hwndWinamp = FindWindow("Winamp v1.x",vbNullString)
大家可能會注意到一點,為什麼要使用類名"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 or 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暫停,播放,和跳到下一首音樂
新建一個標準EXE工程,加入一個模組,在模組中加入程式碼----------
Option Explicit
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Const WM_COMMAND = &H111
Const IPC_WINAMP_NextTrackButton = 40048 '跳到下一首音樂
Const IPC_WINAMP_PlayButton = 40045 '播放
Const IPC_WINAMP_PauseUnpauseButton = 40046 '暫停
Public hwndWinamp As Long 'Winamp的控制程式碼
Public Sub WinampPlay() '播放
SendMessage hwndWinamp, WM_COMMAND, IPC_WINAMP_PlayButton, 0
End Sub
Public Sub WinampPause() '暫停
SendMessage hwndWinamp, WM_COMMAND, IPC_WINAMP_PauseUnpauseButton, 0
End Sub
Public Sub WinampNext() '跳到下一首音樂
SendMessage hwndWinamp, WM_COMMAND, IPC_WINAMP_NextTrackButton, 0
End Sub
在Form中加入的程式碼----------
Option Explicit
Private Sub cmdNext_Click()
WinampNext '跳到下一首音樂
End Sub
Private Sub cmdPause_Click()
WinampPause '暫停
End Sub
Private Sub cmdPlay_Click()
WinampPlay '播放
End Sub
Private Sub Form_Load()
hwndWinamp = FindWindow("Winamp v1.x", vbNullString) '得到Winamp的控制程式碼
If hwndWinamp = 0 Then '判斷Winamp是否執行
MsgBox "請啟動Winamp先!"
Unload Me
End If
End Sub
2.WM_USER 訊息:
例程:得到Winamp的版本
新建一個標準EXE工程,加入一個模組,在模組中加入程式碼----------
Option Explicit
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
'這是VB的宣告,不會返回正確的結果
'Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
'這是正確的SendMessage宣告,才能返回版本資訊,讀者可以參考一下兩者的不同
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Const WM_USER = &H400
Const WA_GETVERSION = 0
Public hwndWinamp As Long 'Winamp的控制程式碼
Function WinampGetVersion() As String
Dim VersionNum As Long
Dim ReturnVersion As String
VersionNum = SendMessage(hwndWinamp, WM_USER, 0, WA_GETVERSION)
If Len(Hex(VersionNum)) > 3 Then
ReturnVersion = Left(Hex(VersionNum), 1) & "."
ReturnVersion = ReturnVersion & Mid(Hex(VersionNum), 2, 1)
ReturnVersion = ReturnVersion & Right$(Hex(VersionNum), Len(Hex(VersionNum)) - 3)
WinampGetVersion = ReturnVersion
Else
WinampGetVersion = "UNKNOWN"
End If
End Function
在Form中加入的程式碼----------
Option Explicit
Private Sub Command1_Click()
MsgBox "你的Winamp的版本是:" & WinampGetVersion
End Sub
Private Sub Form_Load()
hwndWinamp = FindWindow("Winamp v1.x", vbNullString) '得到Winamp的控制程式碼
If hwndWinamp = 0 Then '判斷Winamp是否執行
MsgBox "請啟動Winamp先!"
Unload Me
End If
End Sub
3.WM_COPYDATA 訊息:
例程:Winamp的執行時刻在播放列表中加入一首的歌曲
新建一個標準EXE工程,加入一個模組,在模組中加入程式碼----------
Option Explicit
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long
Private Declare Function CopyDataSendMessage Lib "user32" Alias "SendMessageA" (ByVal WndID As Long, ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As COPYDATASTRUCT) As Long
Private Type COPYDATASTRUCT
dwData As Long
cbData As Long
lpData As Long
End Type
Const IPC_PLAYFILE = 100
Const WM_COPYDATA = &H4A
Public hwndWinamp As Long
Public Sub WinampAddSong(Soame As String)
Dim CDS As COPYDATASTRUCT
With CDS
.dwData = IPC_PLAYFILE
.cbData = 100
.lpData = lstrcpy(SongName, SongName)
End With
CopyDataSendMessage hwndWinamp, WM_COPYDATA, 0&, CDS
End Sub
在Form中加入的程式碼----------
Option Explicit
Private Sub Command1_Click()
WinampAddSong "I:Mp3周杰倫-簡單愛.mp3" '加入一首歌
End Sub
Private Sub Form_Load()
hwndWinamp = FindWindow("Winamp v1.x", vbNullString) '得到Winamp的控制程式碼
If hwndWinamp = 0 Then '判斷Winamp是否執行
MsgBox "請啟動Winamp先!"
Unload Me
End If
End Sub
所有例程在 + 中透過
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10748419/viewspace-990030/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 使用Windows訊息控制Winamp(Delphi) (轉)Windows
- VB無所不能之三:VB截獲Windows訊息的鉤子薦Windows
- WINDOWS訊息說明 (轉)Windows
- Windows訊息機制初談 (轉)Windows
- Windows訊息Windows
- 截獲與管理Windows的訊息 (轉)Windows
- Windows訊息大全Windows
- SysListView控制元件類的訊息常量 (轉)View控制元件
- VB與VC混合程式設計中處理訊息的方法 (轉)程式設計
- Windows訊息鉤取Windows
- windows 訊息斷點Windows斷點
- 使用 NSProxy 實現訊息轉發
- 【轉】使用oracle pipe傳遞訊息Oracle
- 在VC中,讓動態建立的ActiveX控制元件響應Windows訊息。 (轉)控制元件Windows
- 自定義訊息獲取訊息(轉)
- Windows訊息機制概述Windows
- WIN7環境使用 windows訊息列隊Win7Windows
- 在VB.Net中建立使用控制元件陣列 (轉)控制元件陣列
- MFC動態建立控制元件的訊息處理 (轉)控制元件
- 使用VB.NET更改WINDOWS 2000的密碼 (轉)Windows密碼
- VB.net chart 控制元件使用控制元件
- 講解VB.NET定製Windows控制元件Windows控制元件
- 訊息處理:利用控制元件的動態派生[RunTime],為控制元件新增該控制元件未定義的訊息對映 (轉)控制元件
- OC訊息機制,訊息轉發機制
- 7- Windows訊息鉤取Windows
- 簡析Windows訊息機制Windows
- windows 字元訊息——WM_CHARWindows字元
- vb開發通訊軟體 (轉)
- iOS 訊息轉發iOS
- Delphi使用VB編寫的ActiveX控制元件全攻略 (轉)控制元件
- OCX 控制元件主動傳送訊息給 MFC 視窗訊息控制元件
- 關於捕獲VCL沒有處理的Windows訊息 (轉)Windows
- Vb中控制元件的自動排列 (轉)控制元件
- vb開發通訊軟體(cloud轉貼) (轉)Cloud
- windows訊息機制與例項Windows
- 常用Windows訊息、常量命名參考Windows
- 用VB除錯串列埠通訊 (轉)除錯串列埠
- 利用Delphi訊息處理建立類似Windows開始選單 (轉)Windows