收藏 分销(赏)

GUI图形用户界面设计基础PPT课件.ppt

上传人:胜**** 文档编号:748871 上传时间:2024-03-01 格式:PPT 页数:103 大小:5.92MB
下载 相关 举报
GUI图形用户界面设计基础PPT课件.ppt_第1页
第1页 / 共103页
GUI图形用户界面设计基础PPT课件.ppt_第2页
第2页 / 共103页
GUI图形用户界面设计基础PPT课件.ppt_第3页
第3页 / 共103页
GUI图形用户界面设计基础PPT课件.ppt_第4页
第4页 / 共103页
GUI图形用户界面设计基础PPT课件.ppt_第5页
第5页 / 共103页
点击查看更多>>
资源描述

1、 C#windows程序设计成都大学信息科学与技术学院软件工程系课程介绍n课程简介l本课程介绍使用 C#开发 Windows 应用程序所需的知识和技能n预备知识l掌握基本的C#语法l掌握数据库的基本知识l掌握面向对象的编程思想l能够使用Visual Studio.NET图形化界面开发环境课程要求n课时:56课时讲解内容nWinForm窗体介绍nWinForm窗体基本操作nWinForm控件的使用n对话框编程n菜单栏、工具栏、状态栏的使用nWindows线程程序设计n文件操作n数据库访问程序设计 (C/S程序设计)nGDI+图形图像处理n网络编程为什么要使用窗体n简单n强大n方便n灵活n什么是窗

2、体nWindows 操作系统中,处处是窗体 GUI编程概述.NET的功能和编程特点Visual Studio 2008 IDEVS解决方案和项目的组织结构.解决方案和项目文件 .sln、.suo、.cs、.csproj、.aspx、.asmx、.resx等。解决方案和项目文件扩展名描述.csVisual C#源程序文件 表示属于单个项目的窗体、用户控件、类和模块文件。.slnVisual Studio解决方案文件 组织解决方案中的项目、项目子项和其他子项。.aspxWeb项目文件 表示 Web 项目子项文件。.suo解决方案用户选项文件 记录所有可能和解决方案相关的选项。.csprojVisu

3、al C#项目文件 表示属于多个项目的窗体、用户控件、类和模块文件。项目开发过程l 编写项目设计说明书l 设计用户界面l 设计用户界面中各种对象的属性l 编写代码添加功能l 测试与调试应用程序l 生成可执行文件l 创建应用程序的安装程序Visual C#解决方案的创建应用程序模板DEMOWinForm简介Windows窗体的继承WinForm的实现l form1类从S.W.F.Form类继承行为认识 WinForms项目名称主程序文件,包含Main方法我们编写代码的文件解决方案名称窗体设计器代码文件,代码为VS自动生成启动窗体Main 方法n位于 Program.cs 文件nWindows程序

4、的入口/Program.cs namespace MyForm static class Program .static void Main()Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);Application.Run(new Form1();Main 方法,程序的入口VS 自动生成一般不需要理会创建 WinForms应用程序 using System;using System.Drawing;using System.Collections;using System

5、.ComponentModel;using System.Windows.Forms;namespace SampleProject/Form1 的摘要说明。/public class Form1:System.Windows.Forms.Form/必需的设计器变量./提供了大量绘图工具的访问权限基础核心命名空间ArrayList、BitArray、Hashtable、Stack、StringCollection 和 StringTable 类 大量窗体和控件从 System.Windows.Forms.Form 派生Visual Studio.NET 生成的代码创建 WinForms应用程序

6、 6-4private System.ComponentModel.Container components=null;public Form1()/Windows 窗体设计器支持所必需的/InitializeComponent();/TODO:在 InitializeComponent 调用之后添加任何构造函数代码/构造函数调用 InitializeComponent()方法private void InitializeComponent()ponents=new System.ComponentModel.Container();this.Size=new System.Drawing.S

7、ize(300,300);this.Text=Form1;项目的容器创建 WinForms应用程序 6-5/清理所有正在使用的资源。/protected override void Dispose(bool disposing)if(disposing)if(components!=null)components.Dispose();base.Dispose(disposing);释放系统资源进一步认识窗体文件nWinForms 窗体有两种编辑窗口:窗体设计器窗口窗体代码编辑窗口为什么使用partial具有双钥匙的密码箱 第一把钥匙 第二把钥匙 为了安全,两把钥匙分开放置 只有两把钥匙同时使用

8、才能打开密码箱 用 partial 将同一个窗体的代码分开放在两个文件中:一个存放 VS 自动生成的代码一个存放我们自己编写的代码 程序员编写的代码 VS自动生成的代码 /Form1.Designer.csnamespace MyForm partial class Form1 /private void InitializeComponent()/VS自动生成的代码 this.ShowInTaskbar=false;this.Name=Form1;this.Text=Form1;this.TopMost=true;运行时合到一起组成一个窗体文件怎样使用partial/Form1.cs nam

9、espace MyForm public partial class Form1:Form public Form1()InitializeComponent();/我们编写的代码 构造函数做一些初始化的工作编译器自动调用认识窗体类/Form1.cs namespace MyForm public partial class Form1:Form public Form1()InitializeComponent();冒号表示继承Form类定义了窗体的基本属性和行为我们创建的窗体都继承自Form类Form1具有相应的属性和行为nForm 类:.NET 预定义的窗体类nForm1 继承自 For

10、m窗体的重要属性Name:窗体对象的名字,类似于变量的名字 BackColor:窗体的背景色 ShowInTaskBar:设置窗体是否出现在任务栏 StartPosition:窗体第一次出现时的位置 Text:窗体标题栏显示的文字 TopMost:设置窗体是否为最顶端的窗体 WindowState:窗体出现时最初的状态(正常、最大化、最小化)为什么使用事件nWindows 系统中处处是事件:l鼠标按下、鼠标释放、键盘键按下nWindows 系统通过事件处理来响应用户的请求显示开始菜单 我的电脑窗口打开事件触发 事件响应的结果 单击双击事件驱动nWindows 通过随时响应用户触发的事件做出相应

11、的响应事件驱动机制我们需要做的:针对我们感兴趣的事件,编写相应的事件处理程序 nWinForms 应用程序也是事件驱动的事件驱动编程事件处理器参数窗体的重要事件nLoad :窗体加载事件nMouseClick:鼠标单击事件nMouseDoubleClick:鼠标双击事件nMouseMove:鼠标移动事件nKeyDown:键盘按下事件nKeyUp:键盘释放事件nn窗体有哪些事件?通用验证方法用于验证文本框中输入的数据是否有效 static class Validation public static void IsInteger(object sender,CancelEventArgs e)T

12、extBox a=sender as TextBox;int s;if(!int.TryParse(a.Text,out s)MessageBox.Show(请输入整形数据,计算器);e.Cancel=true;public static void IsInteger(object sender,CancelEventArgs e)/.private void Form1_Load(object sender,EventArgs e)textBox1.Validating+=new CancelEventHandler(Validation.IsInteger);textBox2.Valida

13、ting+=new CancelEventHandler(Validation.IsInteger);private void button2_Click(object sender,EventArgs e)textBox1.Validating+=new CancelEventHandler(Validation.IsInteger);textBox2.Validating+=new CancelEventHandler(Validation.IsInteger);private void button3_Click(object sender,EventArgs e)textBox1.Va

14、lidating-=new CancelEventHandler(Validation.IsInteger);textBox2.Validating-=new CancelEventHandler(Validation.IsInteger);Windows窗体的基本操作用户界面设计人机交互设计与屏幕设计Windows窗体与控件概述Windows窗体的基本操作1.启动Form创建的窗体操作1.调整窗体尺寸(1)在窗体设计器中手动设置窗体尺寸(2)通过属性窗口设置窗体的尺寸 属性size:Width Height(3)通过代码设置窗体的尺寸 这种形式一般为程序运行期间设置窗体的大小。例如:用窗体显

15、示位图文件 讲窗体大小修改为200200像素 private void button1_Click(object sender,EventArgs e)this.ClientSize=new System.Drawing.Size(200,200);动态调整窗体中控件的位置(1)禁止窗体大小变化(不灵活)(2)根据窗体大小变化比例动态调整控件的位置 public Form1()InitializeComponent();int count=this.Controls.Count*2+2;float factor=new floatcount;int i=0;factori+=Size.Widt

16、h;factori+=Size.Height;foreach(Control ctrl in this.Controls)factori+=ctrl.Location.X/(float)Size.Width;factori+=ctrl.Location.Y/(float)Size.Height;ctrl.Tag=ctrl.Size;Tag=factor;private void Form1_Resize(object sender,EventArgs e)float scale=(float)Tag;int i=2;foreach(Control ctrl in this.Controls)c

17、trl.Left=(int)(Size.Width*scalei+);ctrl.Top=(int)(Size.Height*scalei+);ctrl.Width=(int)(Size.Width/(float)scale0*(Size)ctrl.Tag).Width);ctrl.Height=(int)(Size.Height/(float)scale1*(Size)ctrl.Tag).Height);/每次使用的都是最初始的控件大小,保证准确无误。设置窗体在屏幕上的显示位置(1)设置属性Location 设置窗体的左上角坐标(2)设置属性StartPosition 标识显示区域的边界(3)

18、编程设置显示位置 this.Left=300;或this.Left+=200;this.Top=200;或:this.Location=new Point(300,200);设置窗体的边框风格设置属性FormBorderStylelNone:窗体无边框、无标题栏lFixed3D:使用3D边框效果,不允许重设窗体尺寸lFixedDialog:用于对话框,不允许重设窗体尺寸lFixedSingle:不允许重设窗体尺寸,为窗体创建单线边框lSizable:默认值,通常用于主窗口,可重设尺寸lFixedToolWindow:用于工具窗口,显示不可重设尺寸的窗口。lSizableToolWindows:

19、用于工具窗口,显示可以重设尺寸的窗口。捕捉窗体的鼠标事件在窗体中捕捉鼠标事件主要有:/窗体的鼠标移动事件处理程序private void Form1_MouseMove(object sender,MouseEventArgs e)this.Text=String.Format(捕捉到鼠标了!(0,1),e.X,e.Y);this关键字代表窗体本身鼠标的横纵坐标窗体标题栏显示的文字鼠标事件参数事件源当鼠标在窗体上点击时,窗体的背景色改变:如果是红色,变成黄色如果是黄色,变成绿色否则变成红色提示:处理窗体的 MouseClick 事件 利用Color类得到某种颜色:红色Color.Red 绿色C

20、olor.Green 黄色Color.Yellow窗体的背景色:this.BackColor判断背景色:if(this.BackColor=Color.Red)/鼠标单击事件private void Form1_MouseClick(object sender,MouseEventArgs e)if(this.BackColor=Color.Red)this.BackColor=Color.Yellow;else if(this.BackColor=Color.Yellow)this.BackColor=Color.Green;else this.BackColor=Color.Red;创建不

21、同形式的窗体n创建顶层窗体n创建透明窗体n创建启动屏幕显示另一窗体被调用的窗体类 窗体实例=new 被调用的窗体类();窗体实例.Show();private void cmdShow_Click(object sender,System.EventArgs e)frmA A=new frmA();A.Show();创建顶层窗体顶层窗体始终处于应用程序的所有窗体之上。TopMost属性决定窗体是否为最顶层窗体,即使最顶层窗体处于非激活状态,仍然处于其他非顶层窗体之上。private void Form1_Load_1(object sender,EventArgs e)Form2 newfor

22、m=new Form2();newform.Show();创建透明窗体可以设置窗体的透明度设置属性Opacity,其取值范围为0.0(完全透明)1.0(完全不透明)可以编程设置:this.Opacity=0.5 private void Form1_Load(object sender,EventArgs e)trackBar1.Maximum=90;trackBar1.TickFrequency=3;private void trackBar1_ValueChanged(object sender,EventArgs e)int i,j;i=trackBar1.Value;j=trackBa

23、r1.Maximum;this.Opacity=(double)(j-i)/j;private void button1_Click_1(object sender,EventArgs e)trackBar1.Value=0;button1.Enabled=false;trackBar1.Enabled=false;for(double i=0.02;i=1;i+=0.02)this.Opacity=i;System.Windows.Forms.Application.DoEvents();System.Threading.Thread.Sleep(500);this.Opacity=1;bu

24、tton1.Enabled=true;trackBar1.Enabled=true;创建启动屏幕当应用程序装载大量数据时,可以显示一个启动屏幕。(1)添加一个新的窗体对象Form2 添加启动窗口中的控件 设置:private System.Windows.Forms.Label label1;private System.Windows.Forms.PictureBox pictureBox1;public System.Windows.Forms.ProgressBar progressBar1;public System.Windows.Forms.Label label2;(2)在主窗口

25、中:public Form1()Form2 t=new Form2();t.Show();InitializeComponent();for(int i=1;i=100;i+)for(Int64 j=0;j=100000;j+);t.label2.Text=i.ToString()+%;t.progressBar1.Value=i;t.Refresh();t.Close();改变窗体与控件的形状改变窗体的形状 private void Form1_Paint(object sender,PaintEventArgs e)GraphicsPath shape=new GraphicsPath()

26、;shape.AddEllipse(0,0,this.Width,this.Height);this.Region=new Region(shape);需要使用using System.Drawing.Drawing2D;改变控件的形状 private void button_Paint(object sender,PaintEventArgs e)GraphicsPath shape=new GraphicsPath();shape.AddEllipse(0,0,this.button4.Width,this.button4.Height);this.button.Region=new Re

27、gion(shape);创建任意形状的窗体关闭窗体的实现在窗体中添加一个button控件 private void button_Click(object sender,EventArgs e)this.Close();实现窗体的移动 private Point mouse_offset;/创建一个Point对象,该对象(作为一个变量)决定在什么情况下移动窗体 private void Form1_MouseDown(object sender,System.Windows.Forms.MouseEventArgs e)/创建窗体的MouseDown事件的事件处理函数,用户就可以在任何位置移动

28、窗体 mouse_offset=new Point(-e.X,-e.Y);/记录鼠标按下时的位置(负值)private void Form1_MouseMove(object sender,System.Windows.Forms.MouseEventArgs e)/创建窗体的MouseMove事件的事件处理函数,当鼠标左键被按下,同时鼠标被移动时,窗体的Location属性就被设置为新的位置 if(e.Button=MouseButtons.Left)Point mousePos=Control.MousePosition;/获取当前屏幕位置 mousePos.Offset(mouse_of

29、fset.X,mouse_offset.Y);/以屏幕坐标平移制定的量 Location=mousePos;/设置当前窗体在屏幕中的位置 编码方式 private void Form1_Load(object sender,EventArgs e)Rectangle rect=this.ClientRectangle;GraphicsPath path=new GraphicsPath();path.AddEllipse(rect);this.Region=new Region(path);private void Form1_SizeChanged(object sender,EventAr

30、gs e)Rectangle rect=this.ClientRectangle;GraphicsPath path=new GraphicsPath();path.AddEllipse(rect);this.Region=new Region(path);创建环形窗口 private bool hasNormalRegion=true;private void button2_Click(object sender,EventArgs e)if(hasNormalRegion)System.Drawing.Drawing2D.GraphicsPath p=new System.Drawing

31、.Drawing2D.GraphicsPath();int bigDi=this.ClientSize.Height;int donutWidth=100;p.AddEllipse(0,0,bigDi,bigDi);p.AddEllipse(donutWidth,donutWidth,bigDi-(donutWidth*2),bigDi-(donutWidth*2);this.Region=new Region(p);else this.Region=null;hasNormalRegion=!hasNormalRegion;创建飘动的窗体在窗体上放置两个Timer控件 Timer属性Inte

32、rval设置为10(表示10ms引发Tick 事件)private void Form1_Load(object sender,EventArgs e)Point p=new Point(0,240);this.DesktopLocation=p;private void timer1_Tick(object sender,EventArgs e)Point p=new Point(this.DesktopLocation.X+1,this.DesktopLocation.Y);this.DesktopLocation=p;if(p.X=600)timer1.Enabled=false;tim

33、er2.Enabled=true;private void timer2_Tick(object sender,EventArgs e)Point p=new Point(this.DesktopLocation.X-1,this.DesktopLocation.Y);this.DesktopLocation=p;if(p.X=0)timer1.Enabled=true;timer2.Enabled=false;创建系统托盘.什么是系统托盘采用notifyIcon控件实现。private bool hasNormalRegion=true;private void 环形ToolStripMen

34、uItem_Click(object sender,EventArgs e)this.WindowState=System.Windows.Forms.FormWindowState.Normal;hasNormalRegion=true;if(hasNormalRegion)System.Drawing.Drawing2D.GraphicsPath p=new System.Drawing.Drawing2D.GraphicsPath();int bigDi=this.ClientSize.Height;int donutWidth=100;p.AddEllipse(0,0,bigDi,bi

35、gDi);p.AddEllipse(donutWidth,donutWidth,bigDi-(donutWidth*2),bigDi-(donutWidth*2);this.Region=new Region(p);private void 方形ToolStripMenuItem_Click(object sender,EventArgs e)this.WindowState=System.Windows.Forms.FormWindowState.Normal;hasNormalRegion=false;this.Region=null;private void 自动渐变ToolStripM

36、enuItem_Click(object sender,EventArgs e)this.WindowState=System.Windows.Forms.FormWindowState.Normal;for(double i=0.02;i=1;i+=0.02)this.Opacity=i;System.Windows.Forms.Application.DoEvents();System.Threading.Thread.Sleep(100);this.Opacity=1;private void 退出ToolStripMenuItem_Click(object sender,EventArgs e)Application.Exit();private void notifyIcon1_DoubleClick(object sender,EventArgs e)this.WindowState=System.Windows.Forms.FormWindowState.Normal;

展开阅读全文
相似文档                                   自信AI助手自信AI助手
猜你喜欢                                   自信AI导航自信AI导航
搜索标签

当前位置:首页 > 通信科技 > 计算机应用

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        获赠5币

©2010-2024 宁波自信网络信息技术有限公司  版权所有

客服电话:4008-655-100  投诉/维权电话:4009-655-100

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服