1 Dernière modification par Noham (03-07-2011 13:04)

Sujet : Tahoe II (.NET MF)

Bonjour à tous,

Je pocéde depuis deux jours une Tahoe II, je n'arrive pas à comprendre comment créer des élements sur l'écran, je souhaiterais créer un bouton ave un label et lors que je fait un "Touch" je souhaiterais changer la valeur du label.

Merci de votre aide

2

Re : Tahoe II (.NET MF)

Bonjour,

Voilà avec un peu d'aide (merci Pierre si tu passe dans le coin smile ) donc voici la démarche :

Création d'une classe nommé "Button.cs" :

using Microsoft.SPOT.Input;
using Microsoft.SPOT.Presentation.Media;

namespace Microsoft.SPOT.Presentation
{
    /// <summary>
    /// Permet de déssiner un bouton
    /// </summary>
    public class Button : UIElement
    {
        #region variables

        /// <summary>
        /// Text du bouton
        /// </summary>
        string _text = string.Empty;
        /// <summary>
        /// Font du text
        /// </summary>
        Font _font = null;
        /// <summary>
        /// Couleur du text
        /// </summary>
        Color _foreColor = ColorUtility.ColorFromRGB(0, 0, 0);
        /// <summary>
        /// Couleur du text quand il y a un appuye sur le bouton
        /// </summary>
        Color _pressedForeColor = ColorUtility.ColorFromRGB(255, 255, 255);
        private TextTrimming _trimming = TextTrimming.WordEllipsis;
        private TextAlignment _alignment = TextAlignment.Center;

        private int _textMarginX = 16;

        private int _textMarginY = 8;

        /// <summary>
        /// Permet de savoir l'état du bouton
        /// </summary>
        private bool _pressed = false;


        private SolidColorBrush _normalBackgroundBrush =
           new SolidColorBrush(ColorUtility.ColorFromRGB(192, 192, 192));

        private SolidColorBrush _pressedBackgroundBrush =
            new SolidColorBrush(ColorUtility.ColorFromRGB(128, 128, 128));

        private Pen _borderPen =
            new Pen(ColorUtility.ColorFromRGB(128, 128, 128));

        private Pen _pressedBorderPen =
            new Pen(ColorUtility.ColorFromRGB(128, 128, 128));

        private Pen _lightShade =
            new Pen(ColorUtility.ColorFromRGB(216, 216, 216));

        private Pen _darkShade =
            new Pen(ColorUtility.ColorFromRGB(64, 64, 64));

        #endregion

        #region propriété Public

        /// <summary>
        /// Permet de connaitre l'état du bouton
        /// </summary>
        protected bool IsPressed
        {
            get { return _pressed; }
        }
        protected int TextMarginY
        {
            get { return _textMarginY; }
            set { _textMarginY = value; }
        }
        protected int TextMarginX
        {
            get { return _textMarginX; }
            set { _textMarginX = value; }
        }
        public TextAlignment TextAlignment
        {
            get { return _alignment; }
            set { _alignment = value; }
        }

        public TextTrimming TextTrimming
        {
            get { return _trimming; }
            set { _trimming = value; }
        }
        public Color ForeColorPressed
        {
            get { return _pressedForeColor; }
            set { _pressedForeColor = value; }
        }
        public Color ForeColor
        {
            get { return _foreColor; }
            set { _foreColor = value; }
        }
        /// <summary>
        /// Font du text du bouton
        /// </summary>
        public Font Font
        {
            get { return _font; }
            set { _font = value; }
        }
        /// <summary>
        /// Text du bouton
        /// </summary>
        public string Text
        {
            get { return _text; }
            set { _text = value; }
        }
        public SolidColorBrush BackgroundColorPressed
        {
            get { return _pressedBackgroundBrush; }
            set { _pressedBackgroundBrush = value; }
        }

        public SolidColorBrush BackgroundColor
        {
            get { return _normalBackgroundBrush; }
            set { _normalBackgroundBrush = value; }
        }
        public Pen DarkShade
        {
            get { return _darkShade; }
            set { _darkShade = value; }
        }

        public Pen LightShade
        {
            get { return _lightShade; }
            set { _lightShade = value; }
        }

        public Pen PressedBorderColor
        {
            get { return _pressedBorderPen; }
            set { _pressedBorderPen = value; }
        }
        public Pen BorderColor
        {
            get { return _borderPen; }
            set { _borderPen = value; }
        }

        #endregion

        #region Event

        public event EventHandler Click;
        public event EventHandler Move;

        #endregion

        #region Constructeur
        /// <summary>
        /// Constructeur
        /// </summary>
        /// <param name="Text">Text du bouton</param>
        /// <param name="Font">Type de l'écriture du text du bouton</param>
        public Button(string text, Font font)
        {
            _text = text;
            _font = font;

            HorizontalAlignment = HorizontalAlignment.Left;
            VerticalAlignment = VerticalAlignment.Bottom;

            int textWidth;
            int textHeight;
            _font.ComputeExtent(_text, out textWidth, out textHeight);

            this.Width = textWidth + _textMarginX * 2;
            this.Height = textHeight + _textMarginY * 2;
        }

        /// <summary>
        /// Constructeur
        /// </summary>
        /// <param name="Text">Text du bouton</param>
        /// <param name="Font">Type de l'écriture du text du bouton</param>
        /// <param name="Width">Largeur du bouton</param>
        /// <param name="Height">Hauteur du bouton</param>
        public Button(string caption, Font font, int width, int height)
        {
            this.Width = width;
            this.Height = height;
            _text = caption;
            _font = font;
        }

        #endregion

        #region Méthodes
        /// <summary>
        /// Aspet du bouton
        /// </summary>
        /// <param name="dc"></param>
        public override void OnRender(DrawingContext dc)
        {
            int x;
            int y;

            SolidColorBrush brush;
            Pen pen;
            Color color;
            Pen shade1;
            Pen shade2;

            // Check the pressed state and draw accordingly.
            if (_pressed)
            {
                brush = _pressedBackgroundBrush;
                pen = _pressedBorderPen;
                color = _pressedForeColor;
                shade1 = _darkShade;
                shade2 = _lightShade;
            }
            else
            {
                brush = _normalBackgroundBrush;
                pen = _borderPen;
                color = _foreColor;
                shade1 = _lightShade;
                shade2 = _darkShade;
            }

            GetLayoutOffset(out x, out y);

            // Draw the base rectangle of the button.
            dc.DrawRectangle(brush, pen, 1, 1, this.Width - 1, this.Height - 1);

            // Draw the caption.
            string caption = _text;
            dc.DrawText(ref caption, _font, color, 0, _textMarginY, this.Width,
                this.Height, _alignment, _trimming);

            // Shade the outline of the rectangle for classic button look.
            dc.DrawLine(shade1, 1, 1, this.Width - 1, 1);
            dc.DrawLine(shade1, 1, 1, 1, this.Width - 1);
            dc.DrawLine(shade2, this.Width - 1, 1, this.Width - 1, this.Height - 1);
            dc.DrawLine(shade2, 1, this.Height - 1, this.Width - 1, this.Height - 1);
        }

        /// <summary>
        /// Handles the touch down event.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnTouchDown(TouchEventArgs e)
        {
            // Flag for drawing state.
            _pressed = true;
            // Capture the touch.
            TouchCapture.Capture(this);
            // Trigger a redraw.
            Invalidate();
        }

        /// <summary>
        /// Handles the touch up event.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnTouchUp(TouchEventArgs e)
        {
            // Flag for the drawing state.
            _pressed = false;

            // Capture the touch.
            TouchCapture.Capture(this, CaptureMode.None);
            // Trigger a redraw.
            Invalidate();

            // Fire a click event.
            EventArgs args = new EventArgs();
            OnClick(args);
        }

        /// <summary>
        /// Handles the touch move.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnTouchMove(TouchEventArgs e)
        {            // Fire a click event.
            EventArgs args = new EventArgs();
            OnMove(args);
        }

        /// <summary>
        /// Handles a click.
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnClick(EventArgs e)
        {
            if (Click != null)
            {
                Click(this, e);
            }
        }

        /// <summary>
        /// Handles a click.
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnMove(EventArgs e)
        {
            if (Move != null)
            {
                Move(this, e);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="arrangeWidth"></param>
        /// <param name="arrangeHeight"></param>
        protected override void ArrangeOverride(int arrangeWidth,
            int arrangeHeight)
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="availableWidth"></param>
        /// <param name="availableHeight"></param>
        /// <param name="desiredWidth"></param>
        /// <param name="desiredHeight"></param>
        protected override void MeasureOverride(int availableWidth,
            int availableHeight, out int desiredWidth,
            out int desiredHeight)
        {
            desiredWidth = (availableWidth > this.Width) ?
            this.Width : availableWidth;
            desiredHeight = (availableHeight > this.Height) ?
            this.Height : availableHeight;
        }


        #endregion
    }
}

En suite dans votre méthode "public Window CreateWindow()"
ajouter la création du bouton comme suite :

            Microsoft.SPOT.Presentation.Button button
                = new Microsoft.SPOT.Presentation.Button("http://forum.pobot.org",
                    Resources.GetFont(Resources.FontResources.small), 170, 70);
            button.HorizontalAlignment = HorizontalAlignment.Center;
            button.Click += new EventHandler(button_Click);

Remarquez que j'ai mis "Microsoft.SPOT.Presentation" comme namespace smile

En ensuite l'evenement déclanché sur le "Touch" du bouton.

       void button_Click (object sender, EventArgs e)
        {
            Microsoft.SPOT.Presentation.Button button 
                = (Microsoft.SPOT.Presentation.Button)sender;
            if (button.Text == "[url]http://forum.pobot.org[/url]")
                button.Text = "[url]http://www.pobot.org[/url]";
            else
                button.Text = "[url]http://forum.pobot.org[/url]";
        }

En expérant que ca pourra aider d'autre personnes !!!

3

Re : Tahoe II (.NET MF)

Merci beaucoup pour ce code.