-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindowHelpers.cs
More file actions
155 lines (129 loc) · 4.75 KB
/
WindowHelpers.cs
File metadata and controls
155 lines (129 loc) · 4.75 KB
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
using System;
using System.Text;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace WindowTracker
{
class WindowHelpers
{
#region Window Placement Is Visible
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
private struct WINDOWPLACEMENT
{
public int length;
public int flags;
public int showCmd;
public System.Drawing.Point ptMinPosition;
public System.Drawing.Point ptMaxPosition;
public System.Drawing.Point rcNormalPosition;
}
/// <summary>
/// Returns whether the window placement is visible
/// </summary>
/// <param name="hWnd"></param>
/// <returns></returns>
public static bool WindowPlacementIsVisible(IntPtr hWnd)
{
WINDOWPLACEMENT placement = new WINDOWPLACEMENT();
placement.length = Marshal.SizeOf(placement);
GetWindowPlacement(hWnd, ref placement);
return !placement.rcNormalPosition.IsEmpty;
}
#endregion
#region Window is Visible
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool IsWindowVisible(IntPtr hWnd);
public static bool WindowIsVisible(IntPtr hWnd)
{
return IsWindowVisible(hWnd);
}
#endregion
#region Get Foreground Window Handle
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
private int GetForegroundWindowHandle()
{
IntPtr hndl = GetForegroundWindow();
return (Int32)hndl;
}
#endregion
#region Get Time Since Last User Input
[StructLayout(LayoutKind.Sequential)]
private struct LASTINPUTINFO
{
public static readonly int SizeOf = Marshal.SizeOf(typeof(LASTINPUTINFO));
[MarshalAs(UnmanagedType.U4)]
public UInt32 cbSize;
[MarshalAs(UnmanagedType.U4)]
public UInt32 dwTime;
}
[DllImport("user32.dll")]
private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
public static int GetLastInputTime()
{
int idleTime = 0;
LASTINPUTINFO lastInputInfo = new LASTINPUTINFO();
lastInputInfo.cbSize = (uint)Marshal.SizeOf(lastInputInfo);
lastInputInfo.dwTime = 0;
int envTicks = Environment.TickCount;
if (GetLastInputInfo(ref lastInputInfo))
{
int lastInputTick = (int)lastInputInfo.dwTime;
idleTime = envTicks - lastInputTick;
}
return ((idleTime > 0) ? (idleTime / 1000) : 0);
}
#endregion
//https://stackoverflow.com/questions/8431298/process-mainmodule-access-is-denied
#region Get Process Exe File Path
[Flags]
private enum ProcessAccessFlags : uint
{
QueryLimitedInformation = 0x00001000
}
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool QueryFullProcessImageName(
[In] IntPtr hProcess,
[In] int dwFlags,
[Out] StringBuilder lpExeName,
ref int lpdwSize);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr OpenProcess(
ProcessAccessFlags processAccess,
bool bInheritHandle,
int processId);
/// <summary>
/// Returns the executable path of the running process.
/// </summary>
/// <param name="process"></param>
/// <returns></returns>
public static string GetProcessFileName(Process process)
{
string processFileName = String.Empty;
try
{
processFileName = process.MainModule.FileName;
}
catch (System.ComponentModel.Win32Exception)
{
// do nothing, 'Access is denied' is an error I get with some applications (ex: Task Manager)
}
if (!String.IsNullOrEmpty(processFileName))
{
return processFileName;
}
int capacity = 2000;
StringBuilder builder = new StringBuilder(capacity);
IntPtr ptr = OpenProcess(ProcessAccessFlags.QueryLimitedInformation, false, process.Id);
if (!QueryFullProcessImageName(ptr, 0, builder, ref capacity))
{
return String.Empty;
}
return builder.ToString();
}
#endregion
}
}