?? mainform.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 + -