Added %pn (Process name of active window) to name parser

This commit is contained in:
Jaex 2014-03-25 03:26:08 +02:00
parent 6a1d19764c
commit 652328a672
7 changed files with 66 additions and 35 deletions

View file

@ -37,6 +37,8 @@ public enum ReplacementVariables
{
[Description("Title of active window")]
t,
[Description("Process name of active window")]
pn,
[Description("Current year")]
y,
[Description("Current month")]
@ -109,6 +111,7 @@ public class NameParser
public int AutoIncrementNumber { get; set; } // %i
public Image Picture { get; set; } // %width, %height
public string WindowText { get; set; } // %t
public string ProcessName { get; set; } // %pn
public int MaxTitleLength { get; set; }
public DateTime CustomDate { get; set; }
@ -140,6 +143,11 @@ public string Parse(string pattern)
sb.Replace(ReplacementVariables.t.ToPrefixString(), windowText);
}
if (ProcessName != null)
{
sb.Replace(ReplacementVariables.pn.ToPrefixString(), ProcessName);
}
string width = string.Empty, height = string.Empty;
if (Picture != null)

View file

@ -35,6 +35,12 @@ namespace HelpersLib
{
public static partial class NativeMethods
{
public static string GetForegroundWindowText()
{
IntPtr handle = GetForegroundWindow();
return GetWindowText(handle);
}
public static string GetWindowText(IntPtr handle)
{
if (handle.ToInt32() > 0)
@ -55,6 +61,31 @@ public static string GetWindowText(IntPtr handle)
return null;
}
public static Process GetForegroundWindowProcess()
{
IntPtr handle = GetForegroundWindow();
return GetProcessByWindowHandle(handle);
}
public static Process GetProcessByWindowHandle(IntPtr hwnd)
{
if (hwnd.ToInt32() > 0)
{
try
{
uint processID;
GetWindowThreadProcessId(hwnd, out processID);
return Process.GetProcessById((int)processID);
}
catch (Exception e)
{
DebugHelper.WriteException(e);
}
}
return null;
}
public static string GetClassName(IntPtr handle)
{
if (handle.ToInt32() > 0)
@ -133,40 +164,6 @@ public static Icon GetApplicationIcon(IntPtr handle)
return GetSmallApplicationIcon(handle) ?? GetBigApplicationIcon(handle);
}
public static string GetForegroundWindowText()
{
IntPtr handle = GetForegroundWindow();
return GetWindowText(handle);
}
public static string GetWindowLabel()
{
const int numOfChars = 256;
IntPtr handle = GetForegroundWindow();
StringBuilder sb = new StringBuilder(numOfChars);
if (GetWindowText(handle, sb, numOfChars) > 0)
{
return sb.ToString();
}
return string.Empty;
}
public static IntPtr GetWindowHandle()
{
const int numOfChars = 256;
IntPtr handle = GetForegroundWindow();
StringBuilder sb = new StringBuilder(numOfChars);
if (GetWindowText(handle, sb, numOfChars) > 0)
{
return handle;
}
return IntPtr.Zero;
}
public static bool GetBorderSize(IntPtr handle, out Size size)
{
WINDOWINFO wi = new WINDOWINFO();

View file

@ -25,6 +25,7 @@ You should have received a copy of the GNU General Public License
using HelpersLib;
using System;
using System.Diagnostics;
using System.Drawing;
namespace ScreenCaptureLib
@ -49,6 +50,14 @@ public string ClassName
}
}
public Process Process
{
get
{
return NativeMethods.GetProcessByWindowHandle(Handle);
}
}
public Rectangle Rectangle
{
get

View file

@ -29,6 +29,7 @@ You should have received a copy of the GNU General Public License
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Threading;
using System.Threading.Tasks;
@ -275,6 +276,15 @@ private void CaptureActiveWindow(TaskSettings taskSettings, bool autoHideForm =
{
Image img;
string activeWindowTitle = NativeMethods.GetForegroundWindowText();
string activeProcessName = null;
using (Process process = NativeMethods.GetForegroundWindowProcess())
{
if (process != null)
{
activeProcessName = process.ProcessName;
}
}
if (taskSettings.CaptureSettings.CaptureTransparent && !taskSettings.CaptureSettings.CaptureClientArea)
{
@ -285,7 +295,11 @@ private void CaptureActiveWindow(TaskSettings taskSettings, bool autoHideForm =
img = Screenshot.CaptureActiveWindow();
}
img.Tag = new ImageTag { ActiveWindowTitle = activeWindowTitle };
img.Tag = new ImageTag
{
ActiveWindowTitle = activeWindowTitle,
ActiveProcessName = activeProcessName
};
return img;
}, CaptureType.ActiveWindow, taskSettings, autoHideForm);

View file

@ -797,6 +797,7 @@ private void txtNameFormatPatternActiveWindow_TextChanged(object sender, EventAr
{
AutoIncrementNumber = Program.Settings.NameParserAutoIncrementNumber,
WindowText = Text,
ProcessName = "ShareX",
MaxNameLength = TaskSettings.AdvancedSettings.NamePatternMaxLength,
MaxTitleLength = TaskSettings.AdvancedSettings.NamePatternMaxTitleLength
};

View file

@ -28,5 +28,6 @@ namespace ShareX
public class ImageTag
{
public string ActiveWindowTitle { get; set; }
public string ActiveProcessName { get; set; }
}
}

View file

@ -136,6 +136,7 @@ public static string GetImageFilename(TaskSettings taskSettings, Image image)
if (imageTag != null)
{
nameParser.WindowText = imageTag.ActiveWindowTitle;
nameParser.ProcessName = imageTag.ActiveProcessName;
}
if (string.IsNullOrEmpty(nameParser.WindowText))