-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathSystemMenu.cs
More file actions
332 lines (294 loc) · 14.5 KB
/
SystemMenu.cs
File metadata and controls
332 lines (294 loc) · 14.5 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using SmartSystemMenu.Native;
using SmartSystemMenu.Native.Enums;
using SmartSystemMenu.Native.Structs;
using SmartSystemMenu.Settings;
using static SmartSystemMenu.Native.User32;
namespace SmartSystemMenu
{
class SystemMenu
{
private const int DEFAULT_SYSTEM_MENU_NUMBER_ITEMS = 7;
private readonly MenuItems _menuItems;
private readonly LanguageSettings _languageSettings;
public IntPtr WindowHandle { get; }
public IDictionary<int, IntPtr> MoveToMenuItems { get; }
public IntPtr MenuHandle
{
get
{
return GetSystemMenu(WindowHandle, false);
}
}
public bool Exists
{
get
{
var menuHandle = GetSystemMenu(WindowHandle, false);
var existsWindowMenu = menuHandle != IntPtr.Zero;
return existsWindowMenu;
}
}
public SystemMenu(IntPtr windowHandle, MenuItems menuItems, LanguageSettings languageSettings)
{
_menuItems = menuItems;
_languageSettings = languageSettings;
WindowHandle = windowHandle;
MoveToMenuItems = SystemUtils.GetMonitors().Select((x, i) => new KeyValuePair<int, IntPtr>(i + 1, x)).ToDictionary(x => x.Key, y => y.Value);
}
public bool Create()
{
var menuHandle = GetSystemMenu(WindowHandle, false);
if (menuHandle == IntPtr.Zero)
{
return false;
}
foreach (var item in _menuItems.Items)
{
if (item.Type == MenuItemType.Item && item.Show)
{
var id = MenuItemId.GetId(item.Name);
var title = GetTransparencyTitle(id);
title = GetTitle(item.Name, title, true);
if (!IsMenuItem(menuHandle, id))
{
InsertMenu(menuHandle, MenuItemId.SC_CLOSE, Constants.MF_BYCOMMAND, id, title);
}
}
if (item.Type == MenuItemType.Separator && item.Show)
{
InsertMenu(menuHandle, MenuItemId.SC_CLOSE, Constants.MF_BYCOMMAND | Constants.MF_SEPARATOR, MenuItemId.SC_SEPARATOR, null);
}
if (item.Type == MenuItemType.Group && item.Show)
{
var subMenuHandle = CreateMenu();
if (item.Name.ToLower() == "size")
{
for (int i = 0; i < _menuItems.WindowSizeItems.Count; i++)
{
var windowSizeItem = _menuItems.WindowSizeItems[i];
if (windowSizeItem.Type == MenuItemType.Item)
{
windowSizeItem.Id = MenuItemId.SC_SIZE_DEFINED + i;
if (!IsMenuItem(subMenuHandle, windowSizeItem.Id))
{
AppendMenu(subMenuHandle, Constants.MF_BYCOMMAND, windowSizeItem.Id, GetTitle(windowSizeItem));
}
}
if (windowSizeItem.Type == MenuItemType.Separator)
{
InsertMenu(subMenuHandle, -1, Constants.MF_BYCOMMAND | Constants.MF_SEPARATOR, MenuItemId.SC_SEPARATOR, null);
}
}
}
if (item.Name.ToLower() == "move_to")
{
foreach (var moveToMenuItem in MoveToMenuItems)
{
AppendMenu(subMenuHandle, Constants.MF_BYCOMMAND, MenuItemId.SC_MOVE_TO + moveToMenuItem.Key, GetTitle("monitor") + moveToMenuItem.Key);
}
}
if (item.Name.ToLower() == "start_program")
{
for (int i = 0; i < _menuItems.StartProgramItems.Count; i++)
{
var startProgramItem = _menuItems.StartProgramItems[i];
if (startProgramItem.Type == MenuItemType.Item)
{
AppendMenu(subMenuHandle, Constants.MF_BYCOMMAND, MenuItemId.SC_START_PROGRAM + i, startProgramItem.Title);
}
if (startProgramItem.Type == MenuItemType.Separator)
{
InsertMenu(subMenuHandle, -1, Constants.MF_BYCOMMAND | Constants.MF_SEPARATOR, MenuItemId.SC_SEPARATOR, null);
}
}
}
foreach (var subItem in item.Items)
{
if (subItem.Type == MenuItemType.Item && subItem.Show)
{
var subItemId = MenuItemId.GetId(subItem.Name);
var title = GetTransparencyTitle(subItemId);
title = GetTitle(subItem.Name, title, true);
if (!IsMenuItem(subMenuHandle, subItemId))
{
InsertMenu(subMenuHandle, -1, Constants.MF_BYCOMMAND, subItemId, title);
}
}
if (subItem.Type == MenuItemType.Separator && subItem.Show)
{
InsertMenu(subMenuHandle, -1, Constants.MF_BYCOMMAND | Constants.MF_SEPARATOR, MenuItemId.SC_SEPARATOR, null);
}
}
var id = MenuItemId.GetId(item.Name);
if (!IsMenuItem(menuHandle, id))
{
InsertSubMenu(menuHandle, subMenuHandle, MenuItemId.SC_CLOSE, Constants.MF_BYCOMMAND | Constants.MF_POPUP, (uint)id, GetTitle(item.Name, null, true));
}
subMenuHandle = IntPtr.Zero;
}
}
if (!IsMenuItem(menuHandle, MenuItemId.SC_SEPARATOR_BOTTOM))
{
InsertMenu(menuHandle, MenuItemId.SC_CLOSE, Constants.MF_BYCOMMAND | Constants.MF_SEPARATOR, MenuItemId.SC_SEPARATOR_BOTTOM, null);
}
return true;
}
public void Destroy(bool restoreMenu = true)
{
var menuHandle = GetSystemMenu(WindowHandle, false);
if (menuHandle == IntPtr.Zero)
{
return;
}
foreach (var item in _menuItems.Items.Where(x => x.Show))
{
var id = MenuItemId.GetId(item.Name);
if (id > 0)
{
DeleteMenu(menuHandle, id, Constants.MF_BYCOMMAND);
}
else if (item.Type == MenuItemType.Separator)
{
DeleteMenu(menuHandle, MenuItemId.SC_SEPARATOR, Constants.MF_SEPARATOR);
}
}
DeleteMenu(menuHandle, MenuItemId.SC_SEPARATOR_BOTTOM, Constants.MF_BYCOMMAND);
if (restoreMenu)
{
var numberItems = GetMenuItemCount(menuHandle);
if (numberItems == DEFAULT_SYSTEM_MENU_NUMBER_ITEMS)
{
GetSystemMenu(WindowHandle, true);
}
}
}
public void CheckMenuItem(int id, bool check)
{
var menuHandle = GetSystemMenu(WindowHandle, false);
User32.CheckMenuItem(menuHandle, id, check ? Constants.MF_CHECKED : Constants.MF_UNCHECKED);
}
public void UncheckMenuItems(params int[] ids)
{
var menuHandle = GetSystemMenu(WindowHandle, false);
foreach (var id in ids)
{
User32.CheckMenuItem(menuHandle, id, Constants.MF_UNCHECKED);
}
}
public bool IsMenuItemChecked(int id)
{
var menuHandle = GetSystemMenu(WindowHandle, false);
var flags = GetMenuState(menuHandle, id, Constants.MF_BYCOMMAND);
var isChecked = flags != -1 && (flags & Constants.MF_CHECKED) != 0;
return isChecked;
}
public void UncheckPriorityMenu()
{
var menuHandle = GetSystemMenu(WindowHandle, false);
User32.CheckMenuItem(menuHandle, MenuItemId.SC_PRIORITY_REAL_TIME, Constants.MF_UNCHECKED);
User32.CheckMenuItem(menuHandle, MenuItemId.SC_PRIORITY_HIGH, Constants.MF_UNCHECKED);
User32.CheckMenuItem(menuHandle, MenuItemId.SC_PRIORITY_ABOVE_NORMAL, Constants.MF_UNCHECKED);
User32.CheckMenuItem(menuHandle, MenuItemId.SC_PRIORITY_NORMAL, Constants.MF_UNCHECKED);
User32.CheckMenuItem(menuHandle, MenuItemId.SC_PRIORITY_BELOW_NORMAL, Constants.MF_UNCHECKED);
User32.CheckMenuItem(menuHandle, MenuItemId.SC_PRIORITY_IDLE, Constants.MF_UNCHECKED);
}
public void UncheckAlignmentMenu()
{
var menuHandle = GetSystemMenu(WindowHandle, false);
User32.CheckMenuItem(menuHandle, MenuItemId.SC_ALIGN_TOP_LEFT, Constants.MF_UNCHECKED);
User32.CheckMenuItem(menuHandle, MenuItemId.SC_ALIGN_TOP_CENTER, Constants.MF_UNCHECKED);
User32.CheckMenuItem(menuHandle, MenuItemId.SC_ALIGN_TOP_RIGHT, Constants.MF_UNCHECKED);
User32.CheckMenuItem(menuHandle, MenuItemId.SC_ALIGN_MIDDLE_LEFT, Constants.MF_UNCHECKED);
User32.CheckMenuItem(menuHandle, MenuItemId.SC_ALIGN_MIDDLE_CENTER, Constants.MF_UNCHECKED);
User32.CheckMenuItem(menuHandle, MenuItemId.SC_ALIGN_MIDDLE_RIGHT, Constants.MF_UNCHECKED);
User32.CheckMenuItem(menuHandle, MenuItemId.SC_ALIGN_BOTTOM_LEFT, Constants.MF_UNCHECKED);
User32.CheckMenuItem(menuHandle, MenuItemId.SC_ALIGN_BOTTOM_CENTER, Constants.MF_UNCHECKED);
User32.CheckMenuItem(menuHandle, MenuItemId.SC_ALIGN_BOTTOM_RIGHT, Constants.MF_UNCHECKED);
User32.CheckMenuItem(menuHandle, MenuItemId.SC_ALIGN_CENTER_HORIZONTALLY, Constants.MF_UNCHECKED);
User32.CheckMenuItem(menuHandle, MenuItemId.SC_ALIGN_CENTER_VERTICALLY, Constants.MF_UNCHECKED);
User32.CheckMenuItem(menuHandle, MenuItemId.SC_ALIGN_DEFAULT, Constants.MF_UNCHECKED);
User32.CheckMenuItem(menuHandle, MenuItemId.SC_ALIGN_CUSTOM, Constants.MF_UNCHECKED);
}
public void UncheckSizeMenu()
{
var menuHandle = GetSystemMenu(WindowHandle, false);
var windowSizeMenuItemIds = _menuItems.WindowSizeItems.Select(x => x.Id).ToArray();
UncheckMenuItems(windowSizeMenuItemIds);
User32.CheckMenuItem(menuHandle, MenuItemId.SC_SIZE_DEFAULT, Constants.MF_UNCHECKED);
User32.CheckMenuItem(menuHandle, MenuItemId.SC_SIZE_CUSTOM, Constants.MF_UNCHECKED);
}
public void UncheckTransparencyMenu()
{
var menuHandle = GetSystemMenu(WindowHandle, false);
User32.CheckMenuItem(menuHandle, MenuItemId.SC_TRANS_100, Constants.MF_UNCHECKED);
User32.CheckMenuItem(menuHandle, MenuItemId.SC_TRANS_90, Constants.MF_UNCHECKED);
User32.CheckMenuItem(menuHandle, MenuItemId.SC_TRANS_80, Constants.MF_UNCHECKED);
User32.CheckMenuItem(menuHandle, MenuItemId.SC_TRANS_70, Constants.MF_UNCHECKED);
User32.CheckMenuItem(menuHandle, MenuItemId.SC_TRANS_60, Constants.MF_UNCHECKED);
User32.CheckMenuItem(menuHandle, MenuItemId.SC_TRANS_50, Constants.MF_UNCHECKED);
User32.CheckMenuItem(menuHandle, MenuItemId.SC_TRANS_40, Constants.MF_UNCHECKED);
User32.CheckMenuItem(menuHandle, MenuItemId.SC_TRANS_30, Constants.MF_UNCHECKED);
User32.CheckMenuItem(menuHandle, MenuItemId.SC_TRANS_20, Constants.MF_UNCHECKED);
User32.CheckMenuItem(menuHandle, MenuItemId.SC_TRANS_10, Constants.MF_UNCHECKED);
User32.CheckMenuItem(menuHandle, MenuItemId.SC_TRANS_00, Constants.MF_UNCHECKED);
User32.CheckMenuItem(menuHandle, MenuItemId.SC_TRANS_CUSTOM, Constants.MF_UNCHECKED);
User32.CheckMenuItem(menuHandle, MenuItemId.SC_TRANS_DEFAULT, Constants.MF_UNCHECKED);
}
public bool IsMenuItem(IntPtr menuHandle, int item)
{
var mmi = new MenuItemInfo();
mmi.cbSize = (uint)Marshal.SizeOf(mmi);
mmi.fMask = MIIM.ID;
return GetMenuItemInfo(menuHandle, item, false, ref mmi);
}
private string GetTransparencyTitle(int id) => id switch
{
MenuItemId.SC_TRANS_00 => "0%" + GetTitle("trans_opaque", null, false),
MenuItemId.SC_TRANS_10 => "10%",
MenuItemId.SC_TRANS_20 => "20%",
MenuItemId.SC_TRANS_30 => "30%",
MenuItemId.SC_TRANS_40 => "40%",
MenuItemId.SC_TRANS_50 => "50%",
MenuItemId.SC_TRANS_60 => "60%",
MenuItemId.SC_TRANS_70 => "70%",
MenuItemId.SC_TRANS_80 => "80%",
MenuItemId.SC_TRANS_90 => "90%",
MenuItemId.SC_TRANS_100 => "100%" + GetTitle("trans_invisible", null, false),
_ => null
};
private string GetTitle(string name, string title = null, bool showHotKey = true)
{
title = title != null ? title : _languageSettings.GetValue(name);
if (showHotKey)
{
var hotKey = _menuItems.GetHotKeysCombination(name);
return string.IsNullOrEmpty(hotKey) ? title : title + "\t" + hotKey;
}
else
{
return title;
}
}
private string GetTitle(WindowSizeMenuItem item)
{
var hotKey = _menuItems.GetHotKeysCombination(item.Id);
return string.IsNullOrEmpty(hotKey) ? item.Title : item.Title + "\t" + hotKey;
}
private bool InsertSubMenu(IntPtr menuHandle, IntPtr subMenuHandle, int uPosition, int uFlags, uint uIDNewItem, string lpNewItem)
{
if (InsertMenu(menuHandle, uPosition, uFlags, subMenuHandle, lpNewItem))
{
var mmi = new MenuItemInfo();
mmi.cbSize = (uint)Marshal.SizeOf(mmi);
mmi.fMask = MIIM.ID;
mmi.wID = uIDNewItem;
return SetMenuItemInfo(menuHandle, subMenuHandle.ToInt32(), false, ref mmi);
}
return true;
}
}
}