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

📄 mainform.cs

📁 Windows Mobile 平台应用与开发 源码
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace UsingStringFormat
{
	public partial class MainForm : Form
	{
		string m_text = "When used with the LineAlignment property, this enumeration sets the vertical alignment for a drawn string. When used with the Alignment property, this enumeration sets the horizontal alignment.";
		public MainForm()
		{
			InitializeComponent();
		}

		private void m_onNeedRedraw(object sender, EventArgs e)
		{
			this.Invalidate();
		}

		private void MainForm_Paint(object sender, PaintEventArgs e)
		{
			// 期望的文本显示区域。
			// 这里使用了两个大小一样的矩形,
			// 因为Graphics.DrawString方法只接受RectangleF类型的矩形,
			// 而Graphics.DrawRectangle方法只接受Rectangle类型的矩形。
			Rectangle rcText = new Rectangle(10, m_chkNoClip.Bottom + 10, 180, 100);
			RectangleF rcfText = new RectangleF(
				rcText.Left, rcText.Top, rcText.Width, rcText.Height);

			// 准备绘图所需的对象
			Font f = new Font("Arial", 10.0f, FontStyle.Bold | FontStyle.Italic);
			SolidBrush b = new SolidBrush(Color.Black);
			Pen p = new Pen(Color.Black);

			// 根据用户输入,确定文本的格式
			StringFormat sf = new StringFormat();

			// 确定水平对齐方式
			if(m_cmbHAlign.SelectedIndex == 1)  // Center
				sf.Alignment = StringAlignment.Center;
			else if(m_cmbHAlign.SelectedIndex == 2)  // Far
				sf.Alignment = StringAlignment.Far;
			else
				sf.Alignment = StringAlignment.Near;

			// 确定竖直对齐方式
			if(m_cmbVAlign.SelectedIndex == 1)  // Center
				sf.LineAlignment = StringAlignment.Center;
			else if(m_cmbVAlign.SelectedIndex == 2)  // Far
				sf.LineAlignment = StringAlignment.Far;
			else
				sf.LineAlignment = StringAlignment.Near;

			// 确定是否进行自动裁剪
			if(m_chkNoClip.Checked)
				sf.FormatFlags |= StringFormatFlags.NoClip;
			else
				sf.FormatFlags &= (~StringFormatFlags.NoClip);

			// 确定是否进行自动换行
			if(m_chkNoWrap.Checked)
				sf.FormatFlags |= StringFormatFlags.NoWrap;
			else
				sf.FormatFlags &= (~StringFormatFlags.NoWrap);

			// 绘制文本,同时绘制用于限定文本的矩形
			e.Graphics.DrawString(m_text, f, b, rcfText, sf);
			e.Graphics.DrawRectangle(p, rcText);

			// 释放资源
			f.Dispose();
			b.Dispose();
			p.Dispose();
			sf.Dispose();
		}
	}
}

⌨️ 快捷键说明

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