文章編號: 178456 - 上次校閱: 2008年1月18日 - 版次: 4.0

Volume.exe: 設定使用 Visual Basic 的磁碟區控制層級

系統提示本文適用於您使用的作業系統之外的作業系統。與您不相關的文章內容已停用。

在此頁中

全部展開 | 全部摺疊

結論

Volume.exe 是自動解壓縮的壓縮的檔案,其中包含範例專案,示範如何設定使用 Visual Basic 的磁碟區] 和 [麥克風等級。

其他相關資訊

下列檔案複製到硬碟機上磁碟區層級專案目錄下載及執行自動解壓縮的檔案之後,:
  Form1.frm    - the main form in the project
  Module1.bas  - the module containing the function and type declarations.
  Project1.vbp - the project file
  Project1.vbw - the project workspace file
  Readme.txt   - you are currently reading this file.
從 Visual Basic 中設定這些麥克風及磁碟區層級,使用下列的 Windows API 函式:
  • GlobalAlloc-從堆積配置指定的位元組數。
  • GlobalLock-鎖定全域記憶體的物件並將傳回給物件的記憶體區塊的第一個位元組的指標。與鎖定的記憶體物件相關聯的記憶體區塊不能移動或被丟棄。
  • GlobalFree-釋放指定的全域記憶體物件,並使其控制代碼無效。
  • mixerClose-關閉指定的混音器裝置。
  • mixerGetControlDetails-擷取音訊線相關聯的單一控制項的詳細資料。
  • mixerGetDevCaps-查詢指定混音器裝置,以決定其功能。
  • mixerGetID-擷取指定的裝置控制碼相關聯的混音器裝置的裝置識別項。
  • mixerGetLineControls-擷取音訊線相關聯的一或多個控制項。
  • mixerGetLineInfo-擷取混音器裝置的特定行的相關資訊。
  • mixerGetNumDevs-擷取存在於系統的混音器裝置數目。
  • 將 mixerMessage-自訂混音器驅動程式訊息直接傳送到混音器驅動程式。
  • mixerOpen-開啟指定的混音器裝置,並確保直到應用程式關閉控點,不會移除該裝置。
  • mixerSetControlDetails-設定音訊線相關聯的單一控制項的屬性。
下一節顯示如何建立範例專案使用這些函式來設定磁碟區和麥克風層級。

建立範例專案

  1. 在 Visual Basic 中啟動新的標準 EXE 專案。預設會建立 Form1。
  2. 將兩個命令按鈕、 兩個文字方塊及兩個的標籤加入 Form1。
  3. 將模組加入至專案中,藉由完成下列步驟執行:
    1. 從 [專案] 功能表中,按一下 [新增模組]。加入模組] 對話方塊隨即出現。
    2. 從 [新增] 索引標籤選擇 [模組],然後按一下 [確定]。新的模組加入至您的專案。
  4. 將下列程式碼複製到 Module1 中的 [程式碼] 視窗:
          Option Explicit
    
          Public Const MMSYSERR_NOERROR = 0
          Public Const MAXPNAMELEN = 32
          Public Const MIXER_LONG_NAME_CHARS = 64
          Public Const MIXER_SHORT_NAME_CHARS = 16
          Public Const MIXER_GETLINEINFOF_COMPONENTTYPE = &H3&
          Public Const MIXER_GETCONTROLDETAILSF_VALUE = &H0&
          Public Const MIXER_GETLINECONTROLSF_ONEBYTYPE = &H2&
          Public Const MIXERLINE_COMPONENTTYPE_DST_FIRST = &H0&
          Public Const MIXERLINE_COMPONENTTYPE_SRC_FIRST = &H1000&
    
          Public Const MIXERLINE_COMPONENTTYPE_DST_SPEAKERS = _
                         (MIXERLINE_COMPONENTTYPE_DST_FIRST + 4)
    
          Public Const MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE = _
                         (MIXERLINE_COMPONENTTYPE_SRC_FIRST + 3)
    
          Public Const MIXERLINE_COMPONENTTYPE_SRC_LINE = _
                         (MIXERLINE_COMPONENTTYPE_SRC_FIRST + 2)
    
          Public Const MIXERCONTROL_CT_CLASS_FADER = &H50000000
          Public Const MIXERCONTROL_CT_UNITS_UNSIGNED = &H30000
    
          Public Const MIXERCONTROL_CONTROLTYPE_FADER = _
                         (MIXERCONTROL_CT_CLASS_FADER Or _
                         MIXERCONTROL_CT_UNITS_UNSIGNED)
    
          Public Const MIXERCONTROL_CONTROLTYPE_VOLUME = _
                         (MIXERCONTROL_CONTROLTYPE_FADER + 1)
    
          Declare Function mixerClose Lib "winmm.dll" _
                         (ByVal hmx As Long) As Long
    
          Declare Function mixerGetControlDetails Lib "winmm.dll" _
                         Alias "mixerGetControlDetailsA" _
                         (ByVal hmxobj As Long, _
                         pmxcd As MIXERCONTROLDETAILS, _
                         ByVal fdwDetails As Long) As Long
    
          Declare Function mixerGetDevCaps Lib "winmm.dll" _
                         Alias "mixerGetDevCapsA" _
                         (ByVal uMxId As Long, _
                         ByVal pmxcaps As MIXERCAPS, _
                         ByVal cbmxcaps As Long) As Long
    
          Declare Function mixerGetID Lib "winmm.dll" _
                         (ByVal hmxobj As Long, _
                         pumxID As Long, _
                         ByVal fdwId As Long) As Long
    
          Declare Function mixerGetLineControls Lib "winmm.dll" _
                         Alias "mixerGetLineControlsA" _
                         (ByVal hmxobj As Long, _
                         pmxlc As MIXERLINECONTROLS, _
                         ByVal fdwControls As Long) As Long
    
          Declare Function mixerGetLineInfo Lib "winmm.dll" _
                         Alias "mixerGetLineInfoA" _
                         (ByVal hmxobj As Long, _
                         pmxl As MIXERLINE, _
                         ByVal fdwInfo As Long) As Long
    
          Declare Function mixerGetNumDevs Lib "winmm.dll" () As Long
    
          Declare Function mixerMessage Lib "winmm.dll" _
                         (ByVal hmx As Long, _
                         ByVal uMsg As Long, _
                         ByVal dwParam1 As Long, _
                         ByVal dwParam2 As Long) As Long
    
          Declare Function mixerOpen Lib "winmm.dll" _
                         (phmx As Long, _
                         ByVal uMxId As Long, _
                         ByVal dwCallback As Long, _
                         ByVal dwInstance As Long, _
                         ByVal fdwOpen As Long) As Long
    
          Declare Function mixerSetControlDetails Lib "winmm.dll" _
                         (ByVal hmxobj As Long, _
                         pmxcd As MIXERCONTROLDETAILS, _
                         ByVal fdwDetails As Long) As Long
    
          Declare Sub CopyStructFromPtr Lib "kernel32" _
                         Alias "RtlMoveMemory" _
                         (struct As Any, _
                         ByVal ptr As Long, _
                         ByVal cb As Long)
    
          Declare Sub CopyPtrFromStruct Lib "kernel32" _
                         Alias "RtlMoveMemory" _
                         (ByVal ptr As Long, _
                         struct As Any, _
                         ByVal cb As Long)
    
          Declare Function GlobalAlloc Lib "kernel32" _
                         (ByVal wFlags As Long, _
                         ByVal dwBytes As Long) As Long
    
          Declare Function GlobalLock Lib "kernel32" _
                         (ByVal hmem As Long) As Long
    
          Declare Function GlobalFree Lib "kernel32" _
                         (ByVal hmem As Long) As Long
    
          Type MIXERCAPS
             wMid As Integer                   '  manufacturer id
             wPid As Integer                   '  product id
             vDriverVersion As Long            '  version of the driver
             szPname As String * MAXPNAMELEN   '  product name
             fdwSupport As Long                '  misc. support bits
             cDestinations As Long             '  count of destinations
          End Type
    
          Type MIXERCONTROL
             cbStruct As Long           '  size in Byte of MIXERCONTROL
             dwControlID As Long        '  unique control id for mixer device
             dwControlType As Long      '  MIXERCONTROL_CONTROLTYPE_xxx
             fdwControl As Long         '  MIXERCONTROL_CONTROLF_xxx
             cMultipleItems As Long     '  if MIXERCONTROL_CONTROLF_MULTIPLE
                                        '  set
             szShortName As String * MIXER_SHORT_NAME_CHARS  ' short name of
                                                             ' control
             szName As String * MIXER_LONG_NAME_CHARS        ' long name of
                                                             ' control
             lMinimum As Long           '  Minimum value
             lMaximum As Long           '  Maximum value
             reserved(10) As Long       '  reserved structure space
             End Type
    
          Type MIXERCONTROLDETAILS
             cbStruct As Long       '  size in Byte of MIXERCONTROLDETAILS
             dwControlID As Long    '  control id to get/set details on
             cChannels As Long      '  number of channels in paDetails array
             item As Long           '  hwndOwner or cMultipleItems
             cbDetails As Long      '  size of _one_ details_XX struct
             paDetails As Long      '  pointer to array of details_XX structs
          End Type
    
          Type MIXERCONTROLDETAILS_UNSIGNED
             dwValue As Long        '  value of the control
          End Type
    
          Type MIXERLINE
             cbStruct As Long               '  size of MIXERLINE structure
             dwDestination As Long          '  zero based destination index
             dwSource As Long               '  zero based source index (if
                                            '  source)
             dwLineID As Long               '  unique line id for mixer device
             fdwLine As Long                '  state/information about line
             dwUser As Long                 '  driver specific information
             dwComponentType As Long        '  component type line connects to
             cChannels As Long              '  number of channels line supports
             cConnections As Long           '  number of connections (possible)
             cControls As Long              '  number of controls at this line
             szShortName As String * MIXER_SHORT_NAME_CHARS
             szName As String * MIXER_LONG_NAME_CHARS
             dwType As Long
             dwDeviceID As Long
             wMid  As Integer
             wPid As Integer
             vDriverVersion As Long
             szPname As String * MAXPNAMELEN
          End Type
    
          Type MIXERLINECONTROLS
             cbStruct As Long       '  size in Byte of MIXERLINECONTROLS
             dwLineID As Long       '  line id (from MIXERLINE.dwLineID)
                                    '  MIXER_GETLINECONTROLSF_ONEBYID or
             dwControl As Long      '  MIXER_GETLINECONTROLSF_ONEBYTYPE
             cControls As Long      '  count of controls pmxctrl points to
             cbmxctrl As Long       '  size in Byte of _one_ MIXERCONTROL
             pamxctrl As Long       '  pointer to first MIXERCONTROL array
          End Type
    
          Function GetVolumeControl(ByVal hmixer As Long, _
                                  ByVal componentType As Long, _
                                  ByVal ctrlType As Long, _
                                  ByRef mxc As MIXERCONTROL) As Boolean
    
          ' This function attempts to obtain a mixer control.
          ' Returns True if successful.
             Dim mxlc As MIXERLINECONTROLS
             Dim mxl As MIXERLINE
             Dim hmem As Long
             Dim rc As Long
    
             mxl.cbStruct = Len(mxl)
             mxl.dwComponentType = componentType
    
             ' Obtain a line corresponding to the component type
             rc = mixerGetLineInfo(hmixer, _
                                   mxl, _
                                   MIXER_GETLINEINFOF_COMPONENTTYPE)
    
             If (MMSYSERR_NOERROR = rc) Then
                 mxlc.cbStruct = Len(mxlc)
                 mxlc.dwLineID = mxl.dwLineID
                 mxlc.dwControl = ctrlType
                 mxlc.cControls = 1
                 mxlc.cbmxctrl = Len(mxc)
    
                 ' Allocate a buffer for the control
                 hmem = GlobalAlloc(&H40, Len(mxc))
                 mxlc.pamxctrl = GlobalLock(hmem)
                 mxc.cbStruct = Len(mxc)
    
                 ' Get the control
                 rc = mixerGetLineControls(hmixer, _
                                           mxlc, _
                                           MIXER_GETLINECONTROLSF_ONEBYTYPE)
    
                 If (MMSYSERR_NOERROR = rc) Then
                     GetVolumeControl = True
    
                     ' Copy the control into the destination structure
                     CopyStructFromPtr mxc, mxlc.pamxctrl, Len(mxc)
                 Else
                     GetVolumeControl = False
                 End If
                 GlobalFree (hmem)
                 Exit Function
             End If
    
             GetVolumeControl = False
          End Function
    
          Function SetVolumeControl(ByVal hmixer As Long, _
                                  mxc As MIXERCONTROL, _
                                  ByVal volume As Long) As Boolean
          ' This function sets the value for a volume control.
          ' Returns True if successful
    
             Dim mxcd As MIXERCONTROLDETAILS
             Dim vol As MIXERCONTROLDETAILS_UNSIGNED
    
             mxcd.item = 0
             mxcd.dwControlID = mxc.dwControlID
             mxcd.cbStruct = Len(mxcd)
             mxcd.cbDetails = Len(vol)
    
             ' Allocate a buffer for the control value buffer
             hmem = GlobalAlloc(&H40, Len(vol))
             mxcd.paDetails = GlobalLock(hmem)
             mxcd.cChannels = 1
             vol.dwValue = volume
    
             ' Copy the data into the control value buffer
             CopyPtrFromStruct mxcd.paDetails, vol, Len(vol)
    
             ' Set the control value
             rc = mixerSetControlDetails(hmixer, _
                                        mxcd, _
                                        MIXER_SETCONTROLDETAILSF_VALUE)
    
             GlobalFree (hmem)
             If (MMSYSERR_NOERROR = rc) Then
                 SetVolumeControl = True
             Else
                 SetVolumeControl = False
             End If
          End Function
    
  5. 將下列程式碼複製到 Form1 的程式碼視窗:
          Option Explicit
    
          Dim hmixer As Long          ' mixer handle
          Dim volCtrl As MIXERCONTROL ' waveout volume control
          Dim micCtrl As MIXERCONTROL ' microphone volume control
          Dim rc As Long              ' return code
          Dim ok As Boolean           ' boolean return code
          Dim vol As Long             ' volume  
    
          Private Sub Form_Load()
          ' Open the mixer with deviceID 0.
             rc = mixerOpen(hmixer, 0, 0, 0, 0)
             If ((MMSYSERR_NOERROR <> rc)) Then
                 MsgBox "Couldn't open the mixer."
                 Exit Sub
                 End If
    
             ' Get the waveout volume control
             ok = GetVolumeControl(hmixer, _
                                  MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, _
                                  MIXERCONTROL_CONTROLTYPE_VOLUME, _
                                  volCtrl)
             If (ok = True) Then
                 ' If the function successfully gets the volume control,
                 ' the maximum and minimum values are specified by
                 ' lMaximum and lMinimum
                 Label1.Caption = volCtrl.lMinimum _
                                  & " to " _
                                  & volCtrl.lMaximum
                 End If
    
             ' Get the microphone volume control
             ok = GetVolumeControl(hmixer, _
                                  MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE, _
                                  MIXERCONTROL_CONTROLTYPE_VOLUME, _
                                  micCtrl)
             If (ok = True) Then
                 Label2.Caption = micCtrl.lMinimum _
                                  & " to " _
                                  & micCtrl.lMaximum
                 End If
          End Sub
    
          Private Sub Command1_Click()
             vol = CLng(Text1.Text)
             SetVolumeControl hmixer, volCtrl, vol
          End Sub
    
          Private Sub Command2_Click()
             vol = CLng(Text2.Text)
             SetVolumeControl hmixer, micCtrl, vol
          End Sub
  6. 在 [執行] 功能表按一下 [開始],或按 F5 鍵來啟動程式]。因此所有出現在您的顯示器上為 Form1,最小化 Visual Basic IDE。最大化磁碟區層級程式在任務列中。排列 Form1 和磁碟區層級程式,使這兩個程式都可見。
  7. 一或兩個文字方塊中所顯示的最小和最大值之間輸入值,然後按一下其中一個指令按鈕,在 Form1 中。請注意磁碟區層級程式中對應的滑桿會調整為指定的層級。
(c) Microsoft Corporation 1997,保留所有的權限。
由 Arsenio Locsin,Microsoft 公司所佔的比重

這篇文章中的資訊適用於:
  • Microsoft Visual Basic 5.0 Learning Edition
  • Microsoft Visual Basic 5.0 Professional Edition
  • Microsoft Visual Basic 5.0 Enterprise Edition
  • Microsoft Visual Basic 6.0 Professional Edition
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0
關鍵字:?
kbmt kbdownload kbfile kbhowto kbsample kbsound KB178456 KbMtzh
機器翻譯機器翻譯
重要:本文是以 Microsoft 機器翻譯軟體翻譯而成,而非使用人工翻譯而成。Microsoft 同時提供使用者人工翻譯及機器翻譯兩個版本的文章,讓使用者可以依其使用語言使用知識庫中的所有文章。但是,機器翻譯的文章可能不盡完美。這些文章中也可能出現拼字、語意或文法上的錯誤,就像外國人在使用本國語言時可能發生的錯誤。Microsoft 不為內容的翻譯錯誤或客戶對該內容的使用所產生的任何錯誤或損害負責。Microsoft也同時將不斷地就機器翻譯軟體進行更新。
按一下這裡查看此文章的英文版本:178456? (http://support.microsoft.com/kb/178456/en-us/ )
Microsoft及(或)其供應商不就任何在本伺服器上發表的文字資料及其相關圖表資訊的恰當性作任何承諾。所有文字資料及其相關圖表均以「現狀」供應,不負任何擔保責任。Microsoft及(或)其供應商謹此聲明,不負任何對與此資訊有關之擔保責任,包括關於適售性、適用於某一特定用途、權利或不侵權的明示或默示擔保責任。Microsoft及(或)其供應商無論如何不對因或與使用本伺服器上資訊或與資訊的實行有關而引起的契約、過失或其他侵權行為之訴訟中的特別的、間接的、衍生性的損害或任何因使用而喪失所導致的之損害、資料或利潤負任何責任。