Adjusting Windows Speaker Volume VB.NET - windows

How do I change the Windows Speaker Volume (The Main Output Volume Control Volume Value) via VB.NET? I want a way to like indirectly change the whole system's volume like we do it from the Volume Control application on Windows 7

From:
http://social.msdn.microsoft.com/Forums/vstudio/en-US/8308f020-b9e6-472c-aaac-93619a8a5a7d/vbnet-control-the-system-volume-mute-and-output-the-current-level-to-the-user?forum=vbgeneral
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 was able to throw this together in a minute no problems.

Related

Vb.net animation sonic the hedgehog

Why does my walk not work but the run works properly sonic the hedgehog animation.
Public Class Form1
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
PictureBox1.Image = My.Resources.SONICRUN1_removebg_preview
Timer1.Stop()
Timer2.Start()
End Sub
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
PictureBox1.Image = My.Resources.SONICRUN2_removebg_preview
Timer2.Stop()
Timer1.Start()
End Sub
Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
PictureBox1.Image = My.Resources.SONICRUN3_removebg_preview
Timer3.Stop()
Timer2.Start()
End Sub
Private Sub Timer4_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer4.Tick
PictureBox1.Image = My.Resources.SONICRUN4_removebg_preview
Timer4.Stop()
Timer3.Start()
End Sub
Private Sub btnRUN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRUN.Click
Timer1.Enabled = True
Timer5.Enabled = False
End Sub
Private Sub btnWALK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWALK.Click
Timer5.Enabled = True
Timer1.Enabled = False
End Sub
Private Sub Timer5_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer5.Tick
PictureBox1.Image = My.Resources.SONICWALK1_removebg_preview
Timer5.Stop()
Timer4.Start()
End Sub
Private Sub Timer6_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer6.Tick
PictureBox1.Image = My.Resources.SONICWALK2_removebg_preview
Timer6.Stop()
Timer5.Start()
End Sub
Private Sub Timer7_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer7.Tick
PictureBox1.Image = My.Resources.SONICWALK3_removebg_preview
Timer7.Stop()
Timer6.Start()
End Sub
Private Sub Timer8_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer8.Tick
PictureBox1.Image = My.Resources.SONICWALK4_removebg_preview
Timer8.Stop()
Timer7.Start()
End Sub
Private Sub Timer9_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer9.Tick
PictureBox1.Image = My.Resources.SONICWALK5_removebg_preview
Timer9.Stop()
Timer8.Start()
End Sub
Private Sub Timer10_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer10.Tick
PictureBox1.Image = My.Resources.SONICWALK6_removebg_preview
Timer10.Stop()
Timer9.Start()
End Sub
Private Sub Timer11_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer11.Tick
PictureBox1.Image = My.Resources.SONICWALK7_removebg_preview
Timer11.Stop()
Timer10.Start()
End Sub
Private Sub Timer12_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer12.Tick
PictureBox1.Image = My.Resources.SONICWALK8_removebg_preview
Timer12.Stop()
Timer11.Start()
End Sub
End Class
I tried doing it again erased it and it still didn't walk he was still running.
When I press the run it works ok. But when I press the walk it won't do the walk animation! Also when I start the program it starts running already. I want it to not run when the program is started I want it to run when I press the buttons.
The Run command starts Timer1. When Timer1 is done it starts Timer2. When Timer2 is done it goes back to Timer1. So run is just toggling back and forth between Timer1 and Timer2.
The Walk command starts Timer5, which starts Timer4 --> Timer3 --> Timer2 --> Timer1...and then gets stuck in the Run cycle toggling between Timer1 and Timer2.
What should the Walk sequence be?
If you don't want anything to be animating, then select each Timer and set the Enabled property to FALSE. Make sure you aren't turning any Timers on in the Load() event of your Form.
Try something like this out:
Public Class Form1
Private runSequence() As Image
Private walkSequence() As Image
Private runIndex As Integer = -1
Private walkIndex As Integer = -1
Private WithEvents trmRun As New System.Windows.Forms.Timer
Private WithEvents trmWalk As New System.Windows.Forms.Timer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
runSequence = {My.Resources.SONICRUN1_removebg_preview, My.Resources.SONICRUN2_removebg_preview,
My.Resources.SONICRUN3_removebg_preview, My.Resources.SONICRUN4_removebg_preview}
walkSequence = {My.Resources.SONICWALK1_removebg_preview, My.Resources.SONICWALK2_removebg_preview,
My.Resources.SONICWALK3_removebg_preview, My.Resources.SONICWALK4_removebg_preview,
My.Resources.SONICWALK5_removebg_preview, My.Resources.SONICWALK6_removebg_preview,
My.Resources.SONICWALK7_removebg_preview, My.Resources.SONICWALK8_removebg_preview}
trmRun.Interval = 100
trmRun.Enabled = False
trmWalk.Interval = 100
trmWalk.Enabled = False
End Sub
Private Sub btnRUN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRun.Click
trmWalk.Stop()
trmRun.Start()
End Sub
Private Sub btnWALK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWalk.Click
trmRun.Stop()
trmWalk.Start()
End Sub
Private Sub trmRun_Tick(sender As Object, e As EventArgs) Handles trmRun.Tick
runIndex = runIndex + 1
If runIndex = runSequence.Length Then
runIndex = 0
End If
PictureBox1.Image = runSequence(runIndex)
End Sub
Private Sub trmWalk_Tick(sender As Object, e As EventArgs) Handles trmWalk.Tick
walkIndex = walkIndex + 1
If walkIndex = walkSequence.Length Then
walkIndex = 0
End If
PictureBox1.Image = walkSequence(walkIndex)
End Sub
End Class

Are some modifiers not supported by SendKeys.SendWait?

I have a code that works perfectly, copying selected text from any windows application pressing CTRL+B.
However, when I change the modifiers that trigger the SendKeys.SendWait("^c") from MOD_CONTROL to MOD_ALT + MOD_SHIFT, the command SendKeys doesn't copy the text from the active window.
So pressing ALT+SHIFT+B still launch the command SendKeys, but the command doesn't copy the selected text to the clipboard.
Is there some incopatibility between the sendkeys command and the modifiers other then MOD_CONTROL?
I have tried registering different hotkeys, and different modifiers for each hotkey: sendkeys works with every hotkey with MOD_CONTROL modifier but doesn't with every hotkey with any other modifiers combination.
Public Class Form1
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
UnregisterHotKey(Nothing, HotKeyID)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
RegisterHotKey(Me.Handle, HotKeyID, MOD_CONTROL, VK_b)
End Sub
Private Declare Function GetForegroundWindow Lib "User32" () As IntPtr
Private Declare Function RegisterHotKey Lib "User32.dll" (ByVal hWnd As IntPtr, ByVal ID As Integer, ByVal Modifiers As UInteger, ByVal VK As UInteger) As Boolean
Private Declare Function UnregisterHotKey Lib "User32.dll" (ByVal hWnd As IntPtr, ByVal ID As Integer) As Boolean
Private HotKeyID As Integer = 1
Private Const WM_HOTKEY As Integer = &H312
Private Const MOD_ALT As UInteger = 1
Private Const MOD_CONTROL As UInteger = 2
Private Const MOD_SHIFT As UInteger = 4
Private Const VK_b As UInteger = &H42
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_HOTKEY Then
Dim Modif As UInteger = (CType(m.LParam, Integer) And &HFFFF)
Dim Key As UInteger = CType(m.LParam, Integer) >> 16
If Key = VK_b And Modif = MOD_CONTROL Then
'Check foregroundwindow has a valid handle
If GetForegroundWindow <> IntPtr.Zero Then
'Send the Ctrl+C command to the active window
SendKeys.SendWait("^c")
End If
End If
Else
MyBase.WndProc(m)
End If
End Sub
End Class
If I change
If Key = VK_b And Modif = MOD_CONTROL Then
'Check foregroundwindow has a valid handle
If GetForegroundWindow <> IntPtr.Zero Then
'Send the Ctrl+C command to the active window
SendKeys.SendWait("^c")
End If
End If
With:
If Key = VK_b And Modif = MOD_ALT + MOD_SHIFT Then
'Check foregroundwindow has a valid handle
If GetForegroundWindow <> IntPtr.Zero Then
'Send the Ctrl+C command to the active window
SendKeys.SendWait("^c")
End If
End If
The command SendKeys.SendWait doesn'nt copy the selected text: can someone give me a clue?
Thank you
At last I managed it to work adding the line:
System.Threading.Thread.Sleep(300)
just before the command SendKeys.SendWait("^c")
It seems that SendKeys have issue with timing and the pause inserted resolve the issue.
So, now this code works:
Public Class Form1
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
UnregisterHotKey(Nothing, HotKeyID)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
RegisterHotKey(Me.Handle, HotKeyID, MOD_ALT + MOD_SHIFT, VK_b)
End Sub
Private Declare Function GetForegroundWindow Lib "User32" () As IntPtr
Private Declare Function RegisterHotKey Lib "User32.dll" (ByVal hWnd As IntPtr, ByVal ID As Integer, ByVal Modifiers As UInteger, ByVal VK As UInteger) As Boolean
Private Declare Function UnregisterHotKey Lib "User32.dll" (ByVal hWnd As IntPtr, ByVal ID As Integer) As Boolean
Private Const WM_HOTKEY As Integer = &H312
Private Const MOD_ALT As UInteger = 1
Private Const MOD_CONTROL As UInteger = 2
Private Const MOD_SHIFT As UInteger = 4
Private Const VK_b As UInteger = 66
Private HotKeyID As Integer = 1
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_HOTKEY Then
Dim Modif As UInteger = (CType(m.LParam, Integer) And &HFFFF)
Dim Key As UInteger = CType(m.LParam, Integer) >> 16
If Key = VK_b And Modif = MOD_ALT + MOD_SHIFT Then
'Check foregroundwindow has a valid handle
If GetForegroundWindow <> IntPtr.Zero Then
'Send the Ctrl+C command to the active window
System.Threading.Thread.Sleep(300)
SendKeys.SendWait("^c")
End If
End If
Else
MyBase.WndProc(m)
End If
End Sub
End Class

Visual Basic 2010 Express - How do you switch back and forth between two images that are on top of each other?

I really need some help with this dilemma.
I have two pictures of a light bulb. In one picture the light bulb is shining brightly and in the other it’s turned off. I’m supposed to overlap these pics and make it turn on and off by clicking on the image but I just can’t figure out the code for it. How do you toggle between these images? I am not allowed to use a button to do this. I've got to click on the pic to change it. Please help! Link below as I do not have enough rep to post actual images yet.
http://i1293.photobucket.com/albums/b598/BentoBoy1/ScreenHunter_02Sep202252_zps75800aea.png
Public Class Form1
Private Sub ExitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitButton.Click
'Close the program
Me.Close()
End Sub
Private Sub PrintButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintButton.Click
'Print the form in the print preview window
PrintForm1.PrintAction = Printing.PrintAction.PrintToPreview
PrintForm1.Print()
End Sub
Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
'Turn the light bulb on.
MessageLabel.Text = "Turn on the light"
End Sub
Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MessageLabel.Click
'Display different messages when the light bulbs are clicked.
End Sub
Private Sub RedRadioButton_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RedRadioButton.CheckedChanged
'Set the MessageLabel text to Red.
MessageLabel.ForeColor = Color.Red
End Sub
Private Sub BlackRadioButton_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BlackRadioButton.CheckedChanged
'Set the MessageLabel text to Black.
MessageLabel.ForeColor = Color.Black
End Sub
Private Sub BlueRadioButton_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BlueRadioButton.CheckedChanged
'Set the MessageLabel text to Blue.
MessageLabel.ForeColor = Color.Blue
End Sub
Private Sub GreenRadioButton_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GreenRadioButton.CheckedChanged
'Set the MessageLabel text to Green.
MessageLabel.ForeColor = Color.Green
End Sub
Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProgrammedByLabel.Click
'Programmed by me.
End Sub
Private Sub ColorsGroupBox_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ColorsGroupBox.Enter
'Group of different colors.
End Sub
Private Sub NameTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NameTextBox.TextChanged
'Name field.
End Sub
Private Sub PictureBox1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LightOnPictureBox.Click
'Light bulb is on.
LightOnPictureBox.Image = My.Resources.lighton
MessageLabel.Text = "Thanks for turning me on, " & NameTextBox.Text
End Sub
Private Sub PictureBox2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LightOffPictureBox.Click
'Light bulb is off.
LightOffPictureBox.Image = My.Resources.lightoff
MessageLabel.Text = "Thanks for turning me off, " & NameTextBox.Text
End Sub
Private Sub NameLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NameLabel.Click
'Name label.
End Sub
End Class
Firstly, i think that you should take a look here in order to understand the way images comparisson takes place. Secondly, the proper event to change the picture is the PictureBox.Click...
The code should be like the following one:
Public Class Form1
Dim imageBulbOff As Image = My.Resources.BulbOff
Dim imageBulbOn As Image = My.Resources.BulbOn
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
PictureBox1.Image = imageBulbOff
End Sub
Private Sub PictureBox1_Click(sender As System.Object, e As System.EventArgs) Handles PictureBox1.Click
If PictureBox1.Image Is imageBulbOff Then
PictureBox1.Image = imageBulbOn
Else
PictureBox1.Image = imageBulbOff
End If
End Sub
End Class

MoveWindow API in windows 8

I need to open an external window from an external exe (eg Notepad) and move & size it to a predefined size & position.
I am trying to use MoveWindow API but is seems it is not working. I am using Windows 8 x64 and VS2012.
Here is my code:
<DllImport("user32.dll")> _
Public Function MoveWindow(ByVal hWnd As IntPtr, ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal bRepaint As Boolean) As Boolean
End Function
Public Sub NoveNotepad()
Dim ApplicationProcess = System.Diagnostics.Process.Start("Notepad.exe")
ApplicationProcess.WaitForInputIdle()
Dim ApplicationHandle = ApplicationProcess.MainWindowHandle
Dim z = MoveWindow(ApplicationHandle, 600, 600, 600, 600, True) ' THIS RETURNS TRUE
End Sub
You could try using SetWindowPos() instead? Not sure why MoveWindow() wouldn't work...
Private Declare Function SetWindowPos Lib "user32" Alias "SetWindowPos" _
(ByVal hwnd As IntPtr, ByVal hWndInsertAfter As Integer, _
ByVal x As Integer, ByVal y As Integer, ByVal cx As Integer, ByVal cy As Integer, _
ByVal wFlags As Integer) As Integer
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim ApplicationProcess = System.Diagnostics.Process.Start("Notepad.exe")
ApplicationProcess.WaitForInputIdle()
Dim ApplicationHandle = ApplicationProcess.MainWindowHandle
SetWindowPos(ApplicationHandle, 0, 600, 600, 600, 600, 0)
End Sub

API SendMessage() does not work for external app dialog

I created a form that has 2 buttons. One button pops a msgbox and the other runs form frmAPI (code is listed below) listed below. If I open the msgbox and leave it open and then run the frmAPI it will list the msgbox and its text and then close it. THis is what I expect it to do. If I open another application and generate a msgbox in that app with my frmAPI still running it will in fact list the other apps msgbox and the text but it does not close the msgbox from the other app. If Irun the frmAPI from the other app and do the same test the results are reversed. So in short it will only close the dialog from within the same app.
I would like to be able to close a dialog from any app based on it being a dialog and having text that will match my criteria. Any help on what I am doing wrong?
Thanks
Imports System.Runtime.InteropServices
Imports System.Text
Partial Public Class TestMSgBoxStuff
Inherits Form
Public Sub New()
InitializeComponent()
End Sub
<DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function FindWindow(lpClassName As String, lpWindowName As String) As IntPtr
End Function
<DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function FindWindowEx(hwndParent As IntPtr, hwndChildAfter As IntPtr, lpszClass As String, lpszWindow As String) As IntPtr
End Function
<DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
Private Shared Function GetWindowText(hWnd As IntPtr, lpString As StringBuilder, nMaxCount As Integer) As Integer
End Function
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function GetWindowTextLength(hWnd As IntPtr) As Integer
End Function
<DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function SendMessage(hWnd As HandleRef, Msg As UInteger, wParam As IntPtr, lParam As IntPtr) As IntPtr
End Function
<DllImport("user32", CharSet:=Runtime.InteropServices.CharSet.Auto, SetLastError:=True, ExactSpelling:=True)>
Private Shared Function SetForegroundWindow(ByVal hwnd As IntPtr) As IntPtr
End Function
Private Const WM_IME_NOTIFY As Integer = &H282
Private Const WM_DESTROY As Integer = &H2
Private Const WM_NCDESTROY As Integer = &H82
Private Const WM_CLOSE As Integer = &H10
Private Const IMN_CLOSESTATUSWINDOW As Integer = &H1
Private Const WM_KILLFOCUS As Integer = &H8
Private Const WM_COMMAND As Integer = &H11
Private Sub TestMSgBoxStuff_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim timer As New Timer()
Timer1.Interval = 10000
'detect the MessageBox every seconds
'Timer1.Tick += New EventHandler(Timer1_Tick)
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
'Get the MessageBox handle
Dim handle As IntPtr = FindWindow("#32770", Nothing)
Me.RichTextBox1.AppendText("Handle: " + handle.ToString() + vbLf)
'Get the Text window handle
Dim txtHandle As IntPtr = FindWindowEx(handle, IntPtr.Zero, "Static", Nothing)
Me.RichTextBox1.AppendText(vbTab & "text handle: " + txtHandle.ToString() + vbLf)
Dim len As Integer = GetWindowTextLength(txtHandle)
Dim sb As New StringBuilder()
'Get the text
GetWindowText(txtHandle, sb, len + 1)
Me.RichTextBox1.AppendText(vbTab & "text: " + sb.ToString() + vbLf & vbLf)
Me.RichTextBox1.ScrollToCaret()
SetForegroundWindow(handle)
'close the messagebox WM_CLOSE
Dim lResults As Integer = SendMessage(New HandleRef(Nothing, handle), WM_NCDESTROY, IntPtr.Zero, IntPtr.Zero)
End Sub
End Class
You may be running into User Interface Privilege Isolation. That'll block your messages from going to a higher privilege process. See also ChangeWindowsMessageFilter()
I'd suggest trying to send it a WM_COMMAND instead of a WM_CLOSE; WM_COMMAND is generally treated more gently by the system and may get through. Use BN_CLICKED as the high word of WPARAM and IDOK as the low word (assuming it has an OK button), and the handle to the OK button in LPARAM.. Other button messages are here.

Resources