Click here to Skip to main content
15,886,063 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 33K   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_AboutCalculator : Form
    {
        int version = 606;

        const string strAddRegKey = "Form_AboutCalculator";
        Mover mover;
        Point ptMouse_Down, ptMouse_Up;
        TextM textGeneral;
        LinkedRectangles lrPositioning, lrResizing, lrFonts, lrAuthor;

        Font fntTitles, fntOrdinary;
        Color clrTitles = Color .Blue;

        string strGeneral = "In this Calculator all the parts are alive!  Any object can be resized and moved\n" +
                            "to any position individually, or any group of objects can be moved synchronously.";
        string strTitlePositioning = "Positioning";
        string strPositioning = "To relocate any control to another position, press the left button near\n" +
                                "the control's border and move.  All the positions are saved in the Registry\n" +
                                "and are used on the next occasion. The unneeded controls can be moved\n" +
                                "out across the right or lower borders.\n" +
                                "Standard positioning of Digits, Operations, and Functions can be organized\n" +
                                "via the Settings in the main menu.";
        string strTitleResizing = "Resizing";
        string strResizing =    "Resizing of any control is done by pressing at the special spots around\n" +
                                "its borders; the best places are next to the corners.  The size of the\n" +
                                "buttons can be used as a sample for all the buttons of the same group;\n" +
                                "use the context menu next to the button.";
        string strTitleFonts = "Fonts and colors";
        string strFonts =       "Fonts and colors for the groups can be set via the main menu or\n" +
                                "the context menu next to the buttons.";
        string strAuthor = "By Sergey Andreyev ( andreyev_sergey@yahoo.com )";
        string strDate = "April 2010";

        // -------------------------------------------------
        public Form_AboutCalculator ()
        {
            InitializeComponent ();
            mover = new Mover (this);
            fntOrdinary = Font;
        }
        // -------------------------------------------------        OnLoad
        private void OnLoad (object sender, EventArgs e)
        {
            DefaultView ();
            RestoreFromRegistry ();
        }
        // -------------------------------------------------        OnFormClosing
        private void OnFormClosing (object sender, FormClosingEventArgs e)
        {
            SaveInfoToRegistry ();
        }
        // -------------------------------------------------        DefaultView
        private void DefaultView ()
        {
            Spaces spaces = new Spaces (this);
            fntTitles = new Font (fntOrdinary .Name, (float) (fntOrdinary .SizeInPoints * 1.2), fntOrdinary .Style);

            int cxL = 12;
            int cyT = 12;
            textGeneral = new TextM (this, new Point (cxL, cyT), strGeneral, false);
            textGeneral .Font = fntOrdinary;
            Compose_TextPositioning (textGeneral .Area .Left, textGeneral .Area .Bottom + spaces .VerMin);
            int cxL_text = lrPositioning .Rectangle (1) .Left;
            Compose_TextResizing (lrPositioning .RectAround .Left, lrPositioning .RectAround .Bottom + spaces .VerMin, cxL_text);
            Compose_TextFonts (lrResizing .RectAround .Left, lrResizing .RectAround .Bottom + spaces .VerMin, cxL_text);
            Compose_TextAuthor (cxL_text, lrFonts .RectAround .Bottom + spaces .VerMin * 2);

            int cxR = Math .Max (Math .Max (Math .Max (Math .Max (textGeneral .Area .Right, 
                                                                  lrPositioning .RectAround .Right),
                                                                  lrResizing .RectAround .Right),
                                                                  lrFonts .RectAround .Right),
                                                                  lrAuthor .RectAround .Right);
            ClientSize = new Size (cxR + spaces .FormSideSpace, lrAuthor .RectAround .Bottom + spaces .FormBtmSpace);

            RenewMover ();
        }
        // -------------------------------------------------        Compose_TextPositioning
        public void Compose_TextPositioning (int cxL, int cyT)
        {
            Size sizeTitle = Auxi_Geometry .RoundMeasureString (this, strTitlePositioning, fntTitles);
            Size sizeText = Auxi_Geometry .RoundMeasureString (this, strPositioning, fntOrdinary);
            Rectangle rcTitle = new Rectangle (cxL, cyT, sizeTitle .Width, sizeTitle .Height);
            Rectangle rcText = new Rectangle (rcTitle .Left + rcTitle .Width / 2, rcTitle .Bottom, sizeText .Width, sizeText .Height);
            lrPositioning = new LinkedRectangles (new Rectangle [] { rcTitle, rcText });
        }
        // -------------------------------------------------        Compose_TextResizing
        public void Compose_TextResizing (int cxL, int cyT, int cxL_2)
        {
            Size sizeTitle = Auxi_Geometry .RoundMeasureString (this, strTitleResizing, fntTitles);
            Size sizeText = Auxi_Geometry .RoundMeasureString (this, strResizing, fntOrdinary);
            Rectangle rcTitle = new Rectangle (cxL, cyT, sizeTitle .Width, sizeTitle .Height);
            Rectangle rcText = new Rectangle (cxL_2, rcTitle .Bottom, sizeText .Width, sizeText .Height);
            lrResizing = new LinkedRectangles (new Rectangle [] { rcTitle, rcText });
        }
        // -------------------------------------------------        Compose_TextFonts
        public void Compose_TextFonts (int cxL, int cyT, int cxL_2)
        {
            Size sizeTitle = Auxi_Geometry .RoundMeasureString (this, strTitleFonts, fntTitles);
            Size sizeText = Auxi_Geometry .RoundMeasureString (this, strFonts, fntOrdinary);
            Rectangle rcTitle = new Rectangle (cxL, cyT, sizeTitle .Width, sizeTitle .Height);
            Rectangle rcText = new Rectangle (cxL_2, rcTitle .Bottom, sizeText .Width, sizeText .Height);
            lrFonts = new LinkedRectangles (new Rectangle [] { rcTitle, rcText });
        }
        // -------------------------------------------------        Compose_TextAuthor
        public void Compose_TextAuthor (int cxL, int cyT)
        {
            Size sizeTitle = Auxi_Geometry .RoundMeasureString (this, strAuthor, fntOrdinary);
            Size sizeText = Auxi_Geometry .RoundMeasureString (this, strDate, fntOrdinary);
            Rectangle rcTitle = new Rectangle (cxL, cyT, sizeTitle .Width, sizeTitle .Height);
            Rectangle rcText = new Rectangle (rcTitle .Right - sizeText .Width / 3, rcTitle .Bottom, sizeText .Width, sizeText .Height);
            lrAuthor = new LinkedRectangles (new Rectangle [] { rcTitle, rcText });
        }
        // -------------------------------------------------        RenewMover
        public void RenewMover ()
        {
            mover .Clear ();
            mover .Add (textGeneral);
            mover .Insert (0, lrPositioning);
            mover .Insert (0, lrResizing);
            mover .Insert (0, lrFonts);
            mover .Insert (0, lrAuthor);
        }
        // -------------------------------------------------        OnPaint
        private void OnPaint (object sender, PaintEventArgs e)
        {
            Graphics grfx = e .Graphics;

            textGeneral .Draw (grfx);
            Auxi_Drawing .DrawText (grfx, strTitlePositioning, fntTitles, 0, clrTitles, lrPositioning .Rectangle (0) .Location, TextBasis .NW);
            Auxi_Drawing .DrawText (grfx, strPositioning, fntOrdinary, 0, ForeColor, lrPositioning .Rectangle (1) .Location, TextBasis .NW);

            Auxi_Drawing .DrawText (grfx, strTitleResizing, fntTitles, 0, clrTitles, lrResizing .Rectangle (0) .Location, TextBasis .NW);
            Auxi_Drawing .DrawText (grfx, strResizing, fntOrdinary, 0, ForeColor, lrResizing .Rectangle (1) .Location, TextBasis .NW);

            Auxi_Drawing .DrawText (grfx, strTitleFonts, fntTitles, 0, clrTitles, lrFonts .Rectangle (0) .Location, TextBasis .NW);
            Auxi_Drawing .DrawText (grfx, strFonts, fntOrdinary, 0, ForeColor, lrFonts .Rectangle (1) .Location, TextBasis .NW);

            Auxi_Drawing .DrawText (grfx, strAuthor, fntOrdinary, 0, ForeColor, lrAuthor .Rectangle (0) .Location, TextBasis .NW);
            Auxi_Drawing .DrawText (grfx, strDate, fntOrdinary, 0, ForeColor, lrAuthor .Rectangle (1) .Location, TextBasis .NW);
        }
        // -------------------------------------------------        OnMouseDown
        private void OnMouseDown (object sender, MouseEventArgs e)
        {
            ptMouse_Down = e .Location;
            mover .Catch (e .Location, e .Button);
            ContextMenuStrip = null;
        }
        // -------------------------------------------------        OnMouseUp
        private void OnMouseUp (object sender, MouseEventArgs e)
        {
            ptMouse_Up = e .Location;
            mover .Release ();
            if (e .Button == MouseButtons .Right && Auxi_Geometry .Distance (ptMouse_Down, ptMouse_Up) <= 3)
            {
                ContextMenuStrip = menuAnywhere;
            }
        }
        // -------------------------------------------------        OnMouseMove
        private void OnMouseMove (object sender, MouseEventArgs e)
        {
            if (mover .Move (e .Location))
            {
                Invalidate ();
            }
        }
        // -------------------------------------------------        OnContextMenuChanged
        private void OnContextMenuChanged (object sender, EventArgs e)
        {
            if (ContextMenuStrip != null)
            {
                ContextMenuStrip .Show (PointToScreen (ptMouse_Up));
            }
        }
        // -------------------------------------------------        Click_miFont
        private void Click_miFont (object sender, EventArgs e)
        {
            FontDialog dlg = new FontDialog ();

            dlg .Font = fntOrdinary;
            if (dlg .ShowDialog () == DialogResult .OK)
            {
                fntOrdinary = dlg .Font;
                fntTitles = new Font (fntOrdinary .Name, (float) (fntOrdinary .SizeInPoints * 1.2), fntOrdinary .Style);

                textGeneral = new TextM (this, new Point (textGeneral .Location .X, textGeneral .Location .Y), strGeneral, false);
                textGeneral .Font = fntOrdinary;
                Compose_TextPositioning (lrPositioning .RectAround .Left, lrPositioning .RectAround .Top);
                int dx = lrPositioning .Rectangle (1) .Left - lrPositioning .RectAround .Left;
                Compose_TextResizing (lrResizing .RectAround .Left, lrResizing .RectAround .Top, lrResizing .RectAround .Left + dx);
                Compose_TextFonts (lrFonts .RectAround .Left, lrFonts .RectAround .Top, lrFonts .RectAround .Left + dx);
                Compose_TextAuthor (lrAuthor .RectAround .Left, lrAuthor .RectAround .Top);

                RenewMover ();
                Invalidate ();
            }
        }
        // -------------------------------------------------        Click_miDefaultView
        private void Click_miDefaultView (object sender, EventArgs e)
        {
            fntOrdinary = Font;
            DefaultView ();
            RenewMover ();
            Invalidate ();
        }

        const string nameGeneral = "General";
        // -------------------------------------------------        SaveInfoToRegistry
        private void SaveInfoToRegistry ()
        {
            string strRegKey = Form_Main .strRegKey + strAddRegKey;

            RegistryKey regkey = null;
            try
            {
                regkey = Registry .CurrentUser .CreateSubKey (strRegKey);
                if (regkey != null)
                {
                    string [] strs = {version .ToString (),              // 0
                                      fntOrdinary .Name .ToString (),              // 1
                                      fntOrdinary .SizeInPoints .ToString (),      // 2
                                      ((int) (fntOrdinary .Style)) .ToString (),   // 3
                                      textGeneral .Location .X .ToString (),    // 4
                                      textGeneral .Location .Y .ToString (),    // 5
                                      lrPositioning .RectAround .Left .ToString (),     // 6
                                      lrPositioning .RectAround .Top .ToString (),      // 7
                                      lrResizing .RectAround .Left .ToString (),    // 8
                                      lrResizing .RectAround .Top .ToString (),     // 9
                                      lrResizing .Rectangle (1) .Left .ToString (), // 10
                                      lrFonts .RectAround .Left .ToString (),    // 11
                                      lrFonts .RectAround .Top .ToString (),     // 12
                                      lrFonts .Rectangle (1) .Left .ToString (), // 13
                                      lrAuthor .RectAround .Left .ToString (),      // 14
                                      lrAuthor .RectAround .Top .ToString (),       // 15
                                      ClientSize .Width .ToString (),           // 16
                                      ClientSize .Height .ToString (),          // 17
                                     };
                    regkey .SetValue (nameGeneral, strs, RegistryValueKind .MultiString);
                }
            }
            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 [] strs = (string []) regkey .GetValue (nameGeneral);
                    if (strs != null && strs .Length == 18  &&  Convert .ToInt32 (strs [0]) == 606)
                    {
                        fntOrdinary = Auxi_Convert .ToFont (strs, 1);
                        fntTitles = new Font (fntOrdinary .Name, (float) (fntOrdinary .SizeInPoints * 1.2), fntOrdinary .Style);
                        Spaces spaces = new Spaces (this);
                        textGeneral = new TextM (this, new Point (Convert .ToInt32 (strs [4]), Convert .ToInt32 (strs [5])), strGeneral, false);
                        textGeneral .Font = fntOrdinary;
                        Compose_TextPositioning (Convert .ToInt32 (strs [6]), Convert .ToInt32 (strs [7]));
                        Compose_TextResizing (Convert .ToInt32 (strs [8]), Convert .ToInt32 (strs [9]), Convert .ToInt32 (strs [10]));
                        Compose_TextFonts (Convert .ToInt32 (strs [11]), Convert .ToInt32 (strs [12]), Convert .ToInt32 (strs [13]));
                        Compose_TextAuthor (Convert .ToInt32 (strs [14]), Convert .ToInt32 (strs [15]));
                        ClientSize = new Size (Convert .ToInt32 (strs [16]), Convert .ToInt32 (strs [17]));
                        RenewMover ();
                    }
                }
            }
            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