// Copyright (C) Microsoft Corporation. All rights reserved. using System; using System.Collections; using System.Threading; using Microsoft.SPOT; using Microsoft.SPOT.Input; using Microsoft.SPOT.Hardware; using Microsoft.SPOT.Presentation; using Microsoft.SPOT.Presentation.Media; using Microsoft.SPOT.Presentation.Controls; using Microsoft.SPOT.Presentation.Shapes; using PresentationDemo; /**/// publicsealedclass MyApp : Application ...{ // This static field prevents the object from being GC'd privatestatic GpioButtonInputProvider s_gpioInputProvider; public Font NinaBFont; public Font SmallFont; public Bitmap Snowflake; private MyApp() ...{ // Initialize the Buttons/Pins dispatcher s_gpioInputProvider =new GpioButtonInputProvider(this.Dispatcher, null); // Load some resources NinaBFont = Resources.GetFont(Resources.FontResources.NinaBFont); SmallFont = Resources.GetFont(Resources.FontResources.SmallFont); Snowflake = Resources.GetBitmap(Resources.BitmapResources.Snowflake); } protectedoverridevoid OnStartup(EventArgs e) ...{ // Create and set the application's main window this.MainWindow =new MainMenuWindow(this); base.OnStartup(e); } publicvoid GoHome() ...{ Buttons.Focus(this.MainWindow); // Set focus back to the main window } publicstaticvoid Main() ...{ new MyApp().Run(); // Start the app's main window } } /**/// // This is the base class of all our windows; it makes every window visible, // sets the window's size to the full size of the LCD, and give the window focus internalclass PresentationWindow : Window ...{ protected MyApp m_app; protected PresentationWindow(MyApp app) ...{ m_app = app; // Make the window visible and the size of the LCD this.Visibility = Visibility.Visible; this.Width = SystemMetrics.ScreenWidth; this.Height = SystemMetrics.ScreenHeight; Buttons.Focus(this); // Set focus to this window } protectedoverridevoid OnButtonDown(ButtonEventArgs e) ...{ // Remove this window form the Window Manager this.Close(); // When any button is pressed, go back to the Home page m_app.GoHome(); } } /**/// internalsealedclass MainMenuWindow : PresentationWindow ...{ private ListBox m_listbox; public ListBox MainListBox ...{ get...{ return m_listbox; } } public MainMenuWindow(MyApp app) : base(app) ...{ Color instructionTextColor = ColorUtility.ColorFromRGB(192, 192, 192); Color backgroundColor = ColorUtility.ColorFromRGB(26, 118, 183); Color unselectedItemColor = ColorUtility.ColorFromRGB(192, 192, 255); // Unselected listbox item color Color selectedItemColor = Colors.White; // Selected listbox item color // The Main window contains a veritcal StackPanel StackPanel panel =new StackPanel(Orientation.Vertical); this.Child = panel; // The top child contains text with instructions TextFlow textflow =new TextFlow(); textflow.TextAlignment = TextAlignment.Center; textflow.Visibility = Visibility.Visible; textflow.TextRuns.Add( new TextRun(Resources.GetString(Resources.StringResources.SelectAnItemFromBelow), app.NinaBFont, instructionTextColor)); panel.Children.Add(textflow); // Add a blank line to the stack panel.Children.Add(textflow =new TextFlow()); textflow.TextRuns.Add("", app.NinaBFont, instructionTextColor); textflow.Visibility = Visibility.Visible; // The next child contains a listbox with options m_listbox =new ListBox(); // Prepare the listbox Buttons.Focus(m_listbox); panel.Children.Add(m_listbox); this.Background = m_listbox.Background =new SolidColorBrush(backgroundColor); m_listbox.SelectionChanged +=delegate(Object sender, SelectionChangedEventArgs e) ...{ Int32 previousSelectedIndex = e.PreviousSelectedIndex; if (previousSelectedIndex !=-1) ...{ // If there was a previous index // Change previously-selected listbox item color to unselected color ((Text)m_listbox.Items[previousSelectedIndex].Child).ForeColor = unselectedItemColor; } // Change newly-selected listbox item color to selected color ((Text)m_listbox.Items[e.SelectedIndex].Child).ForeColor = selectedItemColor; }; // Add the items to the listbox foreach (String s innew String[] ...{ "Vertical Stack", "Horizontal Stack", "Canvas", "Diagonal" }) ...{ Text text =new Text(m_app.NinaBFont, s +" Panel Demo"); text.ForeColor = unselectedItemColor; text.TextAlignment = TextAlignment.Center; text.Width =this.Width; ListBoxItem lbi =new ListBoxItem(); lbi.Background = m_listbox.Background; lbi.Child = text; m_listbox.Items.Add(lbi); } m_listbox.SelectedIndex =0; // Add a blank line in the stack panel.Children.Add(textflow =new TextFlow()); textflow.TextRuns.Add("", app.NinaBFont, instructionTextColor); textflow.Visibility = Visibility.Visible; // The bottom child contains text with return instructions textflow =new TextFlow(); textflow.TextAlignment = TextAlignment.Center; textflow.Visibility = Visibility.Visible; textflow.TextRuns.Add( new TextRun("(After viewing a Panel Demo, hit Enter to return to this screen)", app.NinaBFont, instructionTextColor)); panel.Children.Add(textflow); } protectedoverridevoid OnButtonDown(ButtonEventArgs e) ...{ // If <Enter> button is pressed, go into the selected demo if (e.Button == Button.Select) ...{ switch (MainListBox.SelectedIndex) ...{ case0: // Vertical Stack Panel Demo new StackPanelDemo(m_app, Orientation.Vertical); break; case1: // Horizontal Stack Panel Demo new StackPanelDemo(m_app, Orientation.Horizontal); break; case2: // Canvas Panel Demo new CanvasPanelDemo(m_app); break; case3: // Diagonal Panel Demo new DiagonalPanelDemo(m_app); break; } } // Don't call base implementation (base.OnButtonDown) or we'll go back Home } protectedoverridevoid OnGotFocus(FocusChangedEventArgs e) ...{ // Whenever this window gets focus, it gives it to its listbox Buttons.Focus(m_listbox); base.OnGotFocus(e); } } /**/// internalsealedclass StackPanelDemo : PresentationWindow ...{ // This class shows how to build your own shape drawing in a DrawingContext privatesealedclass Cross : Shape ...{ public Cross() ...{ } publicoverridevoid OnRender(DrawingContext dc) ...{ // Draw a line from top, left to bottom, right dc.DrawLine(base.Stroke, 0, 0, Width, Height); // Draw a line from top, right to bottom, left dc.DrawLine(base.Stroke, Width, 0, 0, Height); } } public StackPanelDemo(MyApp app, Orientation orientation) : base(app) ...{ StackPanel panel =new StackPanel(orientation); this.Child = panel; panel.Visibility = Visibility.Visible; Shape[] shapes =new Shape[] ...{ new Ellipse(), new Line(), new Polygon(new Int32[] ...{ 0, 0, 50, 0, 50, 50, 0, 50 }), // A Square new Rectangle(), new Cross() // Our own custom shape }; for (Int32 x =0; x < shapes.Length; x++) ...{ Shape s = shapes[x]; s.Fill =new SolidColorBrush(ColorUtility.ColorFromRGB(0, 255, 0)); s.Stroke =new Pen(Color.Black, 2); s.Visibility = Visibility.Visible; s.HorizontalAlignment = HorizontalAlignment.Center; s.VerticalAlignment = VerticalAlignment.Center; s.Height = Height -1; s.Width = Width -1; if (panel.Orientation == Orientation.Horizontal) s.Width /= shapes.Length; else s.Height /= shapes.Length; panel.Children.Add(s); } } } /**/// internalsealedclass CanvasPanelDemo : PresentationWindow ...{ public CanvasPanelDemo(MyApp app) : base(app) ...{ Canvas canvas =new Canvas(); this.Child = canvas; this.Background =new SolidColorBrush(ColorUtility.ColorFromRGB(0, 255, 255)); for (Int32 x =0; x < Width; x += Width /4) ...{ for (Int32 y =0; y < Height; y += Height /4) ...{ Text text =new Text(m_app.SmallFont, " ("+ x +","+ y +")"); Canvas.SetLeft(text, x); Canvas.SetTop(text, y); canvas.Children.Add(text); } } } } /**/// internalsealedclass DiagonalPanelDemo : PresentationWindow ...{ public DiagonalPanelDemo(MyApp app) : base(app) ...{ DiagonalPanel panel =new DiagonalPanel(); this.Child = panel; this.Background =new LinearGradientBrush( ColorUtility.ColorFromRGB(192, 0, 0), ColorUtility.ColorFromRGB(32, 0, 0), 0, 0, Width, Height); for (Int32 x =0; x <4; x++) ...{ Bitmap b =new Bitmap(Width /4, Height /4); b.StretchImage(0, 0, app.Snowflake, b.Width, b.Height, (UInt16)((x +1) *50)); Image image =new Image(b); panel.Children.Add(image); } } // This class shows how to build your own Panel privatesealedclass DiagonalPanel : Panel ...{ public DiagonalPanel() ...{ } protectedoverridevoid MeasureOverride(int availableWidth, int availableHeight, outint desiredWidth, outint desiredHeight) ...{ // Called to calculate the width/height desired desiredWidth =0; desiredHeight =0; foreach (UIElement child in Children) ...{ if (child.Visibility != Visibility.Collapsed) ...{ child.Measure(Int32.MaxValue, Int32.MaxValue); Int32 childWidth, childHeight; child.GetDesiredSize(out childWidth, out childHeight); desiredWidth += childWidth; desiredHeight += childHeight; } } } protectedoverridevoid ArrangeOverride(int arrangeWidth, int arrangeHeight) ...{ Int32 x =0, y =0; foreach (UIElement child in Children) ...{ if (child.Visibility != Visibility.Collapsed) ...{ Int32 childWidth, childHeight; child.GetDesiredSize(out childWidth, out childHeight); child.Arrange(x, y, childWidth, childHeight); x += childWidth; y += childHeight; } } } } } /**///