创建特定形状的窗体.PPT

上传人:国*** 文档编号:386495 上传时间:2018-09-30 格式:PPT 页数:46 大小:607.50KB
下载 相关 举报
创建特定形状的窗体.PPT_第1页
第1页 / 共46页
创建特定形状的窗体.PPT_第2页
第2页 / 共46页
创建特定形状的窗体.PPT_第3页
第3页 / 共46页
创建特定形状的窗体.PPT_第4页
第4页 / 共46页
创建特定形状的窗体.PPT_第5页
第5页 / 共46页
点击查看更多>>
资源描述

1、创建特定形状的窗体,DEMO1,2,添加窗体的Paint事件处理,/GraphicsPath表示一系列相互连接的直线和曲线 System.Drawing.Drawing2D.GraphicsPath shape = new System.Drawing.Drawing2D.GraphicsPath();shape.AddEllipse(0, 0, this.Width, this.Height);/Control.Region 属性获取或设置与控件关联的窗口区域。 this.Region = new System.Drawing.Region(shape);,创建不规则窗体,DEMO2,4,步

2、骤1,(1)创建Windows 应用程序项目,将其属性设置为移除标题栏并使用位图作为窗体背景。BackgroundImage属性:位图FormBorderStyle属性:NoneTransparencyKey属性:设为要隐藏的颜色,5,步骤2,(2)输入重新创建标题栏所提供功能的代码,例如移动和关闭窗体。为窗体添加数据成员,记录鼠标状态:private Point mouseOffset;private bool isMouseDown = false;,6,步骤3,(3)添加窗体的MouseDown事件处理:private void Form1_MouseDown(object sender

3、, MouseEventArgs e) int xOffset; int yOffset; if (e.Button = MouseButtons.Left) /计算和窗体左上角的偏离量 xOffset = -e.X - SystemInformation.FrameBorderSize.Width; yOffset = -e.Y - SystemInformation.CaptionHeight - SystemInformation.FrameBorderSize.Height; mouseOffset = new Point(xOffset, yOffset); isMouseDown

4、= true; ,7,添加窗体的MouseMove事件处理,private void Form1_MouseMove(object sender, MouseEventArgs e) if (isMouseDown) /获取鼠标光标的位置(以屏幕坐标表示)。 Point mousePos = Control.MousePosition;/将此 Point 平移指定的量。 mousePos.Offset(mouseOffset.X, mouseOffset.Y); Location = mousePos; ,8,添加窗体的MouseUp事件处理,private void Form1_MouseU

5、p(object sender, MouseEventArgs e) if (e.Button = MouseButtons.Left) isMouseDown = false; ,MaskedTextBox控件,DEMO3,10,MaskedTextBox控件(P51),MaskedTextBox控件,也叫掩码文本框。它的主要作用是控制输入文本的格式。如果输入的内容不满足规定的格式,则控件不会接收该输入。 如下图是从工具箱向窗体拖动该控件的图示。,11,1、常用的基本属性(1)InsertKeyMode属性:指示向掩码文本框输入字符时的输入模式,其属性值有:Default、Insert和Ov

6、erwrite3种。当属性值为Default时,表示输入模式由当时键盘的插入/改写状态决定,如果输入时键盘处于改写模式,则会改写输入处的字符,否则为插入字符。当属性值为Insert时,即使键盘的Ins键被按下,也不会以改写方式输入字符。当属性值为Overwrite时,则任何时候都是以改写方式输入字符。,12,(2)PromptChar属性:指定作为占位符的字符,用于指示用户需要输入的字符长度,默认的占位符为下划线“_”,需要修改时可以直接在属性后的空白处输入指定字符。,13,(3)Mask属性:设置当前掩码文本框输入字符的格式。单击Mask属性后的按钮,,14,弹出如图所示的窗口,其中是一些设

7、置好的时间、电话号码的格式,若这些格式都不能满足设计要求,也可以选择自定义格式,或者在Mask属性后的空白处直接输入自定义格式。,15,16,自定义输入格式时,可以使用掩码和分隔符两类符号。掩码用于限制用户可输入的符号类型,程序运行时掩码以占位符显示;而分隔符可作为输入字符之间的关联符,分隔符显示在掩码文本框中,且不可修改。表5-3列出了常用掩码和分隔符的含义。,17,表5-3 常用的掩码和分隔符,18,掩码不能保证用户输入一定会表示给定类型的有效值,例如,输入的年龄值可能为 -9。通过将值的类型的实例赋给 ValidatingType 属性,可以确保用户输入表示一个有效值。通过监视 Type

8、ValidationCompleted 事件,可以检测当 MaskedTextBox 包含无效值时,用户是否将焦点从该控件移开。如果键入验证成功,可以通过 TypeValidationEventArgs 参数的 ReturnValue 属性使用表示该值的对象。,19,(4)TextMaskFormat属性:表示由掩码文本框的Text属性得到的字符串中是否包含占位符、分隔符的内容。该属性共有4个选项:ExcludePromptAndLiterals表示占位符和分隔符均不包含;IncludePrompt表示仅包含占位符;IncludeLiterals表示仅包含分隔符;IncludePromptAn

9、dLiterals表示占位符和分隔符均包含。,20,(5)HidePromptOnLeave属性:指示若当前控件未处于活动状态时,是否显示占位苻,若属性值为true时,表示当控件不是活动状态,隐藏占位苻;若属性值为false时,即使控件不是活动状态,仍然显示占位符。,21,2、 常用的基本事件 MaskInputRejected事件:当输入字符不符合掩码要求时触发的操作。,例1 简单的用户信息登录界面示例。(1)新建一个名为MaskedTextBoxExample的Windows应用程序项目,在解决方案资源管理器中Form1.cs重命名为FormMaskedTextBox.cs。如下各图所示。

10、,22,向Form窗体中添加5个MaskedTextBox控件、1个Button控件和7个Label控件,窗体中各控件的属性、布局及显示效果如图所示。,23,在FormMaskedTextBox类中直接编写MyMaskInputRejected事件和MyMaskInputReset事件 。具体代码为: /若输入不符合要求,则以红色字体显示输入信息 private void MyMaskInputRejected(object sender, MaskInputRejectedEventArgs e) (MaskedTextBox)sender).ForeColor = Color.Red; /

11、重新输入时,以改写方式输入,并恢复正常的字体颜色 private void MyMaskInputReset(object sender, KeyEventArgs e) (MaskedTextBox)sender).InsertKeyMode = InsertKeyMode.Overwrite; (MaskedTextBox)sender).ForeColor = Color.FromName(Window Text); ,24,为所有的MaskedTextBox的MaskInputRejected事件指定事件处理函数:MyMaskInputRejected 为所有的MaskedTextBo

12、x的KeyDown事件指定事件处理函数:MyMaskInputReset,25,添加maskedTextBoxBirthday的TypeValidationCompleted事件代码。,将该事件代码补充完整如下:private void maskedTextBoxBirthday_TypeValidationCompleted(object sender, TypeValidationEventArgs e) if (!e.IsValidInput) MessageBox.Show(您输入的日期不正确!); (MaskedTextBox)sender).Focus(); (MaskedText

13、Box)sender).InsertKeyMode = InsertKeyMode.Overwrite; ,RadioButton实例,DEMO4,27,添加RadioButton的CheckedChanged事件处理,private void rdo_CheckedChanged(object sender, EventArgs e) RadioButton btn=(RadioButton)sender; if (checkBox1.Checked) switch(btn.Text) case 红色: pictureBox1.BackColor=Color.Red; break; case

14、 蓝色: pictureBox1.BackColor=Color.Blue; break; case 黑色: . ,28,添加CheckBox的CheckedChanged事件处理,private void checkBox1_CheckedChanged(object sender, EventArgs e) if (checkBox1.Checked) foreach (Control ctl in this.groupBox1.Controls) if (ctl is RadioButton) RadioButton rdo = (RadioButton)ctl; if (rdo.Che

15、cked) this.rdo_CheckedChanged(rdo, null); break; else pictureBox1.BackColor = this.BackColor; ,VScrollBar实例,DEMO4,30,提示,(1)设置VScrollBar的LargeChange值为1;,31,添加VSCrollBar的ValueChanged事件处理,private void vScrollBar1_ValueChanged(object sender, EventArgs e) int r, g, b; r = vScrRed.Value; g = vScrGreen.Val

16、ue; b = vScrBlue.Value; Color c = Color.FromArgb(r, g, b); pictureBox1.BackColor = c; lblRed.Text = r.ToString(); lblGreen.Text = g.ToString(); lblBlue.Text = b.ToString(); Invalidate(false); ,32,添加CheckBox的CheckedChanged事件处理,private void checkBox1_CheckedChanged(object sender, EventArgs e) if (chec

17、kBox1.Checked) foreach (Control ctl in this.groupBox1.Controls) if (ctl is RadioButton) RadioButton rdo = (RadioButton)ctl; if (rdo.Checked) this.rdo_CheckedChanged(rdo, null); break; else pictureBox1.BackColor = this.BackColor; ,资源管理器实例,DEMO5,34,提示,(1)设置ListView的View值为Details;(2)添加命名空间:using System

18、.IO;/添加引用,以使用文件和目录类,35,初始化左面的TreeView显示,public void RefreshTree()/ Turn off visual updating and clear tree.DirectoryTreeView.BeginUpdate();DirectoryTreeView.Nodes.Clear();/ Make disk drives the root nodes. string astrDrives = Directory.GetLogicalDrives();foreach (string str in astrDrives)TreeNode tn

19、Drive = new TreeNode(str, 0, 0);/ImageIndex = 0;SelectedImageIndex =0DirectoryTreeView.Nodes.Add(tnDrive);AddDirectories(tnDrive);if (str = C:)DirectoryTreeView.SelectedNode = tnDrive;DirectoryTreeView.EndUpdate();,36,添加各个驱动器节点,void AddDirectories(TreeNode tn) tn.Nodes.Clear(); string strPath = tn.F

20、ullPath; DirectoryInfo dirinfo = new DirectoryInfo(strPath); DirectoryInfo adirinfo; try adirinfo = dirinfo.GetDirectories(); catch return; foreach (DirectoryInfo di in adirinfo) TreeNode tnDir = new TreeNode(di.Name, 1, 2); tn.Nodes.Add(tnDir); ,37,添加TreeView的BeforeExpand响应事件,即在节点打开前执行,private void

21、 DirectoryTreeView_BeforeExpand(object sender, System.Windows.Forms.TreeViewCancelEventArgs e)DirectoryTreeView.BeginUpdate();foreach (TreeNode tn in e.Node.Nodes)AddDirectories(tn);DirectoryTreeView.EndUpdate();,38,填充ListView,显示文件信息,public void ShowFiles(string strDirectory)/ Save directory name as

22、 field. this.strDirectory = strDirectory;FileListView.Items.Clear();DirectoryInfo dirinfo = new DirectoryInfo(strDirectory);FileInfo afileinfo;tryafileinfo = dirinfo.GetFiles();catchreturn;,39,foreach (FileInfo fi in afileinfo)/ Create ListViewItem.ListViewItem lvi = new ListViewItem(fi.Name);/ Assi

23、gn ImageIndex based on filename extension.if (Path.GetExtension(fi.Name).ToUpper() = .EXE)lvi.ImageIndex = 4;elselvi.ImageIndex = 3;/ Add file length and modified time sub-items.lvi.SubItems.Add(fi.Length.ToString(N0);/ Add attribute subitem.string strAttr = ; if (fi.Attributes ,40,if (fi.Attributes

24、 ,41,添加TreeView的BeforeExpand响应事件,即在节点打开前执行,private void DirectoryTreeView_BeforeExpand(object sender, System.Windows.Forms.TreeViewCancelEventArgs e)DirectoryTreeView.BeginUpdate();foreach (TreeNode tn in e.Node.Nodes)AddDirectories(tn);DirectoryTreeView.EndUpdate();,42,添加DirectoryTreeView的AfterSele

25、ct响应事件,即在节点被选择后执行,private void DirectoryTreeView_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)ShowFiles(e.Node.FullPath);,后台线程实例,DEMO6,44,提示,(1)添加控件BackgroundWorker;(2)添加Bool flag=false;表示是否画图;(3)添加 Random random = new Random();用来生成随机画图时的坐标和颜色;,45,添加BackgroundWorker的DoWork响应事件

26、,private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) Point pt = new Point2; Graphics graphics = pictureBox1.CreateGraphics(); Color c ; while (flag) pt0 = new Point(random.Next(pictureBox1.Width), random.Next(pictureBox1.Height); pt1 = new Point(random.Next(pictureBox1.Width), ra

27、ndom.Next(pictureBox1.Height); c = Color.FromArgb(random.Next(255), random.Next(255), random.Next(255); graphics.DrawLine(new Pen(c, 3), pt0, pt1); ,46,添加Button的Click响应事件,private void button1_Click(object sender, EventArgs e) this.flag = !this.flag; if (this.flag) button1.Text = 停止; this.backgroundWorker1.RunWorkerAsync(); while (this.backgroundWorker1.IsBusy) Application.DoEvents(); else button1.Text = 开始; ,

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 重点行业资料库 > 1

Copyright © 2018-2021 Wenke99.com All rights reserved

工信部备案号浙ICP备20026746号-2  

公安局备案号:浙公网安备33038302330469号

本站为C2C交文档易平台,即用户上传的文档直接卖给下载用户,本站只是网络服务中间平台,所有原创文档下载所得归上传人所有,若您发现上传作品侵犯了您的权利,请立刻联系网站客服并提供证据,平台将在3个工作日内予以改正。