Click here to Skip to main content
15,885,733 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 MoveGraphLibrary;

namespace UserDrivenApplications
{
    public partial class Form_Colors_RuralHouse : Form
    {
        public event EventHandler Changed;
        Mover mover;
        RuralHouse house;
        Sample_RuralHouse sample;
        Point ptMouseClick;

        // -------------------------------------------------
        public Form_Colors_RuralHouse (RuralHouse srcHouse, Point ptScreenMouseClick)
        {
            InitializeComponent ();
            mover = new Mover (this);

            house = srcHouse;
            ptMouseClick = ptScreenMouseClick;

            // drawing coordinates
            Rectangle rcHouse = new Rectangle (ClientSize .Width / 5, ClientSize .Height * 2 / 5,
                                               ClientSize .Width * 3 / 5, ClientSize .Height / 2);
            int room_W = rcHouse .Width / 2;
            int room_H = rcHouse .Height / 2;
            rcHouse .X -= room_W / 4;
            rcHouse .Width += room_W / 2;
            Point ptPien = new Point ((rcHouse .Left + rcHouse .Right) / 2, ClientSize .Height / 10);
            Size sizeSlope = new Size (rcHouse .Width / 3, (rcHouse .Top - ptPien .Y) / 3);

            int window_W = room_W * 2 / 3;
            int window_H = room_H / 2;
            int [] cxWindow = new int [2];
            int [] cyWindow = new int [2];
            cxWindow [0] = rcHouse .Left + (room_W - window_W) / 2;
            cxWindow [1] = cxWindow [0] + room_W;
            cyWindow [0] = rcHouse .Top + (room_H - window_H) / 2;
            cyWindow [1] = cyWindow [0] + room_H;
            Rectangle [] rcWins = new Rectangle [4] {new Rectangle (cxWindow [0], cyWindow [0], window_W, window_H), 
                                                     new Rectangle (cxWindow [0], cyWindow [1], window_W, window_H),
                                                     new Rectangle (cxWindow [1], cyWindow [0], window_W, window_H),
                                                     new Rectangle (cxWindow [1], cyWindow [1], window_W, window_H) };

            buttonRoof .Location = new Point (ptPien .X - buttonRoof .Width * 3 / 4, ptPien .Y + buttonRoof .Height);
            buttonHouse .Location = new Point (rcHouse .Right - (buttonHouse .Width + 10),
                                               (rcHouse .Top + rcHouse .Bottom) / 2 - buttonHouse .Height / 2);
            buttonWindow .Location = new Point (cxWindow [0] + 6, cyWindow [0] + window_H - buttonWindow .Height + 6);
            buttonEdge .Location = new Point (ptPien .X + sizeSlope .Width, ptPien .Y + sizeSlope .Height - buttonEdge .Height);

            List<Control> ctrls = new List<Control> ();
            ctrls .Add (buttonRoof);
            ctrls .Add (buttonHouse);
            ctrls .Add (buttonWindow);
            ctrls .Add (buttonEdge);

            sample = new Sample_RuralHouse (rcHouse, ptPien, sizeSlope, rcWins, ctrls, house .GetColors ());
            mover .Add (sample);
        }
        // -------------------------------------------------        OnLoad
        private void OnLoad (object sender, EventArgs e)
        {
            Location = ptMouseClick;
        }
        // -------------------------------------------------        OnPaint
        private void OnPaint (object sender, PaintEventArgs e)
        {
            sample .Draw (e .Graphics);
        }
        // -------------------------------------------------        OnMouseDown
        private void OnMouseDown (object sender, MouseEventArgs e)
        {
            mover .Catch (e .Location, e .Button);
        }
        // -------------------------------------------------        OnMouseUp
        private void OnMouseUp (object sender, MouseEventArgs e)
        {
            mover .Release ();
        }
        // -------------------------------------------------        OnMouseMove
        private void OnMouseMove (object sender, MouseEventArgs e)
        {
            if (mover .Move (e .Location))
            {
                Invalidate ();
            }
        }
        // -------------------------------------------------        Click_btnRoof
        private void Click_btnRoof (object sender, EventArgs e)
        {
            ColorDialog dlg = new ColorDialog ();

            dlg .Color = house .RoofColor;
            if (dlg .ShowDialog () == DialogResult .OK)
            {
                house .RoofColor = dlg .Color;
                sample .RoofColor = dlg .Color;
                Invalidate ();
                if (Changed != null) Changed (this, new EventArgs ());
            }
        }
        // -------------------------------------------------        Click_btnHouse
        private void Click_btnHouse (object sender, EventArgs e)
        {
            ColorDialog dlg = new ColorDialog ();

            dlg .Color = house .HouseColor;
            if (dlg .ShowDialog () == DialogResult .OK)
            {
                house .HouseColor = dlg .Color;
                sample .HouseColor = dlg .Color;
                Invalidate ();
                if (Changed != null) Changed (this, new EventArgs ());
            }
        }
        // -------------------------------------------------        Click_btnWindow
        private void Click_btnWindow (object sender, EventArgs e)
        {
            ColorDialog dlg = new ColorDialog ();

            dlg .Color = house .WindowColor;
            if (dlg .ShowDialog () == DialogResult .OK)
            {
                house .WindowColor = dlg .Color;
                sample .WindowColor = dlg .Color;
                Invalidate ();
                if (Changed != null) Changed (this, new EventArgs ());
            }
        }
        // -------------------------------------------------        Click_btnEdge
        private void Click_btnEdge (object sender, EventArgs e)
        {
            ColorDialog dlg = new ColorDialog ();

            dlg .Color = house .EdgeColor;
            if (dlg .ShowDialog () == DialogResult .OK)
            {
                house .EdgeColor = dlg .Color;
                sample .EdgeColor = dlg .Color;
                Invalidate ();
                if (Changed != null) Changed (this, new EventArgs ());
            }
        }
    }

    // ******************************************
    public class Sample_RuralHouse : GraphicalObject
    {
        Rectangle rcHouse;
        Point ptPien;
        Size sizeSlopes;
        Rectangle [] rcWindows;
        List<Control> controls = new List<Control> ();
        SolidBrush brushHouse, brushRoof, brushWindow, brushDoor;
        Pen penEdge;

        // -------------------------------------------------
        public Sample_RuralHouse (Rectangle rcMain, Point ptTop, Size sizeShift, Rectangle [] rcwins, List<Control> ctrls, Color [] clrs)
        {
            rcHouse = rcMain;
            ptPien = ptTop;
            sizeSlopes = sizeShift;
            rcWindows = rcwins;
            controls = ctrls;
            SetColors (clrs);
        }
        // -------------------------------------------------        SetColors
        public void SetColors (Color [] clrs)
        {
            brushHouse = new SolidBrush (clrs [0]);
            brushRoof = new SolidBrush (clrs [1]);
            brushWindow = new SolidBrush (clrs [2]);
            brushDoor = new SolidBrush (clrs [3]);
            penEdge = new Pen (clrs [4]);
        }
        // -------------------------------------------------        HouseColor
        public Color HouseColor
        {
            set { brushHouse = new SolidBrush (value); }
        }
        // -------------------------------------------------        RoofColor
        public Color RoofColor
        {
            set { brushRoof = new SolidBrush (value); }
        }
        // -------------------------------------------------        WindowColor
        public Color WindowColor
        {
            set { brushWindow = new SolidBrush (value); }
        }
        // -------------------------------------------------        DoorColor
        public Color DoorColor
        {
            set { brushDoor = new SolidBrush (value); }
        }
        // -------------------------------------------------        EdgeColor
        public Color EdgeColor
        {
            set { penEdge = new Pen (value); }
        }
        // -------------------------------------------------        Draw
        public void Draw (Graphics grfx)
        {
            grfx .FillRectangle (brushHouse, rcHouse);
            grfx .DrawRectangle (penEdge, rcHouse);
            Point [] ptsRoof = {rcHouse .Location, new Point (ptPien .X - sizeSlopes .Width, ptPien .Y + sizeSlopes .Height), ptPien, 
                                                   new Point (ptPien .X + sizeSlopes .Width, ptPien .Y + sizeSlopes .Height), 
                                new Point (rcHouse .Right, rcHouse .Top) };
            grfx .FillPolygon (brushRoof, ptsRoof);
            grfx .DrawPolygon (penEdge, ptsRoof);
            for (int i = 0; i < rcWindows .Length; i++)
            {
                grfx .FillRectangle (brushWindow, rcWindows [i]);
                grfx .DrawRectangle (penEdge, rcWindows [i]);
            }
        }
        // -------------------------------------------------        DefineCover
        public override void DefineCover ()
        {
            CoverNode [] nodes = new CoverNode [1];
            nodes [0] = new CoverNode (0, new Point [] {rcHouse .Location, 
                                                        new Point (ptPien .X - sizeSlopes .Width, ptPien .Y + sizeSlopes .Height), 
                                                        ptPien, 
                                                        new Point (ptPien .X + sizeSlopes .Width, ptPien .Y + sizeSlopes .Height), 
                                                        new Point (rcHouse .Right, rcHouse .Top), 
                                                        new Point (rcHouse .Right, rcHouse .Bottom), 
                                                        new Point (rcHouse .Left, rcHouse .Bottom) });
            cover = new Cover (nodes);
        }
        // -------------------------------------------------        Move
        public override void Move (int dx, int dy)
        {
            Size size = new Size (dx, dy);
            rcHouse .X += dx;
            rcHouse .Y += dy;
            ptPien += size;
            for (int i = 0; i < rcWindows .Length; i++)
            {
                rcWindows [i] .X += dx;
                rcWindows [i] .Y += dy;
            }
            for (int i = 0; i < controls .Count; i++)
            {
                controls [i] .Location += size;
            }
        }
        // -------------------------------------------------        MoveNode
        public override bool MoveNode (int i, int dx, int dy, Point ptM, MouseButtons catcher)
        {
            bool bRet = false;

            if (catcher == MouseButtons .Left)
            {
                Move (dx, dy);
                bRet = true;
            }
            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