With the following code you can change the volume on a Windows PC. But it changes the global volume.
I would like to change the slider value of Internet Explorer in the Mixer settings. can this be done?
Imports System.Runtime.InteropServices
Public Class Form1
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
End Function
Const WM_APPCOMMAND As UInteger = &H319
Const APPCOMMAND_VOLUME_UP As UInteger = &HA
Const APPCOMMAND_VOLUME_DOWN As UInteger = &H9
Const APPCOMMAND_VOLUME_MUTE As UInteger = &H8
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SendMessage(Me.Handle, WM_APPCOMMAND, &H30292, APPCOMMAND_VOLUME_UP * &H10000)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
SendMessage(Me.Handle, WM_APPCOMMAND, &H30292, APPCOMMAND_VOLUME_DOWN * &H10000)
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
SendMessage(Me.Handle, WM_APPCOMMAND, &H200EB0, APPCOMMAND_VOLUME_MUTE * &H10000)
End Sub
End Class
I found a similar answer somewhere else with a large C# example, you should be able to run this through a C# to vb.net converter and get what you're looking for (the person in this example is trying to control FireFox's volume, so essentially exactly what you're looking for).
Controling Volume Mixer
Related
I use the code below to start a cmd.exe windows and move it into a panel after execution. At this moment, the child window is resized to fit into Panel1 on my Form1. I would like it to be the other way around: my Form1 (and thus Panel1) should resize to fit the size of the child window, so no matter what the size of the default cmd.exe window is on the local computer.
Does anybody know how I should do that?
Thanks for any help in advance!
Kind regards,
Eric
Imports System.Runtime.InteropServices
Public Class Form1
Private WithEvents Tmr As New Timer With {.Interval = 100}
Private Const HWND_BOTTOM As Integer = &H1
Private WithEvents proc As New Process
<DllImport("user32.dll", EntryPoint:="SetParent")>
Private Shared Function SetParent(ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As IntPtr
End Function
<DllImport("user32.dll", EntryPoint:="SetWindowPos")>
Private Shared Function SetWindowPos(ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal uFlags As UInteger) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Text = "My title"
proc.EnableRaisingEvents = True
proc.StartInfo.FileName = "cmd"
proc.Start()
Tmr.Start()
End Sub
Private Sub Tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Tmr.Tick
If SetParent(proc.MainWindowHandle, Panel1.Handle) <> IntPtr.Zero Then
Tmr.Stop()
SetWindowPos(proc.MainWindowHandle, New IntPtr(HWND_BOTTOM), 0, 0, Panel1.ClientSize.Width, Panel1.ClientSize.Height, 0)
End If
End Sub
Private Sub Proc_Exited(ByVal sender As Object, ByVal e As System.EventArgs) Handles proc.Exited
Invoke(Sub() Close())
End Sub
End Class
As suggested by #Jimi, I used DwmGetWindowAttribute to retrieve the size of the child, before moving it into the panel. Afterwards I use ShowWindow to maximize it, to get rid of the borders.
The changed code can be found in a different post of mine.
In one of my project I have to get the title of foreground window so I called GetForegroundWindow() Entry Point form User32.dll for getting the windows Handle then I called GetWindowText() for the title everything goes error less but the output comes nothing, here is the code I am using in my VB.NET program.
Imports System.Runtime.InteropServices
Public Class Form1
<DllImport("user32.dll")> _
Private Shared Function GetForegroundWindow() As IntPtr
End Function
<DllImport("user32.dll")> _
Private Shared Function GetWindowText(ByVal hwnd As Long, ByVal lpString As System.Text.StringBuilder, ByVal cch As Long) As Integer
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim hWnd As IntPtr
hWnd = GetForegroundWindow()
Dim title As New System.Text.StringBuilder(256)
GetWindowText(hWnd, title, title.Capacity)
Me.Text = title.ToString
End Sub
End Class
I found the solution myself, It was the fault in the hWnd parameter as Long value for proper functioning of the program it has to be IntPtr. The new correct code looks something like this.
Imports System.Runtime.InteropServices
Public Class Form1
<DllImport("user32.dll")> _
Private Shared Function GetForegroundWindow() As IntPtr
End Function
<DllImport("user32.dll")> _
Private Shared Function GetWindowText(ByVal hwnd As IntPtr, ByVal lpString As System.Text.StringBuilder, ByVal cch As Long) As Integer
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim hWnd As IntPtr
hWnd = GetForegroundWindow()
Dim title As New System.Text.StringBuilder(256)
GetWindowText(hWnd, title, title.Capacity)
Me.Text = title.ToString
End Sub
End Class
I was going through this code here and:
Private Sub Button3_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button3.Click
SendMessage(Me.Handle, WM_APPCOMMAND, &H200EB0,
APPCOMMAND_VOLUME_MUTE * &H10000)
End Sub
I have no idea what the last two parameters of the sendmessage functions are and what is going on in these two parameters? the "&H200EB0" and "APPCOMMAND_VOLUME_MUTE * &H10000" Parameters?
Here's the full code:
Imports System.Runtime.InteropServices
Public Class Form1
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
Private Shared Function SendMessage(ByVal hWnd As IntPtr,
ByVal Msg As UInteger, ByVal wParam As IntPtr,
ByVal lParam As IntPtr) As IntPtr
End Function
Const WM_APPCOMMAND As UInteger = &H319
Const APPCOMMAND_VOLUME_UP As UInteger = &HA
Const APPCOMMAND_VOLUME_DOWN As UInteger = &H9
Const APPCOMMAND_VOLUME_MUTE As UInteger = &H8
Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
SendMessage(Me.Handle, WM_APPCOMMAND, &H30292,
APPCOMMAND_VOLUME_UP * &H10000)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button2.Click
SendMessage(Me.Handle, WM_APPCOMMAND, &H30292,
APPCOMMAND_VOLUME_DOWN * &H10000)
End Sub
Private Sub Button3_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button3.Click
SendMessage(Me.Handle, WM_APPCOMMAND, &H200EB0,
APPCOMMAND_VOLUME_MUTE * &H10000)
End Sub
End Class
SendMessage is a method which can be used to send a specified message to a window or windows.
Documentation is here: http://msdn.microsoft.com/en-us/library/windows/desktop/ms644950%28v=vs.85%29.aspx
The first parameter is the "handle" (HWND) to which the message is send. The second parameter is a message id (see http://msdn.microsoft.com/en-us/library/windows/desktop/ms644927(v=vs.85).aspx#system_defined for System-Defined Messages).
The last two parameters can be used to pass more data to the receiver. - Normally both parameters have message dependent meanings.
In your case the WM_APPCOMMAND message (http://msdn.microsoft.com/en-us/library/windows/desktop/ms646275%28v=vs.85%29.aspx) is passed (here it is a keyboard command). I think the first parameter could also be NULL (according to the link above it should be a handle to the window where the user clicked the button or pressed the key), but the second one has to specify the command which should be passed (as an APPCOMMAND). The second parameter is 8 (8=APPCOMMAND_VOLUME_MUTE according to the list on the linked page), however we have to do a bitshift, because the information has to be encoded in the high-order bits of the parameter (i.e., 0x80000 - that's APPCOMMAND_VOLUME_MUTE*&H10000; see lParam section on the page I linked).
I'm wondering if it would be possible to simulate iPhone notification sounds on VB.NET, in the sense of how they are handled.
For example, if you get a SMS message on iPhone, while listening to music, the music volume is lowered to about 25% while the alert is played, then the volume is restored.
Could this be done with vb.net?
I've seen ways to reduce the system volume, but wouldn't this reduce the volume for my app as well?
I found this which works great, but I'd like to know if I can separate my app from this
Imports System
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Namespace WindowsFormsApplication1
Partial Public Class Form1
Inherits Form
Private Const APPCOMMAND_VOLUME_MUTE As Integer = &H80000
Private Const APPCOMMAND_VOLUME_UP As Integer = &HA0000
Private Const APPCOMMAND_VOLUME_DOWN As Integer = &H90000
Private Const WM_APPCOMMAND As Integer = &H319
<DllImport("user32.dll")> _
Public Shared Function SendMessageW(ByVal hWnd As IntPtr, _
ByVal Msg As Integer, ByVal wParam As IntPtr, _
ByVal lParam As IntPtr) As IntPtr
End Function
Private Sub btnMute_Click(ByVal sender As Object, ByVal e As EventArgs)
SendMessageW(Me.Handle, WM_APPCOMMAND, _
Me.Handle, New IntPtr(APPCOMMAND_VOLUME_MUTE))
End Sub
Private Sub btnDecVol_Click(ByVal sender As Object, ByVal e As EventArgs)
SendMessageW(Me.Handle, WM_APPCOMMAND, _
Me.Handle, New IntPtr(APPCOMMAND_VOLUME_DOWN))
End Sub
Private Sub btnIncVol_Click(ByVal sender As Object, ByVal e As EventArgs)
SendMessageW(Me.Handle, WM_APPCOMMAND, _
Me.Handle, New IntPtr(APPCOMMAND_VOLUME_UP))
End Sub
End Class
End Namespace
I'm creating an "autoclicker" for use in video games that left clicks my mouse repeatedly at the push of a button. I want to have hotkeys set up that work globally without the autoclicker window being in focus. For some reason, only the hotkey that is meant to stop the clicking is currently functioning (F2). The F1 key is supposed to start the clicking, but currently the only way to start it is by pressing the Start button on the form manually. The code seems to throw an error when attempting to be deployed to version 4.0 of the .NET framework, but setting it to 3.5 solves this issue. I've included my entire code below because I can't figure out why the F1 hotkey is not functioning despite the F2 hotkey working flawlessly. Thanks for your time. A good portion of this code I came up with by looking at examples of other peoples' work, and I only understand it to a certain extent. I hope my code is formatted correctly because I'm new to this site and I don't exactly understand how to paste it correctly. Thanks for your help in advance, I really appreciate it.
Public Class Form1
Private Declare Function GetAsyncKeyState Lib "user32.dll" (ByVal vkey As Long) As Integer
Private Declare Sub mouse_event Lib "user32.dll" (ByVal dwflags As Long, ByVal dx As Long, ByVal cbuttons As Long, ByVal dy As Long, ByVal dwExtraInfo As Long)
Dim hotkey1 As Boolean
Dim hotkey2 As Boolean
Private Const mouseclickup = 4
Private Const mouseclickdown = 2
Dim Test As Integer
Dim Interval As Integer
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
Click.Start()
End Sub
Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
Click.Stop()
End Sub
Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click
Test = Test + 1
Counter.Text = Test
End Sub
Private Sub Click_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Click.Tick
Interval = CInt(timeTextbox.Text)
Click.Interval = Interval
mouse_event(mouseclickdown, 0, 0, 0, 0)
mouse_event(mouseclickup, 0, 0, 0, 0)
hotkey1 = GetAsyncKeyState(Keys.F1)
If hotkey1 = True Then
btnStart.PerformClick()
End If
hotkey2 = GetAsyncKeyState(Keys.F2)
If hotkey2 = True Then
btnStop.PerformClick()
End If
End Sub
Private Sub timeTextbox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timeTextbox.TextChanged
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
KeyPreview = True
End Sub
End Class
Your declarations were for VB6. The data types need to be changed for VB.Net as shown below.
Also, you need two independent timers. One for detecting the keypress, which has to run at a high rate. And another for doing the actual clicking, whose rate is determined by your TextBox value.
Public Class Form1
Private Const MOUSEEVENTF_LEFTDOWN As Integer = &H2
Private Const MOUSEEVENTF_LEFTUP As Integer = &H4
Private Declare Function GetAsyncKeyState Lib "user32.dll" (ByVal vKey As System.Windows.Forms.Keys) As Short
Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Integer, _
ByVal dx As Integer, ByVal dy As Integer, ByVal cButtons As Integer, _
ByVal dwExtraInfo As Integer)
Private WithEvents KeyTimer As New Timer
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
KeyTimer.Interval = 50
KeyTimer.Start()
Click.Enabled = False
End Sub
Private Sub KeyTimer_Tick(sender As Object, e As System.EventArgs) Handles KeyTimer.Tick
If IsKeyDown(Keys.F1) Then
Click.Start()
ElseIf IsKeyDown(Keys.F2) Then
Click.Stop()
End If
End Sub
Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click
Static Test As Integer
Test = Test + 1
Counter.Text = Test
End Sub
Private Sub timeTextbox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timeTextbox.TextChanged
Dim Interval As Integer
If Integer.TryParse(timeTextbox.Text, Interval) Then
Click.Interval = Interval
End If
End Sub
Private Sub Click_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Click.Tick
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
End Sub
Private Function IsKeyDown(ByVal key As System.Windows.Forms.Keys) As Boolean
Return ((GetAsyncKeyState(key) And &H8000) = &H8000)
End Function
End Class