.Net Micro Framework中的Shapes命名空間
在Microsoft.SPOT.Presentation.Shapes命名空間下,包含幾個形狀對象,主要有Ellipse、Line、Polygon、Rectangle,同樣也只有Rectangle實現(xiàn)的***,其他形狀都不支持填充色,雖然每個對象都有Fill屬性。
讓人奇怪的是,每個形狀對象都不能設(shè)置left和top坐標,僅能設(shè)置寬度和高度,用起來很不習(xí)慣。
StackPanel類是Panel的派生類,從字面意思上看,就是可以堆疊的面板。意如其名,它可以包含多個子對象,不過每一對象都不能重疊,以特有的方式堆疊在一起。
有如下幾個屬性方法控制堆疊方式:
1、Orientation屬性,有兩種方式:Orientation.Horizontal,Orientation.Vertical。設(shè)置該屬性后,StackPanel的子對象的坐標系就會發(fā)生變化(很可惜字體的方向并沒有從根本上改變)。
2、HorizontalAlignment、VerticalAlignment屬性,設(shè)置子對象的堆疊方式。枚舉定義如下。
- public enum HorizontalAlignment
 - {
 - Left = 0,
 - Center = 1,
 - Right = 2,
 - Stretch = 3,
 - }
 - public enum VerticalAlignment
 - {
 - Top = 0,
 - Center = 1,
 - Bottom = 2,
 - Stretch = 3,
 - }
 
3、SetMargin方法,設(shè)置邊界空白大小。
完整的代碼如下,
- 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()
 - {
 - //創(chuàng)建窗體
 - WindowsDrawing win = new WindowsDrawing();
 - //程序運行
 - new Program().Run(win);
 - }
 - internal sealed class WindowsDrawing : Window
 - {
 - public WindowsDrawing()
 - {
 - this.Width = SystemMetrics.ScreenWidth;
 - this.Height = SystemMetrics.ScreenHeight;
 - //可設(shè)置顯示方向(水平,垂直)
 - //StackPanel panel = new StackPanel(Orientation.Vertical);
 - StackPanel panel = new StackPanel(Orientation.Horizontal);
 - //設(shè)置邊界空白
 - panel.SetMargin(10);
 - //設(shè)置對象堆疊的方式
 - panel.HorizontalAlignment = HorizontalAlignment.Center;
 - panel.VerticalAlignment = VerticalAlignment.Center;
 - this.Child = panel;
 - //添加文本
 - Text txt = new Text(Resources.GetFont(Resources.FontResources.small), "yefan");
 - //不能設(shè)置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()
 - };
 - //設(shè)置形狀對象必要的參數(shù)(各對象不能重疊,只能堆疊在一起)
 - foreach (Shape s in shapes)
 - {
 - s.Fill = new SolidColorBrush(ColorUtility.ColorFromRGB(0, 255, 0));
 - s.Stroke = new Pen(Color.Black, 2);
 - //不能設(shè)置left,top坐標
 - s.Height = 40;
 - s.Width = 40;
 - panel.Children.Add(s);
 - }
 - }
 - }
 - }
 - }
 
僅修改這句代碼 StackPanel panel = new StackPanel(Orientation.Horizontal);中的參數(shù)就可以實現(xiàn)兩種不同的效果,如下面兩圖所示:
總的來說,我覺得MF提供的圖像對象還很不完善,不僅一些基本功能沒有完成(如填充、線寬),并且無法設(shè)置形狀對象的絕對坐標(left,top),同時總類也特別少,希望以后的版本中能有很大的提升。
【編輯推薦】















 
 
 
 
 
 
 