Click here to Skip to main content
15,886,724 members
Articles / Mobile Apps

Windows Mobile Password Safe

Rate me:
Please Sign up or sign in to vote.
4.87/5 (58 votes)
12 Jan 2009CPOL16 min read 159.6K   3.1K   139  
A password safe with a touch screen UI introducing Fluid Controls.
using System;

using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Diagnostics;
using Fluid.Drawing.GdiPlus;
using Fluid.GdiPlus;

namespace Fluid.Controls
{
    public enum ListBoxItemState
    {
        Default = 0,
        Selected = 1,
        Pressed = 2,
        Hovered = 4,
        Transparent = 8
    }


    public class ListBoxItemPaintEventArgs : ListBoxItemEventArgs
    {

        public ListBoxItemPaintEventArgs()
            : base()
        {
            StringFormat = new StringFormat();
            StringFormat.LineAlignment = StringAlignment.Center;
            StringFormat.Alignment = StringAlignment.Near;
        }

        public FluidTemplate Template { get; internal set; }

        public bool IsSelected
        {
            get { return (State & ListBoxItemState.Selected) != 0; }
        }

        public bool IsTransparent
        {
            get { return (State & ListBoxItemState.Transparent) != 0; }
        }

        public bool IsHovered
        {
            get { return (State & ListBoxItemState.Hovered) != 0; }
        }

        public bool IsPressed
        {
            get { return (State & ListBoxItemState.Pressed) != 0; }
        }


        public ListBoxItemState State { get; internal set; }

        public SizeF ScaleFactor { get; internal set; }

        /// <summary>
        /// Gets or sets the foreground color for the item.
        /// </summary>
        public Color ForeColor { get; set; }

        /// <summary>
        /// Gets or sets the background color for the item.
        /// </summary>
        public Color BackColor { get; set; }

        /// <summary>
        /// Gets or sets the border color for the item.
        /// </summary>
        public Color BorderColor { get; set; }

        /// <summary>
        /// Gets the graphics to paint the item.
        /// </summary>
        public Graphics Graphics { get; internal set; }

        public Region Region { get; internal set; }


        /// <summary>
        /// Get the bounds where to paint the item.
        /// </summary>
        public Rectangle ClientBounds { get; internal set; }

        /// <summary>
        /// Gets or sets the font for the item text.
        /// </summary>
        public Font Font { get; set; }

        /// <summary>
        /// Gets or sets the text for the item.
        /// </summary>
        public string Text { get; set; }

        /// <summary>
        /// Gets or sets whether the event was handled, thus no default paint will occur.
        /// </summary>
        public bool Handled { get; set; }

        /// <summary>
        /// Gets the StringFormat for use with the text.
        /// </summary>
        public StringFormat StringFormat { get; internal set; }

        private FluidPaintEventArgs paintEvent = new FluidPaintEventArgs();

        public void PaintTemplate()
        {

            if (Template != null)
            {
                Template.BackColor = this.BackColor;
                Template.ForeColor = ForeColor;
                FluidPaintEventArgs e = paintEvent;
                e.Graphics = Graphics;
                e.Region = Region;
                e.ControlBounds = ClientBounds;
                e.ScaleFactor = ScaleFactor;
                Template.OnPaint(e);
            }
        }


        /// <summary>
        /// Paints the default background of the item.
        /// </summary>
        public void PaintDefaultBackground()
        {

            using (Brush backGround = new SolidBrush(BackColor))
            {
                Graphics g = Graphics;
                Rectangle r = ClientBounds;
                g.FillRectangle(backGround, r);
            }
            //g.Clear(BackColor);

            PaintDefaultBorder();
        }

        public void PaintDefaultBorder()
        {
            Graphics g = Graphics;
            Rectangle r = ClientBounds;
            r.Width--;
            if (!BorderColor.IsEmpty)
            {
                using (Pen borderPen = new Pen(BorderColor))
                {
                    g.DrawRectangle(borderPen, r);
                }
            }
        }

        /// <summary>
        /// Paints the default content (text) of the item.
        /// </summary>
        public void PaintDefaultContent()
        {
            if (!string.IsNullOrEmpty(Text))
            {
                Graphics g = Graphics;
                Rectangle r = ClientBounds;

                r.Height--;
                r.Width--;
                r.Inflate((int)(-5 * ScaleFactor.Width), -1);
                RectangleF tr = new RectangleF((float)r.Left, (float)r.Top, (float)r.Width, (float)r.Height);
                using (Brush pen = new SolidBrush(ForeColor))
                {
                    g.DrawString(Text, Font, pen, tr, StringFormat);
                }
            }
        }

        /// <summary>
        /// Paints the default item.
        /// </summary>
        public void PaintDefault()
        {
            if (Item is IGroupHeader)
            {
                PaintGroupHeader();
            }
            else
            {
                if (Template != null)
                {
                    PaintTemplateBackground();
                    PaintTemplate();
                    PaintDefaultBorder();
                }
                else
                {
                    PaintDefaultBackground();
                    PaintDefaultContent();
                    PaintDefaultBorder();
                }
            }
        }

        internal static Color DefaultHeaderColor = Color.LightSkyBlue;

        public void PaintHeaderBackground()
        {
            Graphics g = Graphics;
            Rectangle r = ClientBounds;

            Color color = BackColor;
            Color startColor = ColorConverter.AlphaBlendColor(Color.White, color, 100);
            if (IsTransparent)
            {
                color = ColorConverter.AlphaColor(color, 200);
                startColor = ColorConverter.AlphaColor(startColor, 150);
                using (GraphicsPlus gp = new GraphicsPlus(g))
                {
                    PointF p1 = new PointF(r.X, r.Y);
                    PointF p2 = new PointF(r.X, r.Bottom);
                    using (LinearGradientBrush backGround = new LinearGradientBrush(p1, p2, startColor, color))
                    {
                        gp.FillRectangle(backGround, r);
                    }
                }
            }
            else
            {
                GdiExt.GradientFill(g, r, startColor, color, GdiExt.FillDirection.TopToBottom);
            }
        }

        private void PaintTemplateBackground()
        {
            Graphics g = Graphics;
            Rectangle r = ClientBounds;

            Color color = BackColor;
            if (IsTransparent)
            {
                color = ColorConverter.AlphaColor(color, 220);
                using (GraphicsPlus gp = new GraphicsPlus(g))
                {
                    using (SolidBrushPlus backGround = new SolidBrushPlus(color))
                    {
                        gp.FillRectangle(backGround, r);
                    }
                }
            }
            else
            {
                using (SolidBrush brush = new SolidBrush(color))
                {
                    g.FillRectangle(brush, r);
                }
            }
        }

        public void PaintGroupHeader()
        {
            IGroupHeader header = Item as IGroupHeader;
            Graphics g = Graphics;
            Rectangle r = ClientBounds;

            PaintHeaderBackground();
            Text = header.Title;
            PaintTemplateContent();
            BorderColor = BackColor;
            r.Height--;
            ClientBounds = r;
            PaintDefaultBorder();
        }

        static Font templateFont = new Font(FontFamily.GenericSansSerif, 9f, System.Drawing.FontStyle.Bold);

        private void PaintTemplateContent()
        {
            if (!string.IsNullOrEmpty(Text))
            {
                Graphics g = Graphics;
                Rectangle r = ClientBounds;

                r.Height--;
                r.Width--;
                r.Inflate((int)(-5 * ScaleFactor.Width), -1);
                RectangleF tr = new RectangleF((float)r.Left, (float)r.Top, (float)r.Width, (float)r.Height);
                tr.X += 1f;
                tr.Y += 1f;
                using (Brush pen = new SolidBrush(Color.Black))
                {
                    g.DrawString(Text, templateFont, pen, tr, StringFormat);
                }
                tr.X -= 1f;
                tr.Y -= 1f;
                using (Brush pen = new SolidBrush(Color.White))
                {
                    g.DrawString(Text, templateFont, pen, tr, StringFormat);
                }
            }
        }
    }
}

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 Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Germany Germany
MCPD
Enterprise Application Developer 3.5
Windows Developer 3.5
.ASP.NET Developer 3.5
.NET 2.0 Windows Developer
.NET 2.0 Web Developer
.NET 2.0 Enterprise Application Developer


MCTS
.NET 3.5 Windows Forms Applications
.NET 3.5 ASP.NET Applications
.NET 3.5, ADO.NET Application Development
.NET 3.5 WCF
.NET 3.5 WPF
.NET 3.5 WF
Microsoft SQL Server 2008, Database Development
.NET 2.0 Windows Applications
.NET 2.0 Web Applications
.NET 2.0 Distributed Applications
SQL Server 2005
Sharepoint Services 3.0 Application Development
Windows Vista Client Configuration

Comments and Discussions