Click here to Skip to main content
15,894,405 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_Crescent : Form
    {
        int m_version = 620;
        const string strAddRegKey = "Form_Crescent";

        Mover mover;
        Point ptMouse_Down, ptMouse_Up;
        SolitaryControl scCovers;
        Crescent crescent;
        TextM info;
        string infotext = "Resizing can be started at the horns and middle points of both borders.\n" + 
                          "Distance between the horns and width of the crescent are changed independently.\n" +
                          "There are limits on minimal overhanging and minimal width.\n" +
                          "By any inner point a crescent can be moved around or rotated.";
        bool bShowCovers = false;
        Pen penAuxi = new Pen (Color .DarkGray);

        bool bRestore = false;

        // -------------------------------------------------
        public Form_Crescent ()
        {
            InitializeComponent ();
            mover = new Mover (this);
        }
        // -------------------------------------------------        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 (740, 560);
            btnCovers .Location = new Point (20, 20);

            crescent = new Crescent (this, mover, new PointF (400, 300), 80, 40, 140, 0, Color .Blue);

            scCovers = new SolitaryControl (btnCovers);
            info = new TextM (this, new Point (260, 40), infotext);
            info .BackColor = Color .Yellow;
        }
        // -------------------------------------------------        RenewMover
        public void RenewMover ()
        {
            mover .Clear ();
            mover .Add (scCovers);
            mover .Add (info);
            mover .Add (crescent);
        }
        // -------------------------------------------------        OnPaint
        private void OnPaint (object sender, PaintEventArgs e)
        {
            Graphics grfx = e .Graphics;

            crescent .Draw (grfx);
            if (bShowCovers)
            {
                grfx .DrawLine (penAuxi, crescent .InnerCenter, crescent .Point_B);
                grfx .DrawLine (penAuxi, crescent .InnerCenter, crescent .Point_C);
                grfx .DrawLine (penAuxi, crescent .OuterCenter, crescent .Point_B);
                grfx .DrawLine (penAuxi, crescent .OuterCenter, crescent .Point_C);
                grfx .DrawLine (penAuxi, crescent .InnerCenter, crescent .Point_A);
                grfx .DrawLine (penAuxi, crescent .Point_B, crescent .Point_C);

                crescent .DrawCover (grfx, mover);
            }
            info .Draw (grfx);
        }
        // -------------------------------------------------        OnMouseDown
        private void OnMouseDown (object sender, MouseEventArgs e)
        {
            ptMouse_Down = e .Location;
            if (mover .Catch (e .Location, e .Button))
            {
                if (mover .CaughtSource is Crescent)
                {
                    if (e .Button == MouseButtons .Left && mover .CaughtNode < 4)
                    {
                        mover .MouseTraced = false;
                        crescent .StartResizing (e .Location, mover .CaughtNode);
                        mover .MouseTraced = true;
                    }
                    else if (e .Button == MouseButtons .Right)
                    {
                        crescent .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 Crescent)
                    {
                        ColorDialog dlg = new ColorDialog ();

                        dlg .Color = crescent .Color;
                        if (dlg .ShowDialog () == DialogResult .OK)
                        {
                            crescent .Color = dlg .Color;
                            Invalidate ();
                        }
                    }
                    else if (grobj is TextM)
                    {
                        ContextMenuStrip = menuOnInfo;
                    }
                }
            }
        }
        // -------------------------------------------------        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));
            }
        }

        // *****   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 ();
            }
        }
        // -------------------------------------------------        Click_btnCovers
        private void Click_btnCovers (object sender, EventArgs e)
        {
            bShowCovers = !bShowCovers;
            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
                                      RegistryValueKind .MultiString);
                    scCovers .IntoRegistry (regkey, "scCovers");
                    info .IntoRegistry (regkey, "Info");
                    crescent .IntoRegistry (regkey, "one");
                }
            }
            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 == 4 && Convert .ToInt32 (strs [0]) >= 620)
                    {
                        ClientSize = Auxi_Convert .ToSize (strs, 1);
                        bShowCovers = Convert .ToBoolean (strs [3]);
                        scCovers = SolitaryControl .FromRegistry (regkey, "scCovers", btnCovers);
                        info = TextM .FromRegistry (this, regkey, "Info");
                        crescent = Crescent .FromRegistry (this, mover, regkey, "one");
                        if (scCovers == null || info == null || crescent == null)
                        {
                            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