Advertisement
mosmondor

InsideMover updated

May 22nd, 2012
4,829
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.34 KB | None | 0 0
  1. using System;
  2. using System.ComponentModel;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5.  
  6. namespace FirePlay.UI
  7. {
  8.     /// <summary>
  9.     /// Summary description for cInsideMover.
  10.     /// </summary>
  11.     public class InsideMover : Component
  12.     {
  13.         /// <summary>
  14.         /// Required designer variable.
  15.         /// </summary>
  16.         private Container components = null;
  17.  
  18.         public InsideMover(IContainer container)
  19.         {
  20.             ///
  21.             /// Required for Windows.Forms Class Composition Designer support
  22.             ///
  23.             container.Add(this);
  24.             InitializeComponent();
  25.  
  26.             //
  27.             // TODO: Add any constructor code after InitializeComponent call
  28.             //
  29.         }
  30.  
  31.         public InsideMover()
  32.         {
  33.             ///
  34.             /// Required for Windows.Forms Class Composition Designer support
  35.             ///
  36.             InitializeComponent();
  37.  
  38.             //
  39.             // TODO: Add any constructor code after InitializeComponent call
  40.             //
  41.         }
  42.  
  43.         public Control ControlToMove
  44.         {
  45.             set
  46.             {
  47.                 if (_parent != null)
  48.                 {
  49.                     //  odkvaci prijasnje evente
  50.                     _parent.MouseDown -= new MouseEventHandler(_parent_MouseDown);
  51.                     _parent.MouseMove -= new MouseEventHandler(_parent_MouseMove);
  52.                     _parent.MouseUp -= new MouseEventHandler(_parent_MouseUp);
  53.                     _parent.DoubleClick -= new EventHandler(_parent_DoubleClick);
  54.                 }
  55.                 _parent=value;
  56.                 if (value!=null)
  57.                 {
  58.                     //  zakači se na evente od containera koji ti trebaju
  59.                     _parent.MouseDown+=new MouseEventHandler(_parent_MouseDown);
  60.                     _parent.MouseMove+=new MouseEventHandler(_parent_MouseMove);
  61.                     _parent.MouseUp+=new MouseEventHandler(_parent_MouseUp);
  62.                     _parent.DoubleClick+=new EventHandler(_parent_DoubleClick);
  63.                 }
  64.             }
  65.             get
  66.             {
  67.                 return _parent;
  68.             }
  69.         }
  70.  
  71.         Control _parent;
  72.  
  73.         /// <summary>
  74.         /// Clean up any resources being used.
  75.         /// </summary>
  76.         protected override void Dispose( bool disposing )
  77.         {
  78.             if( disposing )
  79.             {
  80.                 if(components != null)
  81.                 {
  82.                     components.Dispose();
  83.                 }
  84.             }
  85.             base.Dispose( disposing );
  86.         }
  87.  
  88.  
  89.         #region Component Designer generated code
  90.         /// <summary>
  91.         /// Required method for Designer support - do not modify
  92.         /// the contents of this method with the code editor.
  93.         /// </summary>
  94.         private void InitializeComponent()
  95.         {
  96.             components = new System.ComponentModel.Container();
  97.         }
  98.         #endregion
  99.  
  100.         int _lastMouseX;
  101.         int _lastMouseY;
  102.         bool _moving;
  103.  
  104.         public void StartMouseDown(MouseEventArgs e)
  105.         {
  106.             _parent_MouseDown(null, e);
  107.         }
  108.  
  109.         private void _parent_MouseDown(object sender, MouseEventArgs e)
  110.         {
  111.             _lastMouseX=e.X;
  112.             _lastMouseY=e.Y;
  113.             _moving=true;
  114.         }
  115.  
  116.         private void _parent_MouseMove(object sender, MouseEventArgs e)
  117.         {
  118.             if (_moving)
  119.             {
  120.                 Point newLocation=_parent.Location;
  121.                 newLocation.X+=e.X-_lastMouseX;
  122.                 newLocation.Y+=e.Y-_lastMouseY;
  123.                 _parent.Location=newLocation;
  124.             }
  125.         }
  126.  
  127.         private void _parent_MouseUp(object sender, MouseEventArgs e)
  128.         {
  129.             _moving=false;
  130.         }
  131.  
  132.         private void _parent_DoubleClick(object sender, EventArgs e)
  133.         {
  134.             if (_parent is Form)
  135.             {
  136.                 Form f=(Form)_parent;
  137.                 if (f.WindowState==FormWindowState.Normal)
  138.                 {
  139.                     f.WindowState=FormWindowState.Maximized;
  140.                 }
  141.                 else
  142.                 {
  143.                     f.WindowState=FormWindowState.Normal;
  144.                 }
  145.             }
  146.         }
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement