Click here to Skip to main content
15,891,734 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.9K   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_Tuning_Spot : Form
    {
        int m_version = 620;
        const string strAddRegKey = "Form_Tuning_Spot";

        Point ptMouseInit;
        Mover mover;
        Point ptMouse_Down, ptMouse_Up;
        RectangleAllCellsInView rrSamples;
        SpotCC m_spot;
        SolitaryControl scColor, scOK;
        Pen penAuxi;
        int iPressedCell = -1;      // must be initialized with -1;  not 0 !!!
        int iSelectedCell;

        bool bRestore = false;

        // -------------------------------------------------
        public Form_Tuning_Spot (Point ptMouseScreen, string title, int rad, Color clr)
        {
            InitializeComponent ();
            ptMouseInit = ptMouseScreen;
            Text = title;
            m_spot = new SpotCC (this, mover, new Point (20, 20), Math .Min (Math .Max (SpotCC .MinRadius, rad), SpotCC .MaxRadius), clr);
            mover = new Mover (this);

            penAuxi = new Pen (Auxi_Colours .IntermediateClr (0.3, BackColor, Color .Black));
            iSelectedCell = m_spot .Radius - SpotCC .MinRadius;
        }
        // -------------------------------------------------        OnLoad
        private void OnLoad (object sender, EventArgs e)
        {
            RestoreFromRegistry ();
            if (!bRestore)
            {
                Spaces spaces = new Spaces (this);

                btnColor .Location = new Point (spaces .FormSideSpace,
                                                Convert .ToInt32 (m_spot .Center .Y + m_spot .Radius + 2 * spaces .Ver_betweenFrames));
                scColor = new SolitaryControl (btnColor);
                int cx = Convert .ToInt32 (Math .Max (m_spot .Center .X + m_spot .Radius, btnColor .Right)) + 2 * spaces .Hor_betweenFrames;
                int nCellsWithDot = SpotCC .MaxRadius - SpotCC .MinRadius + 1;
                int nCellSize = SpotCC .MaxRadius * 2 + 8;
                rrSamples = new RectangleAllCellsInView (
                                    new Rectangle (cx, spaces .FormTopSpace, (nCellsWithDot + 1) / 2 * nCellSize, nCellSize * 2),
                                    nCellsWithDot, nCellSize, nCellSize, DrawSamples);
                int nW = rrSamples .Area .Right + spaces .FormSideSpace;
                btnOK .Location = new Point ((nW - btnOK .Width) / 2,
                                             Math .Max (rrSamples .RectAround .Bottom, btnColor .Bottom) + 2 * spaces .Ver_betweenFrames);
                scOK = new SolitaryControl (btnOK);
                ClientSize = new Size (nW, btnOK .Bottom + spaces .FormBtmSpace);
            }
            Location = Auxi_Common .FormPosition (this, ptMouseInit, m_spot .Radius);

            mover .Add (m_spot);
            mover .Insert (0, rrSamples);
            mover .Insert (0, scColor);
            mover .Insert (0, scOK);
        }
        // -------------------------------------------------        OnFormClosing
        private void OnFormClosing (object sender, FormClosingEventArgs e)
        {
            SaveIntoRegistry ();
        }
        // -------------------------------------------------		DrawSamples
        private void DrawSamples (Graphics grfx)
        {
            Rectangle area = rrSamples .Area;
            int cx, cy;
            for (int i = 1; i <= rrSamples .Columns; i++)
            {
                cx = area .Left + i * rrSamples .CellWidth;
                grfx .DrawLine (penAuxi, cx, area .Top, cx, area .Bottom);
            }
            for (int i = 1; i <= rrSamples .Rows; i++)
            {
                cy = area .Top + i * rrSamples .CellHeight;
                grfx .DrawLine (penAuxi, area .Left, cy, area .Right, cy);
            }
            int nCellsWithDot = SpotCC .MaxRadius - SpotCC .MinRadius + 1;
            Rectangle rc;

            ControlPaint .DrawBorder3D (grfx, area, Border3DStyle .Sunken);
            for (int i = 0; i < nCellsWithDot; i++)
            {
                rc = rrSamples .CellArea (i);
                if (i == iSelectedCell)
                {
                    rc .Inflate (-2, -2);
                    grfx .FillRectangle (Brushes .White, rc);
                }
                Auxi_Drawing .FillEllipse (grfx, Auxi_Geometry .Middle (rc), i + SpotCC .MinRadius, Color .Black);
            }
        }
        // -------------------------------------------------        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 RectangleAllCellsInView && e .Button == MouseButtons .Left &&
                    mover .CaughtNode == (rrSamples .NodesCount - 1))
                {
                    int iRow, jCol;
                    iPressedCell = rrSamples .CellOrderByPoint (e .Location, out iRow, out jCol);
                }
            }
        }
        // -------------------------------------------------        OnMouseUp
        private void OnMouseUp (object sender, MouseEventArgs e)
        {
            ptMouse_Up = e .Location;
            double fDist = Auxi_Geometry .Distance (ptMouse_Down, ptMouse_Up);
            if (mover .Release ())
            {
                GraphicalObject grobj = mover .WasCaughtSource;
                if (grobj is RectangleAllCellsInView && fDist <= 3 && e .Button == MouseButtons .Left && iPressedCell >= 0)
                {
                    iSelectedCell = iPressedCell;
                    m_spot .Radius = iSelectedCell + SpotCC .MinRadius;
                    Invalidate ();
                }
            }
        }
        // -------------------------------------------------        OnMouseMove
        private void OnMouseMove (object sender, MouseEventArgs e)
        {
            if (mover .Move (e .Location))
            {
                Invalidate ();
            }
        }
        // -------------------------------------------------        OnPaint
        private void OnPaint (object sender, PaintEventArgs e)
        {
            Graphics grfx = e .Graphics;

            m_spot .Draw (grfx);
            rrSamples .Draw (grfx);
        }
        // -------------------------------------------------        Click_btnColor
        private void Click_btnColor (object sender, EventArgs e)
        {
            ColorDialog dlg = new ColorDialog ();

            dlg .Color = m_spot .Color;
            if (dlg .ShowDialog () == DialogResult .OK)
            {
                m_spot .Color = dlg .Color;
                Invalidate ();
            }
        }
        // -------------------------------------------------        Radius
        public int Radius
        {
            get { return (m_spot .Radius); }
        }
        // -------------------------------------------------        Color
        public Color Color
        {
            get { return (m_spot .Color); }
        }

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

            RegistryKey regkey = null;
            try
            {
                regkey = Registry .CurrentUser .CreateSubKey (strRegKey);
                if (regkey != null)
                {
                    string [] strs = {m_version .ToString (),               // 0
                                      ClientSize .Width .ToString (),       // 1
                                      ClientSize .Height .ToString (),      // 2
                                      m_spot .Center .X .ToString (),       // 3
                                      m_spot .Center .Y .ToString (),       // 4
                                     };
                    regkey .SetValue (nameMain, strs, RegistryValueKind .MultiString);

                    rrSamples .IntoRegistry (regkey, "rrSamples");
                    scColor .IntoRegistry (regkey, "scColor");
                    scOK .IntoRegistry (regkey, "scOK");
                }
            }
            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 (nameMain);
                    if (strs != null && strs .Length == 5 && Convert .ToInt32 (strs [0]) >= 620)
                    {
                        ClientSize = Auxi_Convert .ToSize (strs, 1);
                        m_spot .Move (Convert .ToInt32 (Convert .ToSingle (strs [3]) - m_spot .Center .X),
                                      Convert .ToInt32 (Convert .ToSingle (strs [4]) - m_spot .Center .Y));

                        rrSamples = RectangleAllCellsInView .FromRegistry (regkey, "rrSamples", DrawSamples);
                        scColor = SolitaryControl .FromRegistry (regkey, "scColor", btnColor);
                        scOK = SolitaryControl .FromRegistry (regkey, "scOK", btnOK);
                        if (rrSamples != null && scColor != null && scOK != null)
                        {
                            bRestore = true;
                        }
                    }
                }
            }
            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