Click here to Skip to main content
15,885,278 members
Articles / Programming Languages / C#

What can be simpler than graphical primitives? Part 3

Rate me:
Please Sign up or sign in to vote.
5.00/5 (8 votes)
8 Apr 2013CPOL43 min read 23.8K   560   32  
using System;
using System .Collections .Generic;
using System .ComponentModel;
using System .Drawing;
using System .Drawing .Drawing2D;
using System .Windows .Forms;
using Microsoft .Win32;

using MoveGraphLibrary;

namespace GraphicalPrimitives
{
    public partial class Form_CircleSector : Form
    {
        int m_version = 620;
        const string strAddRegKey = "Form_CircleSector";

        Mover mover;
        Point ptMouse_Down, ptMouse_Up;
        SolitaryControl scCovers, scAdd;
        CircleSector sectorPressed;
        int iSectorPressed;
        List<CircleSector> sectors = new List<CircleSector> ();
        TextM info;
        string infotext = "Size of a sector can be changed by moving any border.\n" +
                          "The sweep angle is limited by 180 degrees.\n" +
                          "Forward moving and rotation can be started at any inner point.";

        bool bShowCovers = false;
        Random rand;
        bool bRestore = false;

        // --------------------------------------
        public Form_CircleSector ()
        {
            InitializeComponent ();
            mover = new Mover (this);

            int nSeed;
            rand = Auxi_Common .RandomByCurTime (out nSeed);
        }
        // -------------------------------------------------        OnLoad
        private void OnLoad (object sender, EventArgs e)
        {
            RestoreFromRegistry ();
            if (!bRestore)
            {
                DefaultView ();
            }
            RenewMover ();
        }
        // -------------------------------------------------        OnFormClosing
        private void OnFormClosing (object sender, FormClosingEventArgs e)
        {
            SaveIntoRegistry ();
        }
        // -------------------------------------------------        DefaultView
        public void DefaultView ()
        {
            ClientSize = new Size (800, 600);
            btnCovers .Location = new Point (20, 20);
            btnAdd .Location = new Point (60, 20);

            sectors .Clear ();
            sectors .Add (new CircleSector (this, mover, new PointF (240, 240), 170, 20, 170, Color .Blue));
            sectors .Add (new CircleSector (this, mover, new PointF (550, 470), 170, 160, -110, Color .Lime));

            scCovers = new SolitaryControl (btnCovers);
            scAdd = new SolitaryControl (btnAdd);
            info = new TextM (this, new Point (300, 16), infotext);
            info .BackColor = Color .Lime;
        }
        // -------------------------------------------------        RenewMover
        public void RenewMover ()
        {
            mover .Clear ();
            mover .Add (scCovers);
            mover .Add (scAdd);
            mover .Add (info);
            for (int i = 0; i < sectors .Count; i++)
            {
                mover .Add (sectors [i]);
            }
        }
        // -------------------------------------------------        OnPaint
        private void OnPaint (object sender, PaintEventArgs e)
        {
            Graphics grfx = e .Graphics;
            GraphicalObject grobj;

            for (int i = mover .Count - 1; i >= 0; i--)
            {
                grobj = mover [i] .Source;
                if (grobj is CircleSector)
                {
                    (grobj as CircleSector) .Draw (grfx);
                    if (bShowCovers)
                    {
                        mover [i] .DrawCover (grfx);
                    }
                }
            }
            info .Draw (grfx);
        }
        // -------------------------------------------------        OnMouseDown
        private void OnMouseDown (object sender, MouseEventArgs e)
        {
            ptMouse_Down = e .Location;
            if (mover .Catch (e .Location, e .Button))
            {
                GraphicalObject grobj = mover .CaughtSource;
                if (grobj is CircleSector)
                {
                    CircleSector sector = grobj as CircleSector;
                    int iNode = mover .CaughtNode;
                    if (e .Button == MouseButtons .Left  &&  (iNode == 0  ||  iNode == 1  ||  iNode == 5))
                    {
                        sector .StartResizing (e .Location, mover .CaughtNode);
                    }
                    else if (e .Button == MouseButtons .Right)
                    {
                        sector .StartRotation (e .Location);
                    }
                }
            }
            ContextMenuStrip = null;
        }
        // -------------------------------------------------        OnMouseUp
        private void OnMouseUp (object sender, MouseEventArgs e)
        {
            ptMouse_Up = e .Location;
            double dist = Auxi_Geometry .Distance (ptMouse_Down, ptMouse_Up);

            if (mover .Release ())
            {
                GraphicalObject grobj = mover .WasCaughtSource;
                if (e .Button == MouseButtons .Right && dist <= 3)
                {
                    if (grobj is CircleSector)
                    {
                        long idSector = mover .WasCaughtSource .ID;
                        for (int i = sectors .Count - 1; i >= 0; i--)
                        {
                            if (idSector == sectors [i] .ID)
                            {
                                iSectorPressed = i;
                                sectorPressed = sectors [i];
                                break;
                            }
                        }
                        ContextMenuStrip = menuOnSectors;
                    }
                    else if (grobj is TextM)
                    {
                        ContextMenuStrip = menuOnInfo;
                    }
                }
            }
            else
            {
                if (e .Button == MouseButtons .Right && dist <= 3)
                {
                    ContextMenuStrip = menuOnEmpty;
                }
            }
        }
        // -------------------------------------------------        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));
            }
        }

        // *****   menuOnSectors   *****
        // -------------------------------------------------        OnOpening_menuOnSectors
        private void OnOpening_menuOnSectors (object sender, CancelEventArgs e)
        {
            ToolStripItemCollection items = menuOnSectors .Items;

            items ["miPutOnTop"] .Enabled = iSectorPressed > 0;
            items ["miOneLevelUp"] .Enabled = iSectorPressed > 0;
            items ["miOneLevelDown"] .Enabled = iSectorPressed < sectors .Count - 1;
            items ["miPutUnderneath"] .Enabled = iSectorPressed < sectors .Count - 1;
            ((ToolStripMenuItem) (items ["miFixedStartingSide"])) .Checked = sectorPressed .FixedStartingSide;
        }
        // -------------------------------------------------        Click_miPutOnTop
        private void Click_miPutOnTop (object sender, EventArgs e)
        {
            sectors .RemoveAt (iSectorPressed);
            sectors .Insert (0, sectorPressed);
            RenewMover ();
            Invalidate ();
        }
        // -------------------------------------------------        Click_miOneLevelUp
        private void Click_miOneLevelUp (object sender, EventArgs e)
        {
            sectors .Reverse (iSectorPressed - 1, 2);
            RenewMover ();
            Invalidate ();
        }
        // -------------------------------------------------        Click_miOneLevelDown
        private void Click_miOneLevelDown (object sender, EventArgs e)
        {
            sectors .Reverse (iSectorPressed, 2);
            RenewMover ();
            Invalidate ();
        }
        // -------------------------------------------------        Click_miPutUnderneath
        private void Click_miPutUnderneath (object sender, EventArgs e)
        {
            sectors .RemoveAt (iSectorPressed);
            sectors .Add (sectorPressed);
            RenewMover ();
            Invalidate ();
        }
        // -------------------------------------------------        Click_miDuplicate
        private void Click_miDuplicate (object sender, EventArgs e)
        {
            int dx = 50;
            int dy = 50;
            CircleSector sectorNew = sectorPressed .Copy (new PointF (sectorPressed .Center .X + dx, sectorPressed .Center .Y + dy));
            sectors .Insert (0, sectorNew);
            RenewMover ();
            Invalidate ();
        }
        // -------------------------------------------------        Click_miDelete
        private void Click_miDelete (object sender, EventArgs e)
        {
            sectors .RemoveAt (iSectorPressed);
            RenewMover ();
            Invalidate ();
        }
        // -------------------------------------------------        Click_miColor
        private void Click_miColor (object sender, EventArgs e)
        {
            ColorDialog dlg = new ColorDialog ();

            dlg .Color = sectorPressed .Color;
            if (dlg .ShowDialog () == DialogResult .OK)
            {
                sectorPressed .Color = dlg .Color;
                Invalidate ();
            }
        }
        // -------------------------------------------------        Click_miFixedStartingSide
        private void Click_miFixedStartingSide (object sender, EventArgs e)
        {
            sectorPressed .FixedStartingSide = !sectorPressed .FixedStartingSide;
            Invalidate ();
        }

        // *****   menuOnInfo   *****
        // -------------------------------------------------        Click_miInfoFont
        private void Click_miInfoFont (object sender, EventArgs e)
        {
            FontDialogWithTitle dlg = new FontDialogWithTitle ();

            dlg .Font = info .Font;
            dlg .Title = "Information font";
            if (dlg .ShowDialog () == DialogResult .OK)
            {
                info .Font = dlg .Font;
                Invalidate ();
            }
        }
        // -------------------------------------------------        Click_miInfoTextColor
        private void Click_miInfoTextColor (object sender, EventArgs e)
        {
            ColorDialogWithTitle dlg = new ColorDialogWithTitle ();

            dlg .Color = info .Color;
            dlg .Title = "Information color";
            if (dlg .ShowDialog () == DialogResult .OK)
            {
                info .Color = dlg .Color;
                Invalidate ();
            }
        }
        // -------------------------------------------------        Click_miInfoBackColor
        private void Click_miInfoBackColor (object sender, EventArgs e)
        {
            ColorDialogWithTitle dlg = new ColorDialogWithTitle ();

            dlg .Color = info .BackColor;
            dlg .Title = "Information background";
            if (dlg .ShowDialog () == DialogResult .OK)
            {
                info .BackColor = dlg .Color;
                Invalidate ();
            }
        }

        // *****   menuOnEmpty   *****
        // -------------------------------------------------        Click_miDefaultView
        private void Click_miDefaultView (object sender, EventArgs e)
        {
            DefaultView ();
            RenewMover ();
            Invalidate ();
        }
        // -------------------------------------------------        Click_btnCovers
        private void Click_btnCovers (object sender, EventArgs e)
        {
            bShowCovers = !bShowCovers;
            Invalidate ();
        }
        // -------------------------------------------------        Click_btnAdd
        private void Click_btnAdd (object sender, EventArgs e)
        {
            CircleSector sector = new CircleSector (this, mover, Auxi_Geometry .Middle (ClientRectangle),
                                                    Convert .ToSingle (CircleSector .MinimumRadius + rand .Next (300)),
                                                    rand .Next (360),
                                                    rand .Next (360) - 180,
                                                    Auxi_Colours .RandomColor);
            sectors .Insert (0, sector);
            RenewMover ();
            Invalidate ();
        }

        const string name = "Main";
        // -------------------------------------------------        SaveIntoRegistry
        private void SaveIntoRegistry ()
        {
            string strRegKey = Form_Main .strRegKey + strAddRegKey;

            RegistryKey regkey = null;
            try
            {
                regkey = Registry .CurrentUser .CreateSubKey (strRegKey);
                if (regkey != null)
                {
                    regkey .SetValue (name, new string [] { m_version .ToString (),             // 0
                                                            ClientSize .Width .ToString (),     // 1
                                                            ClientSize .Height .ToString (),    // 2
                                                            bShowCovers .ToString (),           // 3
                                                            sectors .Count .ToString () },      // 4
                                      RegistryValueKind .MultiString);
                    scCovers .IntoRegistry (regkey, "scCovers");
                    scAdd .IntoRegistry (regkey, "scAdd");
                    info .IntoRegistry (regkey, "Info");
                    for (int i = 0; i < sectors .Count; i++)
                    {
                        sectors [i] .IntoRegistry (regkey, i .ToString ());
                    }
                }
            }
            catch
            {
            }
            finally
            {
                if (regkey != null) regkey .Close ();
            }
        }
        // -------------------------------------------------        RestoreFromRegistry
        private void RestoreFromRegistry ()
        {
            string namekey = Auxi_DLL_Internal .PathDefinedByExecutable ();
            namekey += strAddRegKey;

            RegistryKey regkey = null;
            try
            {
                regkey = Registry .CurrentUser .OpenSubKey (namekey);
                if (regkey != null)
                {
                    string [] strs = (string []) regkey .GetValue (name);
                    if (strs != null && strs .Length == 5 && Convert .ToInt32 (strs [0]) >= 620)
                    {
                        ClientSize = Auxi_Convert .ToSize (strs, 1);
                        bShowCovers = Convert .ToBoolean (strs [3]);
                        int nFigures = Convert .ToInt32 (strs [4]);
                        scCovers = SolitaryControl .FromRegistry (regkey, "scCovers", btnCovers);
                        scAdd = SolitaryControl .FromRegistry (regkey, "scAdd", btnAdd);
                        info = TextM .FromRegistry (this, regkey, "Info");
                        sectors .Clear ();
                        for (int i = 0; i < nFigures; i++)
                        {
                            CircleSector sector = CircleSector .FromRegistry (this, mover, regkey, i .ToString ());
                            if (sector != null)
                            {
                                sectors .Add (sector);
                            }
                        }
                        if (scCovers == null || scAdd == null || sectors .Count < nFigures)
                        {
                            return;
                        }
                        bRestore = true;
                    }
                    regkey .Close ();
                }
            }
            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