form1.cs

来自「Sams Teach Yourself C# Web Programming i」· CS 代码 · 共 188 行

CS
188
字号
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Persisting_Graphics
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class fclsMain : System.Windows.Forms.Form
	{
		private System.Drawing.Bitmap m_objDrawingSurface;
		private System.Windows.Forms.TextBox txtInput;
		private System.Windows.Forms.Button btnDrawText;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public fclsMain()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.txtInput = new System.Windows.Forms.TextBox();
			this.btnDrawText = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// txtInput
			// 
			this.txtInput.Location = new System.Drawing.Point(56, 184);
			this.txtInput.Name = "txtInput";
			this.txtInput.TabIndex = 0;
			this.txtInput.Text = "";
			// 
			// btnDrawText
			// 
			this.btnDrawText.Location = new System.Drawing.Point(160, 184);
			this.btnDrawText.Name = "btnDrawText";
			this.btnDrawText.TabIndex = 1;
			this.btnDrawText.Text = "Draw Text";
			this.btnDrawText.Click += new System.EventHandler(this.btnDrawText_Click);
			// 
			// fclsMain
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(292, 273);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.btnDrawText,
																		  this.txtInput});
			this.Name = "fclsMain";
			this.Text = "Persisting Graphics";
			this.Load += new System.EventHandler(this.fclsMain_Load);
			this.Closed += new System.EventHandler(this.fclsMain_Closed);
			this.Paint += new System.Windows.Forms.PaintEventHandler(this.fclsMain_Paint);
			this.ResumeLayout(false);

		}
		#endregion

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

		private void fclsMain_Load(object sender, System.EventArgs e)
		{
			// Create a darwing surface with the same dimensions as the client
			// area of the form.
			m_objDrawingSurface = new Bitmap(this.ClientRectangle.Width, 
				this.ClientRectangle.Height, 
				System.Drawing.Imaging.PixelFormat.Format24bppRgb);
			InitializeSurface();
		}
		private void InitializeSurface()
		{
			Graphics objGraphics;
			Rectangle rectBounds;

			// Create a graphics object that references the bitmap and clear it.
			objGraphics = Graphics.FromImage(m_objDrawingSurface);

			objGraphics.Clear(SystemColors.Control);

			// Create a rectangle the same size as the bitmap.
			rectBounds = new Rectangle(0,0,
						 m_objDrawingSurface.Width, m_objDrawingSurface.Height);
			// Reduce the rectangle slightly so the ellipse won't appear on the border.
			rectBounds.Inflate(-1,-1);

			// Draw an ellipse that fills the form.
			objGraphics.DrawEllipse(Pens.Orange, rectBounds);

	// Free up resources.
			objGraphics.Dispose();
			

		}

		private void fclsMain_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
		{
		Graphics objGraphics;
			// You can't modify e.Graphics directly.
			objGraphics = e.Graphics;
			// Draw the contents of the bitmap on the form.
			objGraphics.DrawImage(m_objDrawingSurface,0,0,
				m_objDrawingSurface.Width,
				m_objDrawingSurface.Height);
			objGraphics.Dispose();

		}

		private void btnDrawText_Click(object sender, System.EventArgs e)
		{
		Graphics objGraphics;
		Font objFont;
		int intFontSize, intTextX, intTextY;
			
			Random randomGenerator = new Random();

			// If no text has been entered, get out.
			if (txtInput.Text =="") return;

			// Create a graphics object using the memory bitmap.
			objGraphics = Graphics.FromImage(m_objDrawingSurface);

			// Create a random number for the font size. Keep it between 8 and 48.
			intFontSize = randomGenerator.Next(8,48);
			// Create a random number for X coordinate of the text.
			intTextX = randomGenerator.Next(0, this.ClientRectangle.Width);
			// Create a random number fo the Y cooridnate of the text.
			intTextY = randomGenerator.Next(0,this.ClientRectangle.Height);

			// Create a new font object.
			objFont = new System.Drawing.Font("Arial",intFontSize,FontStyle.Bold);
			// Draw the user's text.
			objGraphics.DrawString(txtInput.Text, objFont,System.Drawing.Brushes.Red, 
				intTextX,intTextY);
			// Clean up.
			objGraphics.Dispose();
			// Force the form to paint itself. This triggers the Paint event.
			this.Invalidate();

		}

		private void fclsMain_Closed(object sender, System.EventArgs e)
		{
			m_objDrawingSurface.Dispose();
		}
	}
}

⌨️ 快捷键说明

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