-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
164 lines (146 loc) · 7.24 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using NAudio.CoreAudioApi;
using SndVolPlus.Properties;
namespace SndVolPlus
{
static class Program
{
static NotifyIcon TrayIcon;
static MMDevice DefaultMediaDevice;
static string SystemDir = Environment.GetFolderPath(Environment.SpecialFolder.System) + @"\";
static Timer SingleClickWindow;
[STAThread]
private static void Main()
{
DefaultMediaDevice = new MMDeviceEnumerator().GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
DefaultMediaDevice.AudioEndpointVolume.OnVolumeNotification += new AudioEndpointVolumeNotificationDelegate(AudioEndpointVolume_OnVolumeNotification);
TrayIcon = new NotifyIcon();
TrayIcon.Icon = IconFromVolume();
TrayIcon.Text = ToolTipFromVolume();
TrayIcon.MouseClick += new MouseEventHandler(TrayIcon_MouseClick);
TrayIcon.MouseDoubleClick += new MouseEventHandler(TrayIcon_MouseDoubleClick);
TrayIcon.Visible = true;
TrayIcon.ContextMenu = new ContextMenu();
TrayIcon.ContextMenu.MenuItems.Add(new MenuItem("Open Volume Mixer", (o, e) => { Process.Start(SystemDir + "sndvol.exe"); }));
TrayIcon.ContextMenu.MenuItems.Add(new MenuItem("-"));
TrayIcon.ContextMenu.MenuItems.Add(new MenuItem("Playback devices", (o, e) => { Process.Start(SystemDir + "rundll32.exe", @"Shell32.dll,Control_RunDLL mmsys.cpl,,playback"); }));
TrayIcon.ContextMenu.MenuItems.Add(new MenuItem("Recording devices", (o, e) => { Process.Start(SystemDir + "rundll32.exe", @"Shell32.dll,Control_RunDLL mmsys.cpl,,recording"); }));
TrayIcon.ContextMenu.MenuItems.Add(new MenuItem("Sounds", (o, e) => { Process.Start(SystemDir + "rundll32.exe", @"Shell32.dll,Control_RunDLL mmsys.cpl,,sounds"); }));
TrayIcon.ContextMenu.MenuItems.Add(new MenuItem("-"));
TrayIcon.ContextMenu.MenuItems.Add(new MenuItem("Volume control options", (o, e) => { Process.Start(SystemDir + "sndvol.exe", "-p"); }));
SingleClickWindow = new Timer();
SingleClickWindow.Interval = SystemInformation.DoubleClickTime;
SingleClickWindow.Tick += (o, e) =>
{
SingleClickWindow.Stop();
StartVolControl();
};
Application.Run();
}
private static string ToolTipFromVolume()
{
if (DefaultMediaDevice.AudioEndpointVolume.Mute)
return "Speakers: Muted";
else
return string.Format("Speakers: {0:F0}%", DefaultMediaDevice.AudioEndpointVolume.MasterVolumeLevelScalar * 100);
}
private static Icon IconFromVolume()
{
if (DefaultMediaDevice.AudioEndpointVolume.Mute)
return Resources.mute;
else
{
if (DefaultMediaDevice.AudioEndpointVolume.MasterVolumeLevelScalar > 0.65)
return Resources.vol68;
else if (DefaultMediaDevice.AudioEndpointVolume.MasterVolumeLevelScalar > 0.32)
return Resources.vol56;
else if (DefaultMediaDevice.AudioEndpointVolume.MasterVolumeLevelScalar > 0)
return Resources.vol44;
else
return Resources.vol32;
}
}
private static void AudioEndpointVolume_OnVolumeNotification(AudioVolumeNotificationData data)
{
TrayIcon.Text = ToolTipFromVolume();
TrayIcon.Icon = IconFromVolume();
}
private static void TrayIcon_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
//We've recieved a second click, so disable the timer to prevent the single click event from occurring
//SingleClickWindow.Stop();
StartVolMixer();
}
}
private static void TrayIcon_MouseClick(object sender, MouseEventArgs e)
{
switch (e.Button)
{
case MouseButtons.Left:
{
if (SingleClickWindow.Enabled)
SingleClickWindow.Stop();
else
SingleClickWindow.Start();
} break;
case MouseButtons.Middle:
{
DefaultMediaDevice.AudioEndpointVolume.Mute = !DefaultMediaDevice.AudioEndpointVolume.Mute;
} break;
}
}
private static void StartVolControl()
{
Process SndVol = Process.Start(SystemDir + @"sndvol.exe", "-f");
MoveSndVolWindow(SndVol.Id);
}
private static void StartVolMixer()
{
Process SndVol = Process.Start(SystemDir + "sndvol.exe", "-r");
MoveSndVolWindow(SndVol.Id);
}
private static void MoveSndVolWindow(int SndVolId)
{
//Wait a little bit for the window to be created, with a max wait time of 100 * 10ms, or 1 second
for (int i = 0; i < 100; i++)
{
System.Threading.Thread.Sleep(10);
foreach (Process p in Process.GetProcessesByName("sndvol"))
{
if (p.Id == SndVolId && p.MainWindowHandle != IntPtr.Zero)
{
Unmanaged.RECT SndVolRect = new Unmanaged.RECT();
Unmanaged.GetWindowRect(p.MainWindowHandle, out SndVolRect);
int margin = 10;
int x = 0, y = 0;
switch (Helper.GetTaskbarEdge())
{
case DockStyle.Right:
case DockStyle.Bottom:
{
x = Screen.PrimaryScreen.WorkingArea.Right - (SndVolRect.right - SndVolRect.left) - margin;
y = Screen.PrimaryScreen.WorkingArea.Bottom - (SndVolRect.bottom - SndVolRect.top) - margin;
} break;
case DockStyle.Left:
{
x = Screen.PrimaryScreen.WorkingArea.Left + margin;
y = Screen.PrimaryScreen.WorkingArea.Bottom - (SndVolRect.bottom - SndVolRect.top) - margin;
} break;
case DockStyle.Top:
{
x = Screen.PrimaryScreen.WorkingArea.Right - (SndVolRect.right - SndVolRect.left) - margin;
y = Screen.PrimaryScreen.WorkingArea.Top + margin;
} break;
}
Unmanaged.SetWindowPos(p.MainWindowHandle.ToInt32(), 0, x, y, 0, 0, 0x4000 | 0x0001);
}
}
}
}
}
}