-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathHelloWorldPaint.cs
More file actions
78 lines (65 loc) · 3.63 KB
/
HelloWorldPaint.cs
File metadata and controls
78 lines (65 loc) · 3.63 KB
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
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Examples {
class Form1 : Form {
public static void Main() {
Application.Run(new Form1());
}
public Form1() {
this.panel.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right;
this.panel.BackColor = SystemColors.Window;
this.panel.Bounds = new Rectangle(20, 20, 290, 60);
this.panel.Font = new Font(this.panel.Font.FontFamily.Name, 22.0f);
this.panel.Paint += new PaintEventHandler(this.OnPanelPaint);
this.panel.SizeChanged += new EventHandler(this.OnPanelSizeChanged);
this.textBox.Anchor = AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right;
this.textBox.Location = new Point(20, 90);
this.textBox.Text = "Hello, World !";
this.textBox.Width = 290;
this.timer.Interval = 60;
this.timer.Tick += new EventHandler(this.OnTimer);
this.timer.Enabled = true;
this.ClientSize = new Size(330, 130);
this.Controls.AddRange(new Control[] {this.panel, this.textBox});
this.Text = "Wiggly";
}
protected void OnPanelPaint(object sender, PaintEventArgs e) {
Point pos = new Point((e.ClipRectangle.Size.Width - (int)e.Graphics.MeasureString(this.textBox.Text, this.panel.Font).Width) / 2, (e.ClipRectangle.Size.Height - (int)e.Graphics.MeasureString(this.textBox.Text, this.panel.Font).Height) / 2);
for (int i = 0; i < this.textBox.Text.Length; i++) {
int index = (this.step + i) % sin.Length;
e.Graphics.DrawString(this.textBox.Text[i].ToString(), this.panel.Font, new SolidBrush(HsbToRgb((float)(15 - index) * 16 / 255 * 360, 1.0f, 0.75f)), new Point(pos.X, pos.Y + sin[index] * (e.ClipRectangle.Height - this.panel.Font.Height) / 400));
pos.X += (int)e.Graphics.MeasureString(string.Format("{0}", this.textBox.Text[i]), this.panel.Font).Width - 9;
}
}
protected void OnPanelSizeChanged(object sener, EventArgs e) {
this.panel.Refresh();
}
protected void OnTimer(object sender, EventArgs e) {
this.step++;
this.panel.Refresh();
}
private static readonly int[] sin = { 0, 38, 71, 92, 100, 92, 71, 38, 0, -38, -71, -92, -100, -92, -71, -38 };
private Panel panel = new Panel();
private TextBox textBox = new TextBox();
private Timer timer = new Timer();
private int step = 0;
private static Color HsbToRgb(float hue, float saturation, float brightness) {
if (saturation == 0)
return Color.FromArgb(255, (byte)(brightness * 255.0), (byte)(brightness * 255.0), (byte)(brightness * 255.0));
hue = hue == 360 ? 0 : hue / 60;
float f = hue - (float)Math.Truncate(hue);
float p = brightness * (1.0f - saturation);
float q = brightness * (1.0f - (saturation * f));
float t = brightness * (1.0f - (saturation * (1.0f - f)));
switch ((int)Math.Truncate(hue)) {
case 0: return Color.FromArgb(255, (byte)(brightness * 255.0), (byte)(t * 255.0), (byte)(p * 255.0));
case 1: return Color.FromArgb(255, (byte)(q * 255.0), (byte)(brightness * 255.0), (byte)(p * 255.0));
case 2: return Color.FromArgb(255, (byte)(p * 255.0), (byte)(brightness * 255.0), (byte)(t * 255.0));
case 3: return Color.FromArgb(255, (byte)(p * 255.0), (byte)(q * 255.0), (byte)(brightness * 255.0));
case 4: return Color.FromArgb(255, (byte)(t * 255.0), (byte)(p * 255.0), (byte)(brightness * 255.0));
default: return Color.FromArgb(255, (byte)(brightness * 255.0), (byte)(p * 255.0), (byte)(q * 255.0));
}
}
}
}