-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWindowHelper.cs
151 lines (130 loc) · 5.95 KB
/
WindowHelper.cs
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
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.Win32;
namespace MoveWindow
{
internal class WindowHelper
{
public delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll")]
public static extern IntPtr GetWindow(IntPtr hWnd, GetWindowCmd uCmd);
[DllImport("user32.dll", SetLastError = true)]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
[DllImport("user32.dll", SetLastError = true)]
public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
public const int GWL_EXSTYLE = -20;
public const int WS_EX_TOOLWINDOW = 0x00000080;
public const int WS_EX_APPWINDOW = 0x00040000;
public const uint SWP_NOSIZE = 0x0001;
public const uint SWP_NOZORDER = 0x0004;
public const uint SWP_NOACTIVATE = 0x0010;
public static readonly IntPtr HWND_TOP = IntPtr.Zero;
public enum GetWindowCmd : uint
{
GW_OWNER = 4
}
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
public static bool IsTopLevelWindow(IntPtr hWnd)
{
return GetWindow(hWnd, GetWindowCmd.GW_OWNER) == IntPtr.Zero &&
(GetWindowLong(hWnd, GWL_EXSTYLE) & WS_EX_TOOLWINDOW) == 0;
}
public static void SaveWindowPositionAndSize(string windowTitle, int x, int y, int width, int height)
{
using (RegistryKey key = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\MoveWindow\" + windowTitle))
{
if (key != null)
{
key.SetValue("X", x);
key.SetValue("Y", y);
key.SetValue("Width", width);
key.SetValue("Height", height);
}
}
}
public static (int x, int y, int width, int height)? LoadWindowPositionAndSize(string windowTitle)
{
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\MoveWindow\" + windowTitle))
{
if (key != null)
{
object xValue = key.GetValue("X");
object yValue = key.GetValue("Y");
object widthValue = key.GetValue("Width");
object heightValue = key.GetValue("Height");
if (xValue != null && yValue != null && widthValue != null && heightValue != null)
{
int x = (int)xValue;
int y = (int)yValue;
int width = (int)widthValue;
int height = (int)heightValue;
return (x, y, width, height);
}
}
}
return null;
}
public static Dictionary<string, Dictionary<string, string>> LoadAllRegistryValues()
{
var registryValues = new Dictionary<string, Dictionary<string, string>>();
try
{
using (var key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\MoveWindow", false))
{
if (key != null)
{
foreach (var subKeyName in key.GetSubKeyNames())
{
using (var subKey = key.OpenSubKey(subKeyName))
{
if (subKey != null)
{
var subKeyValues = new Dictionary<string, string>();
foreach (var valueName in subKey.GetValueNames())
{
string value = subKey.GetValue(valueName)?.ToString();
if (!string.IsNullOrEmpty(value))
{
subKeyValues[valueName] = value;
}
}
registryValues[subKeyName] = subKeyValues;
}
}
}
}
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Error accessing registry: {ex.Message}");
}
return registryValues;
}
}
}