-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGUIImageArray.cs
295 lines (273 loc) · 11.9 KB
/
GUIImageArray.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
/* Copyright © 2014 Dr Peter O’Neill Sheffield Hallam University.
This Class, places a grid of images onto the sent Form with the images referenced in the cardArray,
you place top and left, it calculates the same distance for right and bottom, giving a spacing
between the images of Border. The ImagePath is relevant to the executional application.
On the main form, where the class is instantiated you need the following line and its eventhandler:
<Object_Name_in_Base_Class>.Which_Element_Clicked += new ImageArray.ImageClickedEventHandler(Which_Element_Clicked);
and the evethandler: private void Which_Element_Clicked(object sender, EventArgs e)
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
namespace GUIImageArray
{
class GImageArray : UserControl
{
public delegate void ImageClickedEventHandler(object sender, EventArgs e);
/// <summary>
/// create private void Which_Element_Clicked(object sender, EventArgs e) on base form
/// this becomes the eventhandler for the click on image
/// </summary>
public event ImageClickedEventHandler Which_Element_Clicked;
private int Int_top, Int_left, Int_rows, Int_cols, Int_Border, Int_Element_Width, Int_Element_Height;
private PictureBox[,] ImageElement;
private Form GuiForm;
private string Path_To_Picture;
/// <summary>
/// GImageArray is the constructor and generates an Array of Images from an int array sent to it.
/// </summary>
/// <param name="Display">The Object the images are placed onto.</param>
/// <param name="cardArray">The int Array of the elements</param>
/// <param name="Top">Top corner of the Array of images</param>
/// <param name="Left">Left side of the Array of images</param>
/// <param name="Bottom">Bottom sides of the Array of images</param>
/// <param name="Right">Right side of the Array of images</param>
/// <param name="Border">The gap between each image, e.g., Top, Left, Right and Bottom </param>
/// <param name="ImagePath">The literal path to each image</param>
public GImageArray(Form Display, int[,] cardArray, int Top, int Left, int Bottom, int Right, int Border, string ImagePath)
{
GuiForm = Display;
Int_rows = cardArray.GetLength(0);
Int_cols = cardArray.GetLength(1);
// Calculate the Height of each element related to the size of the Form and number required
int Available_Height = Display.ClientSize.Height - ((Top + Bottom) + (Border * Int_cols - 1));
// Calculate the Width of each element related to the size of the Form and number required
int Available_Width = Display.ClientSize.Width - ((Left + Right) + (Border * Int_rows - 1));
Int_top = Top;
Int_left=Left;
Int_Border = Border;
Path_To_Picture = ImagePath;
Int_Element_Width = ImageElement_Width(Available_Width);
Int_Element_Height = ImageElement_Height(Available_Height);
if ((Int_Element_Width < 5) || (Int_Element_Height < 5))
{
MessageBox.Show("The Images requested will be to small.\rPlease increase the window size or reduce the number of elements required!");
}
else
{
ImageElement = new PictureBox[Int_rows, Int_cols];
Place_in_Grid(Display, cardArray);
}
}
/// <summary>
/// Destructor, Removes the images from the Form and dispose of them before disposing of itself
/// </summary>
~GImageArray()
{
for (int r = 0; r < Int_rows; r++)
{
for (int c = 0; c < Int_cols; c++)
{
GuiForm.Controls.Remove(ImageElement[r, c]);
ImageElement[r, c].Dispose();
}
}
}
/// <summary>
/// This method reloads each image with it's picture, as ststed within the array.
/// </summary>
/// <param name="cardArray"> The array which states which picture is to be used.</param>
public void UpDateImages(int[,] cardArray)
{
for (int r = 0; r < Int_rows; r++)
{
for (int c = 0; c < Int_cols; c++)
{
ImageElement[r, c].ImageLocation = Path_To_Picture + cardArray[r, c].ToString() + ".PNG";
}
}
}
/// <summary>
/// Returns the index of the Column, of the selected object, e.g., the images clicked on by the mouse
/// </summary>
/// <param name="sender">The object selected within the grid array</param>
/// <returns>index value of the column that owns the object</returns>
public int Get_Col(object sender)
{
for (int r = 0; r < Int_rows; r++)
{
for (int c = 0; c < Int_cols; c++)
{
if (sender == ImageElement[r, c])
{
return c;
}
}
}
return -1;
}
/// <summary>
/// Returns the index of the Row, of the selected object, e.g., the images clicked on by the mouse
/// </summary>
/// <param name="sender"> The object selected within the grid array</param>
/// <returns>index value of the column that owns the object</returns>
public int Get_Row(object sender)
{
for (int r = 0; r < Int_rows; r++)
{
for (int c = 0; c < Int_cols; c++)
{
if (sender == ImageElement[r, c])
{
return r;
}
}
}
return -1;
}
/// <summary>
/// Like its title suggests, Get_Element is passed the index r=row and c=column and returns the address of the PictureBox
/// </summary>
/// <param name="r">r is the index row</param>
/// <param name="c">c is the index column</param>
/// <returns>The PictureBox</returns>
public PictureBox Get_Element(int r, int c)
{
return ImageElement[r, c];
}
public PictureBox Get_Element(object sender)
{
for (int r = 0; r < Int_rows; r++)
{
for (int c = 0; c < Int_cols; c++)
{
if (sender == ImageElement[r, c])
{
return ImageElement[r, c];
}
}
}
return null;
}
/// <summary>
/// Here you can Set_Element image. The path is the one used during creation, but different image may be used, the extension is PNG.
/// </summary>
/// <param name="r">index Row of the element</param>
/// <param name="c">index Column of the element</param>
/// <param name="ImageName_Without_Extension">FileName to be used (No extension required and PNG assumed.</param>
/// <returns></returns>
public bool Set_Elememt(int r, int c, string ImageName_Without_Extension)
{
ImageElement[r, c].ImageLocation = Path_To_Picture + ImageName_Without_Extension + ".PNG";
return true;
}
/// <summary>
/// Changes all images within the array to Red.PNG or Blue.PNG
/// </summary>
/// <param name="Colour">This is the filename stored within the default image location e.g. Red or Blue i this instance.</param>
public void Show_All_Backs(string Colour)
{
switch(Colour[0])
{
case 'R': Colour = "Red";
break;
case 'r': Colour = "Red";
break;
default: Colour = "Blue";
break;
}
for (int r = 0; r < Int_rows; r++)
{
for (int c = 0; c < Int_cols; c++)
{
ImageElement[r, c].ImageLocation = Path_To_Picture + Colour + ".PNG";
}
}
}
/// <summary>
/// This method Shows the image stated within the sent array in it location.
/// </summary>
/// <param name="cardArray">The int 2D array</param>
/// <param name="r">The Row</param>
/// <param name="c">The Column</param>
/// <returns>Return True if it succeeds and False if it does not.</returns>
public bool Show_Element(int[,] cardArray, int r, int c)
{
// Checks to see if requested elemenntt it within the boundaries
if ((r < Int_rows) && (c < Int_cols))
{
ImageElement[r, c].ImageLocation = Path_To_Picture + cardArray[r, c].ToString() + ".PNG";
return true;
}
return false;
}
/// <summary>
/// This method update the location of the images within the frame as the frame changes sizes.
/// </summary>
public void UpdateLocation()
{
// Calculate the Height of each element related to the size of the Form and number required
int Available_Height = GuiForm.ClientSize.Height - ((Top + Bottom) + (Int_Border * Int_cols - 1));
// Calculate the Width of each element related to the size of the Form and number required
int Available_Width = GuiForm.ClientSize.Width - ((Left + Right) + (Int_Border * Int_rows - 1));
Int_top = Top;
Int_left = Left;
for (int r = 0; r < Int_rows; r++)
{
for (int c = 0; c < Int_cols; c++)
{
int l = Int_left;
if (c > 0)
l = l + (Int_Element_Width * c) + (Int_Border * c);
int t = Int_top;
if (r > 0)
t = t + (Int_Element_Height * r) + (Int_Border * r);
ImageElement[r, c].Location = new Point(l, t);
ImageElement[r, c].Size = new Size(Int_Element_Width, Int_Element_Height);
}
}
}
private void Place_in_Grid(Form Disaplay, int[,] cardArray)
{
for (int r = 0; r < Int_rows; r++)
{
for (int c = 0; c < Int_cols; c++)
{
int l = Int_left;
if (c > 0)
l = l + (Int_Element_Width * c) + (Int_Border * c);
int t = Int_top;
if (r > 0)
t = t + (Int_Element_Height * r) + (Int_Border * r);
ImageElement[r, c] = new PictureBox();
ImageElement[r, c].SizeMode = PictureBoxSizeMode.StretchImage;
ImageElement[r, c].Location = new Point(l, t);
ImageElement[r, c].Size = new Size(Int_Element_Width, Int_Element_Height);
ImageElement[r, c].ImageLocation = Path_To_Picture + cardArray[r, c].ToString() + ".PNG";
ImageElement[r, c].Click += new EventHandler(E_Clicked);
GuiForm.Controls.Add(ImageElement[r, c]);
}
}
}
private void E_Clicked(object sender, EventArgs e)
{
// Delegate the event to the caller
if (this != null)
Which_Element_Clicked(sender, e);
}
private int ImageElement_Height(int Available_Size)
{
int n = Available_Size/Int_cols;
return n;
}
private int ImageElement_Width(int Available_Size)
{
int n = Available_Size/Int_rows;
return n;
}
}
}