Csharp/C Sharp/2D Graphics/Text — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 11:41, 26 мая 2010
Содержание
- 1 Center each line of text horizontally and vertically
- 2 Clip Text
- 3 Deal with Tab and New Line in Painting String
- 4 Draw line and string
- 5 Draw multiline text: auto wrap
- 6 Draw rectangle and string inside
- 7 Draw string with new font and solid brush
- 8 Draw the string, using the rectangle as a bounding box
- 9 Draw Vertical String Text
- 10 Emboss
- 11 Engrave
- 12 String Format based on Tab info data
- 13 Tall in the Center
- 14 Text Format
- 15 Text Rotate
- 16 Text Rotate 2
- 17 Text string"s containing rectangle
Center each line of text horizontally and vertically
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Imaging;
public class Form1 : System.Windows.Forms.Form
{
public Form1()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Text = "";
this.Resize += new System.EventHandler(this.Form1_Resize);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
}
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Paint(object sender, System.Windows.Forms.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; // horizontal alignment
sf.LineAlignment = StringAlignment.Center; // vertical alignment
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();
}
private void Form1_Resize(object sender, System.EventArgs e)
{
Invalidate();
}
}
Clip 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;
class FontMenuForm : Form {
protected string strText = "Clip Text";
protected Font font = new Font("Times New Roman", 24);
public static void Main() {
Application.Run(new FontMenuForm());
}
public FontMenuForm() {
ResizeRedraw = true;
}
protected void DoPage(Graphics grfx, Color clr, int cx, int cy) {
GraphicsPath path = new GraphicsPath();
float fFontSize = PointsToPageUnits(grfx, font);
path.AddString(strText, font.FontFamily, (int)font.Style,
fFontSize, new PointF(0, 0), new StringFormat());
grfx.SetClip(path);
RectangleF rectfBounds = path.GetBounds();
grfx.TranslateClip(
(cx - rectfBounds.Width) / 2 - rectfBounds.Left,
(cy - rectfBounds.Height) / 2 - rectfBounds.Top);
Random rand = new Random();
for (int y = 0; y < cy; y++) {
Pen pen = new Pen(Color.FromArgb(rand.Next(255),
rand.Next(255),
rand.Next(255)));
grfx.DrawBezier(pen, new Point(0, y),
new Point(cx / 3, y + cy / 3),
new Point(2 * cx / 3, y - cy / 3),
new Point(cx, y));
}
}
public float GetAscent(Graphics grfx, Font font) {
return font.GetHeight(grfx) *
font.FontFamily.GetCellAscent(font.Style) /
font.FontFamily.GetLineSpacing(font.Style);
}
public float GetDescent(Graphics grfx, Font font) {
return font.GetHeight(grfx) *
font.FontFamily.GetCellDescent(font.Style) /
font.FontFamily.GetLineSpacing(font.Style);
}
public float PointsToPageUnits(Graphics grfx, Font font) {
float fFontSize;
if (grfx.PageUnit == GraphicsUnit.Display)
fFontSize = 100 * font.SizeInPoints / 72;
else
fFontSize = grfx.DpiX * font.SizeInPoints / 72;
return fFontSize;
}
}
Deal with Tab and New Line in Painting String
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Imaging;
public class Form1 : System.Windows.Forms.Form
{
public Form1()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Text = "";
this.Resize += new System.EventHandler(this.Form1_Resize);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
}
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Paint(object sender, System.Windows.Forms.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);
// The \t escape-sequence in these lines specifies the tab
string s1 = "\tName\tEye Color\tHeight";
string s2 = "\tBob\tBrown\t175cm";
string s3 = "\tMary\tBlond\t161cm\n\tBill\tBlack\t168cm";
g.DrawString(s1, bf, Brushes.Black, 20, 20, sf);
g.DrawString(s2, f, Brushes.Blue, 20, 20 + bf.Height, sf);
g.DrawString(s3, f, Brushes.Blue, 20,
20 + bf.Height + f.Height, sf);
f.Dispose();
bf.Dispose();
}
private void Form1_Resize(object sender, System.EventArgs e)
{
Invalidate();
}
}
Draw line and string
/*
User Interfaces in C#: Windows Forms and Custom Controls
by Matthew MacDonald
Publisher: Apress
ISBN: 1590590457
*/
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace GDI_Basics
{
/// <summary>
/// Summary description for PenCaps.
/// </summary>
public class PenCaps1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ruponentModel.Container components = null;
public PenCaps1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// PenCaps
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(424, 330);
this.Name = "PenCaps";
this.Text = "PenCaps";
this.Resize += new System.EventHandler(this.PenCaps_Resize);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.PenCaps_Paint);
}
#endregion
private void PenCaps_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Pen myPen = new Pen(Color.Blue, 10);
int y = 20;
foreach (LineCap cap in System.Enum.GetValues(typeof(LineCap)))
{
myPen.StartCap = cap;
myPen.EndCap = cap;
e.Graphics.DrawLine(myPen, 20, y, 100, y);
e.Graphics.DrawString(cap.ToString(), new Font("Tahoma", 8), Brushes.Black, 120, y - 10);
y += 30;
}
}
[STAThread]
static void Main()
{
Application.Run(new PenCaps1());
}
private void PenCaps_Resize(object sender, System.EventArgs e)
{
this.Invalidate();
}
}
}
Draw multiline text: auto wrap
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 multiline text
Font trFont = new Font("Times New Roman", 12);
Rectangle rect = new Rectangle(0, 0, 400, trFont.Height * 3);
g.DrawRectangle(Pens.Blue, rect);
String longString = "Text Text Text Text Text Text Text Text Text Text ";
longString += "Text Text Text Text Text Text Text Text Text Text ";
longString += "Text Text Text Text Text Text Text Text Text Text ";
longString += "Text Text Text Text Text Text Text Text Text .";
g.DrawString(longString, trFont, 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 and string inside
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class MainForm : System.Windows.Forms.Form
{
public MainForm()
{
InitializeComponent();
CenterToScreen();
SetStyle(ControlStyles.ResizeRedraw, true);
}
private void InitializeComponent()
{
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Text = "Pens...";
this.Resize += new System.EventHandler(this.Form1_Resize);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.MainForm_Paint);
}
static void Main()
{
Application.Run(new MainForm());
}
private void MainForm_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
// And a rect with some text...
Rectangle r = new Rectangle(150, 10, 130, 60);
g.DrawRectangle(Pens.Blue, r);
g.DrawString("www.nfex.ru",
new Font("Arial", 12), Brushes.Black, r);
}
private void Form1_Resize(object sender, System.EventArgs e)
{
Invalidate();
}
}
Draw string with new font and solid brush
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class MainForm : System.Windows.Forms.Form
{
private System.ruponentModel.Container components;
public MainForm()
{
InitializeComponent();
BackColor = Color.Tomato;
Opacity = 0.5d;
Text = "www.nfex.ru";
Cursor = Cursors.WaitCursor;
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void InitializeComponent()
{
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Text = "Form1";
}
[STAThread]
static void Main()
{
Application.Run(new MainForm());
}
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawString("www.nfex.ru",
new Font("Times New Roman", 20),
new SolidBrush(Color.Black), 40, 10);
}
}
Draw the string, using the rectangle as a bounding box
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Imaging;
public class Form1 : System.Windows.Forms.Form
{
public Form1()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Text = "Pen Cap App";
this.Resize += new System.EventHandler(this.Form1_Resize);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
}
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
g.FillRectangle(Brushes.White, this.ClientRectangle);
String s = "Text Text Text Text Text Text Text Text Text Text ";
s += "Text Text Text Text Text Text Text Text Text Text Text Text Text ";
s += "Text Text Text Text Text Text Text Text Text Text Text Text ";
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();
}
private void Form1_Resize(object sender, System.EventArgs e)
{
Invalidate();
}
}
Draw Vertical String Text
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Imaging;
public class Form1 : System.Windows.Forms.Form
{
public Form1()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Text = "";
this.Resize += new System.EventHandler(this.Form1_Resize);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
}
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
g.FillRectangle(Brushes.White, this.ClientRectangle);
String s = "Accrington Stanley";
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();
}
private void Form1_Resize(object sender, System.EventArgs e)
{
Invalidate();
}
}
Emboss
using System;
using System.Drawing;
using System.Windows.Forms;
class FontMenuForm: Form
{
protected string strText = "Emboss";
protected Font font = new Font("Times New Roman", 24);
public static void Main()
{
Application.Run(new FontMenuForm());
}
public FontMenuForm()
{
ResizeRedraw = true;
}
protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
{
int iOffset = 2;
SizeF sizef = grfx.MeasureString(strText, font);
float x = (cx - sizef.Width) / 2;
float y = (cy - sizef.Height) / 2;
grfx.Clear(Color.White);
grfx.DrawString(strText, font, Brushes.Gray, x, y);
grfx.DrawString(strText, font, Brushes.White, x - iOffset,
y - iOffset);
}
public float GetAscent(Graphics grfx, Font font)
{
return font.GetHeight(grfx) *
font.FontFamily.GetCellAscent(font.Style) /
font.FontFamily.GetLineSpacing(font.Style);
}
public float GetDescent(Graphics grfx, Font font)
{
return font.GetHeight(grfx) *
font.FontFamily.GetCellDescent(font.Style) /
font.FontFamily.GetLineSpacing(font.Style);
}
public float PointsToPageUnits(Graphics grfx, Font font)
{
float fFontSize;
if (grfx.PageUnit == GraphicsUnit.Display)
fFontSize = 100 * font.SizeInPoints / 72;
else
fFontSize = grfx.DpiX * font.SizeInPoints / 72;
return fFontSize;
}
}
Engrave
using System;
using System.Drawing;
using System.Windows.Forms;
class FontMenuForm: Form
{
protected string strText = "Engrave";
protected Font font = new Font("Times New Roman", 24);
public static void Main()
{
Application.Run(new FontMenuForm());
}
public FontMenuForm()
{
ResizeRedraw = true;
}
protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
{
int iOffset = -2;
SizeF sizef = grfx.MeasureString(strText, font);
float x = (cx - sizef.Width) / 2;
float y = (cy - sizef.Height) / 2;
grfx.Clear(Color.White);
grfx.DrawString(strText, font, Brushes.Gray, x, y);
grfx.DrawString(strText, font, Brushes.White, x - iOffset,
y - iOffset);
}
public float GetAscent(Graphics grfx, Font font)
{
return font.GetHeight(grfx) *
font.FontFamily.GetCellAscent(font.Style) /
font.FontFamily.GetLineSpacing(font.Style);
}
public float GetDescent(Graphics grfx, Font font)
{
return font.GetHeight(grfx) *
font.FontFamily.GetCellDescent(font.Style) /
font.FontFamily.GetLineSpacing(font.Style);
}
public float PointsToPageUnits(Graphics grfx, Font font)
{
float fFontSize;
if (grfx.PageUnit == GraphicsUnit.Display)
fFontSize = 100 * font.SizeInPoints / 72;
else
fFontSize = grfx.DpiX * font.SizeInPoints / 72;
return fFontSize;
}
}
String Format based on Tab info data
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Imaging;
public class Form1 : System.Windows.Forms.Form
{
public Form1()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Text = "";
this.Resize += new System.EventHandler(this.Form1_Resize);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
}
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
g.FillRectangle(Brushes.White, this.ClientRectangle);
Font f = new Font("Courier New", 12);
Font bf = new Font(f, FontStyle.Bold);
StringFormat sf = new StringFormat();
float[] ts = { 100.0f, 150.0f, 170.0f };
sf.SetTabStops(0.0f, ts);
string fs = "{0}\t{1,8}\t{2,10}\t{3,12}";
string s1 = String.Format(fs, "Name", "Salary", "New Salary", "Difference");
string s2 = String.Format(fs, "January", "900.00", "1050.00", "-150.00");
string s3 = String.Format(fs, "February", "1100.00", "990.00", "110.00");
// Draw the text strings
g.DrawString(s1, bf, Brushes.Black, 20, 20, sf);
g.DrawString(s2, f, Brushes.Blue, 20, 20 + bf.Height, sf);
g.DrawString(s3, f, Brushes.Blue, 20, 20 + bf.Height + f.Height, sf);
f.Dispose();
bf.Dispose();
}
private void Form1_Resize(object sender, System.EventArgs e)
{
Invalidate();
}
}
Tall in the Center
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
class FontMenuForm: Form
{
protected string strText = "Tall in the Center";
protected Font font = new Font("Times New Roman", 24);
public static void Main()
{
Application.Run(new FontMenuForm());
}
public FontMenuForm()
{
ResizeRedraw = true;
}
protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
{
GraphicsPath path = new GraphicsPath();
float fFontSize = PointsToPageUnits(grfx, font);
path.AddString(strText, font.FontFamily, (int) font.Style,
fFontSize, new PointF(0, 0), new StringFormat());
RectangleF rectf = path.GetBounds();
path.Transform(new Matrix(1, 0, 0, 1,
-(rectf.Left + rectf.Right) / 2,
-(rectf.Top + rectf.Bottom) / 2));
rectf = path.GetBounds();
PointF[] aptf = path.PathPoints;
for (int i = 0; i < aptf.Length; i++)
aptf[i].Y *= 2 * (rectf.Width - Math.Abs(aptf[i].X)) /
rectf.Width;
path = new GraphicsPath(aptf, path.PathTypes);
grfx.TranslateTransform(cx / 2, cy / 2);
grfx.FillPath(new SolidBrush(clr), path);
}
public float GetAscent(Graphics grfx, Font font)
{
return font.GetHeight(grfx) *
font.FontFamily.GetCellAscent(font.Style) /
font.FontFamily.GetLineSpacing(font.Style);
}
public float GetDescent(Graphics grfx, Font font)
{
return font.GetHeight(grfx) *
font.FontFamily.GetCellDescent(font.Style) /
font.FontFamily.GetLineSpacing(font.Style);
}
public float PointsToPageUnits(Graphics grfx, Font font)
{
float fFontSize;
if (grfx.PageUnit == GraphicsUnit.Display)
fFontSize = 100 * font.SizeInPoints / 72;
else
fFontSize = grfx.DpiX * font.SizeInPoints / 72;
return fFontSize;
}
}
Text Format
/*
GDI+ Programming in C# and VB .NET
by Nick Symmonds
Publisher: Apress
ISBN: 159059035X
*/
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
namespace TextFormat_c
{
public class TextFormat : System.Windows.Forms.Form
{
#region Class Local Variables
Rectangle UserRect = Rectangle.Empty;
bool RectStarted = false;
bool RectFinished = false;
bool Typing = false;
bool InsideRect = false;
Cursor ClientCursor = Cursors.Default;
string RectText = string.Empty;
StringFormat BoxFormat = StringFormat.GenericDefault;
#endregion
private System.Windows.Forms.Button cmdNew;
private System.Windows.Forms.Button cmdLeft;
private System.Windows.Forms.Button cmdTop;
private System.ruponentModel.Container components = null;
public TextFormat()
{
InitializeComponent();
//All the mouse move events use the same delegate
this.cmdNew.MouseMove += new MouseEventHandler(this.Button_MouseMove);
this.cmdLeft.MouseMove += new MouseEventHandler(this.Button_MouseMove);
this.cmdTop.MouseMove += new MouseEventHandler(this.Button_MouseMove);
this.KeyPress += new KeyPressEventHandler(this.FormKeyPress);
BoxFormat.Alignment = StringAlignment.Center;
BoxFormat.LineAlignment = StringAlignment.Center;
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
ClientCursor.Dispose();
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.cmdNew = new System.Windows.Forms.Button();
this.cmdLeft = new System.Windows.Forms.Button();
this.cmdTop = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// cmdNew
//
this.cmdNew.Location = new System.Drawing.Point(16, 328);
this.cmdNew.Name = "cmdNew";
this.cmdNew.Size = new System.Drawing.Size(56, 24);
this.cmdNew.TabIndex = 0;
this.cmdNew.Text = "&New";
this.cmdNew.Click += new System.EventHandler(this.cmdNew_Click);
//
// cmdLeft
//
this.cmdLeft.Location = new System.Drawing.Point(104, 328);
this.cmdLeft.Name = "cmdLeft";
this.cmdLeft.Size = new System.Drawing.Size(104, 24);
this.cmdLeft.TabIndex = 1;
this.cmdLeft.Text = "Center Left Align";
this.cmdLeft.Click += new System.EventHandler(this.cmdLeft_Click);
//
// cmdTop
//
this.cmdTop.Location = new System.Drawing.Point(248, 328);
this.cmdTop.Name = "cmdTop";
this.cmdTop.Size = new System.Drawing.Size(104, 24);
this.cmdTop.TabIndex = 2;
this.cmdTop.Text = "Top Left Align";
this.cmdTop.Click += new System.EventHandler(this.cmdTop_Click);
//
// TextFormat
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(392, 373);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.cmdTop,
this.cmdLeft,
this.cmdNew});
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "TextFormat";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "TextFormat";
this.Load += new System.EventHandler(this.TextFormat_Load);
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new TextFormat());
}
private void TextFormat_Load(object sender, System.EventArgs e)
{
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics G = e.Graphics;
using(Pen P = new Pen(Brushes.Black, 1))
{
if ( !RectFinished )
P.DashStyle = DashStyle.Dash;
G.DrawRectangle(P,UserRect);
}
if (RectFinished && ClientCursor == Cursors.IBeam)
G.DrawString(RectText, new Font("Arial", 16), Brushes.Black,
UserRect, BoxFormat);
base.OnPaint(e);
}
protected override void OnMouseDown(MouseEventArgs e)
{
//If left button then start the rectangle
if ( e.Button == MouseButtons.Left )
{
if (UserRect == Rectangle.Empty)
{
UserRect.X = e.X;
UserRect.Y = e.Y;
RectStarted = true;
RectFinished = false;
}
}
//If right button then start the edit
else if ( e.Button == MouseButtons.Right )
{
if ( UserRect != Rectangle.Empty )
{
ClientCursor = Cursors.IBeam;
this.Cursor = ClientCursor;
Point pos = new Point(UserRect.X+UserRect.Width/2,
UserRect.Y+UserRect.Height/2);
//Translate cursor screen postion to position on form
int Offset = this.Height-this.ClientRectangle.Height;
pos += new Size(this.Location.X, this.Location.Y+Offset);
Cursor.Position = pos;
Typing = true;
this.KeyPreview = true;
}
}
base.OnMouseDown(e);
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
// A negative rectangle is not allowed.
// Mouse_down then Mouse_up without Mouse_move is not allowed
if (UserRect.Width <= 0 || UserRect.Height <=0 )
UserRect = Rectangle.Empty;
//Rectangle has ended
RectStarted = false;
RectFinished = true;
Invalidate();
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMove(e);
//Let program know if cursor is inside user rectangle
InsideRect = false;
if (UserRect != Rectangle.Empty)
if (UserRect.Contains(new Point(e.X, e.Y)))
InsideRect = true;
this.Cursor = ClientCursor;
//Increase the size of the rectangle each time the mouse moves.
if (RectStarted)
{
Size s = new Size(e.X-UserRect.X, e.Y-UserRect.Y);
UserRect.Size=s;
Invalidate();
}
}
private void cmdNew_Click(object sender, System.EventArgs e)
{
if ( Typing && InsideRect )
return;
//Start a new blank form
UserRect = Rectangle.Empty;
ClientCursor = Cursors.Default;
RectStarted = false;
RectFinished = false;
RectText = string.Empty;
this.KeyPreview=false;
Invalidate();
}
private void Button_MouseMove(object sender, MouseEventArgs e)
{
this.Cursor = Cursors.Default;
}
void FormKeyPress(object sender, KeyPressEventArgs e)
{
//Handle backspace key
if (e.KeyChar == (char)8)
{
if ( RectText != string.Empty )
RectText = RectText.Remove(RectText.Length-1, 1);
}
else
RectText += e.KeyChar;
Invalidate();
}
private void cmdLeft_Click(object sender, System.EventArgs e)
{
//Change horizontal alignement and redraw
BoxFormat.Alignment = StringAlignment.Near;
Invalidate();
}
private void cmdTop_Click(object sender, System.EventArgs e)
{
//Chnage vertical alignment and redraw
BoxFormat.LineAlignment = StringAlignment.Near;
Invalidate();
}
}
}
Text Rotate
/*
GDI+ Programming in C# and VB .NET
by Nick Symmonds
Publisher: Apress
ISBN: 159059035X
*/
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
namespace TextRotate_c
{
public class TextRotate : System.Windows.Forms.Form
{
Bitmap bmp;
Point UL, UR, BL;
Rectangle InvRect;
int Direction = -1;
String s = "ROTATING TEXT";
Font fnt = new Font("Arial", 12);
private System.Windows.Forms.VScrollBar Skew;
private System.Windows.Forms.Timer T1;
private System.ruponentModel.IContainer components;
public TextRotate()
{
InitializeComponent();
Skew.Minimum = 50;
Skew.Maximum = 250;
Skew.SmallChange = 1;
Skew.LargeChange = 10;
Skew.Value = 150;
using (Graphics G = this.CreateGraphics())
{
SizeF sz = G.MeasureString(s, fnt);
bmp = new Bitmap((int)sz.Width, (int)sz.Height);
}
for ( int k=0; k<bmp.Height; k++ )
{
for ( int j=0; j<bmp.Width; j++ )
bmp.SetPixel(j, k, Color.White);
}
bmp.MakeTransparent(Color.White);
UL = new Point(150, 150);
UR = new Point(UL.X+bmp.Width, Skew.Value);
BL = new Point(150, UL.Y+bmp.Height);
InvRect = new Rectangle(-UR.X, Skew.Minimum, 2*UR.X, Skew.Maximum);
using (Graphics G = Graphics.FromImage(bmp))
{
G.SmoothingMode = SmoothingMode.AntiAlias;
G.TextRenderingHint = TextRenderingHint.AntiAlias;
G.DrawString(s, fnt, Brushes.Black, 0, 0);
}
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.DoubleBuffer, true);
T1.Interval=10; //milliseconds
T1.Enabled=false;
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
bmp.Dispose();
fnt.Dispose();
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.ruponents = new System.ruponentModel.Container();
this.Skew = new System.Windows.Forms.VScrollBar();
this.T1 = new System.Windows.Forms.Timer(this.ruponents);
this.SuspendLayout();
//
// Skew
//
this.Skew.Location = new System.Drawing.Point(352, 54);
this.Skew.Name = "Skew";
this.Skew.Size = new System.Drawing.Size(16, 264);
this.Skew.TabIndex = 1;
this.Skew.Scroll += new System.Windows.Forms.ScrollEventHandler(this.Skew_Scroll);
//
// T1
//
this.T1.Tick += new System.EventHandler(this.T1_Tick);
//
// TextRotate
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(392, 373);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.Skew});
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "TextRotate";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "TextRotate";
this.Load += new System.EventHandler(this.TextRotate_Load);
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new TextRotate());
}
private void TextRotate_Load(object sender, System.EventArgs e)
{
}
protected override void OnPaint(PaintEventArgs e)
{
Point[] dest = {UL, UR, BL};
// Draw the image mapped to the parallelogram.
e.Graphics.DrawImage(bmp, dest);
e.Graphics.DrawLine(Pens.Black, UL, BL+new Size(0, 20));
if (T1.Enabled==false)
T1.Enabled=true;
}
private void Skew_Scroll(object sender,
System.Windows.Forms.ScrollEventArgs e)
{
UR.Y = Skew.Value;
}
private void T1_Tick(object sender, System.EventArgs e)
{
UR.X += Direction;
if ( UR.X == UL.X + bmp.Width*Direction )
Direction *=-1;
Invalidate(InvRect);
}
}
}
Text Rotate 2
/*
Professional Windows GUI Programming Using C#
by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury,
Zach Greenvoss, Shripad Kulkarni, Neil Whitlow
Publisher: Peer Information
ISBN: 1861007663
*/
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Drawing2D;
namespace Altamira
{
/// <summary>
/// Summary description for Altamira.
/// </summary>
public class Altamira : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ruponentModel.Container components = null;
public Altamira()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
this.Text = "Text direction";
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// Altamira
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(224, 141);
this.Name = "Altamira";
this.Text = "Altamira";
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Altamira());
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = CreateGraphics();
string txt = "HELLO";
float alpha = 45.0f;
int fontSize = 24;
Point center = new Point(90,20);
// Vertical text:
FontFamily ff = new FontFamily("Times New Roman");
Font f = new Font(ff, fontSize, FontStyle.Regular);
StringFormat sf = new StringFormat();
sf.FormatFlags = StringFormatFlags.DirectionVertical;
g.DrawString(txt, f, new SolidBrush(Color.Blue), center, sf);
// Global shift of the origin:
g.TranslateTransform(center.X, center.Y); // X + fontSize/2
g.DrawEllipse(Pens.Magenta, new Rectangle(0,0, 1, 1)); // center
// Local rotation of vertcal text (sf):
GraphicsPath gp = new GraphicsPath();
gp.AddString(txt, ff, (int)FontStyle.Bold, fontSize + 4,
new Point(0, 0), sf);
Matrix m = new Matrix();
m.Rotate(alpha); // clockwise
gp.Transform(m);
g.DrawPath(Pens.Red, gp); //g.FillPath(Brushes.Black, gp);
// Global rotation of vertical text (sf):
g.RotateTransform(-alpha); // anticlockwise
g.DrawString(txt, f, new SolidBrush(Color.Black), 0, 0, sf);
gp.Dispose(); g.Dispose(); m.Dispose();
}
}
}
Text string"s containing rectangle
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Imaging;
public class Form1 : System.Windows.Forms.Form
{
public Form1()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Text = "";
this.Resize += new System.EventHandler(this.Form1_Resize);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
}
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
g.FillRectangle(Brushes.White, this.ClientRectangle);
String s = "Accrington Stanley";
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();
}
private void Form1_Resize(object sender, System.EventArgs e)
{
Invalidate();
}
}