Click here to Skip to main content
15,896,269 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.9K   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 MoveGraphLibrary;

namespace Test_MoveGraphLibrary
{
    // nonresizable square
    // text (TextMR) can be moved inside
    class ColoredSquare : GraphicalObject
    {
        Form form;
        Point ptCenter;
        int halfside;   
        Color color;

        Color clrText = Color .DarkGray;
        string str = "";
        double angle = 0.0;
        Font font = new Font ("Times New Roman", 12, FontStyle .Bold | FontStyle .Italic);

        double compensation;    // used only between MouseDown and MouseUp; calculated and used only OUTSIDE !!!

        // ------------------------------------------------- 
        public ColoredSquare (Form formParent, Point pt, int half, Color clr, string text)
        {
            form = formParent;
            ptCenter = pt;
            halfside = Math .Max (Math .Abs (half), 2);
            color = clr;
            str = text;
        }
        // ------------------------------------------------- 
        public ColoredSquare (Form formParent, Point pt, int half, Color clr)
            : this (formParent, pt, half, clr, "")
        {
        }
        // -------------------------------------------------        Center
        public Point Center
        {
            get { return (ptCenter); }
            set
            {
                ptCenter = value;
                DefineCover ();
            }
        }
        // -------------------------------------------------        Side
        public int Side
        {
            get { return (2 * halfside); }
            set
            {
                halfside = Math .Max (Math .Abs (value) / 2, 2);
                DefineCover ();
            }
        }
        // -------------------------------------------------        Color
        public Color Color
        {
            get { return (color); }
            set { color = value; }
        }
        // -------------------------------------------------        TextColor
        public Color TextColor
        {
            get { return (clrText); }
            set { clrText = value; }
        }
        // -------------------------------------------------        Text
        public string Text
        {
            get { return (str); }
            set
            {
                str = value;
                DefineCover ();
            }
        }
        // -------------------------------------------------        TextAngle
        public double TextAngle
        {
            get { return (angle); }
            set
            {
                angle = Auxi_Common .LimitedRadian (value);
                DefineCover ();
            }
        }
        // -------------------------------------------------        Font
        public Font Font
        {
            get { return (font); }
            set
            {
                font = value;
                DefineCover ();
            }
        }
        // -------------------------------------------------        StartRotation
        public void StartRotation (Point ptMouse)
        {
            double angleMouse = -Math .Atan2 (ptMouse .Y - ptCenter .Y, ptMouse .X - ptCenter .X);
            compensation = Auxi_Common .LimitedRadian (angleMouse - angle);
        }
        // -------------------------------------------------        Draw
        public void Draw (Graphics grfx)
        {
            Rectangle rc = new Rectangle (ptCenter .X - halfside, ptCenter .Y - halfside, 2 * halfside, 2 * halfside);
            grfx .FillRectangle (new SolidBrush (color), rc);
            Auxi_Drawing .DrawText (grfx, str, font, angle, clrText, ptCenter, TextBasis .M);
        }
        // -------------------------------------------------        DefineCover
        public override void DefineCover ()
        {
            Rectangle rc = new Rectangle (ptCenter .X - halfside, ptCenter .Y - halfside, 2 * halfside, 2 * halfside);
            if (str .Length > 0)
            {
                SizeF sizef = Auxi_Geometry .MeasureString (form, str, font);
                Point [] corners = Auxi_Geometry .TextCorners (sizef .Width, sizef .Height, angle, ptCenter, TextBasis .M);
                PointF [] cornersF = new PointF [corners .Length];
                for (int i = 0; i < corners .Length; i++)
                {
                    cornersF [i] = new PointF (corners [i] .X, corners [i] .Y);
                }
                CoverNode [] nodes = new CoverNode [2] {new CoverNode (0, cornersF, Cursors.Hand), 
                                                        new CoverNode (1, rc) };    //Auxi_Geometry .CornersOfRectangle (rc)
                cover = new Cover (nodes);
            }
            else
            {
                cover = new Cover (rc, Resizing .None);
            }
        }
        // -------------------------------------------------
        public override void Move (int cx, int cy)
        {
            ptCenter += new Size (cx, cy);
        }
        // -------------------------------------------------        MoveNode
        public override bool MoveNode (int i, int cx, int cy, Point ptMouse, MouseButtons catcher)
        {
            bool bRet = false;

            if (catcher == MouseButtons .Left)
            {
                Move (cx, cy);
                bRet = true;
            }
            else if (catcher == MouseButtons .Right && i == 0 && str .Length > 0)
            {
                double angleMouse = -Math .Atan2 (ptMouse .Y - ptCenter .Y, ptMouse .X - ptCenter .X);
                angle = Auxi_Common .LimitedRadian (angleMouse - compensation);      // Compensation
                bRet = true;
            }
            return (bRet);
        }

    }
}

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