FunctionX Practical Learning Logo

Moving a Form by its Body

 

Introduction

This is an example of moving a form by dragging its body

Creating and Implementing the Common Event

  1. Start Microsoft Visual C++ .Net and create a Windows Form Application named MoveMe
  2. Add a few control to the body of the form
  3. Double-click an empty area in the body of the form to access its Load event
  4. Declare a Boolean variable named MouseIsDown and initialize it to false in the Load event of the form:
     
    #pragma once
    
    
    namespace MoveMe
    {
    	using namespace System;
    	using namespace System::ComponentModel;
    	using namespace System::Collections;
    	using namespace System::Windows::Forms;
    	using namespace System::Data;
    	using namespace System::Drawing;
    
    	/// <summary> 
    	/// Summary for Form1
    	///
    	/// WARNING: If you change the name of this class, you will need to change the 
    	///          'Resource File Name' property for the managed resource compiler tool 
    	///          associated with all .resx files this class depends on.  Otherwise,
    	///          the designers will not be able to interact properly with localized
    	///          resources associated with this form.
    	/// </summary>
    	public __gc class Form1 : public System::Windows::Forms::Form
    	{	
    	public:
    		Form1(void)
    		{
    			InitializeComponent();
    		}
      
    	protected:
    		void Dispose(Boolean disposing)
    		{
    			if (disposing && components)
    			{
    				components->Dispose();
    			}
    			__super::Dispose(disposing);
    		}
    
    	private:
    		/// <summary>
    		/// Required designer variable.
    		/// </summary>
    		System::ComponentModel::Container * components;
    		bool MouseIsDown;
    
    		/// <summary>
    		/// Required method for Designer support - do not modify
    		/// the contents of this method with the code editor.
    		/// </summary>
    		void InitializeComponent(void)
    		{
    			// 
    			// Form1
    			// 
    			this->AutoScaleBaseSize = System::Drawing::Size(5, 13);
    			this->ClientSize = System::Drawing::Size(292, 266);
    			this->Name = S"Form1";
    			this->Text = S"Form1";
    			this->Load += new System::EventHandler(this, Form1_Load);
    
    		}	
    	private: System::Void Form1_Load(System::Object *  sender, System::EventArgs *  e)
    			 {
    				 MouseIsDown = false;
    			 }
    
    	};
    }
  5. Click the form
  6. In the Properties window, click the Events button
  7. Double-click the right side of MouseDown
  8. Declare two integer variables in the class. Call them OldX and OldY
  9. Implement the MouseDown event as follows:
     
    #pragma once
    
    
    namespace MoveMe
    {
    	using namespace System;
    	using namespace System::ComponentModel;
    	using namespace System::Collections;
    	using namespace System::Windows::Forms;
    	using namespace System::Data;
    	using namespace System::Drawing;
    
    	/// <summary> 
    	/// Summary for Form1
    	///
    	/// WARNING: If you change the name of this class, you will need to change the 
    	///          'Resource File Name' property for the managed resource compiler tool 
    	///          associated with all .resx files this class depends on.  Otherwise,
    	///          the designers will not be able to interact properly with localized
    	///          resources associated with this form.
    	/// </summary>
    	public __gc class Form1 : public System::Windows::Forms::Form
    	{	
    	public:
    		Form1(void)
    		{
    			InitializeComponent();
    		}
      
    	protected:
    		void Dispose(Boolean disposing)
    		{
    			if (disposing && components)
    			{
    				components->Dispose();
    			}
    			__super::Dispose(disposing);
    		}
    
    	private:
    		/// <summary>
    		/// Required designer variable.
    		/// </summary>
    		System::ComponentModel::Container * components;
    		bool MouseIsDown;
    		int OldX, OldY;
    
    		/// <summary>
    		/// Required method for Designer support - do not modify
    		/// the contents of this method with the code editor.
    		/// </summary>
    		void InitializeComponent(void)
    		{
    			// 
    			// Form1
    			// 
    			this->AutoScaleBaseSize = System::Drawing::Size(5, 13);
    			this->ClientSize = System::Drawing::Size(292, 266);
    			this->Name = S"Form1";
    			this->Text = S"Form1";
    			this->MouseDown += 
    new System::Windows::Forms::MouseEventHandler(this, Form1_MouseDown);
    			this->Load += new System::EventHandler(this, Form1_Load);
    
    		}	
    	private: System::Void Form1_Load(System::Object *  sender, System::EventArgs *  e)
    			 {
    				 MouseIsDown = false;
    			 }
    
    	private: System::Void Form1_MouseDown(System::Object *  sender, 
    System::Windows::Forms::MouseEventArgs *  e)
    			 {
    				 if( e->Button == MouseButtons::Left )
    				 {
    					 OldX = e->X;
    					 OldY = e->Y;
    					 MouseIsDown = true;
    				 }
    			 }
    
    	};
    }
  10. Click the form and, in the Properties window, click the Events button
  11. Double-click the right side of MouseDown and implement the event as follows:
     
    private: System::Void Form1_MouseMove(System::Object *  sender, System::Windows::Forms::MouseEventArgs *  e)
    			 {
    				 int TempX, TempY;
    
    				 if( MouseIsDown == true )
    				 {
    					 TempX = e->X - OldX;
    					 TempY = e->Y - OldY;
    					 Top    = Top  + TempY;
    					 Left   = Left + TempX;
    				 }
    			 }
  12. Click the form and, in the Properties window, click the Events button
  13. Double-click the right side of MouseUp and implement the event as follows:
     
    private: System::Void Form1_MouseUp(System::Object *  sender, System::Windows::Forms::MouseEventArgs *  e)
    			 {
    				 MouseIsDown = false;
    			 }
  14. Test the application
 

Home Copyright © 2004-2005 FunctionX, Inc.