📄 gdiptext.cs
字号:
//------------------------------------------------------------------------------
/// <copyright from='1997' to='2001' company='Microsoft Corporation'>
/// 版权所有 (c) Microsoft Corporation。保留所有权利。
///
/// 此源代码仅作为 Microsoft 开发工具和/或联机文档
/// 的补充。有关 Microsoft 代码示例的详细信息,请
/// 参阅这些其他资料。
///
/// </copyright>
//------------------------------------------------------------------------------
namespace Microsoft.Samples.WinForms.Cs.GDIPlus.GDIPText {
using System;
using System.Windows.Forms;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Reflection;
public class TextSample : Form {
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components;
private Brush backgroundBrush;
private Brush textTextureBrush;
private Font titleFont;
private Font textFont;
private Brush titleShadowBrush;
private string flowedText1="我去了圣詹姆斯医院,\n到那里去看望我的爱人,\n她平躺在一张白色的桌子上,\n看上去是那么甜美、那么冰冷、那么美丽,\n让她去吧,让她去吧,愿上帝保佑她,\n不论她去哪里,\n她都可以俯瞰这个广阔的世界,\n但她永远不会找到一个像我这样的好男人,\n当我死时,希望你给我穿上整齐的系带鞋,\n我想穿一件直筒外套,戴一顶宽边帽,\n在我的表链上放上一个二十美元的金币,\n这样男孩们将知道我是站着死的。";
private string flowedText2="当你躺在摇篮里时,天空看上去很饱满\n雨就要落下来洗刷你的梦想\n星星在那里使劲地发着光亮...\n现在指责天空,因为它是空虚的\n你的所有梦想都正在枯竭\n星星在那里冷酷地发着光亮...\n一旦我想哭两次,我就想笑\n快告诉我,我到底像什么";
private Font japaneseFont ;
private string japaneseText = new string(new char[] {(char)31169, (char)12398, (char)21517, (char)21069, (char)12399, (char)12463, (char)12522, (char)12473, (char)12391, (char)12377, (char)12290});
private Brush linearGradBrush;
private bool doJapaneseSample=true;
private FontFamily serifFontFamily;
public TextSample() {
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
serifFontFamily = new FontFamily (GenericFontFamilies.Serif);
this.SetStyle(System.Windows.Forms.ControlStyles.Opaque, true);
//确保在调整窗体大小后重新绘制窗体
this.SetStyle(ControlStyles.ResizeRedraw,true);
//从可执行文件的资源分支中加载要用于背景的图像
Image backgroundImage = new Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("colorbars.jpg"));
//现在创建要用于绘制背景的画笔
backgroundBrush = new TextureBrush(backgroundImage);
//从可执行文件的资源分支中加载纹理文本要使用的图像
Image textImage = new Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("marble.jpg"));
textTextureBrush = new TextureBrush(textImage);
//加载要使用的字体
this.Font = new Font(serifFontFamily, 20);
titleFont = new Font(serifFontFamily, 60);
textFont = new Font(serifFontFamily, 11);
//设置阴影画笔 - 使其为半透明的
titleShadowBrush = new SolidBrush(Color.FromArgb(70, Color.Black));
//设置用于显示日语文本的字体和画笔
try {
japaneseFont = new Font("MS Mincho", 36);
linearGradBrush = new LinearGradientBrush(new Point(0, 0), new Point(0, 45), Color.Blue, Color.Red);
} catch (Exception ex) {
MessageBox.Show("需要使用日语字体 MS Mincho 来运行此示例的日语部分\n\n" + ex.Message);
doJapaneseSample = false;
}
}
protected override void OnPaint(PaintEventArgs e) {
Graphics g = e.Graphics;
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
//用粗画笔填充背景
//然后应用白色涂料
g.FillRectangle(backgroundBrush, ClientRectangle);
g.FillRectangle(new SolidBrush(Color.FromArgb(180, Color.White)), ClientRectangle);
//只需绘制 hello world
g.DrawString("Hello World", this.Font, new SolidBrush(Color.Black), 10,10);
//绘制纹理字符串
string titleText = "图形示例";
g.DrawString(titleText, titleFont, titleShadowBrush, 15, 25);
g.DrawString(titleText, titleFont, textTextureBrush, 10, 20);
string textToDraw="Hello Symetrical World";
//使用 Measure 字符串以在窗口中心显示字符串
float windowCenter=this.DisplayRectangle.Width/2;
SizeF stringSize=e.Graphics.MeasureString(textToDraw, textFont);
float startPos=windowCenter-(stringSize.Width/2);
g.DrawString(textToDraw, textFont, new SolidBrush(Color.Red), startPos, 10);
//现在绘制可移动到长方形中的字符串
RectangleF rectangle1 = new RectangleF(20, 150, 250, 300);
g.FillRectangle(new SolidBrush(Color.Gainsboro), rectangle1);
g.DrawString(flowedText1, textFont, new SolidBrush(Color.Blue), rectangle1);
//绘制更多的移动文本,但现在使其居中
RectangleF rectangle2 = new RectangleF(450, 150, 250, 300);
g.FillRectangle(new SolidBrush(Color.Gainsboro), rectangle2);
StringFormat format = new StringFormat();
format.Alignment=StringAlignment.Center;
g.DrawString(flowedText2, textFont, new SolidBrush(Color.Blue), rectangle2,format);
//计算出刚才所显示的行数和字符数
int characters=0;
int lines=0;
e.Graphics.MeasureString(flowedText2, textFont, rectangle2.Size, format, out characters, out lines);
string whatRenderedText=String.Format("我们打印了 {0} 个字符和 {1} 行", characters, lines);
e.Graphics.DrawString(whatRenderedText, textFont, new SolidBrush(Color.Black), 400,440);
//如果我们用日语语言包绘制日语文本
//将其旋转,看会产生什么效果
if (doJapaneseSample) {
g.RotateTransform(-30);
g.TranslateTransform(-180, 300);
g.DrawString(japaneseText, japaneseFont, linearGradBrush, 200, 140);
g.ResetTransform();
}
}
/// <summary>
/// 清理正在使用的所有资源。
/// </summary>
protected override void Dispose(bool disposing)
{
if (disposing) {
if (components != null) {
components.Dispose();
}
}
base.Dispose(disposing);
}
/// <summary>
/// 设计器支持所必需的方法,不要使用y
/// 代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent() {
this.components = new System.ComponentModel.Container ();
this.Size = new System.Drawing.Size(750, 500);
this.Text = "GDI+ 文本示例";
}
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
public static void Main() {
Application.Run(new TextSample());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -