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

The theory of moveable objects

Rate me:
Please Sign up or sign in to vote.
5.00/5 (126 votes)
24 Jan 2010CPOL97 min read 119K   3.2K   230  
This article describes an algorithm by which an object of an arbitrary shape can be made moveable and resizable.
using System;
using System .Collections .Generic;
using System .ComponentModel;
using System .Drawing;
using System .Windows .Forms;

using MoveGraphLibrary;

namespace TheoryOfMoveableObjects
{
    public partial class Form_ControlsIndividual : Form
    {
        Mover mover;
        LinkedRectangles lrsView;
        bool bShowCovers = false;
        string [] strs = new string [] {"Node size",
                                        "Frame"
                                       };
        TextM info;
        string infotext = "Controls can be moved by the border and resized\r\n" +
                          "by nodes in the corners or on the sides.\r\n" +
                          "The width of the sensitive frame and the size\r\n" +
                          "of the nodes for resizing can be changed.\r\n" +
                          "The size of these nodes can't be less than the frame."; 

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

            numericUD_NodeSize .Minimum = numericUD_Frame .Value;

            SizeF [] sizefStrs = Auxi_Geometry .MeasureStrings (this, strs);
            lrsView = new LinkedRectangles (new Control [] { numericUD_NodeSize, numericUD_Frame });
            lrsView .Add (Auxi_Geometry .RectangleToRectangleSide (numericUD_NodeSize .Bounds, Side .E, sizefStrs [0], 4), "Node");
            lrsView .Add (Auxi_Geometry .RectangleToRectangleSide (numericUD_Frame .Bounds, Side .E, sizefStrs [1], 4), "Frame");
            lrsView .AddUnionRectangle ();

            bool bMove;
            int nodesize = Convert .ToInt32 (numericUD_NodeSize .Value);
            int frame = Convert .ToInt32 (numericUD_Frame .Value);
            foreach (Control ctrl in Controls)
            {
                if (ctrl != numericUD_NodeSize && ctrl != numericUD_Frame)
                {
                    bMove = (ctrl == listviewResizeNotMove) ? false : true;
                    mover .Add (new FramedControl (ctrl, bMove, nodesize, frame));
                }
            }
            mover .Add (lrsView);

            info = new TextM (this, new Point (280, 350), infotext);
            info .BackColor = Color .Lime;
            mover .Add (info);
        }
        // -------------------------------------------------        OnPaint
        private void OnPaint (object sender, PaintEventArgs e)
        {
            Graphics grfx = e .Graphics;

            info .Draw (grfx);
            Auxi_Drawing .DrawText (grfx, strs [0], Font, 0, ForeColor, lrsView .Rectangle ("Node") .Location, TextBasis .NW);
            Auxi_Drawing .DrawText (grfx, strs [1], Font, 0, ForeColor, lrsView .Rectangle ("Frame") .Location, TextBasis .NW);
            if (bShowCovers)
            {
                mover .DrawCovers (grfx);
            }
        }
        // -------------------------------------------------        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 ();
                if (mover .CaughtSource is FramedControl)
                {
                    Update ();
                }
            }
        }
        // -------------------------------------------------        Click_btnCovers
        private void Click_btnCovers (object sender, EventArgs e)
        {
            bShowCovers = !bShowCovers;
            Invalidate ();
        }
        // -------------------------------------------------        ValueChanged_numericNodeSize
        private void ValueChanged_numericNodeSize (object sender, EventArgs e)
        {
            GraphicalObject grobj;
            int nodesize = Convert .ToInt32 (numericUD_NodeSize .Value);
            for (int i = 0; i < mover .Count; i++)
            {
                grobj = mover [i] .Source;
                if (grobj is FramedControl)
                {
                    (grobj as FramedControl) .NodeSize = nodesize;
                }
            }
            Invalidate ();
        }
        // -------------------------------------------------        ValueChanged_numericFrame
        private void ValueChanged_numericFrame (object sender, EventArgs e)
        {
            numericUD_NodeSize .Minimum = numericUD_Frame .Value;

            GraphicalObject grobj;
            int frame = Convert .ToInt32 (numericUD_Frame .Value);
            for (int i = 0; i < mover .Count; i++)
            {
                grobj = mover [i] .Source;
                if (grobj is FramedControl)
                {
                    (grobj as FramedControl) .FrameWidth = frame;
                }
            }
            Invalidate ();
        }
    }
}

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