/
MainForm.cs
204 lines (186 loc) · 6.77 KB
/
MainForm.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
using CoreAudio;
namespace WindowsFormsApplication1
{
public partial class MainForm : Form
{
private static IntPtr windowHandle = IntPtr.Zero;
private static int windowPid = 0;
private static bool active = false;
private static bool? windowActivePrev = null;
private static MMDeviceEnumerator DevEnum;
private static MMDevice device;
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern int GetWindowThreadProcessId(IntPtr handle, out int processId);
public MainForm()
{
DevEnum = new MMDeviceEnumerator();
device = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia);
InitializeComponent();
Rectangle workingArea = Screen.GetWorkingArea(this);
this.Location = new Point(workingArea.Right - Size.Width,
workingArea.Bottom - Size.Height);
lblStatusBar.Text = "Ready | Stopped";
}
private void start()
{
active = true;
btnToggleActive.Text = "Stop";
lblStatusBar.Text = "Starting...";
btnToggleActive.BackColor = Color.FromArgb(43, 189, 121);
btnToggleActive.FlatAppearance.MouseOverBackColor = Color.FromArgb(32, 201, 103);
timer1.Start();
}
private void stop()
{
active = false;
btnToggleActive.Text = "Start";
btnToggleActive.BackColor = Color.FromArgb(214, 41, 49);
btnToggleActive.FlatAppearance.MouseOverBackColor = Color.FromArgb(186, 17, 25);
lblStatusBar.Text = "Ready | Stopped";
timer1.Stop();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (windowHandle != IntPtr.Zero)
{
windowP.Refresh();
if (windowP.HasExited)
{
lblStatusBar.Text = "Game Window Exited | Searching for new window.";
doWindowSearch();
}
IntPtr currentForegroundWindow = GetForegroundWindow();
if (currentForegroundWindow == windowHandle)
{
lblStatusBar.Text = "Running | Game window active.";
if (windowActivePrev == null || windowActivePrev == false)
{
windowActivePrev = true;
lblStatusBar.Text = "Turning Volume On";
setMute(false, (uint)windowPid);
this.Icon = MuteStar.Properties.Resources.Speaker;
}
}
else
{
lblStatusBar.Text = "Running | Game window inactive.";
if (windowActivePrev != null && windowActivePrev == true)
{
windowActivePrev = false;
lblStatusBar.Text = "Turning Volume Off";
setMute(true, (uint)windowPid);
this.Icon = MuteStar.Properties.Resources.Mute;
}
}
}
else
{
lblStatusBar.Text = "Searching for wildstar window...";
doWindowSearch();
}
}
private void doWindowSearch()
{
IntPtr handle;
int pid;
if (findWindow(out handle, out pid))
{
windowPid = pid;
windowHandle = handle;
windowP = Process.GetProcessById(pid);
// start();
}
}
private bool findWindow(out IntPtr handle, out int pid)
{
Process[] pList = Process.GetProcesses();
foreach (Process p in pList)
{
int buildNumber;
bool parsed = int.TryParse(p.MainWindowTitle.Substring(Math.Max(0, p.MainWindowTitle.Length - 4)), out buildNumber);
if (p.MainWindowTitle.Contains("WildStar ") && p.MainWindowTitle.Length == 13 && parsed && buildNumber > 1000 && buildNumber < 9999)
{
handle = p.MainWindowHandle;
GetWindowThreadProcessId(handle, out pid);
return true;
}
}
handle = IntPtr.Zero;
pid = 0;
return false;
}
private void setMute(bool mute, uint windowPid)
{
for (int i = 0; i < device.AudioSessionManager2.Sessions.Count; i++)
{
AudioSessionControl2 session = device.AudioSessionManager2.Sessions[i];
uint pid = session.GetProcessID;
AudioMeterInformation mi = session.AudioMeterInformation;
SimpleAudioVolume vol = session.SimpleAudioVolume;
if (pid == windowPid)
{
vol.Mute = mute;
}
}
}
private void btnToggleActive_Click(object sender, EventArgs e)
{
if (active == true)
{
stop();
}
else
{
start();
}
}
private void toolStripStatusLabel4_Click(object sender, EventArgs e)
{
}
private void MainForm_Resize(object sender, EventArgs e)
{
if (FormWindowState.Minimized == this.WindowState)
{
notify.Visible = true;
notify.ShowBalloonTip(500);
this.Hide();
}
else if (FormWindowState.Normal == this.WindowState)
{
notify.Visible = false;
}
}
private void notify_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
this.Show();
this.WindowState = FormWindowState.Normal;
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
if (System.Windows.Forms.Application.MessageLoop)
{
// WinForms app
System.Windows.Forms.Application.Exit();
}
else
{
// Console app
System.Environment.Exit(1);
}
}
public Process windowP { get; set; }
}
}