[Gadgeteer]自製小畫家

有購買Gadgeteer套組的人大概會覺得顯示螢幕是跟其他機器人套組最不一樣、當中最新奇的一個模組吧!上次只有在Camera程式有使用到顯示幕的基礎功能,大家是否意猶未盡呢?這次就要教你如何寫像用粉筆在黑板上寫字,一個簡易版的小畫家!!

 

程式碼:

 

using System;

using System.Collections;

using System.Threading;

using Microsoft.SPOT;

using Microsoft.SPOT.Presentation;

using Microsoft.SPOT.Presentation.Controls;

using Microsoft.SPOT.Presentation.Media;

using Microsoft.SPOT.Touch;

 

using Gadgeteer.Networking;

using GT = Gadgeteer;

using GTM = Gadgeteer.Modules;

using Gadgeteer.Modules.GHIElectronics;

 

namespace singlecolorprinter

{

    public partial class Program

    {

        Window mainWindow;

        Canvas layout;

        Image background;

        Color drawingColor;

 

        void ProgramStarted()

        {

            drawingColor = GT.Color.White;    //定義畫筆顏色為白色

            SetupUI();

            background.TouchMove += new Microsoft.SPOT.Input.TouchEventHandler(mainWindow_T
ouchMove);                                       //設定觸碰事件

            Debug.Print("Program Start");

        }

 

        void SetupUI()

        {

            mainWindow = display_T35.WPFWindow;  

 

            layout = new Canvas();

            background = new Image();

            background.Bitmap = new Bitmap(320, 240);

            background.Height = 240;

            background.Width = 320;

 

            layout.Children.Add(background);

            Canvas.SetLeft(background, 0);

            Canvas.SetTop(background, 0);

 

            mainWindow.Child = layout;

        }

 

        void mainWindow_TouchMove(object sender, Microsoft.SPOT.Input.TouchEventArgs e)

        {

            TouchInput[] touches = e.Touches;

            int oldX = -1;

            int oldY = -1;

            foreach (TouchInput touch in touches)

            {

                int x = touch.X;

                int y = touch.Y;

                if (oldX != -1)

                {

                    background.Bitmap.DrawLine(drawingColor, 3, oldX, oldY, x, y);

                }

                oldX = x;

                oldY = y;

            }

            background.Invalidate();

        }

    }

}

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *