相册程序(09.2.24)
今天开始构建一个相册程序,相关功能将逐步实现,敬请留言我的博客。谢谢指教......
目标:1、熟悉Win Form编程;
2、菜单,上下文菜单和状态栏的使用以及涉及的类结构;
目标:1、熟悉Win Form编程;
2、菜单,上下文菜单和状态栏的使用以及涉及的类结构;
3、方法的封装,动态显示图片框和状态栏的显示模式
1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Drawing;
6
using System.Text;
7
using System.Windows.Forms;
8![]()
9
namespace MyPhotos
10
{
11
public partial class MainForm : Form
12
{
13
// MainForm构造函数
14
public MainForm()
15
{
16
InitializeComponent();
17
// 菜单和上下文菜单的连接
18
memuView.DropDown = ctxMenuPhoto;
19
20
}
21
22
// /打开图片
23
private void menuFileLoad_Click(object sender, EventArgs e)
24
{
25
openFileDialog1.Title = "Load Photos";
26
openFileDialog1.Filter = "jpg files (*.jpg)" + "|*.jpg|All files(*.*)|*.*";
27
openFileDialog1.InitialDirectory = "C:\\Documents and Settings\\Administrator\\My Documents"; // 默认路径
28
if (openFileDialog1.ShowDialog() == DialogResult.OK)
29
{
30
try
31
{
32
pbxPhoto.Image = new Bitmap(openFileDialog1.OpenFile());
33
// 显示状态栏
34
SetStatusStrip(openFileDialog1.FileName);
35
}
36
catch (ArgumentException ex) //ArgumentException 在向方法提供的其中一个参数无效时引发的异常。
37
{
38
MessageBox.Show("Unable to load file:" + ex.Message);
39
}
40
openFileDialog1.Dispose();
41
}
42
}
43![]()
44
// 清除图片
45
private void menuFileClear_Click(object sender, EventArgs e)
46
{
47
pbxPhoto.Image = null;
48
}
49![]()
50
// 退出程序
51
private void menuFileExit_Click(object sender, EventArgs e)
52
{
53
Close();
54
}
55![]()
56
// 单击下拉菜单时触发
57
private void menuImage_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
58
{
59
//MessageBox.Show("DropDownItemClicked");
60
ProcessImageClick(e);
61
}
62![]()
63
// 实现ProcessImageClick方法,根据所选的菜单项更改图片框控件的SizeMode属性
64
private void ProcessImageClick(ToolStripItemClickedEventArgs e) // 接收ImageClick事件参数
65
{
66
ToolStripItem item = e.ClickedItem; // 定义被单击的项
67
string enumVal = item.Tag as string; // 把Tag(Object类)转换成字符
68
if (enumVal != null)
69
{
70
// 把字符转换成SizeMode值,
71
pbxPhoto.SizeMode = (PictureBoxSizeMode)Enum.Parse(typeof(PictureBoxSizeMode), enumVal);
72
}
73
}
74![]()
75
// 当与一个项相关联的下拉列表即将显示时就会发生的事件
76
private void menuImage_DropDownOpening(object sender, EventArgs e)
77
{
78
//MessageBox.Show("DropDownOpening");
79
ProcessImageOpening(sender as ToolStripDropDownItem);
80
}
81![]()
82
// 实现ProcessImageOpening方法,设置给定的下拉列表仲每个菜单项的Enabled和Checked属性
83
private void ProcessImageOpening(ToolStripDropDownItem parent)
84
{
85
if (parent != null)
86
{
87
string enumVal = pbxPhoto.SizeMode.ToString(); // 获得当前枚举的字符串形式
88
foreach (ToolStripMenuItem item in parent .DropDownItems )
89
{
90
item.Enabled = (pbxPhoto.Image != null);
91
item.Checked = item.Tag.Equals(enumVal);
92
}
93
}
94
}
95![]()
96
// ///封装状态条变化的逻辑方法
97
private void SetStatusStrip(string path) // 接受文件的路径为唯一参数
98
{
99
if (pbxPhoto.Image != null)
100
{
101
statusInfo.Text = path;
102
// 显示图像大小
103
statusImageSize.Text = String.Format("{0:#}×{1:#}", pbxPhoto.Image.Width, pbxPhoto.Image.Height);
104
}
105
else
106
// 如果没有图像则设置为空白
107
{
108
statusInfo.Text = null;
109
statusImageSize.Text = null;
110
statusAlbumPos.Text = null;
111
}
112
}
113
114
// 设置下拉菜单的DropDownButton项目,动态修改边框风格
115
private void statusBorder_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
116
{
117
ToolStripItem item = e.ClickedItem; // 定义被单击的项
118
string enumVal = item.Tag as string; // 把Tag(Object类)转换成字符
119
if (enumVal != null)
120
{
121
// 把字符转换成BorderStyle值,
122
statusImageSize.BorderStyle = (Border3DStyle)Enum.Parse(typeof(Border3DStyle), enumVal);
123
}
124
}
125![]()
126
}
127
}
using System;2
using System.Collections.Generic;3
using System.ComponentModel;4
using System.Data;5
using System.Drawing;6
using System.Text;7
using System.Windows.Forms;8

9
namespace MyPhotos10
{11
public partial class MainForm : Form12
{13
// MainForm构造函数14
public MainForm()15
{16
InitializeComponent();17
// 菜单和上下文菜单的连接18
memuView.DropDown = ctxMenuPhoto;19
20
}21
22
// /打开图片23
private void menuFileLoad_Click(object sender, EventArgs e)24
{25
openFileDialog1.Title = "Load Photos";26
openFileDialog1.Filter = "jpg files (*.jpg)" + "|*.jpg|All files(*.*)|*.*";27
openFileDialog1.InitialDirectory = "C:\\Documents and Settings\\Administrator\\My Documents"; // 默认路径28
if (openFileDialog1.ShowDialog() == DialogResult.OK)29
{30
try31
{32
pbxPhoto.Image = new Bitmap(openFileDialog1.OpenFile());33
// 显示状态栏34
SetStatusStrip(openFileDialog1.FileName);35
}36
catch (ArgumentException ex) //ArgumentException 在向方法提供的其中一个参数无效时引发的异常。 37
{38
MessageBox.Show("Unable to load file:" + ex.Message);39
}40
openFileDialog1.Dispose();41
}42
}43

44
// 清除图片45
private void menuFileClear_Click(object sender, EventArgs e)46
{47
pbxPhoto.Image = null;48
}49

50
// 退出程序51
private void menuFileExit_Click(object sender, EventArgs e)52
{53
Close();54
}55

56
// 单击下拉菜单时触发57
private void menuImage_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)58
{59
//MessageBox.Show("DropDownItemClicked");60
ProcessImageClick(e);61
}62

63
// 实现ProcessImageClick方法,根据所选的菜单项更改图片框控件的SizeMode属性64
private void ProcessImageClick(ToolStripItemClickedEventArgs e) // 接收ImageClick事件参数65
{66
ToolStripItem item = e.ClickedItem; // 定义被单击的项67
string enumVal = item.Tag as string; // 把Tag(Object类)转换成字符68
if (enumVal != null)69
{70
// 把字符转换成SizeMode值,71
pbxPhoto.SizeMode = (PictureBoxSizeMode)Enum.Parse(typeof(PictureBoxSizeMode), enumVal);72
}73
}74

75
// 当与一个项相关联的下拉列表即将显示时就会发生的事件76
private void menuImage_DropDownOpening(object sender, EventArgs e)77
{78
//MessageBox.Show("DropDownOpening");79
ProcessImageOpening(sender as ToolStripDropDownItem);80
}81

82
// 实现ProcessImageOpening方法,设置给定的下拉列表仲每个菜单项的Enabled和Checked属性83
private void ProcessImageOpening(ToolStripDropDownItem parent)84
{85
if (parent != null)86
{87
string enumVal = pbxPhoto.SizeMode.ToString(); // 获得当前枚举的字符串形式88
foreach (ToolStripMenuItem item in parent .DropDownItems )89
{90
item.Enabled = (pbxPhoto.Image != null); 91
item.Checked = item.Tag.Equals(enumVal);92
}93
}94
}95

96
// ///封装状态条变化的逻辑方法97
private void SetStatusStrip(string path) // 接受文件的路径为唯一参数98
{99
if (pbxPhoto.Image != null)100
{101
statusInfo.Text = path;102
// 显示图像大小103
statusImageSize.Text = String.Format("{0:#}×{1:#}", pbxPhoto.Image.Width, pbxPhoto.Image.Height);104
}105
else106
// 如果没有图像则设置为空白107
{108
statusInfo.Text = null;109
statusImageSize.Text = null;110
statusAlbumPos.Text = null;111
}112
}113
114
// 设置下拉菜单的DropDownButton项目,动态修改边框风格115
private void statusBorder_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)116
{117
ToolStripItem item = e.ClickedItem; // 定义被单击的项118
string enumVal = item.Tag as string; // 把Tag(Object类)转换成字符119
if (enumVal != null)120
{121
// 把字符转换成BorderStyle值,122
statusImageSize.BorderStyle = (Border3DStyle)Enum.Parse(typeof(Border3DStyle), enumVal);123
}124
}125

126
}127
}运行结果:
小评:学好业务的封装和结合系统的动态需求编写合适的代码


浙公网安备 33010602011771号