Csharp/CSharp Tutorial/2D — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 12:18, 26 мая 2010
- BMP
- Clip
- Clock
- Color
- Coordinate
- Draw Image
- Draw Rectangle
- Draw String
- Ellipse
- Gif
- Gradient Brush
- Graphics
- GraphicsUnit
- HatchBrush
- Hotkey
- Ico
- Image
- ImageCodecInfo
- JPG
- Line
- LinearGradientBrush
- Measure String
- Mouse Draw
- Path
- Pens
- Pie
- Point
- Rectangle
- Region
- SmoothingMode
- SolidBrush
- Texture Brush
- Tif
Содержание
- 1 All in the font family: Ascent, Descent, Line spacing, Height
- 2 Draw centered text
- 3 Draw Font Families
- 4 Draw Font Families Formatted
- 5 Draw left justified text based on font size
- 6 Draw right justified text based on font size
- 7 FontStyle.Bold
- 8 FontStyle.Italic
- 9 FontStyle.Regular
- 10 Get Font Height
All in the font family: Ascent, Descent, Line spacing, Height
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class FontFamilyAscentDescentLineHeight : System.Windows.Forms.Form
{
private System.ruponentModel.Container components = null;
public FontFamilyAscentDescentLineHeight()
{
InitializeComponent();
}
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(392, 237);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.OnPaint);
}
[STAThread]
static void Main()
{
Application.Run(new FontFamilyAscentDescentLineHeight());
}
protected void OnPaint (object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
FontFamily myFamily = new FontFamily("Verdana");
Font myFont = new Font(myFamily, 12);
int y = 0;
int fontHeight = myFont.Height;
this.Text = "Measurements are in GraphicsUnit." + myFont.Unit.ToString();
g.DrawString("The Verdana family.", myFont, Brushes.Blue, 10, y);
Console.WriteLine("Ascent for bold Verdana: " + myFamily.GetCellAscent(FontStyle.Bold));
Console.WriteLine("Descent for bold Verdana: " + myFamily.GetCellDescent(FontStyle.Bold));
Console.WriteLine("Line spacing for bold Verdana: " + myFamily.GetLineSpacing(FontStyle.Bold));
Console.WriteLine("Height for bold Verdana: " + myFamily.GetEmHeight(FontStyle.Bold));
}
}
Draw centered text
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
class Form1 : Form
{
public Form1()
{
SetStyle(ControlStyles.Opaque, true);
Bounds = new Rectangle(0, 0, 500, 300);
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
int y = 0;
g.FillRectangle(Brushes.White, ClientRectangle);
// draw multiline text
Font trFont = new Font("Times New Roman", 12);
Rectangle rect = new Rectangle(0, y, 400, trFont.Height * 3);
g.DrawRectangle(Pens.Blue, rect);
String longString = "this is a test. ";
longString += "this is a test. this is a test. this is a test. this is a test. ";
longString += "this is a test. this is a test. this is a test. this is a test. ";
longString += "this is a test. this is a test. this is a test. this is a test. ";
g.DrawString(longString, trFont, Brushes.Black, rect);
trFont.Dispose();
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}
}
Draw Font Families
using System;
using System.Drawing;
using System.Windows.Forms;
public class DrawFontFamilies : Form
{
public DrawFontFamilies()
{
Size = new Size(350,200);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
float x= 10;
float y= 10;
Font fnt;
Graphics g = e.Graphics;
FontFamily[] ffArray = FontFamily.Families;
foreach( FontFamily ff in ffArray )
{
if (ff.IsStyleAvailable(FontStyle.Regular))
{
fnt = new Font(ff, 10);
g.DrawString(ff.Name, fnt, Brushes.Black, x, y);
Brush b = new SolidBrush(ForeColor);
g.DrawString(ff.Name, fnt, b, x, y);
y += fnt.GetHeight();
}
}
}
static void Main()
{
Application.Run(new DrawFontFamilies());
}
}
Draw Font Families Formatted
using System;
using System.Drawing;
using System.Windows.Forms;
public class DrawFontFamiliesFormatted : Form
{
public DrawFontFamiliesFormatted()
{
ResizeRedraw = true;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
float y= 10;
Font fnt;
Graphics g = e.Graphics;
StringFormat fmt = new StringFormat();
fmt.Alignment = StringAlignment.Center;
FontFamily[] ffArray = FontFamily.Families;
foreach( FontFamily ff in ffArray )
{
if (ff.IsStyleAvailable(FontStyle.Regular))
{
fnt = new Font(ff, 10);
Brush b = new SolidBrush(ForeColor);
g.DrawString(ff.Name, fnt, b, ClientSize.Width / 2, (int)y, fmt);
y += fnt.GetHeight();
}
}
}
static void Main()
{
Application.Run(new DrawFontFamiliesFormatted());
}
}
Draw left justified text based on font size
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
class Form1 : Form
{
public Form1()
{
SetStyle(ControlStyles.Opaque, true);
Bounds = new Rectangle(0, 0, 500, 300);
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
int y = 0;
g.FillRectangle(Brushes.White, ClientRectangle);
// draw left justified text
Rectangle rect = new Rectangle(0, y, 400, Font.Height);
g.DrawRectangle(Pens.Blue, rect);
g.DrawString("This text is left justified.", Font,Brushes.Black, rect);
y += Font.Height + 20;
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}
}
Draw right justified text based on font size
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
class Form1 : Form
{
public Form1()
{
SetStyle(ControlStyles.Opaque, true);
Bounds = new Rectangle(0, 0, 500, 300);
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
int y = 0;
g.FillRectangle(Brushes.White, ClientRectangle);
Font aFont = new Font("Arial", 16, FontStyle.Bold | FontStyle.Italic);
Rectangle rect = new Rectangle(0, y, 400, aFont.Height);
g.DrawRectangle(Pens.Blue, rect);
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Far;
g.DrawString("This text is right justified.", aFont, Brushes.Blue,rect, sf);
y += aFont.Height + 20;
aFont.Dispose();
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}
}
FontStyle.Bold
using System;
using System.Drawing;
using System.Windows.Forms;
class FontNames: Form
{
public static void Main()
{
Application.Run(new FontNames());
}
public FontNames()
{
Text = "Font Names";
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)
{
string[] astrFonts = { "Courier New", "Arial",
"Times New Roman" };
FontStyle[] afs = { FontStyle.Bold,
FontStyle.Bold | FontStyle.Italic };
Brush brush = new SolidBrush(clr);
float y = 0;
foreach (string strFont in astrFonts)
{
foreach (FontStyle fs in afs)
{
Font font = new Font(strFont, 18, fs);
grfx.DrawString(strFont, font, brush, 0, y);
y += font.GetHeight(grfx);
}
}
}
}
FontStyle.Italic
using System;
using System.Drawing;
using System.Windows.Forms;
class FontNames: Form
{
public static void Main()
{
Application.Run(new FontNames());
}
public FontNames()
{
Text = "Font Names";
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)
{
string[] astrFonts = { "Courier New", "Arial",
"Times New Roman" };
FontStyle[] afs = { FontStyle.Italic,
FontStyle.Bold | FontStyle.Italic };
Brush brush = new SolidBrush(clr);
float y = 0;
foreach (string strFont in astrFonts)
{
foreach (FontStyle fs in afs)
{
Font font = new Font(strFont, 18, fs);
grfx.DrawString(strFont, font, brush, 0, y);
y += font.GetHeight(grfx);
}
}
}
}
FontStyle.Regular
using System;
using System.Drawing;
using System.Windows.Forms;
class FontNames: Form
{
public static void Main()
{
Application.Run(new FontNames());
}
public FontNames()
{
Text = "Font Names";
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)
{
string[] astrFonts = { "Courier New", "Arial",
"Times New Roman" };
FontStyle[] afs = { FontStyle.Regular,
FontStyle.Bold | FontStyle.Italic };
Brush brush = new SolidBrush(clr);
float y = 0;
foreach (string strFont in astrFonts)
{
foreach (FontStyle fs in afs)
{
Font font = new Font(strFont, 18, fs);
grfx.DrawString(strFont, font, brush, 0, y);
y += font.GetHeight(grfx);
}
}
}
}
Get Font Height
using System;
using System.Drawing;
using System.Windows.Forms;
class TwentyFourPointScreenFonts: Form
{
public static void Main()
{
Application.Run(new TwentyFourPointScreenFonts());
}
public TwentyFourPointScreenFonts()
{
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 y = 0;
Font font;
string strFamily = "Times New Roman";
font = new Font(strFamily, 24);
grfx.DrawString("No GraphicsUnit, 24 points", font, brush, 0, y);
y += font.GetHeight(grfx);
font = new Font(strFamily, 24, GraphicsUnit.Point);
grfx.DrawString("GraphicsUnit.Point, 24 units", font, brush, 0, y);
}
}