G
Gwolf2u
Guest
Hey guys
Am trying to create an app to change my default audio device on a hey combination
So ALT + Q should set to my first and ALT + W on second
Now all is ok, except the fact that if I minimize the form to tray, the key assignment is not working anymore
Below is my code
Imports System.IO
Imports System.Runtime.InteropServices
Public Class Form1
Dim DeviceID As Integer = 0
Dim ac As String = "ac.exe"
<summary>
modes defined for ac.exe
</summary>
Public Enum mode As Integer
output = 0
change = 1
End Enum
<summary>
Gets info using the "ac" executable
</summary>
<param name="mode Mode to run (output/change)</param>
<param name="DeviceID Optional: The device ID to be set as default</param>
<returns></returns>
Private Function ACLoader(ByVal mode As mode, Optional DeviceID As Integer = 0)
System.IO.File.WriteAllBytes(Application.StartupPath & "\" & ac, My.Resources.ac)
Dim p As New Process
With p.StartInfo
.UseShellExecute = False
.RedirectStandardOutput = True
.CreateNoWindow = True
.FileName = ac
If mode = Form1.mode.change Then
.Arguments = DeviceID
End If
End With
p.Start()
Return p.StandardOutput.ReadToEnd
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
RegisterHotKey(Me.Handle, 100, MOD_ALT, Keys.Q)
RegisterHotKey(Me.Handle, 200, MOD_ALT, Keys.W)
Dim regKey As Object = My.Computer.Registry.LocalMachine.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run", True).GetValue(Application.ProductName)
If regKey Is Nothing Then
GhostCheckbox1.Checked = False
Else
GhostCheckbox1.Checked = True
End If
GhostTheme1.Text = "Audio Changer v" & Application.ProductVersion.ToString & " - Gwolf2u Development"
Textbox1.Text = ACLoader(mode.output)
For Each line As String In ACLoader(mode.output).ToString.Split(vbNewLine)
ComboBox1.Items.Add(line)
Next
ComboBox1.Items.RemoveAt(ComboBox1.Items.Count - 1)
NotifyIcon1.Text = "Audio Changer v" & Application.ProductVersion.ToString & " - Gwolf2u Development"
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
File.Delete(ac)
UnregisterHotKey(Me.Handle, 100)
UnregisterHotKey(Me.Handle, 200)
NotifyIcon1.Visible = False
End Sub
Private Sub GhostButton1_Click(sender As Object, e As EventArgs) Handles GhostButton1.Click
Me.Close()
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
ACLoader(mode.change, ComboBox1.SelectedIndex)
End Sub
Public Const MOD_ALT As Integer = &H1 Alt key
Public Const WM_HOTKEY As Integer = &H312
<DllImport("User32.dll")> _
Public Shared Function RegisterHotKey(ByVal hwnd As IntPtr,ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As Integer
End Function
<DllImport("User32.dll")> _
Public Shared Function UnregisterHotKey(ByVal hwnd As IntPtr,ByVal id As Integer) As Integer
End Function
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_HOTKEY Then
Dim id As IntPtr = m.WParam
Select Case (id.ToString)
Case "100"
ComboBox1.SelectedIndex = 0
Dim devicename As String() = ComboBox1.SelectedItem.ToString.Split(":")
NotifyIcon1.BalloonTipText = "Default audio device set to:" & vbNewLine & Trim(devicename(1))
NotifyIcon1.ShowBalloonTip(500)
Case "200"
ComboBox1.SelectedIndex = 1
Dim devicename As String() = ComboBox1.SelectedItem.ToString.Split(":")
NotifyIcon1.BalloonTipText = "Default audio device set to:" & vbNewLine & Trim(devicename(1))
NotifyIcon1.ShowBalloonTip(500)
End Select
End If
MyBase.WndProc(m)
End Sub
Private Sub GhostCheckbox1_Click(sender As System.Object, e As System.EventArgs) Handles GhostCheckbox1.Click
If GhostCheckbox1.Checked = False Then
My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True).SetValue(Application.ProductName, Application.ExecutablePath)
Else
My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True).DeleteValue(Application.ProductName)
End If
End Sub
Private Sub GhostButton2_Click(sender As System.Object, e As System.EventArgs) Handles GhostButton2.Click
Me.ShowInTaskbar = False
Me.Hide()
End Sub
Private Sub NotifyIcon1_Click(sender As System.Object, e As System.EventArgs) Handles NotifyIcon1.Click
Me.ShowInTaskbar = True
Me.Show()
End Sub
End Class
Now any help on this one maybe.
As I said if I minimize it will no longer work on my key assignment
Continue reading...
Am trying to create an app to change my default audio device on a hey combination
So ALT + Q should set to my first and ALT + W on second
Now all is ok, except the fact that if I minimize the form to tray, the key assignment is not working anymore
Below is my code
Imports System.IO
Imports System.Runtime.InteropServices
Public Class Form1
Dim DeviceID As Integer = 0
Dim ac As String = "ac.exe"
<summary>
modes defined for ac.exe
</summary>
Public Enum mode As Integer
output = 0
change = 1
End Enum
<summary>
Gets info using the "ac" executable
</summary>
<param name="mode Mode to run (output/change)</param>
<param name="DeviceID Optional: The device ID to be set as default</param>
<returns></returns>
Private Function ACLoader(ByVal mode As mode, Optional DeviceID As Integer = 0)
System.IO.File.WriteAllBytes(Application.StartupPath & "\" & ac, My.Resources.ac)
Dim p As New Process
With p.StartInfo
.UseShellExecute = False
.RedirectStandardOutput = True
.CreateNoWindow = True
.FileName = ac
If mode = Form1.mode.change Then
.Arguments = DeviceID
End If
End With
p.Start()
Return p.StandardOutput.ReadToEnd
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
RegisterHotKey(Me.Handle, 100, MOD_ALT, Keys.Q)
RegisterHotKey(Me.Handle, 200, MOD_ALT, Keys.W)
Dim regKey As Object = My.Computer.Registry.LocalMachine.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run", True).GetValue(Application.ProductName)
If regKey Is Nothing Then
GhostCheckbox1.Checked = False
Else
GhostCheckbox1.Checked = True
End If
GhostTheme1.Text = "Audio Changer v" & Application.ProductVersion.ToString & " - Gwolf2u Development"
Textbox1.Text = ACLoader(mode.output)
For Each line As String In ACLoader(mode.output).ToString.Split(vbNewLine)
ComboBox1.Items.Add(line)
Next
ComboBox1.Items.RemoveAt(ComboBox1.Items.Count - 1)
NotifyIcon1.Text = "Audio Changer v" & Application.ProductVersion.ToString & " - Gwolf2u Development"
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
File.Delete(ac)
UnregisterHotKey(Me.Handle, 100)
UnregisterHotKey(Me.Handle, 200)
NotifyIcon1.Visible = False
End Sub
Private Sub GhostButton1_Click(sender As Object, e As EventArgs) Handles GhostButton1.Click
Me.Close()
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
ACLoader(mode.change, ComboBox1.SelectedIndex)
End Sub
Public Const MOD_ALT As Integer = &H1 Alt key
Public Const WM_HOTKEY As Integer = &H312
<DllImport("User32.dll")> _
Public Shared Function RegisterHotKey(ByVal hwnd As IntPtr,ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As Integer
End Function
<DllImport("User32.dll")> _
Public Shared Function UnregisterHotKey(ByVal hwnd As IntPtr,ByVal id As Integer) As Integer
End Function
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_HOTKEY Then
Dim id As IntPtr = m.WParam
Select Case (id.ToString)
Case "100"
ComboBox1.SelectedIndex = 0
Dim devicename As String() = ComboBox1.SelectedItem.ToString.Split(":")
NotifyIcon1.BalloonTipText = "Default audio device set to:" & vbNewLine & Trim(devicename(1))
NotifyIcon1.ShowBalloonTip(500)
Case "200"
ComboBox1.SelectedIndex = 1
Dim devicename As String() = ComboBox1.SelectedItem.ToString.Split(":")
NotifyIcon1.BalloonTipText = "Default audio device set to:" & vbNewLine & Trim(devicename(1))
NotifyIcon1.ShowBalloonTip(500)
End Select
End If
MyBase.WndProc(m)
End Sub
Private Sub GhostCheckbox1_Click(sender As System.Object, e As System.EventArgs) Handles GhostCheckbox1.Click
If GhostCheckbox1.Checked = False Then
My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True).SetValue(Application.ProductName, Application.ExecutablePath)
Else
My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True).DeleteValue(Application.ProductName)
End If
End Sub
Private Sub GhostButton2_Click(sender As System.Object, e As System.EventArgs) Handles GhostButton2.Click
Me.ShowInTaskbar = False
Me.Hide()
End Sub
Private Sub NotifyIcon1_Click(sender As System.Object, e As System.EventArgs) Handles NotifyIcon1.Click
Me.ShowInTaskbar = True
Me.Show()
End Sub
End Class
Now any help on this one maybe.
As I said if I minimize it will no longer work on my key assignment
Continue reading...