-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathHidableForm.cs
151 lines (125 loc) · 4.95 KB
/
HidableForm.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Text;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace CM0102_Starter_Kit {
[TypeDescriptionProvider(typeof(AbstractControlDescriptionProvider<HidableForm, Form>))]
abstract partial class HidableForm : Form {
public HidableForm()
{
LoadFonts();
}
protected MainMenu mainMenu;
protected abstract List<Button> GetButtons();
protected virtual void RefreshForm() { }
private void ToggleButtons(bool toggle) {
exit.Enabled = toggle;
back_button.Enabled = toggle;
foreach (Button button in GetButtons()) {
button.Enabled = toggle;
}
}
protected void ShowLoader() {
this.Cursor = System.Windows.Forms.Cursors.WaitCursor;
this.loader.Visible = true;
ToggleButtons(false);
}
protected void HideLoader() {
this.Cursor = System.Windows.Forms.Cursors.Default;
ToggleButtons(true);
this.loader.Visible = false;
}
private void RenderLoader(PaintEventArgs e) {
StringFormat format = new StringFormat {
LineAlignment = StringAlignment.Center,
Alignment = StringAlignment.Center
};
e.Graphics.DrawString("Please Wait...", new Font("Verdana", 18, FontStyle.Bold), Brushes.White, this.ClientRectangle, format);
}
private void Loader_Paint(object sender, PaintEventArgs e) {
RenderLoader(e);
}
protected void DisplayMessage(string message) {
using (new CentreMessageBox(this)) {
MessageBox.Show(message);
}
}
protected void ShowNewScreen(HidableForm newForm) {
this.Hide();
newForm.StartPosition = FormStartPosition.Manual;
newForm.Location = this.Location;
newForm.Size = this.Size;
newForm.RefreshForm();
newForm.Show();
}
private void BackButton_Click(object sender, EventArgs e) {
ShowNewScreen(mainMenu);
}
private void Exit_Click(object sender, EventArgs e) {
this.Close();
}
// Nick's wacky font code :)
public static PrivateFontCollection pfcErasDemiBt = null;
public static FontFamily ffErasDemiBt = null;
public static PrivateFontCollection pfcHandelGo = null;
public static FontFamily ffHandelGo = null;
void LoadFonts()
{
if (pfcErasDemiBt == null)
{
pfcErasDemiBt = LoadFont("ErasDemiBT.ttf");
ffErasDemiBt = pfcErasDemiBt.Families[0];
pfcHandelGo = LoadFont("HandelGo.ttf");
ffHandelGo = pfcHandelGo.Families[0];
}
}
PrivateFontCollection LoadFont(string fontName)
{
var pfc = new PrivateFontCollection();
var assembly = Assembly.GetExecutingAssembly();
var resNames = assembly.GetManifestResourceNames();
string resourceName = "";
foreach (var r in resNames)
{
if (r.EndsWith(fontName))
{
resourceName = r;
break;
}
}
using (var fontStream = assembly.GetManifestResourceStream(resourceName))
{
// Copy font stream into a byte array
var fontBytes = new byte[fontStream.Length];
fontStream.Read(fontBytes, 0, fontBytes.Length);
// Create unsafe memory block
var newMemory = Marshal.AllocCoTaskMem(fontBytes.Length);
// copy the bytes to the unsafe memory block
Marshal.Copy(fontBytes, 0, newMemory, fontBytes.Length);
pfc.AddMemoryFont(newMemory, fontBytes.Length);
// Free the memory
Marshal.FreeCoTaskMem(newMemory);
}
return pfc;
}
}
public class AbstractControlDescriptionProvider<TAbstract, TBase> : TypeDescriptionProvider {
public AbstractControlDescriptionProvider() : base(TypeDescriptor.GetProvider(typeof(TAbstract))) { }
public override Type GetReflectionType(Type objectType, object instance) {
if (objectType == typeof(TAbstract)) {
return typeof(TBase);
}
return base.GetReflectionType(objectType, instance);
}
public override object CreateInstance(IServiceProvider provider, Type objectType, Type[] argTypes, object[] args) {
if (objectType == typeof(TAbstract)) {
objectType = typeof(TBase);
}
return base.CreateInstance(provider, objectType, argTypes, args);
}
}
}