Csharp/C Sharp/2D Graphics/Text Justify — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 11:42, 26 мая 2010
Содержание
- 1 a long string that will wrap and centered both vertically and horizontally
- 2 Draw left justified text
- 3 Draw Rectangle surrounding the Text
- 4 Draw right justified text
- 5 Draw the rectangle and draw the string, using the rectangle as a bounding box
- 6 Rotate the text 45 degrees clockwise and translate the text 150 pixels horizontally
- 7 String Rectangle
- 8 This text is centered and underlined
- 9 use a 12pt font, and assume the text string must fit into a width of 150 pixels
- 10 Use tab to control the DrawString
- 11 With a 200px-width rectangle, and a 12pt font, it requires six lines to display the string in its entirety
a long string that will wrap and centered both vertically and horizontally
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form {
protected override void OnPaint(PaintEventArgs e) {
Graphics g = e.Graphics;
g.FillRectangle(Brushes.White, this.ClientRectangle);
String s = "This is a long string that will wrap. ";
s += "It will be centered both vertically and horizontally.";
Font f = new Font("Arial", 12);
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
Rectangle r = new Rectangle(10, 10, 300, f.Height * 4);
g.DrawRectangle(Pens.Black, r);
g.DrawString(s, f, Brushes.Black, r, sf);
f.Dispose();
}
public static void Main() {
Application.Run(new Form1());
}
}
Draw left justified text
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class Form1 : System.Windows.Forms.Form{
public Form1()
{
InitializeComponent();
SetStyle(ControlStyles.Opaque, true);
Bounds = new Rectangle(0, 0, 500, 300);
}
protected override void OnPaint(PaintEventArgs e) {
Graphics g = e.Graphics;
g.FillRectangle(Brushes.White, ClientRectangle);
// Draw left justified text
Rectangle rect = new Rectangle(0, 0, 400, Font.Height);
g.DrawRectangle(Pens.Blue, rect);
g.DrawString("www.nfex.ru", Font,
Brushes.Black, rect);
}
private void InitializeComponent()
{
this.Size = new System.Drawing.Size(300,300);
this.Text = "Form1";
}
static void Main()
{
Application.Run(new Form1());
}
}
Draw Rectangle surrounding the Text
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form {
protected override void OnPaint(PaintEventArgs e) {
Graphics g = e.Graphics;
g.PageUnit = GraphicsUnit.Inch;
Pen p = new Pen(Color.Black, 1 / 96f);
Font f = new Font("Times New Roman", 16);
String s = "Abc";
SizeF sf = g.MeasureString(s, f);
g.DrawRectangle(p, 1, 1, sf.Width, sf.Height);
g.DrawString(s, f, Brushes.Black, 1, 1);
f.Dispose();
p.Dispose();
}
public static void Main() {
Application.Run(new Form1());
}
}
Draw right justified text
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class Form1 : System.Windows.Forms.Form{
public Form1()
{
InitializeComponent();
SetStyle(ControlStyles.Opaque, true);
Bounds = new Rectangle(0, 0, 500, 300);
}
protected override void OnPaint(PaintEventArgs e) {
Graphics g = e.Graphics;
g.FillRectangle(Brushes.White, ClientRectangle);
// Draw right justified text
Font aFont = new Font("Arial", 16, FontStyle.Bold | FontStyle.Italic);
Rectangle rect = new Rectangle(0, 0, 400, aFont.Height);
g.DrawRectangle(Pens.Blue, rect);
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Far;
g.DrawString("www.nfex.ru", aFont, Brushes.Blue,
rect, sf);
}
private void InitializeComponent()
{
this.Size = new System.Drawing.Size(300,300);
this.Text = "Form1";
}
static void Main()
{
Application.Run(new Form1());
}
}
Draw the rectangle and draw the string, using the rectangle as a bounding box
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form {
protected override void OnPaint(PaintEventArgs e) {
Graphics g = e.Graphics;
g.FillRectangle(Brushes.White, this.ClientRectangle);
String s = "This string is long enough to wrap. ";
Font f = new Font("Arial", 12);
Rectangle r = new Rectangle(20, 20, 150, f.Height * 4);
StringFormat sf = new StringFormat();
sf.FormatFlags = StringFormatFlags.NoWrap;
g.DrawRectangle(Pens.Black, r);
g.DrawString(s, f, Brushes.Black, r, sf);
f.Dispose();
}
public static void Main() {
Application.Run(new Form1());
}
}
Rotate the text 45 degrees clockwise and translate the text 150 pixels horizontally
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form {
protected override void OnPaint(PaintEventArgs e) {
Graphics g = e.Graphics;
g.FillRectangle(Brushes.White, this.ClientRectangle);
Font f = new Font("Times New Roman", 24);
g.DrawString("Rotate then Translate", f, Brushes.Black, 0, 0);
g.RotateTransform(45);
g.TranslateTransform(150, 0);
g.DrawString("Rotate then Translate ", f, Brushes.Black, 0, 0);
}
public static void Main() {
Application.Run(new Form1());
}
}
String Rectangle
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form {
protected override void OnPaint(PaintEventArgs e) {
Graphics g = e.Graphics;
g.FillRectangle(Brushes.White, this.ClientRectangle);
String s = "AAAAAAAAAA";
StringFormat sf = new StringFormat(StringFormatFlags.DirectionVertical);
Font f = new Font("Times New Roman", 14);
SizeF sizef = g.MeasureString(s, f, Int32.MaxValue, sf);
RectangleF rf = new RectangleF(20, 20, sizef.Width, sizef.Height);
g.DrawRectangle(Pens.Black, rf.Left, rf.Top, rf.Width, rf.Height);
g.DrawString(s, f, Brushes.Black, rf, sf);
f.Dispose();
}
public static void Main() {
Application.Run(new Form1());
}
}
This text is centered and underlined
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class Form1 : System.Windows.Forms.Form{
public Form1()
{
InitializeComponent();
SetStyle(ControlStyles.Opaque, true);
Bounds = new Rectangle(0, 0, 500, 300);
}
protected override void OnPaint(PaintEventArgs e) {
Graphics g = e.Graphics;
g.FillRectangle(Brushes.White, ClientRectangle);
// draw centered text
Font cFont = new Font("Courier New", 12, FontStyle.Underline);
Rectangle rect = new Rectangle(0, 0, 400, cFont.Height);
g.DrawRectangle(Pens.Blue, rect);
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
g.DrawString("This text is centered and underlined.", cFont,
Brushes.Red, rect, sf);
}
private void InitializeComponent()
{
this.Size = new System.Drawing.Size(300,300);
this.Text = "Form1";
}
static void Main()
{
Application.Run(new Form1());
}
}
use a 12pt font, and assume the text string must fit into a width of 150 pixels
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form {
protected override void OnPaint(PaintEventArgs e) {
Graphics g = e.Graphics;
g.FillRectangle(Brushes.White, this.ClientRectangle);
String s = "This string is long enough to wrap. ";
Font f = new Font("Arial", 12);
SizeF sf = g.MeasureString(s, f, 150);
RectangleF rf = new RectangleF(20, 20, sf.Width, sf.Height);
g.DrawRectangle(Pens.Black, rf.Left, rf.Top, rf.Width, rf.Height);
g.DrawString(s, f, Brushes.Black, rf);
f.Dispose();
}
public static void Main() {
Application.Run(new Form1());
}
}
Use tab to control the DrawString
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form {
protected override void OnPaint(PaintEventArgs e) {
Graphics g = e.Graphics;
g.FillRectangle(Brushes.White, this.ClientRectangle);
Font f = new Font("Times New Roman", 12);
Font bf = new Font(f, FontStyle.Bold);
StringFormat sf = new StringFormat();
float[] ts = { 10.0f, 70.0f, 100.0f, 90.0f };
sf.SetTabStops(0.0f, ts);
string s1 = "\tA\tAAA\tAAAAA\tAAAAAAAAAA";
string s2 = "\tAAAAAAA\tAAAAA\tAA\tAAA";
g.DrawString(s1, bf, Brushes.Black, 20, 20, sf);
g.DrawString(s2, f, Brushes.Blue, 20, 20 + bf.Height, sf);
f.Dispose();
bf.Dispose();
}
public static void Main() {
Application.Run(new Form1());
}
}
With a 200px-width rectangle, and a 12pt font, it requires six lines to display the string in its entirety
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form {
protected override void OnPaint(PaintEventArgs e) {
Graphics g = e.Graphics;
g.FillRectangle(Brushes.White, this.ClientRectangle);
String s = "This string is long enough to wrap. ";
Font f = new Font("Arial", 12);
Rectangle r = new Rectangle(20, 20, 200, f.Height * 6);
g.DrawRectangle(Pens.Black, r);
g.DrawString(s, f, Brushes.Black, r);
f.Dispose();
}
public static void Main() {
Application.Run(new Form1());
}
}