Click here to Skip to main content
15,883,901 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 .Drawing;
using System .Windows .Forms;

using MoveGraphLibrary;

namespace UserDrivenApplications
{
    public class StripInRectangle : GraphicalObject
    {
        RectangleF rc;
        double coefX_anchor, coefY_anchor;  // from [0, 1];  [0,0] is for left-top corner;  [1,1] is for right-bottom corner
        PointF ptAnchor;
        LineDir linedir;
        double coefX_size, coefY_size;          // can be of any sign
        double coef_min_size, coef_max_size;    // can be of any sign

        SolidBrush brush;
        int halfsense = 3;

        // -------------------------------------------------
        public StripInRectangle (RectangleF rcParent, float coefX_pt, float coefY_pt,
                                 LineDir dirUnchangeable, float coefX_initialsize, float coefY_initialsize,
                                 float minSizeCoef, float maxSizeCoef, Color clr)
        {
            rc = rcParent;
            coefX_anchor = Math .Min (Math .Max (0, coefX_pt), 1);
            coefY_anchor = Math .Min (Math .Max (0, coefY_pt), 1);
            ptAnchor = new PointF (rc .Left + Convert .ToSingle (coefX_anchor * rc .Width),
                                   rc .Top + Convert .ToSingle (coefY_anchor * rc .Height));

            linedir = dirUnchangeable;
            coefX_size = coefX_initialsize;
            coefY_size = coefY_initialsize;

            coef_min_size = Math .Min (minSizeCoef, maxSizeCoef);
            coef_max_size = Math .Max (minSizeCoef, maxSizeCoef);
            switch (linedir)
            {
                case LineDir .Hor:
                    coefY_size = Math .Min (Math .Max (coef_min_size, coefY_size), coef_max_size);
                    break;
                case LineDir .Ver:
                    coefX_size = Math .Min (Math .Max (coef_min_size, coefX_size), coef_max_size);
                    break;
            }
            brush = new SolidBrush (clr);
        }
        // -------------------------------------------------        ParentRect
        public RectangleF ParentRect
        {
            get { return (rc); }
            set
            {
                rc = value;
                ptAnchor = new PointF (rc .Left + Convert .ToSingle (coefX_anchor * rc .Width),
                                       rc .Top + Convert .ToSingle (coefY_anchor * rc .Height));
                DefineCover ();
            }
        }
        // -------------------------------------------------        Ycoef
        public double Ycoef
        {
            get { return (coefY_size); }
        }
        // -------------------------------------------------        Color
        public Color Color
        {
            get { return (brush .Color); }
            set { brush = new SolidBrush (value); }
        }
        // -------------------------------------------------        Draw
        public void Draw (Graphics grfx)
        {
            float dw = Convert .ToSingle (rc .Width * coefX_size);
            float dh = Convert .ToSingle (rc .Height * coefY_size);
            float cxL = Math .Min (ptAnchor .X, ptAnchor .X + dw);
            float cxR = Math .Max (ptAnchor .X, ptAnchor .X + dw);
            float cyT = Math .Min (ptAnchor .Y, ptAnchor .Y + dh);
            float cyB = Math .Max (ptAnchor .Y, ptAnchor .Y + dh);

            grfx .FillRectangle (brush, cxL, cyT, cxR - cxL, cyB - cyT);
        }
        // -------------------------------------------------        Draw
        public override void DefineCover ()
        {
            float dw = Convert .ToSingle (rc .Width * coefX_size);
            float dh = Convert .ToSingle (rc .Height * coefY_size);
            float cxL = Math .Min (ptAnchor .X, ptAnchor .X + dw);
            float cxR = Math .Max (ptAnchor .X, ptAnchor .X + dw);
            float cyT = Math .Min (ptAnchor .Y, ptAnchor .Y + dh);
            float cyB = Math .Max (ptAnchor .Y, ptAnchor .Y + dh);
            CoverNode [] nodes = new CoverNode [2];
            switch (linedir)
            {
                case LineDir .Hor:
                    float cy = ptAnchor .Y + dh;
                    nodes [0] = new CoverNode (0, new RectangleF (cxL, cy - halfsense, cxR - cxL, 2 * halfsense), Cursors .SizeNS);
                    break;

                case LineDir .Ver:
                    float cx = ptAnchor .X + dw;
                    nodes [0] = new CoverNode (0, new RectangleF (cx - halfsense, cyT, 2 * halfsense, cyB - cyT), Cursors .SizeWE);
                    break;
            }
            nodes [1] = new CoverNode (1, new RectangleF (cxL, cyT, cxR - cxL, Math .Max (2, cyB - cyT)),
                                          MovementFreedom .None, Cursors .Default);
            cover = new Cover (nodes);
        }
        // -------------------------------------------------        Move
        public override void Move (int dx, int dy)
        {
            ptAnchor += new Size (dx, dy);
        }
        // -------------------------------------------------        MoveNode
        public override bool MoveNode (int i, int dx, int dy, Point ptM, MouseButtons catcher)
        {
            bool bRet = false;
            if (catcher == MouseButtons .Left && i == 0)
            {
                float distCur, distNext;
                float coefNext;
                switch (linedir)
                {
                    case LineDir .Hor:
                        distCur = Convert .ToSingle (rc .Height * coefY_size);
                        distNext = distCur + dy;
                        coefNext = distNext / rc .Height;
                        if (coef_min_size <= coefNext && coefNext <= coef_max_size)
                        {
                            coefY_size = coefNext;
                            bRet = true;
                        }
                        break;

                    case LineDir .Ver:
                        distCur = Convert .ToSingle (rc .Width * coefX_size);
                        distNext = distCur + dx;
                        coefNext = distNext / rc .Width;
                        if (coef_min_size <= coefNext && coefNext <= coef_max_size)
                        {
                            coefX_size = coefNext;
                            bRet = true;
                        }
                        break;
                }
            }
            return (bRet);
        }

    }
}

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