// Created by David W.M. Sattler 2014
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using System.Drawing;
using System.Runtime.InteropServices;
namespace MDC.TaskBar_Misc
{
public class ProgramWatcher
{
[DllImport("user32.dll")]
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);
static BackgroundWorker bg = new BackgroundWorker();
public static List<string> openProcesses = new List<string>();
static List<int> openId = new List<int>();
public static bool isRunning(string progName, bool isId, int id)
{
foreach (Process pro in Process.GetProcesses("."))
{
if (pro.MainWindowTitle == "")
continue;
if (isId)
{
if (pro.Id == id)
return true;
}
else
{
if (pro.ProcessName == progName)
return true;
}
}
foreach (Process pro in Process.GetProcessesByName(progName))
if (pro.MainWindowHandle != (IntPtr)0)
return true;
return false;
}
public static void StartProgram(string path)
{
foreach (Process pro in Process.GetProcesses("."))
{
if (pro.MainWindowTitle != "")
{
if (pro.ProcessName == Path.GetFileNameWithoutExtension(path))
{
SetWindowPos(pro.MainWindowHandle, (IntPtr)0, 0, 0, 0, 0, (short)0x2 | (short)1 | (int)0x0040);
return;
}
}
}
Process p = Process.Start(path);
try
{
if (p.ProcessName != null)
if (!openProcesses.Contains(p.ProcessName))
{
openProcesses.Add(p.ProcessName);
openId.Add(p.Id);
}
}
catch { }
}
public static void InitProgramWatch()
{
bg.DoWork += new DoWorkEventHandler(bg_DoWork);
bg.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bg_RunWorkerCompleted);
bg.RunWorkerAsync();
}
private static void bg_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 0; i < 5; i++)
{
System.Threading.Thread.Sleep(100);
}
if (Application.OpenForms["MDC_TaskBar"] == null)
return;
foreach (Panel p in Application.OpenForms["MDC_TaskBar"].Controls.OfType<Panel>())
{
foreach (Panel w in p.Controls.OfType<Panel>())
{
Application.OpenForms["MDC_TaskBar"].Invoke(new MethodInvoker(() =>
{
try
{
openApps.dynamicTT.SetToolTip(w, Process.GetProcessById(int.Parse(w.Name)).MainWindowTitle);
}
catch { }
}));
}
}
List
<string
> remove = new List
<string
>(); List<int> imove = new List<int>();
bool showMMP = false;
foreach (Process proc in Process.GetProcesses("."))
if (proc.MainWindowTitle != "" && !excluded.Contains(proc.ProcessName))
{
if (proc.MainWindowTitle.Contains("Metro Media Player"))
showMMP = true;
if (!openId.Contains(proc.Id))
{
openApps.AddApp(proc);
openProcesses.Add(proc.ProcessName);
openId.Add(proc.Id);
}
else
openApps.AddWindow(proc);
}
MMP_Controller.ShowHide(showMMP);
for (int i = 0; i < openProcesses.Count; i++)
{
if (!isRunning(openProcesses[i], false, -1))
{
foreach (Panel p in Application.OpenForms["MDC_TaskBar"].Controls.OfType<Panel>())
{
if (p.Name == "MMP_Next" || p.Name == "MMP_Previos" || p.Name == "MMP_PausePlay")
continue;
if (p.Name != openProcesses[i])
continue;
else
{
Application.OpenForms["MDC_TaskBar"].Invoke(new MethodInvoker(() => { p.Dispose(); }));
imove.Add(openId[i]);
openApps.Reposition();
}
}
}
else if (!isRunning(openProcesses[i], true, openId[i]))
{
openApps.RemoveWindow(openProcesses[i]);
imove.Add(openId[i]);
}
}
openProcesses.Remove(p);
foreach (int i in imove)
openId.Remove(i);
}
static string[] excluded = new string[] { "explorer" };
private static void bg_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
bg.RunWorkerAsync();
}
}
}