WPF and Silverlight 学习笔记(六):WPF窗体
一、窗体类
在Visual Studio和Expression Blend中,自定义的窗体均继承System.Windows.Window类(类型化窗体)。定义的窗体由两部分组成:
1、XAML文件
1: <Window
2: xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3: xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4: x:Class="WpfWindow.BasicWindow"
5: x:Name="Window"
6: Title="BasicWindow"
7: Width="300" Height="200">
8: <Canvas>
9: <Button x:Name="btnMessage" Width="79" Height="24" Content="OK"
10: Canvas.Left="172" Canvas.Top="93" Click="btnMessage_Click"/>
11: <TextBox x:Name="txtValue" Width="215" Height="25"
12: Canvas.Left="36" Canvas.Top="48" Text="" TextWrapping="Wrap"/>
13: </Canvas>
14: </Window>
2、后台代码文件
1: using System;
2: using System.Windows;
3:
4: namespace WpfWindow
5: {
6: public partial class BasicWindow : Window
7: {
8: public BasicWindow()
9: {
10: this.InitializeComponent();
11: }
12:
13: private void btnMessage_Click(object sender, System.Windows.RoutedEventArgs e)
14: {
15: txtValue.Text = "Hello World";
16: }
17: }
18: }
也可以将后台代码放在XAML文件中,上面的例子可以改写为:
1: <Window
2: xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3: xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4: x:Class="WpfWindow.BasicWindow"
5: x:Name="Window"
6: Title="BasicWindow"
7: Width="300" Height="200">
8: <Canvas>
9: <Button x:Name="btnMessage" Width="79" Height="24" Content="OK"
10: Canvas.Left="172" Canvas.Top="93" Click="btnMessage_Click"/>
11: <x:Code><![CDATA[
12: void btnMessage_Click(object sender, System.Windows.RoutedEventArgs e)
13: {
14: txtValue.Text = "Hello World";
15: }
16: ]]>
17: </x:Code>
18: <TextBox x:Name="txtValue" Width="215" Height="25"
19: Canvas.Left="36" Canvas.Top="48" Text="" TextWrapping="Wrap"/>
20: </Canvas>
21: </Window>
二、窗体的生存周期
1、显示窗体
- 构造方法
- Show()、ShowDialog()方法:Show()方法显示非模态窗口,ShowDialog()方法显示模态窗口
- Loaded事件:窗体第一次Show()或ShowDialog()时引发的事件,通常在此事件中加载窗体的初始化数据
2、关闭窗体
- Close()方法:关闭窗体,并释放窗体的资源
- Closing事件、Closed事件:关闭时、关闭后引发的事件,通常在Closing事件中提示用户是否退出。
3、窗体的激活
- Activate()方法:激活某窗体
- Activated、Deactivated事件:当窗体激动、失去焦点时引发的事件
4、窗体的生存周期
示例程序:
- 在窗体载入时显示当前日期,并开始播放媒体
- 当窗体失去焦点时暂停播放媒体
- 当窗体重新获得焦点时继承播放窗体
- 当点击窗体的关闭按钮时,询问用户是否退出应用程序
XAML文件:
1: <Window x:Class="WpfWindow.WindowLifeCycle"
2: xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3: xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4: Title="WindowLifeCycle" Height="200" Width="300"
5: Loaded="Window_Loaded"
6: Activated="Window_Activated"
7: Deactivated="Window_Deactivated"
8: Closing="Window_Closing">
9: <Canvas>
10: <TextBlock Canvas.Right="15" Canvas.Bottom="15" Height="21" Name="txtDate"/>
11: <MediaElement Canvas.Left="89" Canvas.Top="12" Height="100" Width="100"
12: Name="myMedia" Source="numbers.wmv"
13: Stretch="Fill" LoadedBehavior="Manual" />
14: </Canvas>
15: </Window>
代码文件:
1: using System;
2: using System.Windows;
3:
4: namespace WpfWindow
5: {
6: public partial class WindowLifeCycle : Window
7: {
8: public WindowLifeCycle()
9: {
10: InitializeComponent();
11: }
12:
13: // 开关变量,判断是否正在播放媒体
14: private bool isPlaying;
15:
16: private void Window_Loaded(object sender, RoutedEventArgs e)
17: {
18: // 窗体加载时,显示当前日期及开始播放媒体
19: txtDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
20:
21: myMedia.Play();
22: isPlaying = true;
23: }
24:
25: private void Window_Activated(object sender, EventArgs e)
26: {
27: // 如果窗体被激活,则继承播放媒体
28: if (!isPlaying)
29: {
30: myMedia.Play();
31: isPlaying = true;
32: }
33: }
34:
35: private void Window_Deactivated(object sender, EventArgs e)
36: {
37: // 如果窗体失去焦点,则暂停播放媒体
38: if (isPlaying)
39: {
40: myMedia.Pause();
41: isPlaying = false;
42: }
43: }
44:
45: private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
46: {
47: // 点击窗体的“关闭”按钮,询问用户是否退出程序
48:
49: string message = "Quit the application?";
50: string title = "System Information";
51: MessageBoxButton button = MessageBoxButton.OKCancel;
52: MessageBoxImage img = MessageBoxImage.Question;
53:
54: MessageBoxResult result = MessageBox.Show(
55: message, title, button, img);
56:
57: if (result == MessageBoxResult.Cancel)
58: {
59: e.Cancel = true; // 取消退出
60: }
61: }
62: }
63: }
三、其他窗体相关的属性、方法、事件
WPF窗体的详细的属性、方法、事件请参考MSDN,有很多的属性、方法、事件与Windows应用程序中System.Windows.Forms.Form类相同或近似,其中常用的一些属性、方法、事件有:
- 窗体边框模式(WindowStyle属性)和是否允许更改窗体大小(ResizeMode属性)
- 窗体启动位置(WindowStartupLocation属性)和启动状态(WindowState属性)
- 窗体标题(Title属性)
- 始终在最前(TopMost属性)
- 是否显示在任务栏(ShowInTaskbar)
四、定义异形窗体
使用异形窗体,可以将窗体的背景设置为透明,边框设置为空,然后利用控件做出异形的窗体,例如:
XAML:
1: <Window x:Class="WpfWindow.CustomerWindow"
2: xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3: xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4: Title="NonRectangularWindowSample" SizeToContent="WidthAndHeight"
5: MouseLeftButtonDown="NonRectangularWindow_MouseLeftButtonDown"
6: WindowStyle="None"
7: AllowsTransparency="True"
8: Background="Transparent">
9: <Canvas Width="200" Height="200" >
10: <Path Stroke="DarkGray" StrokeThickness="2">
11: <Path.Fill>
12: <LinearGradientBrush StartPoint="0.2,0" EndPoint="0.8,1" >
13: <GradientStop Color="White" Offset="0"></GradientStop>
14: <GradientStop Color="White" Offset="0.45"></GradientStop>
15: <GradientStop Color="LightBlue" Offset="0.9"></GradientStop>
16: <GradientStop Color="Gray" Offset="1"></GradientStop>
17: </LinearGradientBrush>
18: </Path.Fill>
19: <Path.Data>
20: <PathGeometry>
21: <PathFigure StartPoint="40,20" IsClosed="True">
22: <LineSegment Point="160,20"></LineSegment>
23: <ArcSegment Point="180,40" Size="20,20" SweepDirection="Clockwise"></ArcSegment>
24: <LineSegment Point="180,80"></LineSegment>
25: <ArcSegment Point="160,100" Size="20,20" SweepDirection="Clockwise"></ArcSegment>
26: <LineSegment Point="90,100"></LineSegment>
27: <LineSegment Point="90,150"></LineSegment>
28: <LineSegment Point="60,100"></LineSegment>
29: <LineSegment Point="40,100"></LineSegment>
30: <ArcSegment Point="20,80" Size="20,20" SweepDirection="Clockwise"></ArcSegment>
31: <LineSegment Point="20,40"></LineSegment>
32: <ArcSegment Point="40,20" Size="20,20" SweepDirection="Clockwise"></ArcSegment>
33: </PathFigure>
34: </PathGeometry>
35: </Path.Data>
36: </Path>
37: <Label Width="200" Height="120" FontSize="15" HorizontalContentAlignment="Center" VerticalContentAlignment="Center">Drag Me</Label>
38: <Button Canvas.Left="155" Canvas.Top="30" Click="closeButtonRectangle_Click">
39: <Button.Template>
40: <ControlTemplate>
41: <Canvas>
42: <Rectangle Width="15" Height="15" Stroke="Black" RadiusX="3" RadiusY="3">
43: <Rectangle.Fill>
44: <SolidColorBrush x:Name="myAnimatedBrush" Color="Red" />
45: </Rectangle.Fill>
46: </Rectangle>
47: <Line X1="3" Y1="3" X2="12" Y2="12" Stroke="White" StrokeThickness="2"></Line>
48: <Line X1="12" Y1="3" X2="3" Y2="12" Stroke="White" StrokeThickness="2"></Line>
49: </Canvas>
50: </ControlTemplate>
51: </Button.Template>
52: </Button>
53: </Canvas>
54: </Window>
代码文件:
1: using System.Windows;
2: using System.Windows.Input;
3:
4: namespace WpfWindow
5: {
6: public partial class CustomerWindow : Window
7: {
8: public CustomerWindow()
9: {
10: InitializeComponent();
11: }
12:
13: void NonRectangularWindow_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
14: {
15: this.DragMove();
16: }
17:
18: void closeButtonRectangle_Click(object sender, RoutedEventArgs e)
19: {
20: this.Close();
21: }
22: }
23: }
转载于:https://www.cnblogs.com/J-FoX/archive/2012/06/07/2539959.html
WPF and Silverlight 学习笔记(六):WPF窗体相关推荐
- WPF and Silverlight 学习笔记(九):WPF布局管理之Canvas、InkCanvas
一.Canvas 在WPF中子元素的绝对定位的布局控件 其子元素使用Width.Height定义元素的宽度和高度 使用Convas.Left(Convas.Right).Convas.Top(Conv ...
- WPF and Silverlight 学习笔记(二十):WPF数据绑定概述
WPF数据绑定为应用程序提供了一种表示数据和与数据交互的简单而又一致的方法.元素能够以公共语言运行库 (CLR) 对象和 XML 的形式绑定到各种数据源中的数据. 一.数据绑定的基本概念: 数据绑定涉 ...
- WPF and Silverlight 学习笔记(十四):键盘输入、鼠标输入、焦点处理[转]
一.键盘类和键盘事件 WPF提供了基础的键盘类(System.Input.Keyboard类),该类提供与键盘相关的事件.方法和属性,这些事件.方法和属性提供有关键盘状态的信息.Keyboard的事件 ...
- WPF and Silverlight 学习笔记(十九):WPF更换主题
如果要做到一个应用程序其基本的内容不变,但改变整个应用程序的外观可以这样做: 对于每一套外观定义一个ResourceDictionary 在应用程序中,动态加载此应用程序(或窗体)的Resource ...
- WPF and Silverlight 学习笔记(十):WPF控件模型
WPF对控件其类型的继承方式如下 (其中绿色表示的类是抽象类,蓝色表示的类是非抽象类) 控件内容模型 System.Windows.Controls.Control类:表示 用户界面 (UI ...
- WPF and Silverlight 学习笔记(十八):WPF样式(Style)与模板(Template)
一.WPF样式 类似于Web应用程序中的CSS,在WPF中可以为控件定义统一的样式(Style).样式属于资源的一种,例如为Button定义统一的背景颜色和字体: 1: <Window.Reso ...
- WPF and Silverlight 学习笔记(二十二):使用代码实现绑定、绑定数据的验证
一.通过代码实现数据绑定 通过代码实现数据绑定,使用的是System.Windows.Data命名空间的Binding类,主要使用Binding类的如下的属性: Source属性:绑定到的数据源 Mo ...
- WPF and Silverlight 学习笔记(十二):WPF Panel内容模型、Decorator内容模型及其他...
由于园子里昨天使用Live Writer上传出现问题,昨天只能使用Web上的文本编辑器上传本文,造成代码.内容等格式的错误,现重发本文. 一.Panel内容模型 Panel内容模型指从System.W ...
- 【HLSL学习笔记】WPF Shader Effect Library算法解读之[DirectionalBlur]
原文:[HLSL学习笔记]WPF Shader Effect Library算法解读之[DirectionalBlur] 方位模糊是一个按照指定角度循环位移并叠加纹理,最后平均颜色值并输出的一种特效. ...
最新文章
- python解压_python解压缩
- python爬虫案例-Python爬取租房数据实例,据说可以入门爬虫的小案例!
- 服务中没有listen_Go语言微服务框架实战:2.Go语言实现RPC编程绍
- error_reporting()函数用法
- 修改节点大小_重磅前瞻!K8S 1.18即将发布:OIDC发现、Windows节点支持,还有哪些新特性值得期待?...
- 计算机入门与学习回忆(一)
- 个人黑名单 抄袭耻辱墙
- “对不起,我们只招父母毕业于名牌院校的孩子”:最可怕的,是你还以为学历不重要...
- 格式化输入数字_Python格式化输出的三种方式
- STM32F405 标准库 SHT20温湿度传感器
- 简述对象和类的关系python_(一)Python入门-6面向对象编程:02类的定义-类和对象的关系-构造函数-实例属性-实例方法...
- eclipse 没有web项目解决办法
- 自己用过最好用的pdf转word软件
- zipentry java_zipentry?java实现解压缩?substring?Java用ZIP格式压缩和解压缩文件
- windows 如何设置定时任务执行程序
- 【操作系统概念-作业4】Threads
- 阿里云IoT:控制掌控板板载灯亮灭
- C语言表达式是运算符和,C语言之运算符和表达式
- php头像获取,php获取QQ头像并显示的方法
- 英文手写之意大利斜体
热门文章
- 前后端开发是怎么合作分工的
- 误人子弟的网络,谈谈HTTP协议中的短轮询、长轮询、长连接和短连接(转载)
- BZOJ 1101: [POI2007]Zap( 莫比乌斯反演 )
- Oracle数据库管理员经常使用的表和视图
- nm命令中符号类型详解
- css中改变边距会影响原大小,CSS:更改父容器中子项的边距会改变子项的宽度吗?...
- java 连接 oracle 10_java-连接到oracle 10g数据库时,获得IOException“...
- java hashmap遍历顺序_Java中HashMap遍历的两种方式
- 满意度调查access模板_洪安镇推进综合便民服务中心标准化建设,着力提升群众满意度...
- linux 添加路由_linux网络知识:路由策略(ip rule,ip route)