Skip to content

Commit

Permalink
Merge pull request #98 from psouza4/master
Browse files Browse the repository at this point in the history
Version 7.7
  • Loading branch information
Andrew Sampson committed Jan 11, 2015
2 parents 4b2fe45 + 5b9f3ac commit 15493d4
Show file tree
Hide file tree
Showing 10 changed files with 580 additions and 211 deletions.
25 changes: 19 additions & 6 deletions Common/ProcessDetails.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class ProcessDetails
public IntPtr _WindowHandle = IntPtr.Zero;
public bool Manageable = false;
public bool MadeBorderless = false;
private bool NoAccess = false;
public int MadeBorderlessAttempts = 0;
public WindowsAPI.WindowStyleFlags OriginalStyleFlags_Standard = 0;
public WindowsAPI.WindowStyleFlags OriginalStyleFlags_Extended = 0;
Expand All @@ -31,7 +32,7 @@ public class ProcessDetails
//{
// this.Proc = p;

// this._WindowHandle = this.Proc.MainWindowHandle;
// this._WindowHandle = WindowsAPI.Native.GetMainWindowForProcess(this.Proc);
// this.WindowTitle = WindowsAPI.Native.GetWindowTitle(this.WindowHandle);
// //this.WindowClass = WindowsAPI.Native.GetWindowClassName(this.WindowHandle); // note: this isn't used, currently
//}
Expand All @@ -56,10 +57,7 @@ public IntPtr WindowHandle
return IntPtr.Zero;

if (!WindowsAPI.Native.IsWindow(this._WindowHandle))
{
this.Proc.Refresh();
this._WindowHandle = this.Proc.MainWindowHandle;
}
this._WindowHandle = WindowsAPI.Native.GetMainWindowForProcess(this.Proc);
}
catch { }

Expand All @@ -77,9 +75,18 @@ public bool ProcessHasExited
{
try
{
if (this.NoAccess)
return false;

if (this.Proc != null)
return this.Proc.HasExited;
}
catch (System.ComponentModel.Win32Exception)
{
this.NoAccess = true;

return false; // Access is denied
}
catch { }

return true;
Expand All @@ -90,7 +97,13 @@ public string BinaryName
{
get
{
return this.Proc.ProcessName;
try
{
return this.Proc.ProcessName;
}
catch { }

return "<error>";
}
}

Expand Down
28 changes: 15 additions & 13 deletions Forms/MainWindow.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

111 changes: 98 additions & 13 deletions Forms/MainWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,51 @@ public MainWindow()
this.InitializeComponent();
}

#region External access and processing

// Code commented (but not removed) by psouza4 2015/01/10: there were no references to this method, so no need to compile it and bloat the software.
//public static MainWindow ext()
//{
// foreach (Form form in Application.OpenForms)
// {
// if (form.GetType() == typeof(MainWindow))
// return (MainWindow)form;
// }

// return null;
//}

private static object _DoEvents_locker = new object();
private static bool _DoEvents_engaged = false;
public static void DoEvents()
{
try
{
bool local__DoEventsEngaged = false;
lock (MainWindow._DoEvents_locker)
{
local__DoEventsEngaged = MainWindow._DoEvents_engaged;

if (!local__DoEventsEngaged)
MainWindow._DoEvents_engaged = true;
}

if (!local__DoEventsEngaged)
{
// hack-y, but it lets the window message pump process user inputs to keep the UI alive on the main thread
Application.DoEvents();
}

lock (MainWindow._DoEvents_locker)
{
MainWindow._DoEvents_engaged = false;
}
}
catch { }
}

#endregion

#region Process enumeration and handling

/// <summary>
Expand Down Expand Up @@ -118,23 +163,27 @@ private void UpdateProcessList(bool bReset = false)
{
ProcessDetails pd = (ProcessDetails)this.lstProcesses.Items[i];

if ((pd.ProcessHasExited) || (!pd.Manageable) || (!processes.Any(p => p.Id == pd.Proc.Id)) || HiddenProcesses.IsHidden(pd) || (pd.WindowTitle != Native.GetWindowTitle(pd.WindowHandle)))
if (pd.ProcessHasExited || (pd.WindowTitle != Native.GetWindowTitle(pd.WindowHandle)))
{
this.HandlePrunedProcess(pd);

this.lstProcesses.Items.RemoveAt(i);

if (ProcessDetails.List.Contains(pd))
ProcessDetails.List.Remove(pd);
lock (ProcessDetails.List)
{
if (ProcessDetails.List.Contains(pd))
ProcessDetails.List.Remove(pd);
}
}
}

// Let the user know that we're still alive and well
this.lblUpdateStatus.Text = "Last updated " + DateTime.Now.ToString();

// add new processes
foreach (Process process in processes)
{
// Skip the system idle process
if (process.Id == 0)
continue;

// No longer using a sexy linq query, but a case-insensitive text comparison is easier to manage when blacklisting processes.
if (HiddenProcesses.IsHidden(process))
continue;
Expand All @@ -149,21 +198,27 @@ private void UpdateProcessList(bool bReset = false)
{
// moved in here -- if the process list hasn't changed, then the handle isn't even necessary
// this will further optimize the loop since 'MainWindowHandle' is expensive
IntPtr pMainWindowHandle = process.MainWindowHandle;
IntPtr pMainWindowHandle = Native.GetMainWindowForProcess(process);

// If the application doesn't have a primary window handle, we don't display it
if (pMainWindowHandle != IntPtr.Zero)
{
ProcessDetails curProcess = new ProcessDetails(process, pMainWindowHandle) { Manageable = true };

this.lstProcesses.Items.Add(curProcess);
ProcessDetails.List.Add(curProcess);
lock (ProcessDetails.List)
{
ProcessDetails.List.Add(curProcess);
}

// getting MainWindowHandle is expensive -> pause a bit to spread the load
Thread.Sleep(10);
}
}
}

// Let the user know that we're still alive and well
this.lblUpdateStatus.Text = "Last updated " + DateTime.Now.ToString();
}

#endregion
Expand All @@ -184,6 +239,10 @@ private void tmrWork_Tick(object sender, EventArgs e)
/// </summary>
private delegate void Delegate__Void_Bool(bool Bool1);


private delegate void Delegate__Void_ProcessDetails(ProcessDetails pd1);


/// <summary>
/// Update the processlist and process the favorites
/// </summary>
Expand All @@ -197,14 +256,17 @@ private void wrkBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
if (!this.ProcessingIsPaused)
{
foreach (ProcessDetails pd in ProcessDetails.List)
lock (ProcessDetails.List)
{
foreach (Favorites.Favorite fav_process in Favorites.List)
foreach (ProcessDetails pd in ProcessDetails.List)
{
if (((fav_process.Kind == Favorites.Favorite.FavoriteKinds.ByBinaryName) && (pd.BinaryName == fav_process.SearchText)) ||
((fav_process.Kind == Favorites.Favorite.FavoriteKinds.ByTitleText) && (pd.WindowTitle == fav_process.SearchText)))
foreach (Favorites.Favorite fav_process in Favorites.List)
{
this.RemoveBorder(pd, fav_process);
if (((fav_process.Kind == Favorites.Favorite.FavoriteKinds.ByBinaryName) && (pd.BinaryName == fav_process.SearchText)) ||
((fav_process.Kind == Favorites.Favorite.FavoriteKinds.ByTitleText) && (pd.WindowTitle == fav_process.SearchText)))
{
this.RemoveBorder(pd, fav_process);
}
}
}
}
Expand Down Expand Up @@ -326,6 +388,25 @@ private void fullApplicationRefreshToolStripMenuItem_Click(object sender, EventA

#region Application Form Events

private void lstProcesses_SelectedIndexChanged(object sender, EventArgs e)
{
bool valid_selection = false;

if (this.lstProcesses.SelectedItem != null)
{
ProcessDetails pd = ((ProcessDetails)this.lstProcesses.SelectedItem);

valid_selection = pd.Manageable;
}

this.btnMakeBorderless.Enabled = this.btnRestoreWindow.Enabled = this.addSelectedItem.Enabled = valid_selection;
}

private void lstFavorites_SelectedIndexChanged(object sender, EventArgs e)
{
this.btnRemoveFavorite.Enabled = (this.lstFavorites.SelectedItem != null);
}

private void setWindowTitleToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.lstProcesses.SelectedItem == null) return;
Expand Down Expand Up @@ -795,6 +876,10 @@ private void MainWindow_Shown(object sender, EventArgs e)
// initialize lists
this.UpdateProcessList();

// Update buttons' enabled/disabled state
this.lstProcesses_SelectedIndexChanged(sender, e);
this.lstFavorites_SelectedIndexChanged(sender, e);

// Start the background worker
this.tmrWork.Start();
}
Expand Down
Loading

0 comments on commit 15493d4

Please sign in to comment.