using System; using System.Drawing; using System.Windows.Forms; class TrimmingTheText: Form { public static void Main() { Application.Run(new TrimmingTheText()); } public TrimmingTheText() { Text = "Trimming the Text"; ResizeRedraw = true; } protected override void OnPaint(PaintEventArgs pea) { DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height); } protected void DoPage(Graphics grfx, Color clr, int cx, int cy) { Brush brush = new SolidBrush(clr); float cyText = Font.GetHeight(grfx); float cyRect = cyText; RectangleF rectf = new RectangleF(0, 0, cx, cyRect); string str = "Those text text text text text text text text text text text text text text text "; StringFormat strfmt = new StringFormat(); strfmt.Trimming = StringTrimming.EllipsisPath; grfx.DrawString("EllipsisPath: " + Environment.GetFolderPath (Environment.SpecialFolder.Personal), Font, brush, rectf, strfmt); } }
StringFormat: StringTrimming.None
using System; using System.Drawing; using System.Windows.Forms; class TrimmingTheText: Form { public static void Main() { Application.Run(new TrimmingTheText()); } public TrimmingTheText() { Text = "Trimming the Text"; ResizeRedraw = true; } protected override void OnPaint(PaintEventArgs pea) { DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height); } protected void DoPage(Graphics grfx, Color clr, int cx, int cy) { Brush brush = new SolidBrush(clr); float cyText = Font.GetHeight(grfx); float cyRect = cyText; RectangleF rectf = new RectangleF(0, 0, cx, cyRect); string str = "Those text text text text text text text text text text text text text text text "; StringFormat strfmt = new StringFormat(); strfmt.Trimming = StringTrimming.None; grfx.DrawString("None: " + str, Font, brush, rectf, strfmt); } }
StringAlignment.Far
using System; using System.Drawing; using System.Windows.Forms; class FourCorners: Form { public static void Main() { Application.Run(new FourCorners()); } public FourCorners() { Text = "Four Corners Text Alignment"; BackColor = SystemColors.Window; ForeColor = SystemColors.WindowText; ResizeRedraw = true; } protected override void OnPaint(PaintEventArgs pea) { Graphics graphics = pea.Graphics; Brush brush = new SolidBrush(ForeColor); StringFormat strfmt = new StringFormat(); strfmt.Alignment = StringAlignment.Far; strfmt.LineAlignment = StringAlignment.Far; graphics.DrawString("Lower right corner", Font, brush, ClientSize.Width, ClientSize.Height, strfmt); } }
StringAlignment.Near
using System; using System.Drawing; using System.Windows.Forms; class FourCorners: Form { public static void Main() { Application.Run(new FourCorners()); } public FourCorners() { Text = "Four Corners Text Alignment"; BackColor = SystemColors.Window; ForeColor = SystemColors.WindowText; ResizeRedraw = true; } protected override void OnPaint(PaintEventArgs pea) { Graphics graphics = pea.Graphics; Brush brush = new SolidBrush(ForeColor); StringFormat strfmt = new StringFormat(); strfmt.Alignment = StringAlignment.Near; strfmt.LineAlignment = StringAlignment.Near; graphics.DrawString("Upper left corner", Font, brush, 0, 0, strfmt); } }
StringAlignment.Center
using System; using System.Drawing; using System.Windows.Forms; class HelloCenteredRectangle: Form { public static void Main() { Application.Run(new HelloCenteredRectangle()); } public HelloCenteredRectangle() { Text = "Hello Centered Using Rectangle"; BackColor = SystemColors.Window; ForeColor = SystemColors.WindowText; ResizeRedraw = true; } protected override void OnPaint(PaintEventArgs pea) { Graphics grfx = pea.Graphics; StringFormat strfmt = new StringFormat(); strfmt.Alignment = StringAlignment.Center; strfmt.LineAlignment = StringAlignment.Center; grfx.DrawString("Hello, world!", Font, new SolidBrush(ForeColor), ClientRectangle, strfmt); } }
String Alignment (PointF in DrawString)
using System;
using System.Drawing;
using System.Windows.Forms;
class StringAlignmentPoint: Form
{
public static void Main()
{
Application.Run(new StringAlignmentPoint());
}
public StringAlignmentPoint()
{
Text = “”;
ResizeRedraw = true;
}
protected override void OnPaint(PaintEventArgs pea)
{
DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
}
protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
{
Brush brush = new SolidBrush(clr);
Pen pen = new Pen(clr);
string[] strAlign = { “Near”, “Center”, “Far” };
StringFormat strfmt = new StringFormat();
grfx.DrawLine(pen, 0, cy / 2, cx, cy / 2);
grfx.DrawLine(pen, cx / 2, 0, cx / 2, cy);
for (int iVert = 0; iVert < 3; iVert += 2){ for (int iHorz = 0; iHorz < 3; iHorz += 2) { strfmt.LineAlignment = (StringAlignment)iVert; strfmt.Alignment = (StringAlignment)iHorz; grfx.DrawString( String.Format("LineAlignment = {0} Alignment = {1}", strAlign[iVert], strAlign[iHorz]), Font, brush, cx / 2, cy / 2, strfmt); } } } } [/csharp]
Enuermate the StringAlignment value
using System;
using System.Drawing;
using System.Windows.Forms;
class StringAlignmentRectangle: Form
{
public static void Main()
{
Application.Run(new StringAlignmentRectangle());
}
public StringAlignmentRectangle()
{
Text = “String Alignment (RectangleF in DrawString)”;
ResizeRedraw = true;
}
protected override void OnPaint(PaintEventArgs pea)
{
DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
}
protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
{
Brush brush = new SolidBrush(clr);
RectangleF rectf = new RectangleF(0, 0, cx, cy);
string[] strAlign = { “Near”, “Center”, “Far” };
StringFormat strfmt = new StringFormat();
for (int iVert = 0; iVert < 3; iVert++){ for (int iHorz = 0; iHorz < 3; iHorz++) { strfmt.LineAlignment = (StringAlignment)iVert; strfmt.Alignment = (StringAlignment)iHorz; grfx.DrawString( String.Format("LineAlignment = {0} Alignment = {1}", strAlign[iVert], strAlign[iHorz]), Font, brush, rectf, strfmt); } } } } [/csharp]