-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.cs
504 lines (419 loc) · 15 KB
/
Utils.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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Microsoft.Win32;
namespace memory_game
{
public enum CenterMethod { CenterHorizontal, CenterVertical, CenterBoth }
public class CustomUtils
{
public bool SelectComboItem(ComboBox cb, string what)
{
for (var k = 0; k < cb.Items.Count; k++)
{
if (!cb.Items[k].Equals(what)) continue;
cb.SelectedIndex = k;
return true;
}
return false;
}
public bool SelectComboItem(ToolStripComboBox cb, string what)
{
try
{
for (var k = 0; k < cb.Items.Count - 1; k++)
{
if (!cb.Items[k].Equals(what)) continue;
cb.SelectedIndex = k;
return true;
}
cb.Text = what;
}
catch { }
return false;
}
public void CenterForm(Form frm)
{
int width = frm.Size.Width,
height = frm.Size.Height,
scrW = Screen.PrimaryScreen.Bounds.Width,
scrH = Screen.PrimaryScreen.Bounds.Height;
var x = scrW / 2 - width / 2;
var y = scrH / 2 - height / 2;
frm.Location = new Point(x, y);
}
public void CenterControl(Form parentForm, Control ctrl, CenterMethod method)
{
var x = parentForm.Width / 2 - ctrl.Width / 2;
var y = parentForm.Height / 2 - ctrl.Height / 2;
switch (method)
{
case CenterMethod.CenterVertical:
ctrl.Location = new Point(ctrl.Location.X, y);
break;
case CenterMethod.CenterHorizontal:
ctrl.Location = new Point(x, ctrl.Location.Y);
break;
case CenterMethod.CenterBoth:
ctrl.Location = new Point(x, y);
break;
default:
throw new ArgumentOutOfRangeException("method", method, null);
}
}
}
/// <summary>
/// main config repository
/// </summary>
///
public class Defaults
{
// window position
public int WinPosX = -1;
public int WinPosY = -1;
public int WinPosWidth = 600;
public int WinPosHeight = 400;
public bool SoundEnabled = true;
}
public class AppConfig
{
private readonly RegistryKey _registry;
private readonly bool _registryEnabled;
public Defaults Defaults;
/// <summary>
/// Constructor
/// </summary>
public AppConfig()
{
// otvori registry key
try
{
Defaults = new Defaults();
var currentUser = Registry.CurrentUser.OpenSubKey("Software", RegistryKeyPermissionCheck.ReadWriteSubTree);
_registry = currentUser.CreateSubKey(Program.AppRegistryRoot);
_registryEnabled = true;
}
catch
{
_registryEnabled = false;
}
}
public bool IsEnabled
{
get { return _registryEnabled; }
}
public void DeleteKey(string keyName)
{
_registry.DeleteSubKey(keyName, false);
}
public string GetCFG(string name, string defaultVal)
{
if (!_registryEnabled)
return defaultVal;
try
{
var val = _registry.GetValue(name, defaultVal).ToString();
return val;
}
catch
{
return defaultVal;
}
}
public int GetCFG(string name, int defaultVal)
{
if (!_registryEnabled)
return defaultVal;
try
{
var val = _registry.GetValue(name, defaultVal).ToString();
return Convert.ToInt32(val);
}
catch
{
return defaultVal;
}
}
public double GetCFG(string name, double defaultVal)
{
if (!_registryEnabled)
return defaultVal;
try
{
var val = _registry.GetValue(name, defaultVal).ToString();
return Convert.ToDouble(val);
}
catch
{
return defaultVal;
}
}
public bool GetCFG(string name, bool defaultVal)
{
if (!_registryEnabled)
return defaultVal;
try
{
var val = _registry.GetValue(name, defaultVal).ToString();
return Convert.ToBoolean(val);
}
catch
{
return defaultVal;
}
}
public Font GetCFG(string name, Font defaultVal)
{
if (!_registryEnabled)
return defaultVal;
try
{
var fontName = _registry.GetValue(name + "Title", defaultVal.FontFamily.Name).ToString();
var fontSize = (float) Convert.ToDouble(_registry.GetValue(name + "Size", defaultVal.Size).ToString());
var fontStyle = _registry.GetValue(name + "Style", defaultVal.Style.ToString()).ToString().ToLower();
var style = FontStyle.Regular;
if (fontStyle.Equals("bold"))
style = FontStyle.Bold;
if (fontStyle.Equals("italic"))
style = FontStyle.Italic;
if (fontStyle.Equals("strikeout"))
style = FontStyle.Strikeout;
if (fontStyle.Equals("underline"))
style = FontStyle.Underline;
return new Font(fontName, fontSize, style, GraphicsUnit.Point);
}
catch
{
return defaultVal;
}
}
public Color GetCFG(string name, Color defaultVal)
{
if (!_registryEnabled)
return defaultVal;
try
{
var val = _registry.GetValue(name, defaultVal).ToString();
return ColorTranslator.FromHtml(val);
}
catch
{
return defaultVal;
}
}
//********************************
// SAVE SETTINGS routines
//********************************
public void SaveCFG(string name, string val)
{
if (!_registryEnabled)
throw new Exception(Program.StrRegFailed);
_registry.SetValue(name, val, RegistryValueKind.String);
}
public void SaveCFG(string name, int val)
{
if (!_registryEnabled)
throw new Exception(Program.StrRegFailed);
_registry.SetValue(name, val.ToString(), RegistryValueKind.String);
}
public void SaveCFG(string name, double val)
{
if (!_registryEnabled)
throw new Exception(Program.StrRegFailed);
_registry.SetValue(name, val.ToString(), RegistryValueKind.String);
}
public void SaveCfg(string name, bool val)
{
if (!_registryEnabled)
throw new Exception(Program.StrRegFailed);
_registry.SetValue(name, val.ToString(), RegistryValueKind.String);
}
public void SaveCFG(string name, Font val)
{
if (!_registryEnabled)
throw new Exception(Program.StrRegFailed);
_registry.SetValue(name + "Title", val.FontFamily.Name, RegistryValueKind.String);
_registry.SetValue(name + "Size", val.Size.ToString(), RegistryValueKind.String);
_registry.SetValue(name + "Style", val.Style.ToString(), RegistryValueKind.String);
}
public void SaveCFG(string name, Color val)
{
if (!_registryEnabled)
throw new Exception(Program.StrRegFailed);
_registry.SetValue(name, ColorTranslator.ToHtml(val), RegistryValueKind.String);
}
}
/***
*
* Posudjeno sa codeguru.com
*
*/
public class NoSystemMenuException : Exception
{
}
// Values taken from MSDN.
public enum ItemFlags
{
// The item ...
MfUnchecked = 0x00000000, // ... is not checked
MfString = 0x00000000, // ... contains a string as label
MfDisabled = 0x00000002, // ... is disabled
MfGrayed = 0x00000001, // ... is grayed
MfChecked = 0x00000008, // ... is checked
MfPopup = 0x00000010, // ... Is a popup menu. Pass the
// menu handle of the popup
// menu into the ID parameter.
MfBarBreak = 0x00000020, // ... is a bar break
MfBreak = 0x00000040, // ... is a break
MfByPosition = 0x00000400, // ... is identified by the position
MfByCommand = 0x00000000, // ... is identified by its ID
MfSeparator = 0x00000800 // ... is a seperator (String and
// ID parameters are ignored).
}
public enum WindowMessages
{
WmSysCommand = 0x0112
}
/// <summary>
/// A class that helps to manipulate the system menu
/// of a passed form.
///
/// Written by Florian "nohero" Stinglmayr
/// </summary>
public class SystemMenu
{
// I havn't found any other solution than using plain old
// WinAPI to get what I want.
// If you need further information on these functions, their
// parameters, and their meanings, you should look them up in
// the MSDN.
// All parameters in the [DllImport] should be self explanatory.
// NOTICE: Use never stdcall as a calling convention, since Winapi
// is used.
// If the underlying structure changes, your program might cause
// errors that are hard to find.
// First, we need the GetSystemMenu() function.
// This function does not have an Unicode counterpart
[DllImport("USER32", EntryPoint = "GetSystemMenu", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]
private static extern IntPtr apiGetSystemMenu(IntPtr windowHandle, int bReset);
// And we need the AppendMenu() function. Since .NET uses Unicode,
// we pick the unicode solution.
[DllImport("USER32", EntryPoint = "AppendMenuW", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]
private static extern int apiAppendMenu(IntPtr menuHandle, int flags, int newId, String item);
// And we also may need the InsertMenu() function.
[DllImport("USER32", EntryPoint = "InsertMenuW", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]
private static extern int apiInsertMenu(IntPtr hMenu, int position, int flags, int newId, String item);
private IntPtr _mSysMenu = IntPtr.Zero; // Handle to the System Menu
// Insert a separator at the given position index starting at zero.
public bool InsertSeparator(int pos)
{
return InsertMenu(pos, ItemFlags.MfSeparator | ItemFlags.MfByPosition, 0, "");
}
// Simplified InsertMenu(), that assumes that Pos is a relative
// position index starting at zero
public bool InsertMenu(int pos, int id, String item)
{
return InsertMenu(pos, ItemFlags.MfByPosition | ItemFlags.MfString, id, item);
}
// Insert a menu at the given position. The value of the position
// depends on the value of Flags. See the article for a detailed
// description.
public bool InsertMenu(int pos, ItemFlags flags, int id, String item)
{
return apiInsertMenu(_mSysMenu, pos, (Int32) flags, id, item) == 0;
}
// Appends a seperator
public bool AppendSeparator()
{
return AppendMenu(0, "", ItemFlags.MfSeparator);
}
// This uses the ItemFlags.mfString as default value
public bool AppendMenu(int id, String item)
{
return AppendMenu(id, item, ItemFlags.MfString);
}
// Superseded function.
public bool AppendMenu(int id, String item, ItemFlags flags)
{
return apiAppendMenu(_mSysMenu, (int) flags, id, item) == 0;
}
// Retrieves a new object from a Form object
public static SystemMenu FromForm(Form frm)
{
var cSysMenu = new SystemMenu();
cSysMenu._mSysMenu = apiGetSystemMenu(frm.Handle, 0);
if (cSysMenu._mSysMenu == IntPtr.Zero)
{
// Throw an exception on failure
throw new NoSystemMenuException();
}
return cSysMenu;
}
// Reset's the window menu to it's default
public static void ResetSystemMenu(Form frm)
{
apiGetSystemMenu(frm.Handle, 1);
}
// Checks if an ID for a new system menu item is OK or not
public static bool VerifyItemId(int id)
{
return id < 0xF000 && id > 0;
}
}
public class Top10
{
private readonly AppConfig _cfg;
private string[][] _entries; // tu cemo drzati unose
public Top10(AppConfig config)
{
_cfg = config;
}
/// <summary>
/// vraca redni broj u top10 ili -1 ako je van top 10.
/// u slucaju -1 ne sprema se unos, hvataj ga kako god znas
/// </summary>
/// <returns></returns>
public int AddEntry(int score, int time, int difficulty)
{
_entries = GetEntries();
return -1;
}
/// <summary>
/// preuzmi, otpakiraj i davaj probavljeni string
/// </summary>
/// <returns></returns>
public string[][] GetEntries()
{
var entries = new string[10][];
var raw = _cfg.GetCFG("lstTop", "");
// potrebni decrypt!!
raw = Convert.FromBase64String(raw).ToString();
// TODO: validate data
var rawEntries = raw.Split(new[] {';'}, 10);
var row = new string[4];
for (var k = 0; k < 10; k++)
{
try
{
row = rawEntries[k].Split(new[] {'&'}, 4);
}
catch
{
}
if (row[0].Equals(string.Empty))
{
row = new[] {"-", "-", "-", "-"};
}
entries[k] = row;
}
return entries;
}
/// <summary>
/// zapakiraj i spremi u registry
/// </summary>
public void Flush()
{
}
}
}