1.Content属性及字体相关的属性
using System; using System.Windows; using System.Windows.Media; namespace LY.DisplaySomeText { public class DisplaySomeText:Window { Brush brush = new LinearGradientBrush(Colors.Black, Colors.White, new Point(0, 0), new Point(1, 1)); [STAThread] public static void Main() { new Application().Run(new DisplaySomeText()); } public DisplaySomeText() { Title = "Display Some Text"; //Content = "Content can be simple text!"; //Content = System.EventArgs.Empty; Content = DateTime.Now; //设置字体系列 FontFamily = new FontFamily("宋体"); FontSize = 48; FontStyle = FontStyles.Italic; FontWeight = FontWeights.Bold; //Background = brush; Foreground = brush; //将窗口大小调整为适应内容的大小 SizeToContent = SizeToContent.WidthAndHeight; //设置边框的画刷 BorderBrush = Brushes.Red; //设置上下左右边框的宽度 BorderThickness = new Thickness(25, 50, 75, 100); } } }
1)没有Font类,需通过FontFamily(字体系列),FontSize、FontStyle、FontWeight等属性来设置。
using System; using System.Windows; using System.Windows.Media; using System.Windows.Input; namespace LY.RecordKeyStrokes { public class RecordKeyStrokes:Window { [STAThread] public static void Main() { Application app = new Application(); app.Run(new RecordKeyStrokes()); } public RecordKeyStrokes() { Title = "Record Key Strokes"; Content = ""; } protected override void OnTextInput(TextCompositionEventArgs e) { base.OnTextInput(e); string str = Content as string; if (e.Text == "\b") { if (str.Length > 0) str = str.Substring(0, str.Length - 1); } else { str += e.Text; } Content = str; } } }
1)Content属性的值发生变化后,屏幕会自动更新。
2.图像的显示——Image类、Shape类
using System; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; namespace LY.ShowMyFace { public class ShowMyFace:Window { [STAThread] public static void Main() { new Application().Run(new ShowMyFace()); } public ShowMyFace() { Title = "Show My Face"; Uri uri = new Uri(@"C:\Users\LZ\Desktop\XX.png"); BitmapImage bitmap = new BitmapImage(uri); Image image = new Image(); image.Source = bitmap; image.HorizontalAlignment = HorizontalAlignment.Left; image.VerticalAlignment = VerticalAlignment.Center; //设置图片的边界 image.Margin = new Thickness(10, 20, 30, 40); Content = image; //显示一个椭圆 //Ellipse elips = new Ellipse(); //elips.Fill = Brushes.AliceBlue; //elips.StrokeThickness = 50; //Content = elips; } } }
1)通过Image.Source指定一个Image对象的图片实例。
2)常见的几何图形类在System.Windows.Shapes命名空间中。
3)TextBlock对象可以将一段文字以不同的格式拼接在一起。
4)ContentControl类与ContentElement类不同,前者是控件,可以直接显示出来,后者要借助于前者才能显示出来。