2014-06-27 46 views
1

我目前正在編程的東西,但我遇到了一個小問題,它是:Me.Handle in Module - Alternative?

在模塊中使用Me.Handle將被用作CodeDom編譯器的源。

我想或者說需要在以下方法來使用它:

Private Const APPCOMMAND_VOLUME_MUTE As Integer = &H80000 
Private Const WM_APPCOMMAND As Integer = &H319 
Declare Function SendMessageW Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr 

Private Sub Mute() 
    SendMessageW(Me.Handle, WM_APPCOMMAND, Me.Handle, CType(APPCOMMAND_VOLUME_MUTE, IntPtr)) 
End Sub 

你的想法,我想靜音系統聲音。我或多或少尋找某種方式做 這不使用Me.Handle,因爲它不工作在我的模塊出於某種原因...

任何幫助表示讚賞,在此先感謝傢伙!

回答

0

好的傢伙,我設法這樣做是爲了解決它:

Private frm As New System.Windows.Forms.Form() 
SendMessageW(frm.Handle, WM_APPCOMMAND, frm.Handle, CType(APPCOMMAND_VOLUME_MUTE, IntPtr)) 

這基本上它做什麼內的模塊,謝謝反正!

2

SendMessage需要一個窗體的句柄,而您在模塊或標準類中沒有該窗體。

三個選項無論是在引用傳遞到像這樣的形式:

Private Sub Mute(formRef As Form) 
    SendMessageW(formRef.Handle, WM_APPCOMMAND, formRef.Handle, CType(APPCOMMAND_VOLUME_MUTE, IntPtr)) 
End Sub 

或者集合中使用的句柄第一種形式:

Private Sub Mute() 
    SendMessageW(Application.OpenForms(0).Handle, WM_APPCOMMAND, Application.OpenForms(0).Handle, CType(APPCOMMAND_VOLUME_MUTE, IntPtr)) 
End Sub 

或者使用一個參考MainWindowHandle:

Private Sub Mute() 
    SendMessageW(Process.GetCurrentProcess().MainWindowHandle, WM_APPCOMMAND, Process.GetCurrentProcess().MainWindowHandle, CType(APPCOMMAND_VOLUME_MUTE, IntPtr)) 
End Sub 
+0

您需要替換第一個'Me.Handle'參考! – Grim

+0

是@Grim well spotted –

+0

沒問題!這讓我在工作中無聊的一天感覺很有成效,如果沒有別的東西! – Grim

1

作爲替代方案,您可以使用Vista Core Audio API

例I類寫道:

Public Class MasterVolume 

    ''' <summary> 
    ''' The device enumerator. 
    ''' </summary> 
    Private DeviceEnumerator As New CoreAudioApi.MMDeviceEnumerator() 

    ''' <summary> 
    ''' The default device. 
    ''' </summary> 
    Private DefaultDevice As CoreAudioApi.MMDevice = 
     DeviceEnumerator.GetDefaultAudioEndpoint(CoreAudioApi.EDataFlow.eRender, CoreAudioApi.ERole.eMultimedia) 

    ''' <summary> 
    ''' Gets or sets the current volume. 
    ''' </summary> 
    ''' <value>The current volume.</value> 
    Public Property Volume As Integer 
     Get 
      Return CInt(Me.DefaultDevice.AudioEndpointVolume.MasterVolumeLevelScalar * 100I) 
     End Get 
     Set(ByVal value As Integer) 
      Me.DefaultDevice.AudioEndpointVolume.MasterVolumeLevelScalar = CSng(value/100I) 
     End Set 
    End Property 

    ''' <summary> 
    ''' Mutes the volume. 
    ''' </summary> 
    Public Sub Mute() 
     Me.DefaultDevice.AudioEndpointVolume.Mute = True 
    End Sub 

    ''' <summary> 
    ''' Unmutes the volume. 
    ''' </summary> 
    Public Sub Unmute() 
     Me.DefaultDevice.AudioEndpointVolume.Mute = False 
    End Sub 

End Class 

實例應用:

Private Sub Test() Handles MyBase.Shown 

    Dim MasterVolume As New MasterVolume 

    With MasterVolume 

     ' Mutes the device. 
     .Mute() 

     ' Unmutes the device. 
     .Unmute() 

     ' Set device volume at 50% 
     .Volume = 50I 

     ' Shows the current device volume. 
     MessageBox.Show(String.Format("Current Vol.: {0}%", .Volume())) 

    End With 

End Sub