基本原则
- Windows Mobile程序永远不由用户关闭,通过处理Activate和Deactive Event在前后台切换的时候处理使用的资源;通过处理Closing和Closed Event来处理被OS自行关闭的时候要处理的动作;通过设置MinimizeBox=true来将OK按钮替换成X按钮,点击X按钮会造成系统最小化 (Deactive)而不是关闭;
- 通过Project Property->Common Propertiies->General->Application Icon来设置程序的关联图标;
常用控件
- Form Control
对PocketPC中的Form,FormBorderStyle.None会得到一个无边框无TitleBar的窗口;FillBorderStyle.FixedSingle或其他值得到一个全屏化的窗口;
对WindowsCE 中的Form,FormBorderStyle.FixedDialog或FormBorderStyle.None会得到一个无边框无TitleBar 的窗口;FillBorderStyle.FixedSingle或其他值得到一个按照缺省的Size属性设置大小的窗口;
Form的ControlBox属性决定了最大/最小/X按钮的可见性;对于PocketPC,ControlBox属性最多包含一个按钮;WindowsCE可以包含多个按钮,单独按钮的可见性可以通过MinimizeBox和MaximizeBox属性设置;
Form 的WindowsState属性决定了窗口的初始视觉效果,只能设置为FormWindowState.Normal或 FormWindowState.Maximized;对于PocketPC来说这两个值都会造成程序充满屏幕,但是Normal会显示 StartMenu和MainMenu,Maximize则不显示;
对于PocketPC,只有FormBorderStyle.None的情况下Form的Size属性和Location属性才生效;对于WindowsCE则总是生效;
- Button Control(主要使用Click事件):
- TextBox Control支持BackColor、ForeColor、PasswordChar属性,支持KeyPress、KeyUp、KeyDown事件 (Directional Pad的各个对应键值是Keys.Up,Keys.Down,Keys.Left,Keys.Right,Keys.Return;),不支持Click 事件;
- Label Control主要使用Text属性,使用TextAlign属性控制显示的对齐方式,支持TextChanged事件;
- RadioButton Control位于同一个Container(例如Panel)中的时候被认为是同一组,当用户点击RadioButton的时候触发Click事件,当其选中状态变化的时候触发CheckedChanged事件;
- CheckBox Control主要使用CheckState属性(Unchecked、Checked、Indeterminate);当CheckState= Indeterminate且ThreeState=true的时候,CheckBox变灰并选中,这时如果AutoCheck=false则 CheckBox不响应用户点击、AutoCheck=true则用户点击会enable这个控件;
- ComboBox Control/ListBox Control的列表既可以在设计的时候通过Items属性修改,也可以在运行的时候通过Add/Remove/Clear方法修改—— comboBox1.Items.Add("Hi");ComboBox也可以绑定DataSource属性到一个Collection object,缺省情况下ComboxBox通过调用DataSource每个成员的ToString来生成要显示的字符串,可以通过ComboBox的 DisplayName属性来定制要显示的属性;ComboBox Control通过SelectedIndex/SelectedItem属性来获取选中的项目;
class Customer {
string m_First;
string m_Middle;
string m_Last;
public Customer(string first, string middle, string last) {
m_First = (first == null) ? string.Empty : first;
m_Middle = (middle == null) ? string.Empty : middle;
m_Last = (last == null) ? string.Empty : last;
}
public string FirstName {
get { return m_First; }
}
public string MiddleName {
get { return m_Middle; }
}
public string LastName {
get { return m_Last; }
}
static string FullNameWithInitial = "{0} {1}. {2}";
static string FullNameNoInitial = "{0} {1}";
public string FullName {
get {
return (m_Middle.Length > 0) ?
string.Format(FullNameWithInitial, m_First, m_Middle[0], m_Last) :
string.Format(FullNameNoInitial, m_First, m_Last);
}
}
}
private void LoadCustomers() {
if(customers != null)
return;
customers = new Customer[6];
customers[0] = new Customer("Ronnie", "Donnell", "Yates");
customers[1] = new Customer("Moya", "Alicia", "Hines");
customers[2] = new Customer("Veronica", "Christine", "Yates");
customers[3] = new Customer("Diane", "", "Taylor");
customers[4] = new Customer("Kindell", "Elisha", "Yates");
customers[5] = new Customer("Zion", "Donnell", "Yates");
this.comboBox1.DataSource = customers;
this.comboBox1.DisplayMember = "FullName";
}
...
string selItem = comboBox1.Items[comboBox1.SelectedIndex].ToString();
string selItem = comboBox1.SelectedItem.ToString();
...
- NumericUpDown Control只能接受整数,有上下限,常用属性为Minimum, Maximum, Value, Increment, ReadOnly, Text;如果用户输入的数据合法,Value==Text,否则Text=用户输入,Value=Maximum;支持ValueChanged事件 (只在通过代码或者上下箭头改变Value的时候触发,用户输入不触发这个事件)
- DomainUpDown Control类似NumericUpDown control但是显示的是一系列string而不是整数,主要使用Items和Text属性;Items的操作方法类似ComboBox;支持 TextChanged和SelectedIndexChanged事件(SelectedIndexChanged只在通过代码或者上下箭头改变的时候 触发,用户输入不触发这个事件);
- ProgressBar Control主要使用Minimum, Maximum, Value属性;
- StatusBar Control只能显示字符串,主要使用Text属性;
- TrackBar Control是一个滑动条,和ProgressBar类似,主要使用Minimum, Maximum, Value属性;Orientation属性控制滑动条的方向,TickFrequency属性控制tick mark的距离,SmallChange和LargeChange分别是键盘和点击的步进量;主要使用ValueChanged事件;
- ToolBar Control不能显示Text,使用ImageList Control来设置图标;要使用ToolBar遵循如下步骤:
添加ImageList Control,通过Images属性添加图标,图标会自动resize成16x16;
添加ToolBar Control,设置ImageList属性;
通过Button属性弹出的对话框添加按钮,Button的Style属性可以是DropDownButton、PushButton、Separator、ToggleButton
添加ButtonClick事件的handler,通过ToolBarButtonClickEventArgs.Button来判定哪个按钮被点击
private void
toolBar1_ButtonClick(object sender,
System.Windows.Forms.ToolBarButtonClickEventArgs e) {
if(e.Button == this.toolBarButton1) {
OpenFileDialog dlg = new OpenFileDialog();
if(dlg.ShowDialog() == DialogResult.OK) {
this.lblOpenFile.Text = dlg.FileName;
}
}
else if(e.Button == this.toolBarButton2) {
SaveFileDialog dlg = new SaveFileDialog();
if(dlg.ShowDialog() == DialogResult.OK ) {
this.lblSaveFile.Text = dlg.FileName;
}
}
}
- MainMenu Control可以包含MenuItem Control,主要触发Click事件;
MenuItem fileMenu = new MenuItem();
MenuItem newItem = new MenuItem();
MenuItem sepItem = new MenuItem();
MenuItem exitItem = new MenuItem();
fileMenu.Text = "File";
newItem.Text = "New";
sepItem.Text = "-";
exitItem.Text = "Exit";
fileMenu.MenuItems.Add(newItem);
fileMenu.MenuItems.Add(sepItem);
fileMenu.MenuItems.Add(exitItem);
mainMenu1.MenuItems.Add(fileMenu);
- ContextMenu Control和MainMenu几乎一样,但是MainMenu关联于Form,ContextMenu关联于其他Control;ContextMenu在显示前会触发Popup事件,在点击时会触发CLick事件;
ContextMenu cMenu = new ContextMenu();
MenuItem menuItem1 = new MenuItem();
MenuItem menuItem2 = new MenuItem();
menuItem1.Text = "Default Item 1";
menuItem2.Text = "Default Item 2";
// Add menuItem2 as a child of menuItem1
menuItem1.MenuItems.Add(this.menuItem2);
// Add menuItem1 to the context menu
cMenu.MenuItems.Add(this.menuItem1);
// Add the context menu to a label control
label1.ContextMenu = cMenu;
......
private void contextMenu1_Popup(object sender, System.EventArgs e) {
this.contextMenu1.MenuItems.Clear();
if(this.checkBox1.Checked)
this.contextMenu1.MenuItems.Add(this.menuItem1);
if(this.checkBox2.Checked)
this.contextMenu1.MenuItems.Add(this.menuItem2);
if(this.checkBox3.Checked)
this.contextMenu1.MenuItems.Add(this.menuItem3);
// Always add the default menu
this.contextMenu1.MenuItems.Add(this.menuItem4);
}
private void menuItem1_Click(object sender, System.EventArgs e) {
MessageBox.Show("You selected MenuItem 1");
}
......
- Timer Control允许你周期性触发事件,event handler在窗口代码的同一个Thread内运行;当Timer的Enabled=true的时候,每经过Interval指定的毫秒数,就触发Tick事件;
- OpenFileDialog 和SaveFileDialog Control,使用Filter属性设置显示的文件类型,可以通过改变FilterIndex属性(从1开始)来选择file filter;使用InitalDirectory属性来设置起始目录;通过ShowDialog方法来显示对话框;通过Filename属性返回选中的 文件名;
OpenFileDialog ofDlg = new OpenFileDialog();
ofDlg.Filter = "DLL|*.dll|Executable|*.exe";
ofDlg.IntialDirectory = "//My Documents";
if(DialogResult.OK == ofDlg.ShowDialog()) {
MessageBox.Show("You Selected " + ofDlg.FileName);
} else {
MessageBox.Show("Go ahead, select a file!");
}
- Panel Control是其他控件的容器,主要使用Add方法;
- HScrollBar 和VScrollBar Control,主要使用Minimum、Maximum(实际最边上的值是Maximum-LargeChange+1)、SmallChange、 LargeChange、Value;Value属性改变的时候会触发ValueChanged事件;
- ImageList Control中如果你想要16x16以外的大小,使用ImageSize属性;主要使用Images属性及其Add方法(支持System.Drawing.Icon和System.Drawing.Image);
- PictureBox Control用于显示图像,主要使用Image属性,但是只能在左上角显示且没有拉伸功能;Image可以从file system获得,也可以从resource获得,还可以从ImageList得到;
// resize the image
imageList1.ImageSize = new System.Drawing.Size(92, 156);
// load the resized image
pictureBox1.Image = imageList1.Images[0];
- ListView Control支持4中显示模式Details, LargeIcon, List, SmallIcon,通过View属性类设置;通过操作Columns来控制Details模式下的SubItems,既可以在设计的时候通过 Columns属性来设置,也可以在运行的时候生成;
System.Windows.Forms.ColumnHeader columnHeader1 = new ColumnHeader();
System.Windows.Forms.ColumnHeader columnHeader2 = new ColumnHeader();
System.Windows.Forms.ColumnHeader columnHeader3 = new ColumnHeader();
columnHeader1.Text = "Name";
columnHeader2.Text = "Purpose";
columnHeader3.Text = "Availability";
listView1.Columns.Add(columnHeader1);
listView1.Columns.Add(columnHeader2);
listView1.Columns.Add(columnHeader3);
ListView Control中的每一行都是一个ListViewItem,每个ListViewItem包含ListViewSubItem(通过SubItems属 性操作,数量=columns-1);ListView可以关联两个ImageList,SmallImageList和LargeImageList, 每个ListViewItem包含ImageIndex属性表示关联的图标;
ListViewItem listViewItem1 = new ListViewItem();
ListViewSubItem listViewSubItem1 = new ListViewSubItem();
ListViewSubItem listViewSubItem2 = new ListViewSubItem();
listViewItem1.Text = "Red Delicious";
listViewSubItem1.Text = "Snacking";
listViewSubItem2.Text = "All Year";
listViewItem1.SubItems.Add(listViewSubItem1);
listViewItem1.SubItems.Add(listViewSubItem2);
listView1.Items.Add(listViewItem1);
ListView Control的SelectedIndex变化的时候触发SelectedIndexChanged事件(每次改变选择实际上会触发两次事件,第一次表示前一个选择失效,第二次表示新选择);
private void listView1_SelectedIndexChanged(object sender,
System.EventArgs e) {
if(this.listView1.SelectedIndices.Count <= 0)
return;
int selNdx = this.listView1.SelectedIndices[0];
label3.Text = listView1.Items[selNdx].Text;
}
ListView Control还有几个重要属性CheckBoxes(改变CheckState的时候触发ItemCheck)、FullRowSelect、 HeaderStyle(Clickable会触发ColumnClick事件,Nonclickable不触发事件,None不显示column header);
- TabControl Control包含多个TabPage,可以通过TabPages属性操作;包含SelectIndex属性表示选择的TabPage,在改变的时候触发SelectedIndexChanged事件;
- TreeView Control具有Nodes属性,包含一系列TreeNode,每个TreeNode也包含Nodes属性;TreeView具有ImageList、 ImageIndex(可以通过TreeNode.ImageIndex对父层的设置进行Override)、SelectedImageIndex(可 以通过TreeNode.SelectedImageIndex对父层的设置进行Override)、ShowPlusMinus、ShowLines (在TreeNode间画线)、ShowRootLines(是否在TreeNode和Root之间画线)、CheckBoxes(对应的状态属性为 Checked,check node的操作会触发AfterCheck事件);TreeNode具有Text、ImageIndex、SelectedImageIndex属性;
TreeNode treeNode1 = new TreeNode();
TreeNode treeNode2 = new TreeNode();
treeNode1.Text = "Red Apples";
treeNode2.Text = "Red Delicious";
treeNode1.Nodes.Add(treeNode2);
treeView1.Nodes.Add(treeNode1);
TreeView Control具有SelectedNode属性,引用当前被选中的TreeNode,选中TreeNode会触发AfterSelect事件, TreeViewEventArgs包含了Node和Action(表示触发事件的原因:ByKeyboard、ByMouse、Collapse、 Expand、Unknown)属性
private void
treeView1_AfterSelect(object sender,
System.Windows.Forms.TreeViewEventArgs e) {
TreeNode selNode = e.Node;
label2.Text = selNode.Text;
}