(WPF学习记录)第三章 Content的概念
来源:互联网 发布:2016网络知识竞赛答题 编辑:程序博客网 时间:2024/05/21 03:28
Window类有超过100个公开的属性,其中一些(如Title等)相当重要,但迄今为止,Window最重要的属性是Content。
我们可以将Content设定成一个字符串、位图、矢量图、按钮、滚动条或者是WPF所支持的50多个控件。我们几乎可以将Content设定成任何东西。
第01个小程序:显示文本(DisplaySomeText.cs)
在客户区显示字符串。
- using System;
- using System.Windows;
- using System.Windows.Input;
- using System.Windows.Media;
- namespace Chapter03
- {
- public class DisplaySomeText : Window
- {
- [STAThread]
- public static void Main()
- {
- Application app = new Application();
- app.Run(new DisplaySomeText());
- }
- public DisplaySomeText()
- {
- Title = "显示文字";
- Width = 640;
- Height = 480;
- WindowStartupLocation = WindowStartupLocation.CenterScreen;
- Content = "将进酒,杯莫停!"; //显示内容
- FontFamily = new FontFamily("华文行楷"); //字体样式
- FontSize = 32; //字体大小
- FontStyle = FontStyles.Italic; //斜体
- FontWeight = FontWeights.Bold; //粗体
- Brush brush1 = new LinearGradientBrush(Colors.Silver,Colors.White,
- new Point(0,0),new Point(1,1));
- Brush brush2 = new LinearGradientBrush(Colors.Red, Colors.Blue,
- new Point(0, 0), new Point(1, 1));
- Background = brush1; //背景色
- Foreground = brush2; //前景色。字体颜色
- SizeToContent = SizeToContent.WidthAndHeight; //窗口大小根据文字内容尺寸作调整
- ResizeMode = ResizeMode.NoResize; //窗口大小调整方式(NoResize表示不可更改)
- }
- }
- }
第02个小程序:按钮记录(RecordKeystrokes.cs)
- using System;
- using System.Windows;
- using System.Windows.Input;
- using System.Windows.Media;
- namespace Chapter03
- {
- public class RecordKeystrokes : Window
- {
- string str = ""; //定义字符串
- [STAThread]
- public static void Main()
- {
- Application app = new Application();
- app.Run(new RecordKeystrokes());
- }
- public RecordKeystrokes()
- {
- Title = "按键记录";
- //Content = "";
- }
- protected override void OnTextInput(TextCompositionEventArgs args)
- {
- base.OnTextInput(args);
- //string str = Content as string;
- //BackSpace键时
- if (args.Text == "/b")
- {
- if (str.Length > 0)
- str = str.Substring(0, str.Length - 1);
- }
- else
- {
- str += args.Text;
- }
- Content = str; //Content为指定字符串
- }
- }
- }
运行后,敲击键盘,客户区会显示按键相应的字符。类似于第一章的TypeYourTitle程序,不过这里还可以Enter(换行)和Tab(跳格)。
现在把这个程序作下修改:
- using System;
- using System.Windows;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Text;
- namespace Chapter03
- {
- public class RecordKeystrokes : Window
- {
- StringBuilder build = new StringBuilder("呵呵!");
- [STAThread]
- public static void Main()
- {
- Application app = new Application();
- app.Run(new RecordKeystrokes());
- }
- public RecordKeystrokes()
- {
- Title = "按键记录";
- Content = build;
- }
- protected override void OnTextInput(TextCompositionEventArgs args)
- {
- base.OnTextInput(args);
- //BackSpace键时
- if (args.Text == "/b")
- {
- if (build.Length > 0)
- build.Remove(build.Length - 1, 1);
- }
- else
- {
- build.Append(args.Text);
- }
- //Content = null;
- Content = build;
- }
- }
- }
与前一代码相比,换了种方式,功能完全一样。
但此时,程序却不能正常执行。运行后,客户区只会出现build初始化时的“呵呵!”,然后不管我们怎么敲击键盘它就是不再多显示一个字符。为什么呢?
原来窗口够聪明,它知道我指定相同的对象build给Cotent,就偷懒地不更新了,这时我们在OnTextInput()里的Content = build前加一句“Content = null;”,改变下Content的设定,让窗口活动下脑筋,它就回过神来了,程序也就恢复正常。
第03个小程序:显示图片(ShowMyPicture .cs)
- using System;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- namespace Chpter03
- {
- class ShowMyPicture : Window
- {
- [STAThread]
- public static void Main()
- {
- Application app = new Application();
- app.Run(new ShowMyFace());
- }
- public ShowMyPicture()
- {
- Title = "显示图片";
- Width = 384;
- Height = 288;
- Background = new LinearGradientBrush(Colors.Red,Colors.Blue,
- new Point(0,0),new Point(1,1));
- Uri uri = new Uri( //建立Uri对象,用来表示位图的位置
- "http://profile.csdn.net/changjiangboy/picture/1.jpg");
- //Uri uri = new Uri("file://C://Documents and Settings//Administrator//桌面//001.jpg");
- BitmapImage bitmap = new BitmapImage(uri); //将uri载入内存
- Image img = new Image(); //图像Image实例
- img.Source = bitmap; //设置图像资源
- img.Stretch = Stretch.None; //图像拉伸方式
- //img.Margin = new Thickness(10,10,10,10); //图像与窗口边界距离
- img.Opacity = 0.6; //图像透明度
- img.LayoutTransform = new RotateTransform(30); //顺时针旋转30度
- //图像放在客户区左上角
- //img.HorizontalAlignment = HorizontalAlignment.Left;
- //img.VerticalAlignment = VerticalAlignment.Top;
- Content = img; //实例img指给Content
- }
- }
- }
效果如下
第04个小程序:绘制椭圆(ShapeAnEllipse .cs)
- using System;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Shapes;
- namespace Chapter03
- {
- class ShapeAnEllipse : Window
- {
- [STAThread]
- public static void Main()
- {
- Application app = new Application();
- app.Run(new ShapeAnEllipse());
- }
- public ShapeAnEllipse()
- {
- Title = "绘制椭圆";
- Width = 384;
- Height = 288;
- Background = new LinearGradientBrush(Colors.Red, Colors.Blue,
- new Point(0, 0), new Point(1, 1));
- Ellipse elips = new Ellipse(); //定义椭圆对象
- elips.Width = 320; //椭圆宽度
- elips.Height = 240; //椭圆高度
- elips.Fill = Brushes.DimGray; //椭圆内部填充方式
- elips.StrokeThickness = 24; //椭圆轮廓宽度
- //把椭圆放置在左下角
- elips.HorizontalAlignment = HorizontalAlignment.Left;
- elips.VerticalAlignment = VerticalAlignment.Bottom;
- elips.Stroke = new LinearGradientBrush( //椭圆轮廓绘制方式
- Colors.CadetBlue, Colors.Chocolate,
- new Point(1, 0), new Point(0, 1));
- Content = elips;
- }
- }
- }
运行效果如下
第05个小程序:格式化文本(FormatTheText)
- using System;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Documents;
- namespace Chapter03
- {
- class FormatTheText : Window
- {
- [STAThread]
- public static void Main()
- {
- Application app = new Application();
- app.Run(new FormatTheText());
- }
- public FormatTheText()
- {
- Title = "格式化文本";
- TextBlock txt = new TextBlock();
- txt.FontSize = 32;
- txt.Inlines.Add("这是");
- txt.Inlines.Add(new Italic(new Run("斜体字")));
- txt.Inlines.Add(",这是");
- txt.Inlines.Add(new Bold(new Run("粗体字")));
- txt.Inlines.Add(",这是");
- txt.Inlines.Add(new Bold(new Italic(new Run("斜体粗字"))));
- txt.Inlines.Add("。");
- txt.TextWrapping = TextWrapping.Wrap; //文本换行方式
- Content = txt;
- }
- }
- }
程序用来组合TextBlock的Inlines集,InlineCollection类形的Inlines实现了Add方法,可以加入字符串对象、Inline对象、以及UIElement对象。然而,Bold和Italic构造接受Inline对象,而不接受字符串对象,所以程序先为每个Bold或Italic对象使用Run构造函数。
第06个小程序:粗体和斜体(ToggleBoldAndItalic.cs)
- using System;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- namespace Chapter03
- {
- public class ToggleBoldAndItalic : Window
- {
- [STAThread]
- public static void Main()
- {
- Application app = new Application();
- app.Run(new ToggleBoldAndItalic());
- }
- public ToggleBoldAndItalic()
- {
- Title = "粗体否?斜体否?";
- TextBlock text = new TextBlock();
- text.FontSize = 32;
- text.HorizontalAlignment = HorizontalAlignment.Center;
- text.VerticalAlignment = VerticalAlignment.Center;
- Content = text;
- string strQuote = "嘿! 你到底想怎么样?!";
- string[] strWords = strQuote.Split(); //分隔字符串
- foreach (string str in strWords)
- {
- Run run = new Run(str);
- run.MouseDown += RunOnMouseDown;
- text.Inlines.Add(run);
- text.Inlines.Add(" ");
- }
- }
- void RunOnMouseDown(object sender, MouseButtonEventArgs args)
- {
- Run run = sender as Run;
- //鼠标左击时,斜体切换
- if (args.ChangedButton == MouseButton.Left)
- run.FontStyle = run.FontStyle == FontStyles.Italic ?
- FontStyles.Normal : FontStyles.Italic;
- //鼠标右击时,粗体切换
- if (args.ChangedButton == MouseButton.Right)
- run.FontWeight = run.FontWeight == FontWeights.Bold ?
- FontWeights.Normal : FontWeights.Bold;
- }
- }
- }
Run类实例对象包含一连串格式化或未格式化文本的内联级别的流内容元素。
第07个小程序:显示椭圆(RenderTheGraphic.cs)
- using System;
- using System.Windows;
- using System.Windows.Media;
- namespace Chapter03
- {
- class RenderTheGraphic : Window
- {
- [STAThread]
- public static void Main()
- {
- Application app = new Application();
- app.Run(new RenderTheGraphic());
- }
- public RenderTheGraphic()
- {
- Title = "Render the Graphic";
- SimpleEllipse elips = new SimpleEllipse();
- Content = elips;
- }
- }
- class SimpleEllipse : FrameworkElement
- {
- protected override void OnRender(DrawingContext dc)
- {
- dc.DrawEllipse(Brushes.Blue, //填充椭圆的画笔
- new Pen(Brushes.Red, 24), //绘制椭圆笔画的笔(颜色,宽度)
- new Point(RenderSize.Width / 2, //椭圆的中心位置
- RenderSize.Height / 2),
- RenderSize.Width / 2 - 12, //椭圆的水平半径
- RenderSize.Height / 2 - 12); //椭圆的垂直半径
- }
- }
- }
窗口的Content属性想要的是继承自UIElement类的实例,因为此类定义一个OnRender的方法,负责在屏幕上显示此对象。
这里继承自FrameworkElement(它直接继承自UIElement)的SimpleEllipse 类重载OnRender方法,获得DrawingContext 对象,保留DrawEllipse信息。然后在构造函数中实例化SimpleEllipse 对象,并将对象指给Content。
- (WPF学习记录)第三章 Content的概念
- 数据挖掘:概念与技术(第三版)之第三章的学习记录
- 数据挖掘:概念与技术(第三版)之第二章的学习记录
- 数据挖掘:概念与技术(第三版)之第四章的学习记录
- 数据挖掘:概念与技术(第三版)之第五章的学习记录
- 数据挖掘:概念与技术(第三版)之第六章的学习记录
- 数据挖掘:概念与技术(第三版)之第七章的学习记录
- 数据挖掘:概念与技术(第三版)之第八章的学习记录
- 数据挖掘:概念与技术(第三版)之第九章的学习记录
- 数据挖掘:概念与技术(第三版)之第十章的学习记录
- 数据挖掘:概念与技术(第三版)之第十一章的学习记录
- 数据挖掘:概念与技术(第三版)之第十二章的学习记录
- 【《WPF手册》读书笔记系列】第三章-内容的概念
- WPF学习记录3_WPF重要概念
- [WPF] 简单画刷动画及Content的概念
- 数据挖掘:概念与技术(第三版)之第一章的学习记录
- (WPF学习记录)第十一章 单一孩子的Element
- (WPF学习记录)第七章 Canvas
- 全局意译 之 校对方法
- VC中基于 Windows 的精确定时
- MOSS字段编辑权限控制方案--发布源码
- 计算时间和空间复杂度
- MySQL远程维护时出错的原因。
- (WPF学习记录)第三章 Content的概念
- 厦门启动“移动旅游通”项目 提升旅游信息服务水平
- 带宽和增益带宽
- 一个典型的关于java(Overriding && Overloading)问题
- CFNetwork 基本结构
- DHTML设计VC界面《五》- 容器
- 软件测试开发(SDET) at Microsoft(1)
- 广东移动“旅游通”提供导游服务 - 导游服务
- 剪切板循环工具V1.0