-
Notifications
You must be signed in to change notification settings - Fork 2
/
FontAppearanceSetting.cs
178 lines (154 loc) · 5.18 KB
/
FontAppearanceSetting.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
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AdvancedWindowsAppearence
{
public class FontAppearanceSetting : AppearanceSetting
{
private bool isItalic;
public Font Font { get => _font; set
{
if (_font == value) return;
_font = value;
base.NotifyPropertyChanged();
}
}
public string FontName
{
get
{
return Font.Name;
}
}
bool isBold;
public bool IsBold
{
get
{
return isBold;
}
set
{
isBold = value;
IsEdited = true;
base.NotifyPropertyChanged();
}
}
private Font _font;
public bool IsItalic
{
get
{
return isItalic;
}
set
{
isItalic = value;
IsEdited = true;
base.NotifyPropertyChanged();
}
}
public readonly string FontRegistryPath;
const int fontNameStartIndex = 28; //odtialto zacina string (nazov fontu) vramci jedneko keyu v registri
public FontAppearanceSetting() { }
public FontAppearanceSetting(string _name, string _regeditPath, string _colorRegistryPath)
{
Name = _name;
FontRegistryPath = _regeditPath;
ColorRegistryPath = _colorRegistryPath;
LoadValues();
}
void LoadValues()
{
Font = GetFontFromRegistry(FontRegistryPath);
ItemColor = GetColorFromRegistry(ColorRegistryPath);
IsEdited = false;
}
public void ChangeFontName(string name) // verify the way how this works
{
if (FontName == name) return;
this.Font = FontManager.FindFontFromString(name);
IsEdited = true;
}
internal Font GetFontFromRegistry(string registrypath)
{
Font regeditFont = new Font(FontFamily.GenericSansSerif, 11);
if (registrypath == null || registrypath == "") return null;
RegistryKey registryKey = Registry.CurrentUser.CreateSubKey(@"Control Panel\Desktop\WindowMetrics");
try {
byte[] fonttemp = (byte[])registryKey.GetValue(registrypath);
List<byte> fontNametemp = new List<byte>();
int i = 0;
foreach (byte b in fonttemp)
{
if (i >= fontNameStartIndex && b != 0)
{
fontNametemp.Add(b);
}
i++;
}
string fontstring = Encoding.ASCII.GetString(fontNametemp.ToArray());
registryKey.Close();
regeditFont = FontManager.FindFontFromString(fontstring);
if (fonttemp[17] == 02)
{
IsBold = true;
}
if (fonttemp[20] == 01)
{
IsItalic = true;
}
int sizetemp = (int)fonttemp[0];
this.Size = (256 - sizetemp) / (float)FontManager.DPI;
}
catch
{
Console.WriteLine("Unable to load font '{0}'", Name);
}
return regeditFont;
}
public byte[] GetFontInRegistryFormat()
{
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop\WindowMetrics");
List<byte> registryValBytes = ((byte[])key.GetValue(FontRegistryPath)).ToList();
key.Close();
if (IsBold)
registryValBytes[17] = 2;
else
registryValBytes[17] = 1;
if (IsItalic)
registryValBytes[20] = 1;
else
registryValBytes[20] = 0;
registryValBytes.RemoveRange(fontNameStartIndex, registryValBytes.Count - fontNameStartIndex);
byte fontsizetemp = (byte)((256 - Size * FontManager.DPI));
registryValBytes[0] = fontsizetemp;
List<byte> fontNameBytes = new List<byte>();
foreach (char c in this.Font.Name)
{
byte b = (byte)c;
fontNameBytes.Add(b);
fontNameBytes.Add(0);
}
List<byte> newRegistryValBytes = registryValBytes;
newRegistryValBytes.AddRange(fontNameBytes);
return newRegistryValBytes.ToArray();
}
private void SaveFontToRegistry()
{
RegistryKey newKey = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop\WindowMetrics", true);
newKey.SetValue(FontRegistryPath, GetFontInRegistryFormat(), RegistryValueKind.Binary);
newKey.Close();
}
public void SaveToRegistry()
{
base.SaveColorToRegistry();
SaveFontToRegistry();
IsEdited = false;
}
}
}