这是“使用 C# 开发智能手机软件:推箱子” 系列文章的第二十篇。在这篇文章中,介绍 Window/DesignDlg.cs 源程序文件。这个源程序文件包含 DesignDlg 类,该类继承自 System.Windows.Forms.Form 类,表示推箱子的“设计”对话框。如下图所示:
我们先看看 DesignDlg.Designer.cs 的部分源程序代码吧:
1 namespace Skyiv.Ben.PushBox.Window
2 {
3 partial class DesignDlg
4 {
5 private void InitializeComponent()
6 {
7 // 注意:省略了一些代码
8
9 this.rbnCreate.CheckedChanged += new System.EventHandler(this.Action_CheckedChanged);
10 this.rbnEdit.CheckedChanged += new System.EventHandler(this.Action_CheckedChanged);
11 this.rbnDelete.CheckedChanged += new System.EventHandler(this.Action_CheckedChanged);
12 this.tbxDeleteLevel.TextChanged += new System.EventHandler(this.tbxDeleteLevel_TextChanged);
13 }
14
15 private System.Windows.Forms.NumericUpDown nudWidth;
16 private System.Windows.Forms.NumericUpDown nudHeight;
17 private System.Windows.Forms.RadioButton rbnCreate;
18 private System.Windows.Forms.RadioButton rbnEdit;
19 private System.Windows.Forms.RadioButton rbnDelete;
20 private System.Windows.Forms.Button btnOk;
21 private System.Windows.Forms.Button btnCancel;
22 private System.Windows.Forms.CheckBox chkCopy;
23 private System.Windows.Forms.TextBox tbxDeleteLevel;
24 private System.Windows.Forms.TextBox tbxLevel;
25 }
26 }
2 {
3 partial class DesignDlg
4 {
5 private void InitializeComponent()
6 {
7 // 注意:省略了一些代码
8
9 this.rbnCreate.CheckedChanged += new System.EventHandler(this.Action_CheckedChanged);
10 this.rbnEdit.CheckedChanged += new System.EventHandler(this.Action_CheckedChanged);
11 this.rbnDelete.CheckedChanged += new System.EventHandler(this.Action_CheckedChanged);
12 this.tbxDeleteLevel.TextChanged += new System.EventHandler(this.tbxDeleteLevel_TextChanged);
13 }
14
15 private System.Windows.Forms.NumericUpDown nudWidth;
16 private System.Windows.Forms.NumericUpDown nudHeight;
17 private System.Windows.Forms.RadioButton rbnCreate;
18 private System.Windows.Forms.RadioButton rbnEdit;
19 private System.Windows.Forms.RadioButton rbnDelete;
20 private System.Windows.Forms.Button btnOk;
21 private System.Windows.Forms.Button btnCancel;
22 private System.Windows.Forms.CheckBox chkCopy;
23 private System.Windows.Forms.TextBox tbxDeleteLevel;
24 private System.Windows.Forms.TextBox tbxLevel;
25 }
26 }
下面来看看 DesignDlg.cs 的源程序代码吧:
1 using System;
2 using System.Drawing;
3 using System.Windows.Forms;
4 using Skyiv.Ben.PushBox.Common;
5
6 namespace Skyiv.Ben.PushBox.Window
7 {
8 /// <summary>
9 /// “设计”对话框
10 /// </summary>
11 public partial class DesignDlg : Form
12 {
13 Action active;
14 int level;
15 int maxLevel;
16 Size levelSize;
17
18 public Action Active { get { return active; } }
19 public bool IsCopy { get { return chkCopy.Checked; } }
20 public int Level { set { level = value; } }
21 public int MaxLelvel { set { maxLevel = value; } }
22
23 public DesignDlg(bool isTopMost)
24 {
25 InitializeComponent();
26 TopMost = isTopMost;
27 }
28
29 protected override void OnLoad(EventArgs e)
30 {
31 base.OnLoad(e);
32 tbxLevel.Text = (level + 1).ToString() + "/" + maxLevel.ToString();
33 rbnDelete.Enabled = (level == maxLevel - 1 && maxLevel > 1);
34 Action_CheckedChanged(rbnCreate, null);
35 }
36
37 public Size LevelSize
38 {
39 get
40 {
41 return (active == Action.Create) ? new Size((int)nudWidth.Value, (int)nudHeight.Value) : levelSize;
42 }
43 set
44 {
45 levelSize = value;
46 if (value.Width > nudWidth.Maximum) nudWidth.Maximum = value.Width;
47 if (value.Height > nudHeight.Maximum) nudHeight.Maximum = value.Height;
48 nudWidth.Value = value.Width;
49 nudHeight.Value = value.Height;
50 }
51 }
52
53 public int MaxLevelSize
54 {
55 set
56 {
57 nudWidth.Maximum = (nudWidth.Value > value) ? nudWidth.Value : value;
58 nudHeight.Maximum = (nudHeight.Value > value) ? nudHeight.Value : value;
59 }
60 }
61
62 private void Action_CheckedChanged(object sender, EventArgs e)
63 {
64 RadioButton s = (RadioButton)sender;
65 if (s == rbnCreate && s.Checked)
66 {
67 active = Action.Create;
68 chkCopy.Enabled = nudWidth.Enabled = nudHeight.Enabled = true;
69 tbxDeleteLevel.ReadOnly = true;
70 btnOk.Enabled = true;
71 }
72 if (s == rbnEdit && s.Checked)
73 {
74 active = Action.Edit;
75 chkCopy.Enabled = nudWidth.Enabled = nudHeight.Enabled = false;
76 tbxDeleteLevel.ReadOnly = true;
77 btnOk.Enabled = true;
78 }
79 else if (s == rbnDelete && s.Checked)
80 {
81 active = Action.Delete;
82 chkCopy.Enabled = nudWidth.Enabled = nudHeight.Enabled = false;
83 tbxDeleteLevel.ReadOnly = false;
84 tbxDeleteLevel.Text = string.Empty;
85 btnOk.Enabled = (level + 1).ToString() == tbxDeleteLevel.Text;
86 }
87 }
88
89 private void tbxDeleteLevel_TextChanged(object sender, EventArgs e)
90 {
91 btnOk.Enabled = (level + 1).ToString() == tbxDeleteLevel.Text;
92 }
93 }
94 }
2 using System.Drawing;
3 using System.Windows.Forms;
4 using Skyiv.Ben.PushBox.Common;
5
6 namespace Skyiv.Ben.PushBox.Window
7 {
8 /// <summary>
9 /// “设计”对话框
10 /// </summary>
11 public partial class DesignDlg : Form
12 {
13 Action active;
14 int level;
15 int maxLevel;
16 Size levelSize;
17
18 public Action Active { get { return active; } }
19 public bool IsCopy { get { return chkCopy.Checked; } }
20 public int Level { set { level = value; } }
21 public int MaxLelvel { set { maxLevel = value; } }
22
23 public DesignDlg(bool isTopMost)
24 {
25 InitializeComponent();
26 TopMost = isTopMost;
27 }
28
29 protected override void OnLoad(EventArgs e)
30 {
31 base.OnLoad(e);
32 tbxLevel.Text = (level + 1).ToString() + "/" + maxLevel.ToString();
33 rbnDelete.Enabled = (level == maxLevel - 1 && maxLevel > 1);
34 Action_CheckedChanged(rbnCreate, null);
35 }
36
37 public Size LevelSize
38 {
39 get
40 {
41 return (active == Action.Create) ? new Size((int)nudWidth.Value, (int)nudHeight.Value) : levelSize;
42 }
43 set
44 {
45 levelSize = value;
46 if (value.Width > nudWidth.Maximum) nudWidth.Maximum = value.Width;
47 if (value.Height > nudHeight.Maximum) nudHeight.Maximum = value.Height;
48 nudWidth.Value = value.Width;
49 nudHeight.Value = value.Height;
50 }
51 }
52
53 public int MaxLevelSize
54 {
55 set
56 {
57 nudWidth.Maximum = (nudWidth.Value > value) ? nudWidth.Value : value;
58 nudHeight.Maximum = (nudHeight.Value > value) ? nudHeight.Value : value;
59 }
60 }
61
62 private void Action_CheckedChanged(object sender, EventArgs e)
63 {
64 RadioButton s = (RadioButton)sender;
65 if (s == rbnCreate && s.Checked)
66 {
67 active = Action.Create;
68 chkCopy.Enabled = nudWidth.Enabled = nudHeight.Enabled = true;
69 tbxDeleteLevel.ReadOnly = true;
70 btnOk.Enabled = true;
71 }
72 if (s == rbnEdit && s.Checked)
73 {
74 active = Action.Edit;
75 chkCopy.Enabled = nudWidth.Enabled = nudHeight.Enabled = false;
76 tbxDeleteLevel.ReadOnly = true;
77 btnOk.Enabled = true;
78 }
79 else if (s == rbnDelete && s.Checked)
80 {
81 active = Action.Delete;
82 chkCopy.Enabled = nudWidth.Enabled = nudHeight.Enabled = false;
83 tbxDeleteLevel.ReadOnly = false;
84 tbxDeleteLevel.Text = string.Empty;
85 btnOk.Enabled = (level + 1).ToString() == tbxDeleteLevel.Text;
86 }
87 }
88
89 private void tbxDeleteLevel_TextChanged(object sender, EventArgs e)
90 {
91 btnOk.Enabled = (level + 1).ToString() == tbxDeleteLevel.Text;
92 }
93 }
94 }
几点说明:
- “设计”对话框用来选择“新建”、“编辑”、“删除”推箱子游戏的关卡。
- 如果当前关不是最后一关,“删除”单选按钮将不可选(变灰),这是由该类的 OnLoad 方法中的以下语句实现的:
rbnDelete.Enabled = (level == maxLevel - 1 && maxLevel > 1); - 如果选择“删除”的话,必须输入要删除的关数用以确认,否则“确定”按钮将无法点击(变灰),这是由该类的 tbxDeleteLevel_TextChanged 方法实现的,该方法只有一条语句:
btnOk.Enabled = (level + 1).ToString() == tbxDeleteLevel.Text; - 如果不是选择“新建”的话,将无法输入“宽度”和“高度”,这是由该类的 Action_CheckedChanged 实现的。
- 这个类只是一个用户界面,负责按受用户所选择的“设计”动作和相应的参数,实际进行设计工作的代码将在下篇文章中介绍。
上一篇:使用 C# 开发智能手机软件:推箱子(十九)
下一篇:使用 C# 开发智能手机软件:推箱子(二十一)
返回目录