Click here to Skip to main content
15,886,362 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.2K   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 .ComponentModel;
using System .Drawing;
using System .Windows .Forms;
using Microsoft .Win32;

using MoveGraphLibrary;

namespace Test_MoveGraphLibrary
{
    public partial class Form_PersonalInfo_LTP : Form     // text to any CommentedControl has Limited Text Positioning (_LTP)
    {
        Point ptMouse_Up;
        Mover mover;
        TextM tmBirth;
        TextM info;
        Control [] ctrls;
        CommentedControlLTP [] comctrls;

        Spaces spaces;
        List<PersonalInfoView_LTP> views = new List<PersonalInfoView_LTP> ();

        string strInfo = "This form is based on the CommentedControlLTP objects. \n" +
                         "Control and its comment move synchronously. \n" +
                         "To change the relative comment's position, R_Press on it.\n" +
                         "To change the view or save it under some name,\n" +
                         "call context menu at any empty place.";

        // -------------------------------------------------
        public Form_PersonalInfo_LTP ()
        {
            InitializeComponent ();
            mover = new Mover (this);
            mover .Clipping = Clipping .Safe;

            ctrls = new Control [] {textName, textSurname,                                          // 0, 1
                                    textMonth, textDay, textYear,                                   // 2, 3, 4
                                    textProfession,                                                 // 5
                                    textHomePhone, textOfficePhone, textMobilePhone,                // 6, 7, 8
                                    textStreet, textTown, textProvince, textCountry, textZipCode};  // 9 - 13
            comctrls = new CommentedControlLTP [ctrls .Length];

            spaces = new Spaces (Auxi_Geometry .RoundMeasureString (this, "Temporary") .Height);
            tmBirth = new TextM (this, new Point (100, 100), "Birth");
            tmBirth .ShowFrame = false;
            DefaultView ();
            RestoreFromRegistry ();

            RenewMover ();
        }
        // -------------------------------------------------        OnFormClosing
        private void OnFormClosing (object sender, FormClosingEventArgs e)
        {
            SaveInfoToRegistry ();
        }
        // -------------------------------------------------        DefaultView
        private void DefaultView ()
        {
            int nH = textName .Height;
            int cxL = 90;
            textName .Bounds = new Rectangle (cxL, 30, 96, nH);
            textSurname .Bounds = new Rectangle (cxL, textName .Bottom + spaces .VerMin, textName .Width, textName .Height);
            int cy = textSurname .Bottom + spaces .VerMin + nH;
            textMonth .Location = new Point (cxL, cy);
            textDay .Location = new Point (textMonth .Right + spaces .HorMin, cy);
            textYear .Location = new Point (textDay .Right + spaces .HorMin, cy);
            tmBirth .Location = new Point (cxL - (tmBirth .Width + 4), textMonth .Top + textMonth .Height / 2 - tmBirth .Height / 2);

            textProfession .Bounds = new Rectangle (cxL, textDay .Bottom + 2 * spaces .VerMin, 300, nH);
            cy = textProfession .Bottom + spaces .VerMin + nH;
            textHomePhone .Bounds = new Rectangle (cxL, cy, 152, nH);
            textOfficePhone .Bounds = new Rectangle (textHomePhone .Right + spaces .HorMin, cy, 152, nH);
            textMobilePhone .Bounds = new Rectangle (textOfficePhone .Right + spaces .HorMin, cy, 152, nH);

            textStreet .Bounds = new Rectangle (cxL, textHomePhone .Bottom + spaces .VerMin + nH, 152, nH);
            textTown .Bounds = new Rectangle (cxL, textStreet .Bottom + spaces .VerMin, 152, nH);
            textProvince .Bounds = new Rectangle (cxL, textTown .Bottom + spaces .VerMin, 152, nH);
            textCountry .Bounds = new Rectangle (cxL, textProvince .Bottom + spaces .VerMin, 152, nH);
            textZipCode .Bounds = new Rectangle (cxL, textCountry .Bottom + spaces .VerMin, 90, nH);

            Rectangle rcAround = Auxi_Geometry .FrameAroundControls (ctrls, spaces);
            ClientSize = new Size (rcAround .Right + spaces .FormSideSpace, rcAround .Bottom + spaces .FormBtmSpace);

            string [] strCC = new string [] {"Name", "Surname", "Month", "Day", "Year", "Profession", 
                                             "Home phone", "Office phone", "Mobile phone", 
                                             "Street", "Town", "Province", "Country", "ZIP code"};

            comctrls [0] = new CommentedControlLTP (this, ctrls [0], Side .W, StringAlignment .Center, 4, strCC [0]);
            comctrls [1] = new CommentedControlLTP (this, ctrls [1], Side .W, StringAlignment .Center, 4, strCC [1]);
            comctrls [2] = new CommentedControlLTP (this, ctrls [2], Side .N, StringAlignment .Near, strCC [2]);
            comctrls [3] = new CommentedControlLTP (this, ctrls [3], Side .N, StringAlignment .Near, strCC [3]);
            comctrls [4] = new CommentedControlLTP (this, ctrls [4], Side .N, StringAlignment .Near, strCC [4]);
            comctrls [5] = new CommentedControlLTP (this, ctrls [5], Side .W, StringAlignment .Center, strCC [5]);
            comctrls [6] = new CommentedControlLTP (this, ctrls [6], Side .N, StringAlignment .Near, strCC [6]);
            comctrls [7] = new CommentedControlLTP (this, ctrls [7], Side .N, StringAlignment .Near, strCC [7]);
            comctrls [8] = new CommentedControlLTP (this, ctrls [8], Side .N, StringAlignment .Near, strCC [8]);
            comctrls [9] = new CommentedControlLTP (this, ctrls [9], Side .W, StringAlignment .Center, strCC [9]);
            comctrls [10] = new CommentedControlLTP (this, ctrls [10], Side .W, StringAlignment .Center, strCC [10]);
            comctrls [11] = new CommentedControlLTP (this, ctrls [11], Side .W, StringAlignment .Center, strCC [11]);
            comctrls [12] = new CommentedControlLTP (this, ctrls [12], Side .W, StringAlignment .Center, strCC [12]);
            comctrls [13] = new CommentedControlLTP (this, ctrls [13], Side .W, StringAlignment .Center, strCC [13]);

            info = new TextM (this, new Point (textProvince .Right + 30, textProvince .Bottom), strInfo);
            info .BackColor = Color .FromArgb (255, 255, 128);
        }
        // -------------------------------------------------        SetView
        private void SetView (int iView)
        {
            if (0 <= iView && iView < views .Count)
            {
                PersonalInfoView_LTP view = views [iView];
                ClientSize = view .Size;
                tmBirth .Location = view .OneMovableText .Location;
                tmBirth .CopyView (view .OneMovableText);
                for (int i = 0; i < ctrls .Length; i++)
                {
                    comctrls [i] = CommentedControlLTP .FromData (this, ctrls [i], view .CommentsData [i]);
                }
            }
        }
        // -------------------------------------------------        RenewMover
        private void RenewMover ()
        {
            mover .Clear ();
            foreach (CommentedControlLTP cc in comctrls)
            {
                mover .Add (cc);
            }
            mover .Add (tmBirth);
            mover .Add (info);
        }
        // -------------------------------------------------        OnPaint
        private void OnPaint (object sender, PaintEventArgs e)
        {
            Graphics grfx = e .Graphics;

            info .Draw (grfx);
            for (int i = mover .Count - 1; i >= 0; i--)
            {
                if (mover [i] .Source is CommentedControlLTP)
                {
                    (mover [i] .Source as CommentedControlLTP) .Draw (grfx);
                }
            }
            tmBirth .Draw (grfx);
        }
        // -------------------------------------------------        OnMouseDown
        private void OnMouseDown (object sender, MouseEventArgs e)
        {
            mover .Catch (e .Location, e .Button);
            ContextMenuStrip = null;
        }
        // -------------------------------------------------        OnMouseUp
        private void OnMouseUp (object sender, MouseEventArgs e)
        {
            ptMouse_Up = e .Location;
            if (e .Button == MouseButtons .Left)
            {
                mover .Release ();
            }
            else if (e .Button == MouseButtons .Right)
            {
                if (mover .Release ())
                {
                    int iInMover = mover .WasCaughtObject;
                    if (mover [iInMover] .Source is CommentedControlLTP)
                    {
                        CommentedControlLTP cc = mover [iInMover] .Source as CommentedControlLTP;
                        Form_TextLimitedPositioning form = new Form_TextLimitedPositioning (this .PointToScreen (e .Location),
                                                                                            cc .TextSide, cc .TextAlignment);
                        form .ShowDialog ();
                        cc .TextSide = form .Side;
                        cc .TextAlignment = form .TextAlignment;
                        Invalidate ();
                    }
                }
                else
                {
                    ContextMenuStrip = menuOnEmpty;
                }
            }
        }
        // -------------------------------------------------        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));
            }
        }
        // -------------------------------------------------        OnOpening_menuOnEmpty
        private void OnOpening_menuOnEmpty (object sender, CancelEventArgs e)
        {
            for (int i = menuOnEmpty .Items .Count - 1; i >= 3; i--)
            {
                menuOnEmpty .Items .RemoveAt (i);
            }
            for (int i = 0; i < views .Count; i++)
            {
                menuOnEmpty .Items .Add (views [i] .Name, null, Click_OneOfViews);
            }
        }
        // -------------------------------------------------        Click_miNameView
        private void Click_miNameView (object sender, EventArgs e)
        {
            Form_NameView_LTP form = new Form_NameView_LTP (views);
            form .ShowDialog ();
            if (!string .IsNullOrEmpty (form .NewName))
            {
                PersonalInfoView_LTP piv = new PersonalInfoView_LTP (form .NewName, ClientSize, info .Location,
                                                                     TextM .Copy (this, tmBirth .Location, tmBirth), comctrls);
                views .Insert (0, piv);
            }
        }
        // -------------------------------------------------        Click_miSetDefaultView
        private void Click_miSetDefaultView (object sender, EventArgs e)
        {
            DefaultView ();
            RenewMover ();
            Invalidate ();
        }
        // -------------------------------------------------        Click_OneOfViews
        private void Click_OneOfViews (object sender, EventArgs e)
        {
            string name = (sender as ToolStripMenuItem) .Text;
            int iView;
            for (iView = views .Count - 1; iView >= 0; iView--)
            {
                if (name == views [iView] .Name)
                {
                    break;
                }
            }
            if (iView >= 0)
            {
                while (iView > 0)
                {
                    views .Reverse (iView - 1, 2);
                    iView--;
                }
                SetView (0);
                RenewMover ();
                Invalidate ();
            }
        }

        const string strAddRegKey = "Form_PersonalInfo_LTP";
        const string nameSizes = "Sizes";
        const string nameLastView = "LastView";
        // =================================================        SaveInfoToRegistry
        private void SaveInfoToRegistry ()
        {
            string strRegKey = Form_Main .strRegKey + strAddRegKey;

            RegistryKey regkey = null;
            try
            {
                regkey = Registry .CurrentUser .OpenSubKey (strRegKey, true);
                if (regkey == null)
                {
                    regkey = Registry .CurrentUser .CreateSubKey (strRegKey);
                }
                regkey .SetValue (nameSizes, new string [] {ClientSize .Width .ToString (),     // 0            
                                                            ClientSize .Height .ToString (),    // 1
                                                            views .Count .ToString (),          // 2
                                                           },
                                           RegistryValueKind .MultiString);
                for (int i = 0; i < views .Count; i++)
                {
                    views [i] .IntoRegistry (regkey, "View_" + i .ToString ());
                }
                PersonalInfoView_LTP viewOnSave = new PersonalInfoView_LTP ("ViewOnSave", ClientSize, info .Location, 
                                                                            TextM .Copy (this, tmBirth .Location, tmBirth), comctrls);
                viewOnSave .IntoRegistry (regkey, "OnSave");
            }
            catch
            {
            }
            finally
            {
                if (regkey != null) regkey .Close ();
            }
        }
        // =================================================        RestoreFromRegistry
        private void RestoreFromRegistry ()
        {
            string namekey = Form_Main .strRegKey + strAddRegKey;

            RegistryKey regkey = null;
            try
            {
                regkey = Registry .CurrentUser .OpenSubKey (namekey, true);
                if (regkey != null)
                {
                    string [] strSizes = (string []) regkey .GetValue (nameSizes);
                    if (strSizes != null && strSizes .Length == 3)
                    {
                        ClientSize = Auxi_Convert .IntoSize (strSizes, 0);
                        int nViews = Convert .ToInt32 (strSizes [2]);
                        for (int i = 0; i < nViews; i++)
                        {
                            PersonalInfoView_LTP view = PersonalInfoView_LTP .FromRegistry (this, regkey, "View_" + i .ToString ());
                            if (view != null)
                            {
                                views .Add (view);
                            }
                        }
                        PersonalInfoView_LTP viewOnSave = PersonalInfoView_LTP .FromRegistry (this, regkey, "OnSave");
                        if (viewOnSave != null)
                        {
                            ClientSize = viewOnSave .Size;
                            info .Location = viewOnSave .InfoLocation;
                            tmBirth = viewOnSave .OneMovableText;
                            for (int i = 0; i < comctrls .Length; i++)
                            {
                                comctrls [i] = CommentedControlLTP .FromData (this, ctrls [i], viewOnSave .CommentsData [i]);
                            }
                        }
                    }
                    else
                    {
                        return;
                    }
                }
            }
            catch
            {
            }
            finally
            {
                if (regkey != null) regkey .Close ();
            }
        }
    }

    // ******************************************
    //
    //   PersonalInfoView_LTP
    //
    public class PersonalInfoView_LTP
    {
        string name;
        Size size;
        Point ptInfo;
        TextM txm;
        CommentedControlLTP_Data [] dccs;

        // -------------------------------------------------
        public PersonalInfoView_LTP (string nm, Size sizeClient, Point ptinf, TextM tm, CommentedControlLTP [] coms)
        {
            name = nm;
            size = sizeClient;
            ptInfo = ptinf;
            txm = tm;
            dccs = new CommentedControlLTP_Data [coms .Length];
            for (int i = 0; i < coms .Length; i++)
            {
                dccs [i] = new CommentedControlLTP_Data (coms [i]);
            }
        }
        // -------------------------------------------------
        public PersonalInfoView_LTP (string nm, Size sizeClient, Point ptinf, TextM tm, CommentedControlLTP_Data [] comds)
        {
            name = nm;
            size = sizeClient;
            ptInfo = ptinf;
            txm = tm;
            dccs = comds;
        }
        // -------------------------------------------------        Name
        public string Name
        {
            get { return (name); }
        }
        // -------------------------------------------------        Size
        public Size Size
        {
            get { return (size); }
        }
        // -------------------------------------------------        InfoLocation
        public Point InfoLocation
        {
            get { return (ptInfo); }
        }
        // -------------------------------------------------        OneMovableText
        public TextM OneMovableText
        {
            get { return (txm); }
        }
        // -------------------------------------------------        CommentsData
        public CommentedControlLTP_Data [] CommentsData
        {
            get { return (dccs); }
        }

        const string namePIV = "PIL_";
        // =================================================        IntoRegistry
        public void IntoRegistry (RegistryKey regkey, string strNum)
        {
            string [] strs = new string [] {name,                           // 0
                                            size .Width .ToString (),       // 1
                                            size .Height .ToString (),      // 2
                                            ptInfo .X .ToString (),         // 3
                                            ptInfo .Y .ToString (),         // 4
                                            dccs .Length .ToString (),      // 5
                                            };
            regkey .SetValue (namePIV + strNum, strs, RegistryValueKind .MultiString);
            txm .IntoRegistry (regkey, "MovText_" + strNum);
            for (int i = 0; i < dccs .Length; i++)
            {
                dccs [i] .IntoRegistry (regkey, "DataCCL_" + strNum + i .ToString ());
            }
        }
        // -------------------------------------------------        FromRegistry
        public static PersonalInfoView_LTP FromRegistry (Form form, RegistryKey regkey, string strNum)
        {
            string [] strs = (string []) regkey .GetValue (namePIV + strNum);
            if (strs .Length >= 6)
            {
                Size sizeClient = Auxi_Convert .IntoSize (strs, 1);
                Point pt = Auxi_Convert .IntoPoint (strs, 3);
                int nDats = Convert .ToInt32 (strs [5]);
                TextM tm = TextM .FromRegistry (form, regkey, "MovText_" + strNum);
                CommentedControlLTP_Data [] comds = new CommentedControlLTP_Data [nDats];
                for (int i = 0; i < nDats; i++)
                {
                    comds [i] = CommentedControlLTP_Data .FromRegistry (regkey, "DataCCL_" + strNum + i .ToString ());
                }
                return (new PersonalInfoView_LTP (strs [0], sizeClient, pt, tm, comds));
            }
            else
            {
                return (null);
            }
        }
    }
}

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