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

📄 textfield.cs

📁 是用c#实现的一个有关于报表设计的程序代码
💻 CS
📖 第 1 页 / 共 2 页
字号:
			get { return this.mSelectable; }
			set { this.mSelectable=value; }
		}


		/// <summary>
		///  Gets or sets the vertical alignment of the TextField
		/// </summary>
		[Description("Vertical alignment in the page, relative to margins. This property overrides element coordinates.")]
		public override ICustomPaint.VerticalAlignmentTypes VerticalAlignment
		{
			get {return this.verticalAlignment;}
			set 
			{
				this.verticalAlignment = value;
				switch (this.verticalAlignment)
				{
					case ICustomPaint.VerticalAlignmentTypes.Middle:
						this.mRegion.Y = (document.DefaultPageSettings.Bounds.Height-document.Margins.Bottom+document.Margins.Top)/2 - Height/2;
						break;

					case ICustomPaint.VerticalAlignmentTypes.Bottom:
						this.mRegion.Y = document.DefaultPageSettings.Bounds.Bottom - document.Margins.Bottom - Height;
						break;

					case ICustomPaint.VerticalAlignmentTypes.Top:
						this.mRegion.Y = document.Margins.Top;
						break;
				}
			}
		}


		/// <summary>
		///  Gets or sets the width of the TextField.
		/// </summary>
		[Description("The width of the element.")]
		public override int Width
		{
			get {return this.mRegion.Width;}
			set
			{
				if (this.HorizontalAlignment == ICustomPaint.HorizontalAlignmentTypes.None || this.HorizontalAlignment == ICustomPaint.HorizontalAlignmentTypes.Left)
					this.mRegion.Width = value;
				else if (this.HorizontalAlignment == ICustomPaint.HorizontalAlignmentTypes.Right)
				{
					this.mRegion.X = this.mRegion.X - value + this.mRegion.Width;
					this.mRegion.Width = value;									
				}
				else if (this.HorizontalAlignment == ICustomPaint.HorizontalAlignmentTypes.Center)
				{
					this.mRegion.X = this.mRegion.X - value/2 + this.mRegion.Width/2;
					this.mRegion.Width = value;									
				}
			}
		}


		/// <summary>
		/// The X coordinate of the left-upper corner of the TextField
		/// </summary>
		[Description("The X coordinate of the left-upper corner of the element.")]
		public override int X
		{
			get { return this.mRegion.X; }
			set 
			{
				if (this.HorizontalAlignment == ICustomPaint.HorizontalAlignmentTypes.None)
					this.mRegion.X = value;
				else if (this.HorizontalAlignment == ICustomPaint.HorizontalAlignmentTypes.Right)
				{
					this.mRegion.Width = this.mRegion.Width + this.mRegion.X - value;
					this.mRegion.X = value;					
				}
				else if (this.HorizontalAlignment == ICustomPaint.HorizontalAlignmentTypes.Center)
				{
					this.mRegion.Width = this.mRegion.Width + 2*(this.mRegion.X - value);
					this.mRegion.X = value;					
				}

			}
		}

		
		/// <summary>
		/// The Y coordinate of the left-upper corner of the TextField
		/// </summary>
		[Description("The Y coordinate of the left-upper corner of the element.")]
		public override int Y
		{
			get {return this.mRegion.Y;}
			set 
			{
				if (this.VerticalAlignment == ICustomPaint.VerticalAlignmentTypes.None)
					this.mRegion.Y = value;
				else if (this.VerticalAlignment == ICustomPaint.VerticalAlignmentTypes.Bottom)
				{
					this.mRegion.Height = this.mRegion.Height + this.mRegion.Y - value;
					this.mRegion.Y = value;					
				}
				else if (this.VerticalAlignment == ICustomPaint.VerticalAlignmentTypes.Middle)
				{
					this.mRegion.Height = this.mRegion.Height + 2*(this.mRegion.Y - value);
					this.mRegion.Y = value;					
				}
			}
		}

		
		#endregion

		#region Private Functions
		
		private void drawJustified(Graphics g,string text,int yPos,bool isLast,bool isIndented)
		{
			
			string txt = text.Replace("\t","     ");
			float indentSize = g.MeasureString("mmm",mFont).Width;

			float xPos = this.mRegion.X + mInnerPadding + (isIndented?indentSize:0);

			string[] theWords = txt.Split(new char[]{' '});
			CharacterRange[] characterRanges = new CharacterRange[theWords.Length];

			int pos = 0;
			for (int i=0;i<theWords.Length;i++)
			{
				characterRanges[i] = new CharacterRange(txt.IndexOf(theWords[i],pos),theWords[i].Length);
				pos += theWords[i].Length;
				while ( pos+1 < txt.Length && txt.Substring(pos+1,1).Equals(" ") )
					pos++;
			}

			Region[] stringRegions = new Region[theWords.Length];


			StringFormat stringFormat = new StringFormat();
			stringFormat.FormatFlags = StringFormatFlags.MeasureTrailingSpaces ;
			stringFormat.SetMeasurableCharacterRanges(characterRanges);

			
			SizeF sf = g.MeasureString(txt,mFont);

			float theOffset = 0;
			if (isIndented)
			{
				theOffset = (this.mRegion.Width - 2*mInnerPadding - indentSize - sf.Width) / theWords.Length;
				stringRegions = g.MeasureCharacterRanges(txt,mFont,new RectangleF(this.mRegion.X+mInnerPadding+indentSize,yPos,sf.Width,sf.Height),stringFormat);
			}
			else
			{
				theOffset = (this.mRegion.Width - 2*mInnerPadding - sf.Width) / theWords.Length;
				stringRegions = g.MeasureCharacterRanges(txt,mFont,new RectangleF(this.mRegion.X+mInnerPadding,yPos,sf.Width,sf.Height),stringFormat);
			}

			if (isLast)
			{
				g.DrawString(txt,mFont,new SolidBrush(mForegroundColor),xPos,yPos,stringFormat);	
			}
			else
			{
				for (int i=0;i<theWords.Length;i++)
				{

					RectangleF rect = stringRegions[i].GetBounds(g);

					if (i==0)
					{
						//xPos = theRegion.X + innerPadding;
					}
					else if(i==theWords.Length-1)
					{
						xPos = this.mRegion.Right - mInnerPadding - g.MeasureString(theWords[i],mFont).Width;
					}
					else
					{					
						xPos = rect.X + i*theOffset;	
					}
				
					g.DrawString(theWords[i],mFont,new SolidBrush(mForegroundColor),xPos,rect.Y,stringFormat);				
					
				}
			}
		}


		private void drawSimpleString(Graphics g,string txt,int yPos,bool isLast)
		{
			int xPos = 0;

			SizeF sf = g.MeasureString(txt,mFont);

			switch (TextAlignment)
			{
				case TextAlignmentType.Center:
					xPos = this.mRegion.X + this.mRegion.Width/2 - (int)sf.Width/2;
					g.DrawString(txt,mFont,new SolidBrush(mForegroundColor),new Point(xPos,yPos));	
					break;

				case TextAlignmentType.Right:
					xPos = this.mRegion.Right - mInnerPadding - (int)sf.Width;
					g.DrawString(txt,mFont,new SolidBrush(mForegroundColor),new Point(xPos,yPos));	
					break;

				case TextAlignmentType.Justified:
					if (txt.StartsWith("\t"))
					{
						drawJustified(g,txt.Trim(),yPos,isLast,true);
					}
					else
					{
						drawJustified(g,txt.TrimEnd(new char[]{' '}),yPos,isLast,false);	
					}
					break;

				default:
					xPos = this.mRegion.X + mInnerPadding;
					g.DrawString(txt,mFont,new SolidBrush(mForegroundColor),new Point(xPos,yPos));	
					break;

			}			
		}

		
		private void drawText(Graphics g)
		{

			// check for general exceptions
			if (mText.Length == 0 ) return;
			this.mLines.Clear();		

			string line = "";
			int lastBlank = -1;
			int current = -1;

			int yPosition = this.mRegion.Top + mInnerPadding;
			SizeF sf = g.MeasureString(line,mFont);

			while ( yPosition+sf.Height <= (this.mRegion.Bottom - mInnerPadding) && mText.Length > ++current )
			{
				string nextChar = mText.Substring(current,1);

				line += nextChar;
				sf = g.MeasureString(line,mFont);

				if (sf.Width > this.mRegion.Width - 2*mInnerPadding)
				{
					if (lastBlank == -1)
					{
						line = line.Substring(0,line.Length-1);
						current--;
					}
					else
					{
						line = line.Substring(0,line.Length-(current-lastBlank));
						current = lastBlank;

					}
					
					this.mLines.Add(line);
					yPosition += (int)sf.Height + mLineSpacing;
					line = "";

					
					lastBlank = -1;
				}
				else if (current == mText.Length-1)
				{
					this.mLines.Add(line);
				}
				else
				{
					if (nextChar.Equals(" "))
						lastBlank = current;
				}
			}
		
			startPainting(g);

		}

		
		private void startPainting(Graphics g)
		{
			int lineHeight = (int)g.MeasureString("This is dummy text",mFont).Height;
			int yPos = 0;

			switch (this.mTextVerticalAlignment)
			{
				case TextVerticalAlignmentType.Bottom:
					yPos = this.mRegion.Bottom - mInnerPadding - this.mLines.Count*lineHeight - (this.mLines.Count-1)*mLineSpacing; 
					break;

				case TextVerticalAlignmentType.Middle:
					yPos = this.mRegion.Top + this.mRegion.Height/2 - ( this.mLines.Count*lineHeight + (this.mLines.Count-1)*mLineSpacing )/2; 
					break;

				default:
					yPos = this.mRegion.Top + mInnerPadding;
					break;
			}

			for (int i=0;i<this.mLines.Count;i++)
			{				
				drawSimpleString(g,this.mLines[i].ToString(),yPos, i==this.mLines.Count-1 );
				yPos = yPos + lineHeight + mLineSpacing;
			}
		}

		
		#endregion

		#region Creator

		/// <summary>
		/// Initializes a new instance of the TextField class.
		/// </summary>
		public TextField()
		{
			this.mLines = new ArrayList();
		}


		/// <summary>
		/// Initializes a new instance of the TextField class.
		/// </summary>
		/// <param name="originX">x-position of the new TextField</param>
		/// <param name="originY">y-position of the new TextField</param>
		/// <param name="width">Width of the new TextField</param>
		/// <param name="height">Height of the new TextField</param>
		/// <param name="parent">Parent of the new TextField</param>
		public TextField(int originX,int originY,int width,int height, DaPrintDocument parent)
		{
			document = parent;
			mText = "";
			this.mRegion = new Rectangle(originX,originY,width,height);
			this.mLines = new ArrayList();
		}

		
		#endregion

		#region ICustomPaint Members

		/// <summary>
		/// Clones the structure of the TextField, including all properties
		/// </summary>
		/// <returns><see cref="daReport.TextField">daReport.TextField</see></returns>
		public override object Clone()
		{
			TextField tmp = new TextField();
			tmp.document = this.document;
			tmp.Text = this.Text;
			tmp.X = this.X;
			tmp.Y = this.Y;
			tmp.Width = this.Width;
			tmp.Height = this.Height;
			tmp.BorderWidth = this.BorderWidth;
			tmp.BorderColor = this.BorderColor;
			tmp.BackgroundColor = this.BackgroundColor;
			tmp.Font = this.Font;
			tmp.TextAlignment = this.TextAlignment;
			tmp.TextVerticalAlignment = this.TextVerticalAlignment;
			tmp.ForegroundColor = this.ForegroundColor;

			return tmp;
		}


		/// <summary>
		/// Gets the region of the current TextField
		/// </summary>
		/// <returns>System.Drawing.Rectangle</returns>
		public override Rectangle GetRegion()
		{
			return this.mRegion;
		}


		/// <summary>
		/// Paints the TextField
		/// </summary>
		/// <param name="g">The Graphics object to draw</param>
		/// <remarks>Causes the ChartBox to be painted to the screen.</remarks>
		public override void Paint(System.Drawing.Graphics g)
		{

			if ( mBackgroundColor != Color.Transparent )
			{
				g.FillRectangle(new SolidBrush(mBackgroundColor),this.mRegion);
			}

			if ( this.mBorderWidth > 0 )
			{
				g.DrawRectangle(new Pen(this.mBorderColor,mBorderWidth),this.mRegion);
			}

			drawText(g);
		}


		#endregion
	}
}

⌨️ 快捷键说明

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