/
SupportClass.cs
446 lines (416 loc) · 18.5 KB
/
SupportClass.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
//
// In order to convert some functionality to Visual C#, the Java Language Conversion Assistant
// creates "support classes" that duplicate the original functionality.
//
// Support classes replicate the functionality of the original code, but in some cases they are
// substantially different architecturally. Although every effort is made to preserve the
// original architecture of the application in the converted project, the user should be aware that
// the primary goal of these support classes is to replicate functionality, and that at times
// the architecture of the resulting solution may differ somewhat.
//
using System;
/// <summary>
/// Contains conversion support elements such as classes, interfaces and static methods.
/// </summary>
public class SupportClass
{
/// <summary>
/// Give functions to obtain information of graphic elements
/// </summary>
public class GraphicsManager
{
//Instance of GDI+ drawing surfaces graphics hashtable
static public GraphicsHashTable manager = new GraphicsHashTable();
/// <summary>
/// Creates a new Graphics object from the device context handle associated with the Graphics
/// parameter
/// </summary>
/// <param name="oldGraphics">Graphics instance to obtain the parameter from</param>
/// <returns>A new GDI+ drawing surface</returns>
public static System.Drawing.Graphics CreateGraphics(System.Drawing.Graphics oldGraphics)
{
System.Drawing.Graphics createdGraphics;
System.IntPtr hdc = oldGraphics.GetHdc();
createdGraphics = System.Drawing.Graphics.FromHdc(hdc);
oldGraphics.ReleaseHdc(hdc);
return createdGraphics;
}
/// <summary>
/// This method draws a Bezier curve.
/// </summary>
/// <param name="graphics">It receives the Graphics instance</param>
/// <param name="array">An array of (x,y) pairs of coordinates used to draw the curve.</param>
public static void Bezier(System.Drawing.Graphics graphics, int[] array)
{
System.Drawing.Pen pen;
pen = GraphicsManager.manager.GetPen(graphics);
try
{
graphics.DrawBezier(pen, array[0], array[1], array[2], array[3], array[4], array[5], array[6], array[7]);
}
catch(System.IndexOutOfRangeException e)
{
throw new System.IndexOutOfRangeException(e.ToString());
}
}
/// <summary>
/// Gets the text size width and height from a given GDI+ drawing surface and a given font
/// </summary>
/// <param name="graphics">Drawing surface to use</param>
/// <param name="graphicsFont">Font type to measure</param>
/// <param name="text">String of text to measure</param>
/// <returns>A point structure with both size dimentions; x for width and y for height</returns>
public static System.Drawing.Point GetTextSize(System.Drawing.Graphics graphics, System.Drawing.Font graphicsFont, System.String text)
{
System.Drawing.Point textSize;
System.Drawing.SizeF tempSizeF;
tempSizeF = graphics.MeasureString(text, graphicsFont);
textSize = new System.Drawing.Point();
textSize.X = (int) tempSizeF.Width;
textSize.Y = (int) tempSizeF.Height;
return textSize;
}
/// <summary>
/// Gets the text size width and height from a given GDI+ drawing surface and a given font
/// </summary>
/// <param name="graphics">Drawing surface to use</param>
/// <param name="graphicsFont">Font type to measure</param>
/// <param name="text">String of text to measure</param>
/// <param name="width">Maximum width of the string</param>
/// <param name="format">StringFormat object that represents formatting information, such as line spacing, for the string</param>
/// <returns>A point structure with both size dimentions; x for width and y for height</returns>
public static System.Drawing.Point GetTextSize(System.Drawing.Graphics graphics, System.Drawing.Font graphicsFont, System.String text, System.Int32 width, System.Drawing.StringFormat format)
{
System.Drawing.Point textSize;
System.Drawing.SizeF tempSizeF;
tempSizeF = graphics.MeasureString(text, graphicsFont, width, format);
textSize = new System.Drawing.Point();
textSize.X = (int) tempSizeF.Width;
textSize.Y = (int) tempSizeF.Height;
return textSize;
}
/// <summary>
/// Gives functionality over a hashtable of GDI+ drawing surfaces
/// </summary>
public class GraphicsHashTable:System.Collections.Hashtable
{
/// <summary>
/// Gets the graphics object from the given control
/// </summary>
/// <param name="control">Control to obtain the graphics from</param>
/// <returns>A graphics object with the control's characteristics</returns>
public System.Drawing.Graphics GetGraphics(System.Windows.Forms.Control control)
{
System.Drawing.Graphics graphic;
if (control.Visible == true)
{
graphic = control.CreateGraphics();
SetColor(graphic, control.ForeColor);
SetFont(graphic, control.Font);
}
else
{
graphic = null;
}
return graphic;
}
/// <summary>
/// Sets the background color property to the given graphics object in the hashtable. If the element doesn't exist, then it adds the graphic element to the hashtable with the given background color.
/// </summary>
/// <param name="graphic">Graphic element to search or add</param>
/// <param name="color">Background color to set</param>
public void SetBackColor(System.Drawing.Graphics graphic, System.Drawing.Color color)
{
if (this[graphic] != null)
((GraphicsProperties) this[graphic]).BackColor = color;
else
{
GraphicsProperties tempProps = new GraphicsProperties();
tempProps.BackColor = color;
Add(graphic, tempProps);
}
}
/// <summary>
/// Gets the background color property to the given graphics object in the hashtable. If the element doesn't exist, then it returns White.
/// </summary>
/// <param name="graphic">Graphic element to search</param>
/// <returns>The background color of the graphic</returns>
public System.Drawing.Color GetBackColor(System.Drawing.Graphics graphic)
{
if (this[graphic] == null)
return System.Drawing.Color.White;
else
return ((GraphicsProperties) this[graphic]).BackColor;
}
/// <summary>
/// Sets the text color property to the given graphics object in the hashtable. If the element doesn't exist, then it adds the graphic element to the hashtable with the given text color.
/// </summary>
/// <param name="graphic">Graphic element to search or add</param>
/// <param name="color">Text color to set</param>
public void SetTextColor(System.Drawing.Graphics graphic, System.Drawing.Color color)
{
if (this[graphic] != null)
((GraphicsProperties) this[graphic]).TextColor = color;
else
{
GraphicsProperties tempProps = new GraphicsProperties();
tempProps.TextColor = color;
Add(graphic, tempProps);
}
}
/// <summary>
/// Gets the text color property to the given graphics object in the hashtable. If the element doesn't exist, then it returns White.
/// </summary>
/// <param name="graphic">Graphic element to search</param>
/// <returns>The text color of the graphic</returns>
public System.Drawing.Color GetTextColor(System.Drawing.Graphics graphic)
{
if (this[graphic] == null)
return System.Drawing.Color.White;
else
return ((GraphicsProperties) this[graphic]).TextColor;
}
/// <summary>
/// Sets the GraphicBrush property to the given graphics object in the hashtable. If the element doesn't exist, then it adds the graphic element to the hashtable with the given GraphicBrush.
/// </summary>
/// <param name="graphic">Graphic element to search or add</param>
/// <param name="brush">GraphicBrush to set</param>
public void SetBrush(System.Drawing.Graphics graphic, System.Drawing.SolidBrush brush)
{
if (this[graphic] != null)
((GraphicsProperties) this[graphic]).GraphicBrush = brush;
else
{
GraphicsProperties tempProps = new GraphicsProperties();
tempProps.GraphicBrush = brush;
Add(graphic, tempProps);
}
}
/// <summary>
/// Sets the GraphicBrush property to the given graphics object in the hashtable. If the element doesn't exist, then it adds the graphic element to the hashtable with the given GraphicBrush.
/// </summary>
/// <param name="graphic">Graphic element to search or add</param>
/// <param name="brush">GraphicBrush to set</param>
public void SetPaint(System.Drawing.Graphics graphic, System.Drawing.Brush brush)
{
if (this[graphic] != null)
((GraphicsProperties) this[graphic]).PaintBrush = brush;
else
{
GraphicsProperties tempProps = new GraphicsProperties();
tempProps.PaintBrush = brush;
Add(graphic, tempProps);
}
}
/// <summary>
/// Sets the GraphicBrush property to the given graphics object in the hashtable. If the element doesn't exist, then it adds the graphic element to the hashtable with the given GraphicBrush.
/// </summary>
/// <param name="graphic">Graphic element to search or add</param>
/// <param name="color">Color to set</param>
public void SetPaint(System.Drawing.Graphics graphic, System.Drawing.Color color)
{
System.Drawing.Brush brush = new System.Drawing.SolidBrush(color);
if (this[graphic] != null)
((GraphicsProperties) this[graphic]).PaintBrush = brush;
else
{
GraphicsProperties tempProps = new GraphicsProperties();
tempProps.PaintBrush = brush;
Add(graphic, tempProps);
}
}
/// <summary>
/// Gets the HatchBrush property to the given graphics object in the hashtable. If the element doesn't exist, then it returns Blank.
/// </summary>
/// <param name="graphic">Graphic element to search</param>
/// <returns>The HatchBrush setting of the graphic</returns>
public System.Drawing.Drawing2D.HatchBrush GetBrush(System.Drawing.Graphics graphic)
{
if (this[graphic] == null)
return new System.Drawing.Drawing2D.HatchBrush(System.Drawing.Drawing2D.HatchStyle.Plaid,System.Drawing.Color.Black,System.Drawing.Color.Black);
else
return new System.Drawing.Drawing2D.HatchBrush(System.Drawing.Drawing2D.HatchStyle.Plaid,((GraphicsProperties) this[graphic]).GraphicBrush.Color,((GraphicsProperties) this[graphic]).GraphicBrush.Color);
}
/// <summary>
/// Gets the HatchBrush property to the given graphics object in the hashtable. If the element doesn't exist, then it returns Blank.
/// </summary>
/// <param name="graphic">Graphic element to search</param>
/// <returns>The Brush setting of the graphic</returns>
public System.Drawing.Brush GetPaint(System.Drawing.Graphics graphic)
{
if (this[graphic] == null)
return new System.Drawing.Drawing2D.HatchBrush(System.Drawing.Drawing2D.HatchStyle.Plaid,System.Drawing.Color.Black,System.Drawing.Color.Black);
else
return ((GraphicsProperties) this[graphic]).PaintBrush;
}
/// <summary>
/// Sets the GraphicPen property to the given graphics object in the hashtable. If the element doesn't exist, then it adds the graphic element to the hashtable with the given Pen.
/// </summary>
/// <param name="graphic">Graphic element to search or add</param>
/// <param name="pen">Pen to set</param>
public void SetPen(System.Drawing.Graphics graphic, System.Drawing.Pen pen)
{
if (this[graphic] != null)
((GraphicsProperties) this[graphic]).GraphicPen = pen;
else
{
GraphicsProperties tempProps = new GraphicsProperties();
tempProps.GraphicPen = pen;
Add(graphic, tempProps);
}
}
/// <summary>
/// Gets the GraphicPen property to the given graphics object in the hashtable. If the element doesn't exist, then it returns Black.
/// </summary>
/// <param name="graphic">Graphic element to search</param>
/// <returns>The GraphicPen setting of the graphic</returns>
public System.Drawing.Pen GetPen(System.Drawing.Graphics graphic)
{
if (this[graphic] == null)
return System.Drawing.Pens.Black;
else
return ((GraphicsProperties) this[graphic]).GraphicPen;
}
/// <summary>
/// Sets the GraphicFont property to the given graphics object in the hashtable. If the element doesn't exist, then it adds the graphic element to the hashtable with the given Font.
/// </summary>
/// <param name="graphic">Graphic element to search or add</param>
/// <param name="Font">Font to set</param>
public void SetFont(System.Drawing.Graphics graphic, System.Drawing.Font font)
{
if (this[graphic] != null)
((GraphicsProperties) this[graphic]).GraphicFont = font;
else
{
GraphicsProperties tempProps = new GraphicsProperties();
tempProps.GraphicFont = font;
Add(graphic,tempProps);
}
}
/// <summary>
/// Gets the GraphicFont property to the given graphics object in the hashtable. If the element doesn't exist, then it returns Microsoft Sans Serif with size 8.25.
/// </summary>
/// <param name="graphic">Graphic element to search</param>
/// <returns>The GraphicFont setting of the graphic</returns>
public System.Drawing.Font GetFont(System.Drawing.Graphics graphic)
{
if (this[graphic] == null)
return new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
else
return ((GraphicsProperties) this[graphic]).GraphicFont;
}
/// <summary>
/// Sets the color properties for a given Graphics object. If the element doesn't exist, then it adds the graphic element to the hashtable with the color properties set with the given value.
/// </summary>
/// <param name="graphic">Graphic element to search or add</param>
/// <param name="color">Color value to set</param>
public void SetColor(System.Drawing.Graphics graphic, System.Drawing.Color color)
{
if (this[graphic] != null)
{
((GraphicsProperties) this[graphic]).GraphicPen.Color = color;
((GraphicsProperties) this[graphic]).GraphicBrush.Color = color;
((GraphicsProperties) this[graphic]).color = color;
}
else
{
GraphicsProperties tempProps = new GraphicsProperties();
tempProps.GraphicPen.Color = color;
tempProps.GraphicBrush.Color = color;
tempProps.color = color;
Add(graphic,tempProps);
}
}
/// <summary>
/// Gets the color property to the given graphics object in the hashtable. If the element doesn't exist, then it returns Black.
/// </summary>
/// <param name="graphic">Graphic element to search</param>
/// <returns>The color setting of the graphic</returns>
public System.Drawing.Color GetColor(System.Drawing.Graphics graphic)
{
if (this[graphic] == null)
return System.Drawing.Color.Black;
else
return ((GraphicsProperties) this[graphic]).color;
}
/// <summary>
/// This method gets the TextBackgroundColor of a Graphics instance
/// </summary>
/// <param name="graphic">The graphics instance</param>
/// <returns>The color value in ARGB encoding</returns>
public System.Drawing.Color GetTextBackgroundColor(System.Drawing.Graphics graphic)
{
if (this[graphic] == null)
return System.Drawing.Color.Black;
else
{
return ((GraphicsProperties) this[graphic]).TextBackgroundColor;
}
}
/// <summary>
/// This method set the TextBackgroundColor of a Graphics instace
/// </summary>
/// <param name="graphic">The graphics instace</param>
/// <param name="color">The System.Color to set the TextBackgroundColor</param>
public void SetTextBackgroundColor(System.Drawing.Graphics graphic, System.Drawing.Color color)
{
if (this[graphic] != null)
{
((GraphicsProperties) this[graphic]).TextBackgroundColor = color;
}
else
{
GraphicsProperties tempProps = new GraphicsProperties();
tempProps.TextBackgroundColor = color;
Add(graphic,tempProps);
}
}
/// <summary>
/// Structure to store properties from System.Drawing.Graphics objects
/// </summary>
class GraphicsProperties
{
public System.Drawing.Color TextBackgroundColor = System.Drawing.Color.Black;
public System.Drawing.Color color = System.Drawing.Color.Black;
public System.Drawing.Color BackColor = System.Drawing.Color.White;
public System.Drawing.Color TextColor = System.Drawing.Color.Black;
public System.Drawing.SolidBrush GraphicBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
public System.Drawing.Brush PaintBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
public System.Drawing.Pen GraphicPen = new System.Drawing.Pen(System.Drawing.Color.Black);
public System.Drawing.Font GraphicFont = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
}
}
}
/*******************************/
/// <summary>
/// This method returns an Array of System.Int32 containing the size of the non client area of a control.
/// The non client area includes elements such as scroll bars, borders, title bars, and menus.
/// </summary>
/// <param name="control">The control from which to retrieve the values.</param>
/// <returns>An Array of System.Int32 containing the width of each non client area border in the following order
/// top, left, right and bottom.</returns>
public static System.Int32[] GetInsets(System.Windows.Forms.Control control)
{
System.Int32[] returnValue = new System.Int32[4];
returnValue[0] = (control.RectangleToScreen(control.ClientRectangle).Top - control.Bounds.Top);
returnValue[1] = (control.RectangleToScreen(control.ClientRectangle).Left - control.Bounds.Left);
returnValue[2] = (control.Bounds.Right - control.RectangleToScreen(control.ClientRectangle).Right);
returnValue[3] = (control.Bounds.Bottom - control.RectangleToScreen(control.ClientRectangle).Bottom);
return returnValue;
}
/*******************************/
/// <summary>
/// Creates a new positive random number
/// </summary>
/// <param name="random">The last random obtained</param>
/// <returns>Returns a new positive random number</returns>
public static long NextLong(System.Random random)
{
long temporaryLong = random.Next();
temporaryLong = (temporaryLong << 32)+ random.Next();
if (random.Next(-1,1) < 0)
return -temporaryLong;
else
return temporaryLong;
}
}