Skip to content

Commit

Permalink
Merge pull request #91 from DreamSoule/master
Browse files Browse the repository at this point in the history
命名规则增加
  • Loading branch information
huiyadanli authored Jul 22, 2023
2 parents d0bde8f + 4749699 commit f3b37b0
Showing 1 changed file with 61 additions and 3 deletions.
64 changes: 61 additions & 3 deletions PasteEx/Forms/PathGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,39 @@
using System;
using System;
using System.IO;
using System.Runtime;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace PasteEx.Forms
{
public class PathGenerator
{
public static string defaultFileNamePattern = "$yyyyMMdd$\\Clip_$HHmmss$";

[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();

[DllImport("user32.dll")]
static extern int GetWindowTextLength(IntPtr hWnd);

[DllImport("user32.dll")]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

[DllImport("user32.dll")]
private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);


public static string GetTopWindowText()
{
IntPtr hWnd = GetForegroundWindow();
int length = GetWindowTextLength(hWnd);
StringBuilder text = new StringBuilder(length + 1);
GetWindowText(hWnd, text, text.Capacity);
return text.ToString();
}

public static string GenerateDefaultFileName(string folder, string pattern)
{
if (pattern.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0)
Expand All @@ -33,7 +57,41 @@ private static string GenerateWithPattern(string pattern)
isFormatPattern = !isFormatPattern;
if (!isFormatPattern && sbFormatPattern.Length > 0)
{
sb.Append(DateTime.Now.ToString(sbFormatPattern.ToString()));
bool b_window = sbFormatPattern.ToString().CompareTo("window") == 0;
bool b_process = sbFormatPattern.ToString().CompareTo("process") == 0;
if (b_window || b_process)
{
uint TopWndProcessID = 0;
IntPtr TopWindow = GetForegroundWindow();

if (b_process && GetWindowThreadProcessId(TopWindow, out TopWndProcessID) != 0)
{
Process[] process_list = Process.GetProcesses();
foreach (var p in process_list)
{
try
{
if (p.Id == TopWndProcessID) {
sb.Append(p.ProcessName.ToString());
break;
}
}
catch (Exception)
{
continue;
}
}
}

if (b_window)
{
sb.Append(GetTopWindowText().ToString());
}
}
else
{
sb.Append(DateTime.Now.ToString(sbFormatPattern.ToString()));
}
sbFormatPattern.Clear();
}

Expand Down Expand Up @@ -287,4 +345,4 @@ public static bool IsEmptyFolder(string path)
return false;
}
}
}
}

0 comments on commit f3b37b0

Please sign in to comment.