Click here to Skip to main content
15,892,575 members
Articles / Programming Languages / C#

Moveable Resizable Objects

Rate me:
Please Sign up or sign in to vote.
4.98/5 (59 votes)
9 Oct 2009CPOL198 min read 125.7K   8.7K   178  
Here is a description of an extremely powerful mechanism that makes screen objects moveable and resizable.
using System;
using System .Collections .Generic;
using System .Drawing;
using System .Windows .Forms;
using Microsoft .Win32;

using MoveGraphLibrary;

namespace Test_MoveGraphLibrary
{
    public partial class Form_MultiScalePlot : Form
    {
        int version = 507;      // 406;

        Mover mover;
        MSPlot plot = null;
        int iAreaTouched = -1;
        int iScaleTouched = -1;
        //int iCmntTouched = -1;
        bool bVerScaleTouched = false;
        bool bHorScaleTouched = false;
        bool bShowCovers = false;
        const string strAddRegKey = "Form_MultiScalePlot";
        const string nameSizes = "ClientArea";
        TwoColumnInfo info;
        string [] cmnts = new string [] {"L_Press inside ",              "- to move any element", 
                                         "L_Press on border or corner ", "- to resize plot", 
                                         "R_Press on comment ",          "- to rotate it", 
                                         "R_Click ",                     "- to open context menu", 
                                         "L_DblClick on plot or scale ", "- to change parameters" };
        Point ptMouse_Down, ptMouse_Up;

        double [] fParam = new double [] { 1.5, 2, 20 };

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

            CreateInfo (420, 12);
        }
        // -------------------------------------------------        OnLoad
        private void OnLoad (object sender, EventArgs e)
        {
            RestoreFromRegistry ();

            if (plot == null)
            {
                plot = new MSPlot (this, new Rectangle (100, 100, 500, 400),
                                         Side .S, (-4.0) * Math .PI, 4.0 * Math .PI, GridOrigin .ByStep, 3.0,
                                         Side .E, 1.0, -1.0, GridOrigin .ByStep, 0.5);
                plot .VerScales [0] .ValuesFormat = "F2";
                plot .HorScales [0] .ShowNumLT = false;
                plot .HorScales [0] .ShowNumRB = false;

                plot .AddScale (this, Side .E, 40, 4.0, -4.5, GridOrigin .ByStep, 1.0);
                plot .AddScale (this, Side .S, 40, 0.0, 6.0, GridOrigin .ByStep, 2.0); 
                plot .HorScales [1] .AdjustEnds (false, false);
            }
            RenewMover ();
        }
        // -------------------------------------------------        OnFormClosing
        private void OnFormClosing (object sender, FormClosingEventArgs e)
        {
            SaveInfoToRegistry ();
        }
        // -------------------------------------------------        CreateInfo
        private void CreateInfo (int cx, int cy)
        {
            info = new TwoColumnInfo (this, new Point (cx, cy), cmnts);
            info .BackColor = Color .LightYellow;
        }
        // -------------------------------------------------        RenewMover
        private void RenewMover ()
        {
            mover .Clear ();
            plot .IntoMover (mover, 0);
            mover .Insert (0, info);
        }
        // -------------------------------------------------        OnPaint
        private void OnPaint (object sender, PaintEventArgs e)
        {
            Graphics grfx = e .Graphics;

            plot .Draw (grfx);
            FunctionsPainting (grfx);
            if (bShowCovers)
            {
                mover .DrawCovers (grfx);
            }
            info .Draw (grfx);
        }
        // =================================================        FunctionsPainting
        private void FunctionsPainting (Graphics grfx)
        {
            plot .DrawYofX (grfx, new MSPlotAuxi (0, SegmentLocation .Partly_Inside), Math .Sin);
            plot .DrawYofX (grfx, new MSPlotAuxi (1, SegmentLocation .Partly_Inside), Math .Cos);

            plot .DrawYofX (grfx, new MSPlotAuxi (5, 1, 1, SegmentLocation .Partly_Inside),
                            Sample_Func .RepeatDampedVibrations, fParam);
        }
        // =================================================        FullAreaPainting
        private void FullAreaPainting (Graphics grfx)
        {
            plot .Draw (grfx);
            FunctionsPainting (grfx);
        }
        // -------------------------------------------------        OnMouseDown
        private void OnMouseDown (object sender, MouseEventArgs mea)
        {
            ptMouse_Down = mea .Location;
            mover .Catch (mea .Location, mea .Button, false);
            ContextMenuStrip = null;
        }
        // -------------------------------------------------        OnMouseUp
        private void OnMouseUp (object sender, MouseEventArgs mea)
        {
            ptMouse_Up = mea .Location;
            double nDist = Auxi_Geometry .Distance (ptMouse_Down, ptMouse_Up);

            if (mea .Button == MouseButtons .Left)
            {
                mover .Release ();
            }
            else if (mea .Button == MouseButtons .Right)
            {
                if (mover .Release ())
                {
                    if (nDist <= 3)     // ContextMenu if nDist <= 3
                    {
                        MenuSelection (mover .WasCaughtObject);
                    }
                }
                else
                {
                    if (nDist <= 3)     // ContextMenu if nDist <= 3
                    {
                        ContextMenuStrip = contextMenuAnywhere;
                    }
                }
            }
        }
        // -------------------------------------------------        MenuSelection
        private void MenuSelection (int iInMover)
        {
            Identification (iInMover);                                // Identification
            if (mover [iInMover] .Source is Scale)
            {
                ContextMenuStrip = contextMenuOnScale;
            }
            else if (! (mover [iInMover] .Source is TwoColumnInfo))
            {
                ContextMenuStrip = contextMenuAnywhere;
            }
        }
        // -------------------------------------------------        OnMouseMove
        private void OnMouseMove (object sender, MouseEventArgs e)
        {
            if (mover .Move (e .Location))
            {
                Invalidate ();
            }
        }
        // -------------------------------------------------        OnMouseDoubleClick
        private void OnMouseDoubleClick (object sender, MouseEventArgs mea)
        {
            if (mover .Catch (mea .Location, MouseButtons .Left))
            {
                int iInMover = mover .CaughtObject;
                if (mover [iInMover] .Source is MSPlot)
                {
                    plot .ParametersDialog (this, RenewMover, ParametersChanged);
                }
                else if (mover [iInMover] .Source is Scale)
                {
                    Identification (iInMover);                                // Identification
                    (mover [iInMover] .Source as Scale) .ParametersDialog (this, RenewMover, ParametersChanged);
                }
            }
        }
        // -------------------------------------------------        OnContextMenuChanged
        private void OnContextMenuChanged (object sender, EventArgs e)
        {
            if (ContextMenuStrip != null)
            {
                ContextMenuStrip .Show (PointToScreen (ptMouse_Up));
            }
        }
        // -------------------------------------------------        OnMouseCaptureChanged
        private void OnMouseCaptureChanged (object sender, EventArgs e)
        {
            ContextMenuStrip = null;
        }
        // -------------------------------------------------		ParametersChanged
        private void ParametersChanged (object sender, EventArgs ea)
        {
            Invalidate ();
        }
        // -------------------------------------------------        Identification
        private void Identification (int iInMover)
        {
            long id;
            iAreaTouched = -1;
            iScaleTouched = -1;
            bVerScaleTouched = false;
            bHorScaleTouched = false;

            if (mover [iInMover] .Source is MSPlot)
            {
                iAreaTouched = 0;
            }
            else if (mover [iInMover] .Source is Scale)
            {
                id = (mover [iInMover] .Source as Scale) .ID;
                iScaleTouched = plot .VerScaleOrder (id);
                if (iScaleTouched >= 0)
                {
                    bVerScaleTouched = true;
                    return;
                }
                iScaleTouched = plot .HorScaleOrder (id);
                if (iScaleTouched >= 0)
                {
                    bHorScaleTouched = true;
                }
            }
        }
        // -------------------------------------------------        SwitchCovers
        private void SwitchCovers ()
        {
            bShowCovers = !bShowCovers;
            Invalidate ();
        }
        // -------------------------------------------------        Click_miSwitchCovers
        private void Click_miSwitchCovers (object sender, EventArgs e)
        {
            SwitchCovers ();
        }
        // -------------------------------------------------        Click_miGraphAreaParams
        private void Click_miGraphAreaParams (object sender, EventArgs e)
        {
            plot .ParametersDialog (this, RenewMover, ParametersChanged);  
        }
        // -------------------------------------------------        Click_miPurePlotIntoClipboard
        private void Click_miPurePlotIntoClipboard (object sender, EventArgs e)
        {
            plot .IntoClipboard (FunctionsPainting, CopyPart .PurePlot, Color .White, Color .White);
        }
        // -------------------------------------------------        Click_miFullPlotIntoClipboard
        private void Click_miFullPlotIntoClipboard (object sender, EventArgs e)
        {
            plot .IntoClipboard (FullAreaPainting, CopyPart .FullPlotArea, Color .White, Color .White);
        }
        // -------------------------------------------------        Click_miFlipScale
        private void Click_miFlipScale (object sender, EventArgs e)
        {
            if (bVerScaleTouched)
            {
                plot .FlipVerScale (iScaleTouched);
            }
            else
            {
                plot .FlipHorScale (iScaleTouched);
            }
            RenewMover ();
            Invalidate ();
        }
        // -------------------------------------------------        Click_miHideScale
        private void Click_miHideScale (object sender, EventArgs e)
        {
            Scale scale;
            if (bVerScaleTouched)
            {
                scale = plot .VerScales [iScaleTouched];
            }
            else  
            {
                scale = plot .HorScales [iScaleTouched];
            }
            scale .Visible = false;
            if (plot .ParamsForm != null)
            {
                plot .ParamsForm .RefreshView ();
            }
            RenewMover ();
            Invalidate ();
        }
        // =================================================        SaveInfoToRegistry
        private void SaveInfoToRegistry ()
        {
            string strkey = Form_Main .strRegKey + strAddRegKey;

            RegistryKey regkey = null;
            try
            {
                regkey = Registry .CurrentUser .OpenSubKey (strkey, true);
                if (regkey == null)
                {
                    regkey = Registry .CurrentUser .CreateSubKey (strkey);
                }
                string [] strSizes = {  version .ToString (),         // 0 
                                        Font .Name,                   // 1
                                        Font .SizeInPoints .ToString (),    // 2

                                        ClientSize .Width .ToString (),     // 3            
                                        ClientSize .Height .ToString (),    // 4
                                        info .Location .X .ToString (),     // 5
                                        info .Location .Y .ToString (),     // 6 
                                    };
                regkey .SetValue (nameSizes, strSizes, RegistryValueKind .MultiString);
                plot .IntoRegistry (regkey, "MSP");
            }
            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, true);
                if (regkey != null)
                {
                    string [] strSizes = (string []) regkey .GetValue (nameSizes);
                    if (strSizes != null && strSizes .Length == 7)
                    {
                        if (Convert .ToInt32 (strSizes [0]) == 507 &&
                            Font .Name == strSizes [1] &&
                            Font .SizeInPoints == Convert .ToSingle (strSizes [2]))
                        {
                            ClientSize = Auxi_Convert .IntoSize (strSizes, 3);
                            CreateInfo (Convert .ToInt32 (strSizes [5]), Convert .ToInt32 (strSizes [6]));

                            plot = MSPlot .FromRegistry (this, regkey, "MSP");
                        }
                    }
                }
            }
            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