在Microsoft.SPOT.Presentation.Shapes命名空间下,包含几个形状对象,主要有Ellipse、Line、Polygon、Rectangle,同样也只有Rectangle实现的***,其他形状都不支持填充色,虽然每个对象都有Fill属性。
让人奇怪的是,每个形状对象都不能设置left和top坐标,仅能设置宽度和高度,用起来很不习惯。
StackPanel类是Panel的派生类,从字面意思上看,就是可以堆叠的面板。意如其名,它可以包含多个子对象,不过每一对象都不能重叠,以特有的方式堆叠在一起。
有如下几个属性方法控制堆叠方式:
1、Orientation属性,有两种方式:Orientation.Horizontal,Orientation.Vertical。设置该属性后,StackPanel的子对象的坐标系就会发生变化(很可惜字体的方向并没有从根本上改变)。
2、HorizontalAlignment、VerticalAlignment属性,设置子对象的堆叠方式。枚举定义如下。
public enum HorizontalAlignment
{
Left = 0,
Center = 1,
Right = 2,
Stretch = 3,
}
public enum VerticalAlignment
{
Top = 0,
Center = 1,
Bottom = 2,
Stretch = 3,
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
3、SetMargin方法,设置边界空白大小。
完整的代码如下,
using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Input;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Presentation.Shapes;
namespace MFWindow
{
public class Program : Microsoft.SPOT.Application
{
public static void Main()
{
//创建窗体
WindowsDrawing win = new WindowsDrawing();
//程序运行
new Program().Run(win);
}
internal sealed class WindowsDrawing : Window
{
public WindowsDrawing()
{
this.Width = SystemMetrics.ScreenWidth;
this.Height = SystemMetrics.ScreenHeight;
//可设置显示方向(水平,垂直)
//StackPanel panel = new StackPanel(Orientation.Vertical);
StackPanel panel = new StackPanel(Orientation.Horizontal);
//设置边界空白
panel.SetMargin(10);
//设置对象堆叠的方式
panel.HorizontalAlignment = HorizontalAlignment.Center;
panel.VerticalAlignment = VerticalAlignment.Center;
this.Child = panel;
//添加文本
Text txt = new Text(Resources.GetFont(Resources.FontResources.small), "yefan");
//不能设置left,top坐标
txt.Width = 100;
txt.Height = 30;
panel.Children.Add(txt);
//添加不同的形状对象
Shape[] shapes = new Shape[]
{
new Ellipse(),
new Line(),
new Polygon(new Int32[] { 0, 0, 10, 0, 10, 10, 0, 10 }),
new Rectangle()
};
//设置形状对象必要的参数(各对象不能重叠,只能堆叠在一起)
foreach (Shape s in shapes)
{
s.Fill = new SolidColorBrush(ColorUtility.ColorFromRGB(0, 255, 0));
s.Stroke = new Pen(Color.Black, 2);
//不能设置left,top坐标
s.Height = 40;
s.Width = 40;
panel.Children.Add(s);
}
}
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
- 128.
- 129.
- 130.
- 131.
- 132.
- 133.
- 134.
- 135.
- 136.
- 137.
- 138.
- 139.
- 140.
- 141.
- 142.
- 143.
- 144.
- 145.
仅修改这句代码 StackPanel panel = new StackPanel(Orientation.Horizontal);中的参数就可以实现两种不同的效果,如下面两图所示:
总的来说,我觉得MF提供的图像对象还很不完善,不仅一些基本功能没有完成(如填充、线宽),并且无法设置形状对象的绝对坐标(left,top),同时总类也特别少,希望以后的版本中能有很大的提升。
【编辑推荐】