创建特定形状的窗体课件

上传人:91274****mpsvz 文档编号:243149923 上传时间:2024-09-16 格式:PPT 页数:46 大小:607.50KB
返回 下载 相关 举报
创建特定形状的窗体课件_第1页
第1页 / 共46页
创建特定形状的窗体课件_第2页
第2页 / 共46页
创建特定形状的窗体课件_第3页
第3页 / 共46页
点击查看更多>>
资源描述
单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,*,单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,*,创建特定形状的窗体,DEMO1,添加窗体的,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);,2,创建不规则窗体,DEMO2,步骤,1,(1),创建,Windows,应用程序项目,将其属性设,置为移除标题栏并使用位图作为窗体背景。,BackgroundImage,属性,:,位图,FormBorderStyle,属性,:None,TransparencyKey,属性,:,设为要隐藏的颜色,4,步骤,2,(2),输入重新创建标题栏所提供功能的代码,,例如移动和关闭窗体。,为窗体添加数据成员,记录鼠标状态,:,private Point mouseOffset;,private bool isMouseDown = false;,5,步骤,3,(3),添加窗体的,MouseDown,事件处理,:,private void Form1_MouseDown(object sender, 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 = true;,6,添加窗体的,MouseMove,事件处理,private void Form1_MouseMove(object sender, MouseEventArgs e),if (isMouseDown),/,获取鼠标光标的位置(以屏幕坐标表示)。,Point mousePos = Control.MousePosition;,/,将此,Point,平移指定的量。,mousePos.Offset(mouseOffset.X, mouseOffset.Y);,Location = mousePos;,7,添加窗体的,MouseUp,事件处理,private void Form1_MouseUp(object sender, MouseEventArgs e),if (e.Button = MouseButtons.Left),isMouseDown = false;,8,MaskedTextBox,控件,DEMO3,MaskedTextBox,控件,(P51),MaskedTextBox,控件,也叫掩码文本框。它的主要作用是控制输入文本的格式。如果输入的内容不满足规定的格式,则控件不会接收该输入。 如下图是从工具箱向窗体拖动该控件的图示。,10,1,、常用的基本属性,(,1,),InsertKeyMode,属性:指示向掩码文本框输入字符时的输入模式,其属性值有:,Default,、,Insert,和,Overwrite3,种。当属性值为,Default,时,表示输入模式由当时键盘的插入,/,改写状态决定,如果输入时键盘处于改写模式,则会改写输入处的字符,否则为插入字符。当属性值为,Insert,时,即使键盘的,Ins,键被按下,也不会以改写方式输入字符。当属性值为,Overwrite,时,则任何时候都是以改写方式输入字符。,11,(,2,),PromptChar,属性:指定作为占位符的字符,用于指示用户需要输入的字符长度,默认的占位符为下划线,“,_,”,,需要修改时可以直接在属性后的空白处输入指定字符。,12,(,3,),Mask,属性:设置当前掩码文本框输入字符的格式。单击,Mask,属性后的,按钮,,13,弹出如图所示的窗口,其中是一些设置好的时间、电话号码的格式,若这些格式都不能满足设计要求,也可以选择自定义格式,或者在,Mask,属性后的空白处直接输入自定义格式。,14,15,自定义输入格式时,可以使用掩码和分隔符两类符号。掩码用于限制用户可输入的符号类型,程序运行时掩码以占位符显示;而分隔符可作为输入字符之间的关联符,分隔符显示在掩码文本框中,且不可修改。表,5-3,列出了常用掩码和分隔符的含义。,16,表,5-3,常用的掩码和分隔符,符号,含义,掩码,0,数字,“,0,”,“,9,”,9,数字,“,0,”,“,9,”,、空格(,Space,),#,数字,“,0,”,“,9,”,、空格(,Space,)、,“,+,”,、,“,-,”,L,数字,“,a,”,“,z,”,、,“,A,”,“,Z,”,&,键盘可输入字符,A,、,a,字母与数字,强制将其后输入的字母转换为大写,分隔符,.,小数分隔符,即小数点,-,连接分隔符,数字分隔符,:,时间分隔符,/,日期分隔符,$,货币符号,17,掩码不能保证用户输入一定会表示给定类型的有效值,例如,输入的年龄值可能为,-9,。通过将值的类型的实例赋给,ValidatingType,属性,可以确保用户输入表示一个有效值。通过监视,TypeValidationCompleted,事件,可以检测当,MaskedTextBox,包含无效值时,用户是否将焦点从该控件移开。如果键入验证成功,可以通过,TypeValidationEventArgs,参数的,ReturnValue,属性使用表示该值的对象。,18,(,4,),TextMaskFormat,属性:表示由掩码文本框的,Text,属性得到的字符串中是否包含占位符、分隔符的内容。该属性共有,4,个选项:,ExcludePromptAndLiterals,表示占位符和分隔符均不包含;,IncludePrompt,表示仅包含占位符;,IncludeLiterals,表示仅包含分隔符;,IncludePromptAndLiterals,表示占位符和分隔符均包含。,19,(,5,),HidePromptOnLeave,属性:,指示若当前控件未处于活动状态时,是否显示占位苻,若属性值为,true,时,表示当控件不是活动状态,隐藏占位苻;若属性值为,false,时,即使控件不是活动状态,仍然显示占位符。,20,2,、 常用的基本事件,MaskInputRejected,事件:当输入字符不符合掩码要求时触发的操作。,例,1,简单的用户信息登录界面示例。,(,1,),新建一个名为,MaskedTextBoxExample,的,Windows,应用程序项目,在,解决方案资源管理器,中,Form1.cs,重命名为,FormMaskedTextBox.cs,。如下各图所示。,21,向,Form,窗体中添加,5,个,MaskedTextBox,控件、,1,个,Button,控件和,7,个,Label,控件,窗体中各控件的属性、布局及显示效果如图所示。,22,在,FormMaskedTextBox,类中直接编写,MyMaskInputRejected,事件和,MyMaskInputReset,事件,。,具体代码为:,/,若输入不符合要求,则以红色字体显示输入信息,private void,MyMaskInputRejected(object,sender,MaskInputRejectedEventArgs,e),(,MaskedTextBox)sender).ForeColor,=,Color.Red,;,/,重新输入时,以改写方式输入,并恢复正常的字体颜色,private void,MyMaskInputReset(object,sender,KeyEventArgs,e),(,MaskedTextBox)sender).InsertKeyMode,=,InsertKeyMode.Overwrite,;,(,MaskedTextBox)sender).ForeColor,=,Color.FromName(Window,Text);,23,为所有的,MaskedTextBox,的,MaskInputRejected,事件指定事件处理函数,:,MyMaskInputRejected,为所有的,MaskedTextBox,的,KeyDown,事件指定事件处理函数,:,MyMaskInputReset,24,添加,maskedTextBoxBirthday,的,TypeValidationCompleted,事件代码。,将该事件代码补充完整如下:,private void,maskedTextBoxBirthday_TypeValidationCompleted(object,sender,TypeValidationEventArgs,e),if (!,e.IsValidInput,),MessageBox.Show,(,您输入的日期不正确!,);,(,MaskedTextBox)sender).Focus,();,(,MaskedTextBox)sender).InsertKeyMode,=,InsertKeyMode.Overwrite,;,25,RadioButton,实例,DEMO4,添加,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 ,蓝色,:,pictureBox1.BackColor=Color.Blue;,break;,case ,黑色,:,.,27,添加,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.Checked),this.rdo_CheckedChanged(rdo, null);,break;, ,else,pictureBox1.BackColor = this.BackColor;, ,28,VScrollBar,实例,DEMO4,提示,(1),设置,VScrollBar,的,LargeChange,值为,1;,30,添加,VSCrollBar,的,Value,Changed,事件处理,private void vScrollBar1_ValueChanged(object sender, EventArgs e),int r, g, b;,r = vScrRed.Value;,g = vScrGreen.Value;,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);,31,添加,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.Checked),this.rdo_CheckedChanged(rdo, null);,break;, ,else,pictureBox1.BackColor = this.BackColor;, ,32,资源管理器实例,DEMO5,提示,(1),设置,ListView,的,View,值为,Details;,(2),添加命名空间,:,using System.IO;/,添加引用,以使用文件和目录类,34,初始化左面的,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 tnDrive = new TreeNode(str, 0, 0);,/,ImageIndex,= 0;,SelectedImageIndex,=0,DirectoryTreeView.Nodes.Add(tnDrive);,AddDirectories(tnDrive);,if (str = C:),DirectoryTreeView.SelectedNode = tnDrive;,DirectoryTreeView.EndUpdate();,35,添加各个驱动器节点,void AddDirectories(TreeNode tn), tn.Nodes.Clear();,string strPath = tn.FullPath;,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);,36,添加,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();,37,填充ListView,显示文件信息,public void ShowFiles(string strDirectory),/ Save directory name as field.,this.strDirectory = strDirectory;,FileListView.Items.Clear();,DirectoryInfo dirinfo = new DirectoryInfo(strDirectory);,FileInfo afileinfo;,try,afileinfo = dirinfo.GetFiles();,catch,return;,38,foreach (FileInfo fi in afileinfo),/ Create ListViewItem.,ListViewItem lvi = new ListViewItem(fi.Name);,/ Assign ImageIndex based on filename extension.,if (Path.GetExtension(fi.Name).ToUpper() = .EXE),lvi.ImageIndex = 4;,else,lvi.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 & FileAttributes.Archive) != 0),strAttr += A;,if (fi.Attributes & FileAttributes.Hidden) != 0),strAttr += H;,39,if (fi.Attributes & FileAttributes.ReadOnly) != 0),strAttr += R;,if (fi.Attributes & FileAttributes.System) != 0,),strAttr += S;,lvi.SubItems.Add(strAttr);,lvi.SubItems.Add(fi.LastWriteTime.ToString();,/ Add completed ListViewItem to FileListView.,FileListView.Items.Add(lvi);,40,添加,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();,41,添加,DirectoryTreeView,的,AfterSelect,响应事件,即在节点,被选择后执行,private void DirectoryTreeView_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e),ShowFiles(e.Node.FullPath);,42,后台线程实例,DEMO6,提示,(1),添加控件,B,ackgroundWorker,;,(2),添加,Bool flag=false;,表示是否画图,;,(3),添加,Random random = new Random();,用来生成随机画图时的坐标和颜色,;,44,添加,B,ackgroundWorker,的,DoWork,响应事件,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), random.Next(pictureBox1.Height);,c = Color.FromArgb(random.Next(255), random.Next(255), random.Next(255);,graphics.DrawLine(new Pen(c, 3), pt0, pt1);,45,添加,B,utton,的,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 = ,开始,;,46,
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 教学培训


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!