Click here to Skip to main content
15,894,343 members
Articles / Mobile Apps

Windows Mobile - Attractive UI: Part I

Rate me:
Please Sign up or sign in to vote.
4.92/5 (11 votes)
17 Sep 2009GPL32 min read 51K   1K   39  
Windows Mobile - attractive UI (description about AlphaMobilecontrols).
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace AlphaMobileControls
{
    public class AlphaButton : AlphaControl
    {
        private bool _border;
        private bool _selectable;
        private bool _stretchWindow;
        private Color _lineColor = Color.FromArgb(88, 155, 171);
        private Color _selectBackColor = Color.FromArgb(88, 155, 171);
        private Color _selectForeColor = Color.FromArgb(88, 155, 171);


        public AlphaButton()
        {
            this.ParentChanged += new EventHandler(AlphaImageButton_ParentChanged);
            this.EnabledChanged += new EventHandler(AlphaImageButton_EnabledChanged);
            this.GotFocus += new EventHandler(AlphaButton_GotFocus);
            this.LostFocus += new EventHandler(AlphaButton_LostFocus);
        }

        void AlphaButton_LostFocus(object sender, EventArgs e)
        {
            Refresh();
        }

        void AlphaButton_GotFocus(object sender, EventArgs e)
        {
            Refresh();
        }

        public bool Selectable
        {
            get
            {
                return this._selectable;
            }
            set
            {
                this._selectable = value;
            }
        }

        public bool StretchWindow
        {
            get {return this._stretchWindow; }
            set { this._stretchWindow = value; }
        }

        public Color LineColor
        {
            get
            {
                return _lineColor;
            }
            set
            {
                this._lineColor = value;
            }
        }

        public Color SelectBackColor
        {
            get
            {
                return _selectBackColor;
            }
            set
            {
                this._selectBackColor = value;
            }
        }

        public Color SelectForeColor
        {
            get
            {
                return _selectForeColor;
            }
            set
            {
                this._selectForeColor = value;
            }
        }


        void AlphaImageButton_ParentChanged(object sender, EventArgs e)
        {
            this.Parent.MouseDown += new MouseEventHandler(Parent_MouseDown);
            this.Parent.MouseUp += new MouseEventHandler(Parent_MouseUp);
            this.Parent.MouseMove += new MouseEventHandler(Parent_MouseMove);
        }

        void AlphaImageButton_EnabledChanged(object sender, EventArgs e)
        {
            Refresh();
        }

        void Parent_MouseDown(object sender, MouseEventArgs e)
        {
            if (!this.Visible || !this.Enabled || !HitTest(e.X, e.Y))
                return;
            if (Selectable)
            this.Focus();
            Refresh();
        }

        void Parent_MouseUp(object sender, MouseEventArgs e)
        {
            if (!this.Visible || !this.Enabled  || !HitTest(e.X, e.Y))
                return;


            Refresh();
            if(Selectable)
            this.Focus();
            this.OnClick(null);
        }

        void Parent_MouseMove(object sender, MouseEventArgs e)
        {
            if (!this.Visible || !this.Enabled)
                return;

            bool hit = HitTest(e.X, e.Y);

            Refresh();
        }

        /// <summary>
        /// Active or not a thin border around this label.
        /// </summary>
        public bool Border
        {
            get { return _border; }
            set
            {
                if (_border == value)
                    return;
                _border = value;
                Refresh();
            }
        }

        #region AlphaControl Members
        /// <summary>
        /// Draws the Button.
        /// </summary>
        public override void Draw(Graphics gx)
        {
            Pen pen = new Pen(this.ForeColor);
            SolidBrush brush = new SolidBrush(this.ForeColor);
            SolidBrush fillBrush = new SolidBrush(this.ForeColor);
            Rectangle rect =
                new Rectangle(this.Bounds.X, this.Bounds.Y, this.Width - 1, this.Height - 1);

            // Draw the border
            if (_border)
            {
                gx.DrawRectangle(pen, rect);
            }
            //Focus
            if (Focused)
            {
                fillBrush.Color = SelectBackColor;
                gx.FillRectangle(fillBrush, rect);
                brush.Color = SelectForeColor;
            }

            // Specify a rectangle to activate the line wrapping
            rect.Inflate(-4, -2);
            gx.DrawString(this.Text, this.Font, brush, rect);

            //Drawing Line Below
            pen.Color = LineColor;
            gx.DrawLine(pen, this.Bounds.X, this.Bounds.Y + this.Bounds.Height - 1, this.Bounds.X + this.Width - 1, this.Bounds.Y + this.Height - 1);
        }
        #endregion

        
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Technical Lead
India India
Atanu Mandal works as a technical lead in a leading Software service company. He is from Kolkata, India. His primary interests are website development, software architecture and integration.

Comments and Discussions