⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 drawingapp.cs

📁 CSharp的MDI窗口事例
💻 CS
📖 第 1 页 / 共 2 页
字号:
			this.toolBarLine.Text = "Line";
			// 
			// imageList1
			// 
			this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
			this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
			this.imageList1.TransparentColor = System.Drawing.Color.Silver;
			// 
			// frmChild
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.BackColor = System.Drawing.Color.White;
			this.ClientSize = new System.Drawing.Size(524, 293);
			this.Controls.Add(this.toolBar1);
			this.Menu = this.menuMain;
			this.Name = "frmChild";
			this.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.Text = "Sample Drawing Application";
			this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.this_MouseDown);
			this.Load += new System.EventHandler(this.frmChild_Load);
			this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.this_MouseUp);
			this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.this_MouseMove);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		//		[STAThread]
		//		static void Main() 
		//		{
		//			Application.Run(new frmChild());
		//		} // Main().

		protected override void OnPaint(PaintEventArgs e)
		{
			if (bDrawAllowed == false) 
			{
				base.OnPaint(e);
				return;
			} // if ().

			// get device context
			Graphics dc = e.Graphics;
			// create enumerator for shape array
			IEnumerator shapeEnum = shapesArr.GetEnumerator();

			while (shapeEnum.MoveNext())
			{
				Shape thisShape = (Shape) shapeEnum.Current;
				thisShape.DrawShape(dc, m_pen, m_brush);
			} // while().
			
			if ((currShape == ShapeType.cursor) && (bMovingShape == true))
				shape.DrawShape(dc, m_pen, m_brush);

			// execute base class method
			base.OnPaint(e);
		} // OnPaint().

		private void miExit_Click(object sender, System.EventArgs e)
		{
			Application.Exit();
		} // miExit_Click().

		private void mnuAbout_Click(object sender, System.EventArgs e)
		{
			MessageBox.Show(this, "Sample Drawing Application\n" + 
				"version: 1.00\nCreated by Shruti (C) 2003",
				"About ...", MessageBoxButtons.OK, MessageBoxIcon.Information);
		} // miAbout_Click().

		private void miRect_Click(object sender, System.EventArgs e)
		{
			currShape = ShapeType.rectangle;
		} // miRect_Click().

		private void miEllipse_Click(object sender, System.EventArgs e)
		{
			currShape = ShapeType.ellipse;
		} // miEllipse_Click().

		private void miCursor_Click(object sender, System.EventArgs e)
		{
			currShape = ShapeType.cursor;
		} // miCursor_Click().

		private void this_MouseDown(object sender, MouseEventArgs e)
		{
			nMouseDown = 1;
			mousePt = new Point(e.X, e.Y);
			mousePt1 = new Point(e.X, e.Y);
			
			//shape.StartPoint = new Point(e.X, e.Y);

//			if (bMovingShape == false)
//				return;
//			if ((mousePt.X != e.X) || (mousePt.Y != e.Y))
//			{
//				shape.StartPoint = new Point(e.X, e.Y);
//				shape.EndPoint = new Point(e.X + 50, e.Y + 50);
//			} // if().
//			bMovingShape = false;
//			// clear the whole area, coz sometimes there are leftovers form the movement
//			Invalidate();
		
		} // this_MouseDown().

		private bool IfCursorInside(Point pt)
		{

			if ((shape.StartPoint.X <= pt.X) &&
				(shape.StartPoint.Y <= pt.Y) &&
				(shape.EndPoint.X >= pt.X) &&
				(shape.EndPoint.Y >= pt.Y)) 
			{
				return true;
			}
			return false;
		} // IfCursorInside().

		private void this_MouseUp(object sender, MouseEventArgs e)
		{
			nMouseDown = 0;
			//mousePt2 = new Point(e.X, e.Y);

			switch(currShape) 
			{
				case ShapeType.cursor:
					// create enumerator for shapes array
					IEnumerator shapeEnum = shapesArr.GetEnumerator();

					while (shapeEnum.MoveNext())
					{
				
						shape = (Shape) shapeEnum.Current;
						if (IfCursorInside(mousePt)) 
						{
							// if left mouse button is pressed
							if (e.Button == MouseButtons.Left)
							{
								// so we're moving that shape
								bMovingShape = true;
								
							} // if().
							else // delete shape (different form left button)
							{
								shapesArr.Remove(shapeEnum.Current);
								bMovingShape = false;
							} // else.
							return;
						} // if().
					} // while().
					break;
				case ShapeType.rectangle:
					// create new rectangle shape
					shape = new Rect(mousePt1, mousePt2);
					// add this shape to the array list
					shapesArr.Add(shape);
					// allow drawing
					bDrawAllowed = true;										
					break;
				case ShapeType.ellipse:
					// create new ellipse shape
					shape = new Ellipse(mousePt1, mousePt2);
					// add this shape to the array list
					shapesArr.Add(shape);
					// allow drawing
					bDrawAllowed = true;
					// invalidate the view
					//Invalidate(new Rectangle(new Point(e.X, e.Y), new Size(e.X + 50, e.Y + 50)));
					break;
				case ShapeType.triangle:
					// create new rectangle shape
					shape = new Triangle(mousePt1, mousePt2);
					// add this shape to the array list
					shapesArr.Add(shape);
					// allow drawing
					bDrawAllowed = true;
					// invalidate the view
					break;
				case ShapeType.line:
					// create new rectangle shape
					shape = new Line(mousePt1, mousePt2);
					// add this shape to the array list
					shapesArr.Add(shape);
					// allow drawing
					bDrawAllowed = true;
					break;
			} // switch().			
			Invalidate();//(new Rectangle(mousePt1.X-5,mousePt1.Y-5 , (mousePt2.X - mousePt1.X)+5, (mousePt2.Y - mousePt1.Y)+5 ));
		} // this_MouseUp().

		private void this_MouseMove(object sender, MouseEventArgs me)
		{
			if (nMouseDown == 1)
			{
				mousePt2 = new Point(me.X, me.Y);
				// invalidate the view
				int nWidth = (mousePt2.X > mousePt1.X) ? mousePt2.X - mousePt1.X : mousePt1.X - mousePt2.X;
				int nHeight = (mousePt2.Y > mousePt1.Y) ? mousePt2.Y - mousePt1.Y : mousePt1.Y - mousePt2.Y;

				Graphics dc = CreateGraphics();
				if (mousePt2.X > mousePt1.X || mousePt2.Y > mousePt1.Y )
					dc.DrawRectangle(m_pen, mousePt1.X, mousePt1.Y, nWidth, nHeight);
				else
					dc.DrawRectangle(m_pen, mousePt2.X, mousePt2.Y, nWidth, nHeight);
				

				Invalidate(new Rectangle(mousePt1.X-5,mousePt1.Y-5 , (mousePt2.X - mousePt1.X)+5, (mousePt2.Y - mousePt1.Y)+5 ));
				;
			}
//			if (bMovingShape == false)
//				return;
//
//			shape.StartPoint = new Point(me.X, me.Y);
//			shape.EndPoint = new Point(me.X + 50, me.Y + 50);
//			Invalidate(new Rectangle(new Point(me.X - 50, me.Y - 50), new Size(me.X + 50, me.Y + 50)));
		} // this_MouseMove().

		private void miNew_Click(object sender, System.EventArgs e)
		{
			shapesArr.Clear();
			this.CreateGraphics().Clear(Color.White);
			currShape = ShapeType.cursor;
			bDrawAllowed = false;
		} // miNew_Click().

		public void miOpen_Click(object sender, System.EventArgs e)
		{
			OpenFileDialog openFileDialog = new OpenFileDialog();

			if (openFileDialog.ShowDialog() == DialogResult.OK)
			{
				filename = openFileDialog.FileName;
				currentFilename = filename;
				Stream openStream = openFileDialog.OpenFile();
				if (openStream != null)
				{
					BinaryFormatter formatter = new BinaryFormatter();
					shapesArr = (ArrayList) formatter.Deserialize(openStream);
					openStream.Close();
					// allow shapes to be displayed
					bDrawAllowed = true;
					// reset shape type
					currShape = ShapeType.cursor;
					this.Refresh();
				} // if().
			} // if().
		} // miOpen_Click().

		public void miSave_Click(object sender, System.EventArgs e)
		{
			if (bDrawAllowed == false) 
			{ // if no changes were made generate err
				MessageBox.Show(this,"No changes in picture were made!", "Error: Empty Canvas",
					MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			} //if().
			SaveFileDialog saveFileDialog = new SaveFileDialog();
			
			if (saveFileDialog.ShowDialog() == DialogResult.OK)
			{
				filename = saveFileDialog.FileName;
			}
			if (filename == null) return;
			else 
			{
				Stream saveStream ;
				saveStream = File.OpenWrite(filename);
				if (saveStream != null)
				{
					BinaryFormatter formatter = new BinaryFormatter();
			 
					// serialize shapes
					formatter.Serialize(saveStream, shapesArr);
					saveStream.Close();
				} // if().
			} // else.
		} 
		public void AddDocument(IMDIDocument doc) 
		{
			if (this.m_doc != null && this.m_doc != doc)
			{
				MessageBox.Show("This view already has a doc object");
				return;
			}
			this.m_doc = (Document) doc;
		}

		public IMDIDocument GetDocument() 
		{
			return m_doc;
		}

		public void OnUpdate() 
		{
			frmMain fm = (frmMain) this.MdiParent;
			if (fm.ActiveMdiChild != this)
			{
			}
		}
	

		public void OnInitialUpdate()
		{
			// todo
		}

		private void miTriangle_Click(object sender, System.EventArgs e)
		{
			currShape = ShapeType.triangle;
		}

		private void miLine_Click(object sender, System.EventArgs e)
		{
			currShape = ShapeType.line;
		}

		private void menuItem7_Click(object sender, System.EventArgs e)
		{
			int nWidth = ((System.Windows.Forms.MenuItem)(sender)).Index + 1;
			Color col = m_pen.Color;
			m_pen = new Pen(col, nWidth);
		}

		private void miPenColor_Click(object sender, System.EventArgs e)
		{
			Color color;
			ColorDialog dlg = new ColorDialog();
			if (dlg.ShowDialog() == DialogResult.OK)
			{
				color = dlg.Color;
				m_pen = new Pen(color);
			}
		}
		private void miBrushColor_Click(object sender, System.EventArgs e)
		{
			Color color;
			ColorDialog dlg = new ColorDialog();
			if (dlg.ShowDialog() == DialogResult.OK)
			{
				color = dlg.Color;
				m_brush = new SolidBrush(color);
			}
		
		}

		private void miZoomIn_Click(object sender, System.EventArgs e)
		{
		
		}

		private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
		{
			//currShape = (ShapeType)((System.Windows.Forms.ToolBar)(sender));
			currShape = ShapeType.rectangle;
		
		}

		private void frmChild_Load(object sender, System.EventArgs e)
		{
		
		}

		private void mnuHelpTopics_Click(object sender, System.EventArgs e)
		{
			MessageBox.Show("Sorry!!!This is not implemented yet");
		}

		private void toolBar1_ButtonClick_1(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
		{
			switch (toolBar1.Buttons.IndexOf(e.Button))
			{
				case 0 :
					miRect_Click(null, null);
					break;
				case 1 :
					miEllipse_Click(null, null);
					break;
				case 2:
					miTriangle_Click(null, null);
					break;
				case 3 :
					miLine_Click(null, null);
					break;
			}	
		}

		



	} //  class frmChild.
} // namespace DrawingApp.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -