
本人刚接触C#窗体,请高手讲解一下以下代码的作用(详细),谢谢 30
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Da...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace MyPhotos
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
SetTitleBar();
menuView.DropDown = ctxMenuPhoto;
}
private void SetTitleBar()
{
Version ver = new Version(Application.ProductVersion);
Text = String.Format("MyPhotos {0:0}.{1:0}",
ver.Major, ver.Minor);
}
foreach (ToolStripMenuItem item in parent.DropDownItems)
{
item.Enabled = (pbxPhoto.Image != null);
item.Checked = item.Tag.Equals(enumVal);
}
}
}
private void menuFileLoad_Click(object sender, System.EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Title = "Load Photo";
dlg.Filter = "jpg files (*.jpg)"
+ "|*.jpg|All files (*.*)|*.*";
if (dlg.ShowDialog() == DialogResult.OK)
{
try
{
pbxPhoto.Image = new Bitmap(dlg.OpenFile());
}
catch (ArgumentException ex)
{
MessageBox.Show("Unable to load file: "
+ ex.Message);
}
}
dlg.Dispose(); }
private void menuFileExit_Click(object sender, EventArgs e)
{
Close();
}
private void menuImage_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
ProcessImageClick(e);
}
private void ProcessImageClick(ToolStripItemClickedEventArgs e)
{
ToolStripItem item = e.ClickedItem;
string enumVal = item.Tag as string;
if (enumVal != null)
{
pbxPhoto.SizeMode = (PictureBoxSizeMode)
Enum.Parse(typeof(PictureBoxSizeMode),
enumVal);
}
}
private void menuImage_DropDownOpening(object sender, EventArgs e)
{
ProcessImageOpening(sender as ToolStripDropDownItem);
}
private void ProcessImageOpening(ToolStripDropDownItem parent)
{
if (parent != null)
{
string enumVal = pbxPhoto.SizeMode.ToString();
foreach (ToolStripMenuItem item in parent.DropDownItems)
{
item.Enabled = (pbxPhoto.Image != null);
item.Checked = item.Tag.Equals(enumVal);
}
}
}
}
} 展开
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace MyPhotos
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
SetTitleBar();
menuView.DropDown = ctxMenuPhoto;
}
private void SetTitleBar()
{
Version ver = new Version(Application.ProductVersion);
Text = String.Format("MyPhotos {0:0}.{1:0}",
ver.Major, ver.Minor);
}
foreach (ToolStripMenuItem item in parent.DropDownItems)
{
item.Enabled = (pbxPhoto.Image != null);
item.Checked = item.Tag.Equals(enumVal);
}
}
}
private void menuFileLoad_Click(object sender, System.EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Title = "Load Photo";
dlg.Filter = "jpg files (*.jpg)"
+ "|*.jpg|All files (*.*)|*.*";
if (dlg.ShowDialog() == DialogResult.OK)
{
try
{
pbxPhoto.Image = new Bitmap(dlg.OpenFile());
}
catch (ArgumentException ex)
{
MessageBox.Show("Unable to load file: "
+ ex.Message);
}
}
dlg.Dispose(); }
private void menuFileExit_Click(object sender, EventArgs e)
{
Close();
}
private void menuImage_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
ProcessImageClick(e);
}
private void ProcessImageClick(ToolStripItemClickedEventArgs e)
{
ToolStripItem item = e.ClickedItem;
string enumVal = item.Tag as string;
if (enumVal != null)
{
pbxPhoto.SizeMode = (PictureBoxSizeMode)
Enum.Parse(typeof(PictureBoxSizeMode),
enumVal);
}
}
private void menuImage_DropDownOpening(object sender, EventArgs e)
{
ProcessImageOpening(sender as ToolStripDropDownItem);
}
private void ProcessImageOpening(ToolStripDropDownItem parent)
{
if (parent != null)
{
string enumVal = pbxPhoto.SizeMode.ToString();
foreach (ToolStripMenuItem item in parent.DropDownItems)
{
item.Enabled = (pbxPhoto.Image != null);
item.Checked = item.Tag.Equals(enumVal);
}
}
}
}
} 展开
2个回答
展开全部
/// 下面这几行是命名空间的引用,就是说后面的代码会使用到这些命名空间的类。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
/// 程序的命名空间
namespace MyPhotos
{
/// 定义一人派生自Form类的窗体类,该类的代码可以包含在两个或多个cs文件中(partial注明了这一特性)
public partial class MainForm : Form
{
/// 构造函数。
public MainForm()
{
// 初始化成员
InitializeComponent();
// 设置标题栏。
SetTitleBar();
// 菜单下拉的事件。
menuView.DropDown = ctxMenuPhoto;
}
/// 设置标题栏。
private void SetTitleBar()
{
// 获取程序版本对象。
Version ver = new Version(Application.ProductVersion);
Text = String.Format("MyPhotos {0:0}.{1:0}",
ver.Major, ver.Minor);
}
// 设置每个菜单项是否禁用,是否选中。
foreach (ToolStripMenuItem item in parent.DropDownItems)
{
item.Enabled = (pbxPhoto.Image != null);
item.Checked = item.Tag.Equals(enumVal);
}
}
}
/// 装载菜单项单击事件。
private void menuFileLoad_Click(object sender, System.EventArgs e)
{
// 创建一个打开文件对话框对象。
OpenFileDialog dlg = new OpenFileDialog();
// 对放框标题。
dlg.Title = "Load Photo";
// 对话框显示的扩展名。
dlg.Filter = "jpg files (*.jpg)"
+ "|*.jpg|All files (*.*)|*.*";
// 如果对话框OK按钮被单击。
if (dlg.ShowDialog() == DialogResult.OK)
{
// 捕捉异常
try
{
// 根据选中的文件创建位图对象,并赋给位图显示控件。
pbxPhoto.Image = new Bitmap(dlg.OpenFile());
}
// 处理特定异常。
catch (ArgumentException ex)
{
MessageBox.Show("Unable to load file: "
+ ex.Message);
}
}
// 销毁对话框对象。
dlg.Dispose(); }
/// 退出菜单单击事件。
private void menuFileExit_Click(object sender, EventArgs e)
{
// 关闭当前窗口。
Close();
}
/// 菜单下拉项被单击。
private void menuImage_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
ProcessImageClick(e);
}
/// 处理图片被单击的事件。
private void ProcessImageClick(ToolStripItemClickedEventArgs e)
{
ToolStripItem item = e.ClickedItem;
string enumVal = item.Tag as string;
if (enumVal != null)
{
// 根据字符串分析出的枚举值设置图片显示格式。
pbxPhoto.SizeMode = (PictureBoxSizeMode)
Enum.Parse(typeof(PictureBoxSizeMode),
enumVal);
}
}
/// 下拉菜单弹出来的时候的事件。
private void menuImage_DropDownOpening(object sender, EventArgs e)
{
ProcessImageOpening(sender as ToolStripDropDownItem);
}
/// 处理下拉菜单弹出来的事件。
private void ProcessImageOpening(ToolStripDropDownItem parent)
{
if (parent != null)
{
// 根据图片当前的显示形式来更新菜单项的选中状态。
string enumVal = pbxPhoto.SizeMode.ToString();
foreach (ToolStripMenuItem item in parent.DropDownItems)
{
item.Enabled = (pbxPhoto.Image != null);
item.Checked = item.Tag.Equals(enumVal);
}
}
}
}
}
注:似乎是很简单的基本代码。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
/// 程序的命名空间
namespace MyPhotos
{
/// 定义一人派生自Form类的窗体类,该类的代码可以包含在两个或多个cs文件中(partial注明了这一特性)
public partial class MainForm : Form
{
/// 构造函数。
public MainForm()
{
// 初始化成员
InitializeComponent();
// 设置标题栏。
SetTitleBar();
// 菜单下拉的事件。
menuView.DropDown = ctxMenuPhoto;
}
/// 设置标题栏。
private void SetTitleBar()
{
// 获取程序版本对象。
Version ver = new Version(Application.ProductVersion);
Text = String.Format("MyPhotos {0:0}.{1:0}",
ver.Major, ver.Minor);
}
// 设置每个菜单项是否禁用,是否选中。
foreach (ToolStripMenuItem item in parent.DropDownItems)
{
item.Enabled = (pbxPhoto.Image != null);
item.Checked = item.Tag.Equals(enumVal);
}
}
}
/// 装载菜单项单击事件。
private void menuFileLoad_Click(object sender, System.EventArgs e)
{
// 创建一个打开文件对话框对象。
OpenFileDialog dlg = new OpenFileDialog();
// 对放框标题。
dlg.Title = "Load Photo";
// 对话框显示的扩展名。
dlg.Filter = "jpg files (*.jpg)"
+ "|*.jpg|All files (*.*)|*.*";
// 如果对话框OK按钮被单击。
if (dlg.ShowDialog() == DialogResult.OK)
{
// 捕捉异常
try
{
// 根据选中的文件创建位图对象,并赋给位图显示控件。
pbxPhoto.Image = new Bitmap(dlg.OpenFile());
}
// 处理特定异常。
catch (ArgumentException ex)
{
MessageBox.Show("Unable to load file: "
+ ex.Message);
}
}
// 销毁对话框对象。
dlg.Dispose(); }
/// 退出菜单单击事件。
private void menuFileExit_Click(object sender, EventArgs e)
{
// 关闭当前窗口。
Close();
}
/// 菜单下拉项被单击。
private void menuImage_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
ProcessImageClick(e);
}
/// 处理图片被单击的事件。
private void ProcessImageClick(ToolStripItemClickedEventArgs e)
{
ToolStripItem item = e.ClickedItem;
string enumVal = item.Tag as string;
if (enumVal != null)
{
// 根据字符串分析出的枚举值设置图片显示格式。
pbxPhoto.SizeMode = (PictureBoxSizeMode)
Enum.Parse(typeof(PictureBoxSizeMode),
enumVal);
}
}
/// 下拉菜单弹出来的时候的事件。
private void menuImage_DropDownOpening(object sender, EventArgs e)
{
ProcessImageOpening(sender as ToolStripDropDownItem);
}
/// 处理下拉菜单弹出来的事件。
private void ProcessImageOpening(ToolStripDropDownItem parent)
{
if (parent != null)
{
// 根据图片当前的显示形式来更新菜单项的选中状态。
string enumVal = pbxPhoto.SizeMode.ToString();
foreach (ToolStripMenuItem item in parent.DropDownItems)
{
item.Enabled = (pbxPhoto.Image != null);
item.Checked = item.Tag.Equals(enumVal);
}
}
}
}
}
注:似乎是很简单的基本代码。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询