I need some help, I am playing around with VB.NET 2010 and am trying to build my own application that has a desktop and can have plugins installed and run with it. At the moment i am creating a volume control much like windows.
So I have been searching for a number of days and have found a few things that work but not in the way that i need. This application will run on Windows 7 x64 Home Premium, so the code needs to be for that OS.
The code i have at the moment is
Volume Class - Found online
Imports System.Runtime.InteropServices
Public Class Sound
#Region "Comments"
#End Region
#Region "Constants"
Private Const MMSYSERR_NOERROR As Integer = 0
Private Const MAXPNAMELEN As Integer = 32
Private Const MIXER_LONG_NAME_CHARS As Integer = 64
Private Const MIXER_SHORT_NAME_CHARS As Integer = 16
Private Const MIXERCONTROL_CT_CLASS_FADER As Integer = &H50000000
Private Const MIXERCONTROL_CT_UNITS_UNSIGNED As Integer = &H30000
Private Const MIXERCONTROL_CT_UNITS_BOOLEAN As Integer = &H10000
Private Const MIXERCONTROL_CT_CLASS_SWITCH As Integer = &H20000000
Private Const MIXERLINE_COMPONENTTYPE_DST_FIRST As Integer = &H0&
Private Const MIXERLINE_COMPONENTTYPE_DST_SPEAKERS As Integer = (MIXERLINE_COMPONENTTYPE_DST_FIRST + 4)
Private Const MIXERCONTROL_CONTROLTYPE_FADER As Integer = (MIXERCONTROL_CT_CLASS_FADER Or MIXERCONTROL_CT_UNITS_UNSIGNED)
Private Const MIXERCONTROL_CONTROLTYPE_VOLUME As Integer = (MIXERCONTROL_CONTROLTYPE_FADER + 1)
Private Const MIXER_GETLINEINFOF_COMPONENTTYPE As Integer = &H3&
Private Const MIXER_GETLINECONTROLSF_ONEBYTYPE As Integer = &H2
Private Const MIXERCONTROL_CONTROLTYPE_BASS As Integer = (MIXERCONTROL_CONTROLTYPE_FADER + 2)
Private Const MIXERCONTROL_CONTROLTYPE_TREBLE As Integer = (MIXERCONTROL_CONTROLTYPE_FADER + 3)
Private Const MIXERCONTROL_CONTROLTYPE_EQUALIZER As Integer = (MIXERCONTROL_CONTROLTYPE_FADER + 4)
Private Const MIXERCONTROL_CONTROLTYPE_BOOLEAN As Integer = (MIXERCONTROL_CT_CLASS_SWITCH Or MIXERCONTROL_CT_UNITS_BOOLEAN)
Private Const MIXERCONTROL_CONTROLTYPE_MUTE As Integer = (MIXERCONTROL_CONTROLTYPE_BOOLEAN + 2)
#End Region
#Region "Structs"
<StructLayout(LayoutKind.Sequential)> _
Private Structure MIXERCONTROL
<FieldOffset(0)> Public cbStruct As Integer
<FieldOffset(4)> Public dwControlID As Integer
<FieldOffset(8)> Public dwControlType As Integer
<FieldOffset(12)> Public fdwControl As Integer
<FieldOffset(16)> Public cMultipleItems As Integer
<FieldOffset(20), MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=MIXER_SHORT_NAME_CHARS)> Public szShortName As String
<FieldOffset(36), MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=MIXER_LONG_NAME_CHARS)> Public szName As String
<FieldOffset(100)> Public lMinimum As Integer
<FieldOffset(104)> Public lMaximum As Integer
<FieldOffset(108), MarshalAs(UnmanagedType.ByValArray, SizeConst:=11, ArraySubType:=UnmanagedType.AsAny)> Public reserved() As Integer
End Structure
<StructLayout(LayoutKind.Sequential)> _
Private Structure MIXERCONTROLDETAILS
<FieldOffset(0)> Public cbStruct As Integer
<FieldOffset(4)> Public dwControlID As Integer
<FieldOffset(8)> Public cChannels As Integer
<FieldOffset(12)> Public item As Integer
<FieldOffset(16)> Public cbDetails As Integer
<FieldOffset(20)> Public paDetails As IntPtr
End Structure
<StructLayout(LayoutKind.Sequential)> _
Private Structure MIXERCONTROLDETAILS_UNSIGNED
<FieldOffset(0)> Public dwValue As Integer
End Structure
<StructLayout(LayoutKind.Sequential)> _
Private Structure MIXERLINECONTROLS
<FieldOffset(0)> Public cbStruct As Integer
<FieldOffset(4)> Public dwLineID As Integer
<FieldOffset(8)> Public dwControl As Integer
<FieldOffset(12)> Public cControls As Integer
<FieldOffset(16)> Public cbmxctrl As Integer
<FieldOffset(20)> Public pamxctrl As IntPtr
End Structure
<StructLayout(LayoutKind.Sequential)> _
Private Structure MIXERLINE
<FieldOffset(0)> Public cbStruct As Integer
<FieldOffset(4)> Public dwDestination As Integer
<FieldOffset(8)> Public dwSource As Integer
<FieldOffset(12)> Public dwLineID As Integer
<FieldOffset(16)> Public fdwLine As Integer
<FieldOffset(20)> Public dwUser As Integer
<FieldOffset(24)> Public dwComponentType As Integer
<FieldOffset(28)> Public cChannels As Integer
<FieldOffset(32)> Public cConnections As Integer
<FieldOffset(36)> Public cControls As Integer
<FieldOffset(40), MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=MIXER_SHORT_NAME_CHARS)> Public szShortName As String
<FieldOffset(56), MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=MIXER_LONG_NAME_CHARS)> Public szName As String
<FieldOffset(120)> Public dwType As Integer
<FieldOffset(124)> Public dwDeviceID As Integer
<FieldOffset(128)> Public wMid As Integer
<FieldOffset(132)> Public wPid As Integer
<FieldOffset(136)> Public vDriverVersion As Integer
<FieldOffset(168), MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=MAXPNAMELEN)> Public szPname As String
End Structure
#End Region
#Region "API Declarations"
Private Declare Function mixerOpen Lib "winmm.dll" _
(ByRef phmx As Integer, <MarshalAs(UnmanagedType.U4)> ByVal uMxId As Integer, ByVal dwCallback As Integer, ByVal dwInstance As Integer, ByVal fdwOpen As Integer) As Integer
Private Declare Function mixerGetLineInfo Lib "winmm.dll" Alias "mixerGetLineInfoA" _
(<MarshalAs(UnmanagedType.I4)> ByVal hmxobj As Integer, ByRef pmxl As MIXERLINE, ByVal fdwInfo As Integer) As Integer
Private Declare Function mixerGetLineControls Lib "winmm.dll" Alias "mixerGetLineControlsA" _
(<MarshalAs(UnmanagedType.I4)> ByVal hmxobj As Integer, ByRef pmxlc As MIXERLINECONTROLS, ByVal fdwControls As Integer) As Integer
Private Declare Function mixerSetControlDetails Lib "winmm.dll" (<MarshalAs(UnmanagedType.I4)> ByVal hmxobj As Integer, _
ByRef pmxcd As MIXERCONTROLDETAILS, ByVal fdwDetails As Integer) As Integer
#End Region
Public Sub SetVolume(ByVal Level As Integer)
Dim hmixer As Integer
Dim volCtrl As New MIXERCONTROL
Dim lngReturn As Integer
Dim lngVolSetting As Integer
lngReturn = mixerOpen(hmixer, 0, 0, 0, 0)
If lngReturn <> 0 Then Exit Sub
Call GetVolumeControl(hmixer, MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, _
MIXERCONTROL_CONTROLTYPE_VOLUME, volCtrl)
lngVolSetting = CType(volCtrl.lMaximum * (Level / 100), Integer)
SetVolumeControl(hmixer, volCtrl, lngVolSetting)
End Sub
Public Sub SetSound(ByVal boolMute As Boolean)
Dim hmixer As Integer
Dim volCtrl As New MIXERCONTROL
Dim lngReturn As Integer
Dim lngVolSetting As Integer
lngReturn = mixerOpen(hmixer, 0, 0, 0, 0)
If lngReturn <> 0 Then Exit Sub
Call GetVolumeControl(hmixer, MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, _
MIXERCONTROL_CONTROLTYPE_MUTE, volCtrl)
If boolMute Then
lngVolSetting = 1
Else
lngVolSetting = 0
End If
SetVolumeControl(hmixer, volCtrl, lngVolSetting)
End Sub
Private Function GetVolumeControl(ByVal hmixer As Integer, ByVal componentType As Integer, ByVal ctrlType As Integer, _
ByRef mxc As MIXERCONTROL) As Boolean
Dim mxlc As New MIXERLINECONTROLS
Dim mxl As New MIXERLINE
Dim rc As Integer, pmem As IntPtr
mxl.cbStruct = Marshal.SizeOf(mxl)
mxl.dwComponentType = componentType
rc = mixerGetLineInfo(hmixer, mxl, MIXER_GETLINEINFOF_COMPONENTTYPE)
If (MMSYSERR_NOERROR = rc) Then
mxlc.cbStruct = Marshal.SizeOf(mxlc)
mxlc.dwLineID = mxl.dwLineID
mxlc.dwControl = ctrlType
mxlc.cControls = 1
mxlc.cbmxctrl = Marshal.SizeOf(mxc)
pmem = Marshal.AllocHGlobal(Marshal.SizeOf(mxc))
mxlc.pamxctrl = pmem
mxc.cbStruct = Marshal.SizeOf(mxc)
rc = mixerGetLineControls(hmixer, _
mxlc, _
MIXER_GETLINECONTROLSF_ONEBYTYPE)
If (MMSYSERR_NOERROR = rc) Then
mxc = CType(Marshal.PtrToStructure(mxlc.pamxctrl, GetType(MIXERCONTROL)), MIXERCONTROL)
Marshal.FreeHGlobal(pmem)
Return True
End If
Marshal.FreeHGlobal(pmem)
Exit Function
End If
Return False
End Function
Private Function SetVolumeControl(ByVal hmixer As Integer, _
ByVal mxc As MIXERCONTROL, _
ByVal volume As Integer) As Boolean
Dim mxcd As MIXERCONTROLDETAILS
Dim vol As MIXERCONTROLDETAILS_UNSIGNED
Dim rc As Integer
Dim hptr As IntPtr
mxcd.item = 0
mxcd.dwControlID = mxc.dwControlID
mxcd.cbStruct = Marshal.SizeOf(mxcd)
mxcd.cbDetails = Marshal.SizeOf(vol)
hptr = Marshal.AllocHGlobal(Marshal.SizeOf(vol))
mxcd.paDetails = hptr
mxcd.cChannels = 1
vol.dwValue = volume
Marshal.StructureToPtr(vol, hptr, False)
rc = mixerSetControlDetails(hmixer, _
mxcd, _
0)
Marshal.FreeHGlobal(hptr)
If (MMSYSERR_NOERROR = rc) Then
Return True
Else
Return False
End If
End Function
End Class
and then to access that call i use the following code from a form that has a Slider on it. Minimum 0 and Maximum 100
Private Sub tbVolume_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbVolume.Scroll
Sound.SetVolume(tbVolume.Value)
End Sub
So what i need help with is getting the Volume level at application start up and save that in to the backbone database and call it from there when ever the Home form loads or when the user clicks on the little speaker on the Home form and adjusts the volume with the slider.
any help with this would make my life easier as Google searching is now taking up a lot of development time.