Click here to Skip to main content
15,896,348 members
Articles / Programming Languages / C#

User-driven applications

Rate me:
Please Sign up or sign in to vote.
4.88/5 (24 votes)
10 Apr 2010CPOL136 min read 33.1K   5   78  
User-driven applications are the programs in which full control is given to the users. Designers of such programs are responsible only for developing an instrument for solving some task, but they do not enforce users to work with this instrument according with a predefined scenario.
using System;
using System .Collections .Generic;
using System .ComponentModel;
using System .Drawing;
using System .Windows .Forms;
using Microsoft .Win32;

using MoveGraphLibrary;

namespace UserDrivenApplications
{
    public partial class Form_BtnsPlacement_Digits : Form
    {
        int version = 606;
        const string strAddRegKey = "Form_BtnsPlacement_Digits";

        int nView;
        Mover mover;
        ElasticGroup group;
        List<ButtonsSketch> sketches = new List<ButtonsSketch> ();
        SolidBrush brushBtns;

        Calculator_DigitsGroupView viewSelected;
        int [] places = null;
        bool bRestored = false;

        // -------------------------------------------------
        public Form_BtnsPlacement_Digits (int iSelectedVew, int horspace, int verspace)
        {
            InitializeComponent ();
            mover = new Mover (this);

            nView = iSelectedVew;
            brushBtns = new SolidBrush (ControlPaint .Dark (this .BackColor));
            numericUD_HorSpaces .Value = horspace;
            numericUD_VerSpaces .Value = verspace;
        }
        // -------------------------------------------------        OnLoad
        private void OnLoad (object sender, EventArgs e)
        {
            RestoreFromRegistry ();
            if (!bRestored)
            {
                Rectangle rect;
                int nCode;

                nCode = (int) Calculator_DigitsGroupView .OneRow;
                ButtonsSketch sketchRow = new ButtonsSketch (nCode, false, new Point (20, 20),
                                                             new int [] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }, brushBtns);
                sketches .Add (sketchRow);
                rect = sketchRow .RectAround;

                nCode = (int) Calculator_DigitsGroupView .TwoRowsUneven;
                ButtonsSketch sketch_2rows_10_2 = new ButtonsSketch (nCode, false, new Point (rect .Left, rect .Bottom + 20),
                                                                     new int [] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 12, 13 }, brushBtns);
                sketches .Add (sketch_2rows_10_2);
                rect = sketch_2rows_10_2 .RectAround;

                nCode = (int) Calculator_DigitsGroupView .TwoRows;
                ButtonsSketch sketch_2rows = new ButtonsSketch (nCode, false, new Point (rect .Left, rect .Bottom + 20),
                                                                new int [] { 0, 1, 2, 3, 4, 5, 12, 13, 14, 15, 16, 17 }, brushBtns);
                sketches .Add (sketch_2rows);
                rect = sketch_2rows .RectAround;

                nCode = (int) Calculator_DigitsGroupView .ThreeRowsUneven;
                ButtonsSketch sketch_3rows_5_5_2 = new ButtonsSketch (nCode, false, new Point (rect .Left, rect .Bottom + 20),
                                                                      new int [] { 0, 1, 2, 3, 4, 12, 13, 14, 15, 16, 24, 25 }, brushBtns);
                sketches .Add (sketch_3rows_5_5_2);
                rect = sketch_3rows_5_5_2 .RectAround;

                nCode = (int) Calculator_DigitsGroupView .ThreeRows;
                ButtonsSketch sketch_3rows = new ButtonsSketch (nCode, false, new Point (rect .Left, rect .Bottom + 20),
                                                                new int [] { 0, 1, 2, 3, 12, 13, 14, 15, 24, 25, 26, 27 }, brushBtns);
                sketches .Add (sketch_3rows);
                rect = sketch_3rows .RectAround;

                int cx = sketch_2rows .RectAround .Right + 20;
                int cy = sketch_2rows .RectAround .Top;
                nCode = (int) Calculator_DigitsGroupView .FourRows;
                ButtonsSketch sketch_4rows = new ButtonsSketch (nCode, false, new Point (cx, cy),
                                                                new int [] { 0, 1, 2, 12, 13, 14, 24, 25, 26, 36, 37, 38 }, brushBtns);
                sketches .Add (sketch_4rows);
                rect = sketch_4rows .RectAround;

                nCode = (int) Calculator_DigitsGroupView .SixRows;
                ButtonsSketch sketch_6rows = new ButtonsSketch (nCode, false, new Point (rect .Left, rect .Bottom + 20),
                                                                new int [] { 0, 1, 12, 13, 24, 25, 36, 37, 48, 49, 60, 61 }, brushBtns);
                sketches .Add (sketch_6rows);
                rect = sketch_6rows .RectAround;


                cx = sketchRow .RectAround .Right + 20;
                cy = sketchRow .RectAround .Top;
                nCode = (int) Calculator_DigitsGroupView .OneCol;
                ButtonsSketch sketchCol = new ButtonsSketch (nCode, false, new Point (cx, cy),
                                                             new int [] { 0, 12, 24, 36, 48, 60, 72, 84, 96, 108, 120, 132 }, brushBtns);
                sketches .Add (sketchCol);
                rect = sketchCol .RectAround;

                nCode = (int) Calculator_DigitsGroupView .TwoColsUneven;
                ButtonsSketch sketch_2cols_10_2 = new ButtonsSketch (nCode, false, new Point (rect .Right + 20, rect .Top),
                                                                     new int [] { 0, 12, 24, 36, 48, 60, 72, 84, 96, 108, 1, 13 }, brushBtns);
                sketches .Add (sketch_2cols_10_2);
                rect = sketch_2cols_10_2 .RectAround;

                nCode = (int) Calculator_DigitsGroupView .TwoCols;
                ButtonsSketch sketch_2cols = new ButtonsSketch (nCode, false, new Point (rect .Right + 20, rect .Top),
                                                                new int [] { 0, 12, 24, 36, 48, 60, 1, 13, 25, 37, 49, 61 }, brushBtns);
                sketches .Add (sketch_2cols);
                rect = sketch_2cols .RectAround;

                cx = sketchCol .RectAround .Left;
                cy = sketchCol .RectAround .Bottom + 20;
                nCode = (int) Calculator_DigitsGroupView .ThreeColsUneven;
                ButtonsSketch sketch_3colsUnev = new ButtonsSketch (nCode, false, new Point (cx - 20, cy),
                                                                    new int [] { 0, 12, 24, 36, 48, 1, 13, 25, 37, 49, 2, 14 }, brushBtns);
                sketches .Add (sketch_3colsUnev);

                rect = sketch_2cols .RectAround;
                nCode = (int) Calculator_DigitsGroupView .ThreeCols;
                ButtonsSketch sketch_3cols = new ButtonsSketch (nCode, false, new Point (rect .Left, rect .Bottom + 20),
                                                                new int [] { 0, 12, 24, 36, 1, 13, 25, 37, 2, 14, 26, 38 }, brushBtns);
                sketches .Add (sketch_3cols);

                rect = sketch_3colsUnev .RectAround;
                nCode = (int) Calculator_DigitsGroupView .FourCols;
                ButtonsSketch sketch_4cols = new ButtonsSketch (nCode, false, new Point (rect .Right + 20, rect .Top),
                                                                new int [] { 0, 12, 24, 1, 13, 25, 2, 14, 26, 3, 15, 27 }, brushBtns);
                sketches .Add (sketch_4cols);

                rect = sketch_4cols .RectAround;
                nCode = (int) Calculator_DigitsGroupView .SixCols;
                ButtonsSketch sketch_6cols = new ButtonsSketch (nCode, false, new Point (rect .Left, rect .Bottom + 20),
                                                                new int [] { 0, 12, 1, 13, 2, 14, 3, 15, 4, 16, 5, 17 }, brushBtns);
                sketches .Add (sketch_6cols);

                CommentedControl [] ccs = new CommentedControl [] { new CommentedControl (this, numericUD_HorSpaces, Side.E, 0.5, "Horizontal"),
                                                                    new CommentedControl (this, numericUD_VerSpaces, Side.E, 0.5, "Vertical") };
                group = new ElasticGroup (this, ccs, "Spaces between buttons");
                group .SideSpaces = new int [] { 10, 6, 10, 10 };
                group .Location = new Point (sketch_3rows .RectAround .Left + 30, sketch_3rows .RectAround .Bottom + 20);

                btnOK .Location = new Point (sketch_6cols .RectAround .Left, group .FrameArea .Bottom - btnOK .Height);
                ClientSize = new Size (sketch_3cols .RectAround .Right + 16, btnOK .Bottom + 12);
            }
            if (0 <= nView && nView < sketches .Count)
            {
                places = sketches [nView] .Places;
                viewSelected = (Calculator_DigitsGroupView) nView;
            }
            else
            {
                places = null;
                viewSelected = Calculator_DigitsGroupView .Arbitrary;
            }
            for (int i = 0; i < sketches .Count; i++)
            {
                sketches [i] .Selected = sketches [i] .Code == nView;
            }

            group .IntoMover (mover, 0);
            for (int i = 0; i < sketches .Count; i++)
            {
                mover .Add (sketches [i]);
            }
            mover .Insert (0, btnOK);
        }
        // -------------------------------------------------        OnFormClosing
        private void OnFormClosing (object sender, FormClosingEventArgs e)
        {
            SaveInfoToRegistry ();
        }
        // -------------------------------------------------        OnPaint
        private void OnPaint (object sender, PaintEventArgs e)
        {
            Graphics grfx = e .Graphics;

            for (int i = sketches .Count - 1; i >= 0; i--)
            {
                sketches [i] .Draw (grfx);
            }
            group .Draw (grfx);
        }
        // -------------------------------------------------        OnMouseDown
        private void OnMouseDown (object sender, MouseEventArgs e)
        {
            mover .Catch (e .Location, e .Button);
        }
        // -------------------------------------------------        OnMouseUp
        private void OnMouseUp (object sender, MouseEventArgs e)
        {
            int iObject, iNode;
            if (mover .Release (out iObject, out iNode))
            {
                if (mover [iObject] .Source is ButtonsSketch && iNode == 0)
                {
                    ButtonsSketch sketch = mover [iObject] .Source as ButtonsSketch;
                    for (int i = 0; i < sketches .Count; i++)
                    {
                        sketches [i] .Selected = false;
                    }
                    sketch .Selected = true;
                    viewSelected = (Calculator_DigitsGroupView) (sketch .Code);
                    places = sketch .Places;
                    Invalidate ();
                }
            }
        }
        // -------------------------------------------------        OnMouseMove
        private void OnMouseMove (object sender, MouseEventArgs e)
        {
            if (mover .Move (e .Location))
            {
                Invalidate ();
            }
        }
        // -------------------------------------------------        SelectedView
        public Calculator_DigitsGroupView SelectedView
        {
            get { return (viewSelected); }
        }
        // -------------------------------------------------        HorSpaces
        public int HorSpaces
        {
            get { return (Convert .ToInt32 (numericUD_HorSpaces .Value)); }
        }
        // -------------------------------------------------        VerSpaces
        public int VerSpaces
        {
            get { return (Convert .ToInt32 (numericUD_VerSpaces .Value)); }
        }
        // -------------------------------------------------        Places
        public int [] Places
        {
            get { return (places); }
        }

        const string nameAuxi = "Auxi";
        // -------------------------------------------------        SaveInfoToRegistry
        private void SaveInfoToRegistry ()
        {
            string strkey = Form_Main .strRegKey + strAddRegKey;

            RegistryKey regkey = null;
            try
            {
                regkey = Registry .CurrentUser .CreateSubKey (strkey);
                if (regkey != null)
                {
                    string [] strAuxi = {version .ToString (),          // 0
                                         ClientSize .Width .ToString (),    // 1
                                         ClientSize .Height .ToString (),   // 2
                                         btnOK .Left .ToString (),      // 3
                                         btnOK .Top .ToString (),       // 4
                                         btnOK .Width .ToString (),     // 5
                                         btnOK .Height .ToString (),    // 6
                                         sketches .Count .ToString (),      // 7
                                        };
                    regkey .SetValue (nameAuxi, strAuxi, RegistryValueKind .MultiString);

                    group .IntoRegistry (regkey, "Group");
                    for (int i = 0; i < sketches .Count; i++)
                    {
                        sketches [i] .IntoRegistry (regkey, i .ToString ());
                    }
                }
            }
            catch
            {
            }
            finally
            {
                if (regkey != null) regkey .Close ();
            }
        }
        // -------------------------------------------------        RestoreFromRegistry
        private void RestoreFromRegistry ()
        {
            string strkey = Form_Main .strRegKey + strAddRegKey;

            RegistryKey regkey = null;
            try
            {
                regkey = Registry .CurrentUser .OpenSubKey (strkey);
                if (regkey != null)
                {
                    string [] strAuxi = (string []) regkey .GetValue (nameAuxi);
                    if (strAuxi != null && strAuxi .Length == 8 && Convert .ToInt32 (strAuxi [0]) == 606)
                    {
                        ClientSize = Auxi_Convert .ToSize (strAuxi, 1);
                        btnOK .Bounds = Auxi_Convert .ToRectangle (strAuxi, 3);
                        int nSketch = Convert .ToInt32 (strAuxi [7]);
                        sketches .Clear ();
                        for (int i = 0; i < nSketch; i++)
                        {
                            ButtonsSketch sketch = ButtonsSketch .FromRegistry (regkey, i .ToString ());
                            if (sketch != null)
                            {
                                sketches .Add (sketch);
                            }
                        }
                        group = ElasticGroup .FromRegistry (this, regkey, "Group", new Control [] { numericUD_HorSpaces, numericUD_VerSpaces });
                        bRestored = true;
                    }
                }
            }
            catch
            {
            }
            finally
            {
                if (regkey != null) regkey .Close ();
            }
        }
    }
}

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
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions