Click here to Skip to main content
15,886,770 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_NameView_DepFr : Form
    {
        List<ViewOnDependentFrames> views;
        Mover mover;
        CommentedControl ccNewName;
        string newname = "";

        // -------------------------------------------------
        public Form_NameView_DepFr (List<ViewOnDependentFrames> viewsSrc)
        {
            InitializeComponent ();
            mover = new Mover ();

            views = viewsSrc;

            listUsedNames .Columns [0] .Width = listUsedNames .Width - (SystemInformation .VerticalScrollBarWidth + 4);
            ShowUsedNames ();
            RestoreFromRegistry ();
            ccNewName = new CommentedControl (this, textNewName, 0.5, -12, "New name");

            RenewMover ();
        }
        // -------------------------------------------------        OnFormClosing
        private void OnFormClosing (object sender, FormClosingEventArgs e)
        {
            SaveInfoToRegistry ();
        }
        // -------------------------------------------------        NewName
        public string NewName
        {
            get { return (newname); }
        }
        // -------------------------------------------------        ShowUsedNames
        public void ShowUsedNames ()
        {
            listUsedNames .Items .Clear ();
            foreach (ViewOnDependentFrames view in views)
            {
                listUsedNames .Items .Add (view .ViewName);
            }
        }
        // -------------------------------------------------        RenewMover
        public void RenewMover ()
        {
            mover .Clear ();
            ccNewName .IntoMover (mover, 0);
            mover .Add (listUsedNames);
            mover .Add (btnDelete);
        }
        // -------------------------------------------------        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 ();
            }
        }
        // -------------------------------------------------        OnPaint
        private void OnPaint (object sender, PaintEventArgs e)
        {
            ccNewName .Draw (e .Graphics);
        }
        // -------------------------------------------------        KeyPress_textNewName
        private void KeyPress_textNewName (object sender, KeyPressEventArgs e)
        {
            TextBox txtbox = (TextBox) sender;
            if ((int) e .KeyChar == (int) Keys .Enter)
            {
                string str = txtbox .Text .Trim ();
                if (string .Equals (str, "Default", StringComparison .OrdinalIgnoreCase))
                {
                    MessageBox .Show ("Name DEFAULT is forbidden", "Try another name", MessageBoxButtons .OK, MessageBoxIcon .Exclamation);
                }
                else
                {
                    foreach (ListViewItem lvi in listUsedNames .Items)
                    {
                        if (str == lvi .Text)
                        {
                            MessageBox .Show ("This name is already in use", "Try another name", MessageBoxButtons .OK, MessageBoxIcon .Exclamation);
                            return;
                        }
                    }
                    newname = str;
                    Close ();
                }
            }
        }
        // -------------------------------------------------        Click_btnDelete
        private void Click_btnDelete (object sender, EventArgs e)
        {
            if (listUsedNames .SelectedIndices .Count > 0 && listUsedNames .SelectedIndices [0] < listUsedNames .Items .Count)
            {
                string name = listUsedNames .Items [listUsedNames .SelectedIndices [0]] .Text;
                for (int i = views .Count - 1; i >= 0; i--)
                {
                    if (name == views [i] .ViewName)
                    {
                        views .RemoveAt (i);
                        ShowUsedNames ();
                        break;
                    }
                }
            }
            else
            {
                MessageBox .Show ("Name to delete is not selected", "Delete name", MessageBoxButtons .OK, MessageBoxIcon .Warning);
            }
        }

        const string strAddRegKey = "Form_NameView_DepFr";
        const string nameSizes = "Sizes";
        // =================================================        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);
                }
                string [] strSizes = {  ClientSize .Width .ToString (),         // 0            
                                        ClientSize .Height .ToString (),        // 1
                                        textNewName .Left .ToString (),     // 2
                                        textNewName .Top .ToString (),      // 3
                                        textNewName .Width .ToString (),    // 4
                                        textNewName .Height .ToString (),   // 5
                                        listUsedNames .Left .ToString (),     // 6
                                        listUsedNames .Top .ToString (),      // 7
                                        listUsedNames .Width .ToString (),    // 8
                                        listUsedNames .Height .ToString (),   // 9
                                        listUsedNames .Columns [0] .Width .ToString (),     // 10
                                        btnDelete .Left .ToString (),       // 11
                                        btnDelete .Top .ToString (),        // 12
                                     };
                regkey .SetValue (nameSizes, strSizes, RegistryValueKind .MultiString);
            }
            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 == 13)
                    {
                        ClientSize = Auxi_Convert .IntoSize (strSizes, 0);
                        textNewName .Bounds = Auxi_Convert .IntoRectangle (strSizes, 2);
                        listUsedNames .Bounds = Auxi_Convert .IntoRectangle (strSizes, 6);
                        listUsedNames .Columns [0] .Width = Convert .ToInt32 (strSizes [10]);
                        btnDelete .Location = Auxi_Convert .IntoPoint (strSizes, 11);
                    }
                    else
                    {
                        return;
                    }
                }
            }
            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