




已阅读5页,还剩133页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
2020 1 27 1 第6章Windows应用程序开发 2020 1 27 2 6 1Windows应用的基本概念6 2事件驱动的概念6 3Windows应用程序的开发步骤6 4Windows应用程序控件 第6章Windows应用程序开发 2020 1 27 3 6 1Windows应用的基本概念 C 可以为Windows操作系统生成多种应用 其中最基本的两类应用 基于控制台的应用 MS DOS应用程序 输出包含在控制台窗口中的 但窗口中只显示文本字符 基于GUI GraphicsUserInterface 图形用户界面 的应用 WinForm和WebForm 基于GUI的应用采用图形方式 用对话框作为用户界面 借助菜单 按钮等标准的Windows元素和鼠标操作 帮助实现用户和应用程序之间交流 GUI已经成为几乎所有应用软件的事实标准 如Word Excel等等 关键在于窗体 控件 事件驱动模型等概念的理解和掌握 2020 1 27 4 6 1 1Windows应用的面向对象性 在C 中 Windows窗体应用程序是面向对象编程技术的一个重要组成部分 Windows窗体中所有的内容都是按照面向对象编程技术来构建的 预定义或自定义 窗体和控件 对象 属性 对象特征 一种特殊的方法 方法 对象操作 事件 对对象的请求 能够被识别和响应的动作 2020 1 27 5 6 1 2Windows应用的相关基类 C NET提供了一系列与WinForm相关的类 主要包含在System Windows Forms命名空间中 用于供开发人员使用 Object MarshalByRefObject Component Control Form 2020 1 27 6 6 1 2Windows应用的相关基类 其中 Control类为定义窗体及控件的基类 Form为窗体类 用于构建窗体对象 其他标准Windows控件类均派生于Control类 用于构建窗体控件对象 Control类具有非常全面的属性 方法和事件常用属性 BackColor BackgroundImage Enabled Focused Font ForeColor Location Name Size Text Visible 常用方法 Focus Select Refresh Show ToString 常用事件 Click DoubleClick Enter GotFouse KeyDown KeyPress KeyUp Leave LostFocus Move 鼠标事件 2020 1 27 7 6 1 3Windows应用的基本界面 通过Windows窗体设计器 可以开发出各种形式Windows应用程序 使其具有不同的外观 不同的结构 窗体设计器的使用非常简单 拖放控件 修改控件属性 添加控件事件处理代码 例如在图所示的窗体中添加一个标签控件 将其Text属性修改为 HelloWorld 2020 1 27 8 各种控件 属性 放置控件的区域 6 1 3Windows应用的基本界面 2020 1 27 9 鼠标键盘事件示例 6 1 3Windows应用的基本界面 2020 1 27 10 6 1 3Windows应用的基本界面 2020 1 27 11 6 1 4Windows应用的输入 输出 在前两个文本框中输入数据 在最后一个文本框中显示结果 可以通过Windows控件实现数据的输入与输出 从而获取用户输入的数据和向用户显示数据 Control类的属性 Text 2020 1 27 12 消息驱动 Windows系统 用户动作键盘输入 消息循环队列 窗口处理函数 函数1回调 函数n回调 事件驱动 Windows系统 用户动作键盘输入 注册 事件侦听 类 监听处理 函数1 事件处理 函数n 事件处理 6 1 5Windows应用的事件处理 2020 1 27 13 消息驱动和消息循环 被动处理 消息队列 Windows操作系统时刻监视着用户的每个举动 一旦发生某个事件 Windows即发送WM COMMAND消息给相应的应用程序 进入应用程序的消息循环队列 应用程序时刻等着消息的到来 一旦发现它的消息队列中有未处理的信息 就获取并分析该消息 并根据消息所包含的内容采取适当的动作来响应 消息驱动 应用程序提供相应的消息处理程序 回调函数 给Windows 由Windows调用该函数实现消息处理 Windows为每个线程维护了相应的消息队列 线程的任务就是不停地从特定的消息队列中获取消息 分析消息并处理消息 直到消息处理完 WM QUIT 为止 这个过程称为 消息循环 6 1 5Windows应用的事件处理 2020 1 27 14 事件驱动和事件循环 主动侦听 事件广播 不同类 或对象 根据实际情况对事件进行注册 表示将对其感兴趣的事件进行侦听 Windows操作系统时刻监视着用户的每个举动 一旦发生某个事件 Windows对事件进行分析 并把事件发送给相应的应用程序类 类对注册的事件进行侦听 一旦侦听到相应事件后 即对事件进行分析 并根据事件所包含的内容采取适当的动作来响应 启动事件处理代码 事件驱动 6 1 5Windows应用的事件处理 2020 1 27 15 6 2事件驱动的概念 事件驱动 程序的执行依靠事件的发生进行控制 是面向对象程序设计 OOP 的程序执行方式 程序员不必设计程序执行的精确顺序 在程序运行中通过用户激发事件来安排程序执行顺序 实现真正意义上的人机交互 顺序驱动 程序根据程序员的事先安排顺序执行 完成某项具体任务 是结构化程序设计 SP 的程序执行方式 C 的WinForm应用程序是OOP和SP的结合 从总体上说 是基于事件驱动的 在每个事件处理过程中 又是基于顺序驱动 2020 1 27 16 6 2事件驱动的概念 NET的WinForm应用程序是由Windows窗体及其控件所组成的 而窗体和控件都是系统预定义的类 WinForm开发的实质就是在窗体 对象 上合理的安排各种控件 对象 修改对象的外部特征 属性 为对象的各类事件编写相应事件处理代码 方法 控件名 事件名 窗体以及控件的事件模型都是系统预定义的 不同控件的事件集合也不同 按钮 控件的Click事件 窗体 控件的Load事件等 2020 1 27 17 6 3Windows应用程序的开发步骤 NET中提供了一系列用于编写基于Windows应用程序的类 其中最主要的是窗体类和控件类 窗体 WindowsForm 用于创建应用程序所需的GUI 可以是窗体 Form 对话框 Dialog 或者MDI MultipleDocumentInterface 多文档界面 控件 Control 用于实现应用程序的具体功能 包括文本框 组合框 按钮等等 窗体类和控件类包含在System Windows Forms命名空间中 2020 1 27 18 6 3Windows应用程序的开发步骤 设计和实现Windows应用程序的步骤如下 创建窗体 Form 添加控件 根据应用程序需要 添加各种控件 属性设置 指定各个控件在窗体中的布局 Layout 通过属性设置描述各个控件的外部特征 响应事件 定义GUI的事件处理代码 不同的控件 窗体存在不同的事件 设置各个控件的不同事件的处理过程 实现对指定控件事件的响应 2020 1 27 19 开始 程序 MicrosoftVisualStudio2005 MicrosoftVisualStudio2005 1 创建WinForms应用程序 1 2020 1 27 20 创建WinForms应用程序 2 设计窗口 2020 1 27 21 usingSystem usingSystem Collections Generic usingSystem ComponentModel usingSystem Data usingSystem Drawing usingSystem Text usingSystem Windows Forms namespaceFirstWinForm publicpartialclassForm1 Form publicForm1 InitializeComponent 提供了大量绘图工具的访问权限 基础核心命名空间 ArrayList BitArray Hashtable Stack StringCollection和StringTable类 大量窗体和控件 从System Windows Forms Form派生 VisualStudio NET生成的代码 创建WinForms应用程序 3 2020 1 27 22 privatevoidInitializeComponent this SuspendLayout Form1 this AutoScaleDimensions newSystem Drawing SizeF 6F 12F this AutoScaleMode System Windows Forms AutoScaleMode Font this ClientSize newSystem Drawing Size 292 266 this Name Form1 this Text Form1 this Load newSystem EventHandler this Form1 Load this ResumeLayout false 构造函数调用InitializeComponent 方法在Form1 Designer cs文件中 创建WinForms应用程序 4 2020 1 27 23 清理所有正在使用的资源 如果应释放托管资源 为true 否则为false protectedoverridevoidDispose booldisposing if disposing 释放系统资源 创建WinForms应用程序 5 2020 1 27 24 usingSystem usingSystem Collections Generic usingSystem Windows Forms namespaceFirstWinForm staticclassProgram 应用程序的主入口点 STAThread staticvoidMain Application EnableVisualStyles Application SetCompatibleTextRenderingDefault false Application Run newForm1 程序的主入口点在Program cs文件中 创建WinForms应用程序 6 2020 1 27 25 创建WinForms应用程序 7 privatevoidInitializeComponent this button1 newSystem Windows Forms Button this SuspendLayout button1this button1 Dock System Windows Forms DockStyle Bottom this button1 Location newSystem Drawing Point 0 236 this button1 Name button1 this button1 Size newSystem Drawing Size 292 30 this button1 TabIndex 0 this button1 Text 第一个按钮 this button1 UseVisualStyleBackColor true Form1this AutoScaleDimensions newSystem Drawing SizeF 6F 12F this AutoScaleMode System Windows Forms AutoScaleMode Font this ClientSize newSystem Drawing Size 292 266 this Controls Add this button1 this Name Form1 this Text Form1 this ResumeLayout false 2020 1 27 26 创建WinForms应用程序 7 publicpartialclassForm1 Form publicForm1 InitializeComponent privatevoidForm1 MouseMove objectsender MouseEventArgse Pointp Cursor Position 定义一个点对象 用来获取当前鼠标所在点的坐标this Text X Convert ToString p X Y Convert ToString p Y privatevoidbutton1 Click objectsender EventArgse MessageBox Show HelloFirstButton 2020 1 27 27 用户信息录入界面示例 1 采用Windows应用程序模板创建一个Windows应用程序 初始界面如图所示 2020 1 27 28 用户信息录入界面示例 2 在窗体界面中添加控件 本例中包括4个标签 3个文本框和一个按钮 如图所示 2020 1 27 29 用户信息录入界面示例 3 修改窗体及控件的属性 调整界面布局 设置控件的外观特征 如图所示 2020 1 27 30 用户信息录入界面示例 4 设置控件的事件响应方法 编写事件响应代码 如图所示 2020 1 27 31 用户信息录入界面示例 5 执行Windows应用程序 由用户激发各个事件 应用程序监听事件 并根据事件处理代码处理用户的各种操作 如图所示 2020 1 27 32 6 4Windows应用程序控件 Form是由一个个控件有机构成的 因此熟悉控件是进行合理 有效地程序开发的重要前提 2020 1 27 33 可视化界面组件统称为控件 System Windows Forms Control WinForms中的常用控件 2020 1 27 34 标签 按钮 组合框 列表框 文本框 WinForms中的常用控件 例 2020 1 27 35 6 4 1文本类控件 文本类控件主要用于获取用户输入或显示文本 文本类控件有两种 一种是能够进行编辑的 如TextBox 另外一种不能编辑 主要应用于文本显示 如Label 文本显示控件则包括Label和LinkedLabel Label显示用户无法直接编辑的描述性文本 常用作标题或提示等 LinkedLabel则将文本显示为Web样式的链接 并在用户单击该特殊文本时触发事件 以实现到另一个窗口或Web站点的链接 2020 1 27 36 标签 2020 1 27 37 标签 例 2020 1 27 38 6 4 1文本类控件 文本编辑控件包括TextBox RichTextBox和MastedTextBox 能够接受用户的输入 可以做为用户与应用程序之间的交互 TextBox显示设计时输入的文本 它可由用户在运行时编辑或以编程方式更改 RichTextBox使文本能够以纯文本或RTF格式显示 MastedTextBox主要用于控制输入文本的格式 如果输入的内容不满足规定的格式 控件不会接受 也称为掩码文本框 2020 1 27 39 文本框 2020 1 27 40 文本框 例 2020 1 27 41 文本框 例2 事件 TextChanged属性 Text方法 Copy和Paste 2020 1 27 42 按钮类控件可以发布命令或者设置值 主要包括Button CheckBox和RadioBox ButtonButton用来启动 停止或中断程序运行过程 Button通常要处理的是用户按下该按钮的事件 例如 最简单的动作是按下一个按钮后关闭窗体 其代码如下所示 privatevoidbutton1 Click objectsender System EventArgse this Close 6 4 2按钮类控件 2020 1 27 43 Button 2020 1 27 44 Button 例 2020 1 27 45 Button 例2 基于Button和RichTextBox控件开发一个文本编辑器 用于 打开文本保存文本查找文本控制文本 2020 1 27 46 Button 例2 2020 1 27 47 Button 例2 2020 1 27 48 Button 例2 2020 1 27 49 CheckBoxCheckBox由一个复选框和一个文本标签组成 通常用来设置选项 指示某个特定条件是处于打开状态还是处于关闭状态 因此通常处理的是CheckedChanged事件 即当选项发生变化时 该事件发生 例如 一个CheckBox用来表示是否允许窗体极大化 当它的值被改变时 根据用户的选择来允许或禁止窗体极大化的代码如下所示 privatevoidchkDisableMax CheckedChanged objectsender System EventArgse this MaximizeBox chkDisableMax Checked 6 4 2按钮类控件 2020 1 27 50 CheckBox 例 2020 1 27 51 CheckBox 例2 设计一个窗体 含有3个复选框 用于控制窗体外观 2020 1 27 52 CheckBox 例2 privatevoidCB Color CheckedChanged objectsender EventArgse if this CB Color Checked this BackColor Color LightBlue elsethis BackColor Color LightGreen privatevoidCB All CheckedChanged objectsender EventArgse if this CB All Checked this CB Color Checked true this CB FontSize Checked true else this CB Color Checked false this CB FontSize Checked false privatevoidCB FontSize CheckedChanged objectsender EventArgse if this CB FontSize Checked this Font newFont this Font FontFamily Name 15 this Font Style elsethis Font newFont this Font FontFamily Name 10 this Font Style CheckBoxs属性 Checked 是否选中 CheckState 状态CheckedIndeterminateUnchecked 2020 1 27 53 RadioBoxRadioBox显示一个可打开或关闭的按钮 常见的用法是将几个单选框分为一组 在该组中每次只能选中其中的一个 根据用户的选择来决定具体的操作 在一个容器 如Panel控件 GroupBox控件或窗体 内绘制单选按钮即可将它们分组 直接添加到一个窗体中的所有单选按钮将形成一个组 若要添加不同的组 必须将它们放到面板或分组框中 例如 有两个选择数据库的RadioBox 一个表示选择OleDB数据库 另一个表示选择SQLServer数据库 则最简单的示意代码如下 6 4 2按钮类控件 2020 1 27 54 privatevoidOledbB CheckedChanged objectsender System EventArgse if OledbB Checked MessageBox Show 你目前选中的是Oledb供应程序 elseMessageBox Show 你目前选中的是Sql供应程序 6 4 2按钮类控件 2020 1 27 55 RadioBox 例1 2020 1 27 56 RadioBox 例2 2020 1 27 57 RadioBox 例3 2020 1 27 58 RadioBox 例3 privatevoidcheckBox1 CheckedChanged objectsender EventArgse stringtext foreach objectchkboxinthis groupBox1 Controls if chkboxisCheckBox if CheckBox chkbox Checked text CheckBox chkbox Text 循环获取已经被选中的控件的Text属性 if text this label1 Text 兴趣爱好有 text Remove text Length 1 1 显示输出elsethis label1 Text 没有被选中的内容 privatevoidradioButton1 CheckedChanged objectsender EventArgse RadioButtonrb senderasRadioButton if rb Checked this label2 Text 性格为 rb Text 利用Label2显示输出 2020 1 27 59 RadioBox 例3 this radioButton1 CheckedChanged newSystem EventHandler this radioButton1 CheckedChanged this radioButton2 CheckedChanged newSystem EventHandler this radioButton1 CheckedChanged this radioButton3 CheckedChanged newSystem EventHandler this radioButton1 CheckedChanged this checkBox1 CheckedChanged newSystem EventHandler this checkBox1 CheckedChanged this checkBox2 CheckedChanged newSystem EventHandler this checkBox1 CheckedChanged this checkBox3 CheckedChanged newSystem EventHandler this checkBox1 CheckedChanged this checkBox4 CheckedChanged newSystem EventHandler this checkBox1 CheckedChanged 2020 1 27 60 这一类的控件用于从列表中选择数据 包括 ListBox 显示一个文本项和图形项 图标 列表 CheckedListBox 显示一个带有复选框的项列表 ComboBox 显示一个下拉式项列表和一个文本编辑框 它相当于一个ListBox加上一个TextBox 用户既可以从下拉列表中选择一个已有的项 也可以直接在TextBox中编辑输入新的选项 DomainUpDown 显示用户可用向上和向下按钮滚动的文本项列表 6 4 3列表型控件 2020 1 27 61 NumericUpDown 显示用户可用向上和向下按钮滚动的数字项列表 ListView 在四个不同视图之一中显示项 这些视图包括纯文本视图 带有小图标的文本视图 带有大图标的文本视图和详细信息视图 TreeView 显示一个节点对象的分层集合 这些节点对象由带有可选复选框或图标的文本组成 DataGridView 显示表格 6 4 3列表型控件 2020 1 27 62 ListBox列表框 2020 1 27 63 ListBox 例1 2020 1 27 64 ListBox 例1 privatevoidbutton1 Click objectsender System EventArgse if this listBox1 SelectedItem null this listBox2 Items Add this listBox1 SelectedItem this listBox1 Items Remove this listBox1 SelectedItem privatevoidbutton2 Click objectsender System EventArgse if this listBox2 SelectedItem null this listBox1 Items Add this listBox2 SelectedItem this listBox2 Items Remove this listBox2 SelectedItem 2020 1 27 65 ListBox 例2 2020 1 27 66 ListBox 例2 privatevoidbtn Add Click objectsender EventArgse stringaddedText this textBoxNew Text Trim if addedText Length在列表中已经存在 elsethis listBoxALL Items Add addedText this textBoxNew Clear privatevoidbtn Delete Click objectsender EventArgse while this listBoxALL SelectedIndex 0 this listBoxALL Items RemoveAt this listBoxALL SelectedIndex privatevoidbtn DeleteAll Click objectsender EventArgse this listBoxALL Items Clear 2020 1 27 67 CheckedListBox列表框 2020 1 27 68 CheckedListBox 例1 2020 1 27 69 CheckedListBox 例1 privatevoidbutton3 Click objectsender System EventArgse stringmessage foreach objectitemobjinthis checkedListBox1 CheckedItems message itemobj ToString MessageBox Show message 已经选择上 2020 1 27 70 CheckedListBox 例2 2020 1 27 71 CheckedListBox 例2 privatevoidbuttonOK Click objectsender EventArgse stringmystr 您所选择的课程是 intcheckNumber this checkedListBox1 CheckedItems Count if checkNumber 0 MessageBox Show 您还没有选择任何课程 else for inti 0 i checkNumber i mystr this checkedListBox1 CheckedItems i if mystr mystr Length 1 mystr mystr Substring 0 mystr Length 1 mystr MessageBox Show mystr 2020 1 27 72 CheckedListBox 例2 privatevoidbuttonRe Click objectsender EventArgse for inti 0 i this checkedListBox1 Items Count i this checkedListBox1 SetItemChecked i false this checkedListBox1 SetSelected i false this checkedListBox1 SetItemChecked 0 true this checkedListBox1 SetItemChecked 3 true this checkedListBox1 SetItemChecked 6 true 2020 1 27 73 ComboBox组合框 2020 1 27 74 ComboBox 例1 2020 1 27 75 ComboBox 例1 privatevoidcomboBox1 SelectedIndexChanged objectsender System EventArgse MessageBox Show 选择的是第 boBox1 SelectedIndex 1 ToString 选择的信息 MessageBox Show 选择的职务是 boBox1 Text 选择的信息 privatevoidForm2 Load objectsender System EventArgse boBox1 Items Add 总裁 boBox1 Items Add 副总裁 boBox1 Items Add 首席执行官 boBox1 Items Add 经理 boBox1 Items Add 副经理 boBox1 Items Add 组长 boBox1 Items Add 副组长 默认的选择是 副总裁 boBox1 SelectedIndex 1 2020 1 27 76 ComboBox 例2 2020 1 27 77 ComboBox 例2 privatevoidcmbName SelectedIndexChanged objectsender EventArgse if this cmbName Text 2020 1 27 78 ComboBox 例2 privatevoidbtnAdd Click objectsender EventArgse if this cmbName Text Trim boolnewitem true for inti 0 i this cmbName Items Count i if this cmbName Items i ToString this cmbName Text newitem false break if newitem this cmbName Items Add this cmbName Text privatevoidbtnRead Click objectsender EventArgse this richTextBox1 LoadFile d source rtf RichTextBoxStreamType RichText 2020 1 27 79 其他列表型控件例 2020 1 27 80 其他列表型控件例 privatevoidForm3 Load objectsender System EventArgse this domainUpDown1 SelectedIndex 2 this numericUpDown1 Minimum 100 this numericUpDown1 Maximum 200 TreeNodenodeobj newTreeNode nodeobj Nodes Add 南京市 nodeobj Nodes Add 苏州市 nodeobj Nodes Add 无锡市 nodeobj Nodes Add 常州市 nodeobj Text 江苏省 this treeView1 Nodes Add nodeobj nodeobj null nodeobj newTreeNode nodeobj Nodes Add 杭州市 nodeobj Nodes Add 宁波市 nodeobj Nodes Add 金华市 nodeobj Nodes Add 温州市 nodeobj Text 浙江省 this treeView1 Nodes Add nodeobj nodeobj null 2020 1 27 81 日期时间类控件用于日期型和时间类数据的设置 包括DateTimePicker MonthCalendar和Timer DateTimePicker 以组合框的方式显示日期 并提供了一个图形日历以允许用户选择日期或时间 常用属性 format MaxDate MinDate value等 MonthCalendar 显示一个图形日历以允许用户选择日期范围 常用属性 firstdayofweek MaxDate MinDate value todaydate等 6 4 4日期时间类控件 2020 1 27 82 日期时间类控件例 2020 1 27 83 日期时间类控件例 privatevoiddateTimePicker1 ValueChanged objectsender System EventArgse this label1 Text 选择的时间是 this dateTimePicker1 Value privatevoidmonthCalendar1 DateChanged objectsender System Windows Forms DateRangeEventArgse this monthCalendar1 TodayDate this monthCalendar1 SelectionStart this label1 Text 现在的时间是 this monthCalendar1 TodayDate 2020 1 27 84 还有一个日期时间类控件在GUI中不可见 为Timer 它相当于一个定时器 常常用于需要自动处理的场合 Timer是按标准时间间隔引发事件的控件 该控件是为Windows窗体环境设计的 常用属性包括 Interval 引发事件的时间间隔长度 其值以毫秒为单位 若启用了该控件 则每个时间间隔引发一个Tick事件 可以为该事件添加时间处理代码 Start方法 打开计时器 Stop方法 关闭计时器 计时器在关闭时重置 不存在暂停Timer控件的方法 6 4 4日期时间类控件 2020 1 27 85 日期时间类控件例2 2020 1 27 86 日期时间类控件例2 privatevoidtimer1 Tick objectsender System EventArgse this tabControl1 SelectedIndex i i if i 4 0 i 0 privatevoidForm5 Load objectsender System EventArgse this timer1 Start 2020 1 27 87 日期时间类控件例3 2020 1 27 88 日期时间类控件例3 privatevoidbutton1 Click objectsender EventArgse this timer1 Enabled this timer1 Enabled this button1 Text this timer1 Enabled 停止 开始 privatevoidtimer1 Tick objectsender EventArgse this label1 Text DateTime Now ToString 2020 1 27 89 对话框控件包含了系统定义的几个标准对话框 如颜色选择 字体选择 打开文件 存储文件 打印预览 页面设置 打印等 它们的名字和功能如下 ColorDialog 显示允许用户设置界面元素颜色的颜色选择对话框 FontDialog 显示允许用户设置字体及其属性的对话框 OpenFileDialog 显示允许用户定位文件和选择文件的对话框 SaveFileDialog 显示允许用户保存文件的对话框 PrintDialog 显示允许用户选择打印机并设置其属性的对话框 PrintPreviewDialog 显示PrintDocument对象打印时的样子 PageSetUpDialog 显示允许用户设置打印页面属性的对话框 6 4 5对话框类控件 2020 1 27 90 标准对话框的使用 可以通过窗体设计器拖动一个对话框控件到窗体中 它们在GUI中是不可见的 也可以自己编程定义一个标准对话框的变量并将其实例化 6 4 5对话框类控件 2020 1 27 91 对话框类控件例1 usingSystem IO 2020 1 27 92 对话框类控件例1 privatevoidbutton2 Click objectsender System EventArgse this MySaveFileDialog Filter 文本文档 txt txt this MySaveFileDialog CreatePrompt true this MySaveFileDialog OverwritePrompt true this MySaveFileDialog RestoreDirectory true if this MySaveFileDialog ShowDialog DialogResult OK StreamWritersw newStreamWriter this MySaveFileDialog FileName false sw WriteLine this textBox1 Text sw Close this label1 Text 状态 你要保存的文件为 this MySaveFileDialog FileName 2020 1 27 93 对话框类控件例2 2020 1 27 94 对话框类控件例2 privatevoidbutton1 Click objectsender System EventArgse MyColorDialog newColorDialog MyColorDialog AllowFullOpen true MyColorDialog AnyColor true if this MyColorDialog ShowDialog DialogResult OK this textBox1 ForeColor this MyColorDialog Color this label1 Text 状态 将文本框中文字颜色设置为 this MyColorDialog Color ToString 2020 1 27 95 对话框类控件例3 2020 1 27 96 对话框类控件例3 2020 1 27 97 对话框类控件例3 privatevoidbtnOpen Click objectsender EventArgse OpenFileDialogopenFileDialog newOpenFileDialog 实例化OpenFileDialog对话框openFileDialog InitialDirectory c 设置初始路径openFileDialog Filter 文本文件 txt C 文件 cs 所有文件 设置格式过滤openFileDialog RestoreDirectory true openFileDialog FilterIndex 1 if openFileDialog ShowDialog DialogResult OK 选中 OK 按钮 并返回this richTextBox1 LoadFile openFileDialog FileName privatevoidbtnFont Click objectsender EventArgse FontDialogfontDialog newFontDialog 实例化Font对话框对象fontDialog Font richTextBox1 Font fontDialog Color richTextBox1 ForeColor fontDialog AllowScriptChange true fontDialog ShowColor true if fontDialog ShowDialog DialogResult Cancel richTextBox1 Font fontDialog Font 设定RichTextBox的字体richTextBox1 ForeColor fontDialog Color 设定颜色 2020 1 27 98 6 4 6图形类控件 图形类控件包括用于图形显示的PictureBox和用于图形存储的ImageList PictureBox在一个框架中显示图形文件 如位图和图标 常用属性包括image sizemode ImageList用作图像的储存库 该控件及其包含的图像能够在应用程序之间重复使用 例如 在一个名为pictureBox1的PictureBox中打开一个jpg文件并进行显示的代码如下所示 2020 1 27 99 图形类控件例子 privatevoidpictureBox1 Click objectsender System EventArgse 打开目录对话框 选中图片文件OpenFileDialogfodlg newOpenFileDialog fodlg Title 选择图片 fodlg InitialDirectory 文件过滤类型fodlg Filter Allfiles Imagefiles jpg bmp gif jpg bmp gif fodlg FilterIndex 1 缺省过滤文件类型if fodlg ShowDialog DialogResult OK pictureBox1 Image Image FromFile fodlg FileName 设置图像控件的大小和位置pictureBox1 Size pictureBox1 Image Size pictureBox1 Top this Size Height pictureBox1 Size Height 3 pictureBox1 Left this Size Width pictureBox1 Size Width 2 Sizemode属性 2020 1 27 100 图形类控件例子 2020 1 27 101 6 4 7分组类控件 分组类控件用于将其他控件分组 包括Panel GroupBox和TabControl Panel将一组控件分组到未标记 可滚动的框架中 GroupBox将一组控件 如单选按钮 RadioButton 分组到带标记 不可滚动的框架中 TabControl提供一个选项卡式页面以有效地组织和访问已分组对象 扩大了窗体的容纳面积 2020 1 27 102 分组类控件例子 2020 1 27 103 6 4 8标尺类控件 标尺类控件并不是一个标准的名称 在这里我们指的是与标尺有关的一
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年农业产业集群产业组织结构与市场竞争力研究报告
- 2025年新能源行业专利分析报告:技术创新引领应用场景新突破
- 电商平台本地生活服务与餐饮业合作模式研究报告
- 2025年绿色建筑行业信息化建设与发展趋势报告
- 2025重庆市铜梁区少云镇公益性岗位招聘1人考试参考题库及答案解析
- 数字人民币跨境支付技术挑战与跨境支付合规性建设解决方案报告
- 2025年宿州国企公开招聘见习生备考考试题库附答案解析
- 标准园林绿化工程合同范本解析
- 2025山东东营市实验小学招聘劳务派遣教师12人考试参考题库及答案解析
- 电动汽车充电焦虑症缓解策略:2025年用户体验深度研究报告
- 协会工资薪酬管理制度
- 办公烟酒领用管理制度
- CJ/T 233-2006建筑小区排水用塑料检查井
- 淀粉大型设备管理制度
- T/CSPCI 00001-2022汽油中苯胺类化合物的分离和测定固相萃取/气相色谱-质谱法
- 考评员聘用协议书
- 近代中国体育思想的嬗变轨迹与时代特征探寻
- DB31T 1373-2022 海三棱藨草种群生态修复技术规程
- 常见精神科药物的副作用及其处理
- 《农业科技创新政策》课件
- GB/T 2684-2025铸造用砂及混合料试验方法
评论
0/150
提交评论