Click here to Skip to main content
15,881,380 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_PrimitiveHouse : Form
    {
        public event EventHandler Changed;
        Mover mover;
        PrimitiveHouse house;
        Sample_PrimitiveHouse sample;
        Point ptMouseClick;

        // -------------------------------------------------
        public Form_Colors_PrimitiveHouse (PrimitiveHouse 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 / 8);

            int window_W = room_W * 2 / 3;
            int window_H = room_H / 2;
            // LT, LB and RT windows
            int [] cx = new int [3];
            int [] cy = new int [3];
            cx [0] = cx [1] = rcHouse .Left + (room_W - window_W) / 2;
            cy [0] = cy [2] = rcHouse .Top + (room_H - window_H) / 2;
            cx [2] = cx [0] + room_W;
            cy [1] = cy [0] + room_H;
            Rectangle [] rcWins = new Rectangle [3];
            for (int i = 0; i < 3; i++)
            {
                rcWins [i] = new Rectangle (cx [i], cy [i], window_W, window_H);
            }
            int door_H = rcHouse .Bottom - cy [1];
            int door_W = door_H / 2;
            Rectangle rcDoor = new Rectangle (rcHouse .Left + room_W + room_W / 2 - door_W / 2, cy [1], door_W, door_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 (cx [0] + 6, cy [0] + window_H - buttonWindow .Height + 6);
            buttonDoor .Location = new Point (rcDoor .Right - buttonDoor .Width * 2 / 3,
                                              rcDoor .Bottom - (buttonDoor .Height + 6));
            Point ptM = Auxi_Geometry .Middle (ptPien, new Point (rcHouse .Right, rcHouse .Top));
            buttonEdge .Location = new Point (ptM .X, ptM .Y - buttonEdge .Height);

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

            sample = new Sample_PrimitiveHouse (rcHouse, ptPien, rcWins, rcDoor, 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_btnDoor
        private void Click_btnDoor (object sender, EventArgs e)
        {
            ColorDialog dlg = new ColorDialog ();

            dlg .Color = house .DoorColor;
            if (dlg .ShowDialog () == DialogResult .OK)
            {
                house .DoorColor = dlg .Color;
                sample .DoorColor = 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_PrimitiveHouse : GraphicalObject
    {
        Rectangle rcHouse;
        Point ptPien;
        Rectangle [] rcWindows;
        Rectangle rcDoor;
        List<Control> controls = new List<Control> ();
        SolidBrush brushHouse, brushRoof, brushWindow, brushDoor;
        Pen penEdge;

        // -------------------------------------------------
        public Sample_PrimitiveHouse (Rectangle rcMain, Point ptTop, Rectangle [] rcwins, Rectangle rcEntrance, List<Control> ctrls,
                                      Color [] clrs)
        {
            rcHouse = rcMain;
            ptPien = ptTop;
            rcWindows = rcwins;
            rcDoor = rcEntrance;
            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 [] ptRoof = new Point [3] { new Point (rcHouse .Left, rcHouse .Top), ptPien, 
                                              new Point (rcHouse .Right, rcHouse .Top) };
            grfx .FillPolygon (brushRoof, ptRoof);
            grfx .DrawPolygon (penEdge, ptRoof);
            for (int i = 0; i < rcWindows .Length; i++)
            {
                grfx .FillRectangle (brushWindow, rcWindows [i]);
                grfx .DrawRectangle (penEdge, rcWindows [i]);
            }
            grfx .FillRectangle (brushDoor, rcDoor);
            grfx .DrawRectangle (penEdge, rcDoor);
        }
        // -------------------------------------------------        DefineCover
        public override void DefineCover ()
        {
            CoverNode [] nodes = new CoverNode [1];
            nodes [0] = new CoverNode (0, new Point [] {rcHouse .Location, ptPien, 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;
            }
            rcDoor .X += dx;
            rcDoor .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