关于《MicroSoft C# Windows 程序设计》P120 没有使用StringFormatr的认知
在DrawString()函数中,没有使用StringFormat对齐方式时,则输出的文字是在X,Y定位点之后输出;
具体看示例:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Drawing; 6 using System.Windows.Forms; 7 8 namespace FourCorners 9 { 10 class FourCorners:Form 11 { 12 static void Main(string[] args) 13 { 14 Application.Run(new FourCorners()); 15 } 16 public FourCorners() 17 { 18 Text = "Four Corners Text Alignment"; 19 BackColor = SystemColors.Window; 20 ForeColor = SystemColors.WindowText; 21 ResizeRedraw = true; 22 } 23 protected override void OnPaint(PaintEventArgs e) 24 { 25 Graphics grfx = e.Graphics; 26 Brush brush = new SolidBrush(ForeColor); 27 StringFormat strFmt = new StringFormat(); 28 29 strFmt.Alignment = StringAlignment.Near; 30 strFmt.LineAlignment = StringAlignment.Near; 31 grfx.DrawString("我位于原点处",Font,brush,0,0); 32 33 strFmt.Alignment = StringAlignment.Far; 34 strFmt.LineAlignment = StringAlignment.Near; 35 grfx.DrawString("我位于200处", Font, brush, 200, 0); 36 37 strFmt.Alignment = StringAlignment.Near; 38 strFmt.LineAlignment = StringAlignment.Far; 39 grfx.DrawString("我位于这里呀!", Font, brush, (ClientSize.Width-50),0); 40 41 strFmt.Alignment = StringAlignment.Far; 42 strFmt.LineAlignment = StringAlignment.Far; 43 grfx.DrawString("我在右上角后面", Font, brush, ClientSize.Width, 0); 44 } 45 } 46 }
第一个输出在原点0,0之后,输出“我位于原点处”;
第二个输出在坐标点200,0之后,输出“我位于200处”;
第三个输出在最右侧边框减50处,所以,可以看到文字只显示了一半(即,我在这),另一半隐蔽在了边框右侧,无法看到了;
第四个输出是在客户区最右侧之后才输出,所以就完全看不到了;
对于第四种输出情况,形象的理解如下所示: