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

Universal Framework for Science and Engineering - Part 4: Space elevator

Rate me:
Please Sign up or sign in to vote.
4.56/5 (6 votes)
14 Aug 20066 min read 36.6K   2.2K   37  
An article on framework applications to the space elevator.
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

using ResourceService;

namespace ToolBox
{
    [Serializable()]
    public class MovedPictureBox : PictureBox, ISetBorders, ISerializable, ICloneable
    {
        private ControlPanel[] panels;
        private bool moved;
        private int mouseX;
        private int mouseY;
        private Bitmap bmpTemp;
        private bool isActive = false;

        public MovedPictureBox()
        {
            init();
        }

        /// <summary>
        /// Deserialization constructor
        /// </summary>
        /// <param name="info">Serialization info</param>
        /// <param name="context">Streaming context</param>
        public MovedPictureBox(SerializationInfo info, StreamingContext context)
        {
            Left = (int)info.GetValue("Left", typeof(int));
            Top = (int)info.GetValue("Top", typeof(int));
            Width = (int)info.GetValue("Width", typeof(int));
            Height = (int)info.GetValue("Height", typeof(int));
            Image = (Image)info.GetValue("Bitmap", typeof(Bitmap));
            init();
        }

        /// <summary>
        /// ISerializable interface implementation
        /// </summary>
        /// <param name="info">Serialization info</param>
        /// <param name="context">Streaming context</param>
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("Left", Left);
            info.AddValue("Top", Top);
            info.AddValue("Width", Width);
            info.AddValue("Height", Height);
            bmpTemp = new Bitmap(Image);
            info.AddValue("Bitmap", bmpTemp);
        }

        public object Clone()
        {
            MovedPictureBox clone = new MovedPictureBox();
            clone.Image = new Bitmap(Image);
            clone.Left = Left;
            clone.Top = Top;
            clone.Width = Width;
            clone.Height = Height;
            return clone;
        }



        public ControlPanel this[int i]
        {
            get
            {
                return panels[i];
            }
        }

        public void Deactivate()
        {
            ControlPanel.RemovePanels(this);
            isActive = false;
        }

        public bool IsActive
        {
            get
            {
                return isActive;
            }
        }


        public Control MovePicture
        {
            get
            {
                return null;
            }
        }

        public bool IsEmpty
        {
            get
            {
                return false;
            }
        }



        protected void onMouseUpEventHandler(object sender, MouseEventArgs e)
        {
            moved = false;
        }

        protected void onMouseMoveEventHandler(object sender, MouseEventArgs e)
        {
            if (!moved)
            {
                return;
            }
            int dx = e.X - mouseX;
            int dy = e.Y - mouseY;
            ControlPanel.Move(this, dx, dy);

        }

        private void keyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                foreach (Panel p in panels)
                {
                    p.Dispose();
                }
                Dispose();
            }
        }

        private void enter(object sender, EventArgs e)
        {
            ControlPanel.SetPanels(this, panels);
        }


        private void exit(object sender, EventArgs e)
        {
            Deactivate();
        }

        protected void onMouseDownEventHandler(object sender, MouseEventArgs e)
        {
            mouseX = e.X;
            mouseY = e.Y;
            moved = true;
            ControlPanel.SetPanels(this, panels);
            Focus();
            ControlPanel.DeactivateOther(this);
            isActive = true;
        }


        private void init()
        {
            AllowDrop = true;
            panels = new ControlPanel[]{new ControlPanel(this, 0), new ControlPanel(this, 1), 
										   new ControlPanel(this, 2), new ControlPanel(this, 3)};
            for (int i = 0; i < panels.Length; i++)
            {
                panels[i].Width = 4;
                panels[i].Height = 4;
                panels[i].BackColor = Color.Black;
            }
            Enter += new EventHandler(enter);
            Leave += new EventHandler(exit);
            MouseDown += new MouseEventHandler(onMouseDownEventHandler);
            MouseUp += new MouseEventHandler(onMouseUpEventHandler);
            MouseMove += new MouseEventHandler(onMouseMoveEventHandler);
            KeyDown += new KeyEventHandler(keyDown);
            MenuItem itCopy = new MenuItem(Resources.GetResourceString("Copy"));
            MenuItem itPaste = new MenuItem(Resources.GetResourceString("Paste"));
            itPaste.Click += new EventHandler(pasteClick);
            itCopy.Click += new EventHandler(copyClick);
            DragEnter += new DragEventHandler(dragEnter);
            DragDrop += new DragEventHandler(dragDrop);
            ContextMenu = new ContextMenu(new MenuItem[] { itCopy, itPaste });
        }

        private void pasteClick(object sender, EventArgs e)
        {
            IDataObject ob = System.Windows.Forms.Clipboard.GetDataObject();
            if (ob.GetDataPresent("System.Drawing.Bitmap"))
            {
                Image = ob.GetData("System.Drawing.Bitmap") as Image;
            }
        }

        private void copyClick(object sender, EventArgs e)
        {
            Bitmap bmp = new Bitmap(Image);
            System.Windows.Forms.Clipboard.SetDataObject(bmp, true);
        }


        private void dragEnter(object sender, DragEventArgs e)
        {
            /*		string[] str = e.Data.GetFormats();
                    foreach (string s in str)
                    {
                        object o = e.Data.GetData(s);
                        o = o;
                    }*/
            /*	string[] str = e.Data.GetFormats();
                foreach (string s in str)
                {
                    object o = e.Data.GetData(s);
                    o = o;
                }*/

            if (e.Data.GetDataPresent("FileNameW"))
            {
                Array a = e.Data.GetData("FileNameW") as Array;
                string s = a.GetValue(0) as string;
                string fin = s.Substring(s.Length - 4).ToLower();
                if (fin.Equals(".bmp") | fin.Equals(".gif") | fin.Equals(".jpg") | fin.Equals(".ico") | fin.Equals("jpeg"))
                {
                    e.Effect = DragDropEffects.Copy;
                    return;
                }
            }
        }
        private void dragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent("FileNameW"))
            {
                Array a = e.Data.GetData("FileNameW") as Array;
                string s = a.GetValue(0) as string;
                string fin = s.Substring(s.Length - 4).ToLower();
                if (fin.Equals(".bmp") | fin.Equals(".gif") | fin.Equals(".jpg") | fin.Equals(".ico") | fin.Equals("jpeg"))
                {
                    Image = new Bitmap(s);
                    return;
                }
            }
        }

    }

}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Architect
Russian Federation Russian Federation
Ph. D. Petr Ivankov worked as scientific researcher at Russian Mission Control Centre since 1978 up to 2000. Now he is engaged by Aviation training simulators http://dinamika-avia.com/ . His additional interests are:

1) Noncommutative geometry

http://front.math.ucdavis.edu/author/P.Ivankov

2) Literary work (Russian only)

http://zhurnal.lib.ru/editors/3/3d_m/

3) Scientific articles
http://arxiv.org/find/all/1/au:+Ivankov_Petr/0/1/0/all/0/1

Comments and Discussions