你的位置是:网站首页--- .Net图形编程---.Net图形编程接口

Windows Presentation Foundation中一个最重要的概念之一Content


【 字体:


写在前面:WPF中,Window对象最重要的类型是Content,所有在窗口显示的内容都要设置给Content,Content在Window对象中,是唯一的,也就是说只能设置一个事物给Content,这个事物可以是字符,可以是图像,可以是按钮等等,因为Content只是object属性。但是,不可以将另一个Window设置给Content,因为Window必须是一个根对象。

效果图
下面的图形,在WinForm平台下,可以在OnPaint方法内,使用DrawString来设置,但是在WPF中,你最好放弃这个想法。WPF是比较智能的,不用手动设置不断地画(WinFrom平台下需要不断地画才能显示内容)。下面的效果,只是将一个String设置到Content,别的就没什么了。Content属性,是Window类从ContentControl类继承过来的。
 

源代码
在WPF中,没有Font类,你只能学会使用FontFamily,FontSize,FontStyle,FontWeight,属性,下面的代码设置了Content之后,再设置字体格式和大小。注意到,WPF中有很多系统已经定义好的类,如Brushes是系统已经定义的画刷类,而一般系统设置的类都是es或s结尾的,如Colors,FontStyles,FontWeights都遵守这一原则,如果你想设置的内容比较普通,你可以试试使用这个系统的类。
using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;

namespace WPFGradientBrush
{
    public class WindowContent:Window
    {
        [STAThread]
        public static void Main()
        {
            Application _application = new Application();
            _application.Run(new WindowContent());
        }

        public WindowContent()
        {
            Background = Brushes.Pink;
            Title = "测试Content属性";
            Width = 96 * 4;
            Height = 96 * 4;
            Content = "在Content上面DrawString\r\r      事实上,不是在DrawString\r\r        只是将String设置到Content";
            FontFamily = new FontFamily("宋体");
            FontSize = 20;
        }      
    }
}


出处:小作坊网ChakMan原创

Copyright © 2006-2008 小作坊网 All rights reserved.
备案号:粤ICP备09058104号          电子信箱: jingle_guan#163.com