Grundlagen in Visual Studio MFC und .net
Dipl.-Inf., Dipl.-Ing. (FH) Michael Wilhelm Hochschule Harz
FB Automatisierung und Informatik mwilhelm@hs-harz.de
Raum 2.202
Tel. 03943 / 659 338
Inhalt
1. Einführung in .net
2. Dialogfenster / SDI
3. Grafik
DrawArc DrawBezier DrawBeziers
DrawCloseCurve DrawEllipse
DrawLine DrawPath DrawPie
DrawPolygon DrawRectangle DrawString
Wichtige Grafik-Methoden
FillCloseCurve FillEllipse
FillPath FillPie
FillPolygon
FillRectangle
FillRegion
DrawImage
Konstruktoren:
DrawLine
public void DrawLine(
Pen pen, Point pt1, Point pt2 )
public void DrawLine(
Pen pen,
int x1,int y1,
int x2,int y2
Erstellen eines neuen Projektes
o
Name: Grafik1
Rechte Maustaste, Properties Eintrag „paint“
Doppelklick
1. Beispiel:
Paint-Methode in der Form von Grafik1
•
Form_load() {
•
this.ResizeRedraw = true;
•
this.DoubleBuffered = true;
•
}
// e ist die Referenz auf die Leinwand
private void Form1_Paint(object sender, PaintEventArgs e) {
Graphics g = e.Graphics;
Pen pen = new Pen(Color.Black, 3);
g.DrawLine(pen,10,10, 400,200);
pen.Dispose();
using System.Drawing.Drawing2D
■
DashStyle.DashDot;
■
DashStyle.Dash;
■
DashStyle.DashDotDot;
■
DashStyle.Dot;
■
DashStyle.Solid;
■
DashStyle.DashDot;
■
DashStyle.Custom;
Linestyles
myPen.DashStyle = DashStyle.DashDot;
public void DrawRectangle(
Pen pen,
Rectangle rect )
public void DrawRectangle(
Pen pen,
int x, int y,
int width, int height Konstruktoren:
DrawRectangle
Beispiel
Beispiel
Graphics g = e.Graphics;
Pen pen = new Pen(Color.Black, 3);
Pen pen2 = new Pen(Color.Red, 6);
Rectangle rect = new Rectangle(10, 10, 200, 200);
g.DrawRectangle(pen, rect);
g.DrawRectangle(pen2, 20, 20, 180, 180); // width, height
pen.Dispose();
pen2.Dispose();
public void DrawEllipse(
Pen pen,
Rectangle rect )
public void DrawEllipse(
Pen pen,
int x, int y,
int width, int height Kontruktoren:
DrawEllipse
Beispiel
Konstruktoren:
DrawString
public void DrawString(
string s, Font font, Brush brush, PointF point )
public void DrawString(
string s,
Font font,
Brush brush,
PointF point,
Methoden:
Font
public void Font(
string Fontname, float emSize);
public void Font(
string Fontname,
float emSize,
FontStyle style
);
Methoden:
SolidBrush: Version1
SolidBrush drawBrush = new SolidBrush(Color.Black);
Methoden:
HatchBrush: Version2
Brush mybrush = new HatchBrush(
HatchStyle.DiagonalCross, Color.Green
);
•
BackwardDiagonal
•
Cross
•
DarkDownwardDiagonal
•
DarkHorizontal
•
DashedVertical
•
DiagonalBrick
•
DiagonalCross
•
DottedDiamond
•
DottedGrid
•
ForwardDiagonal
•
Horizontal
•
LargeGrid
•
Min
•
NarrowHorizontal
•
NarrowVertical
•
Percent05
•
Percent10
•
Percent90
•
SmallGrid
•
SolidDiamond
•
Sphere
•
Trellis
•
Vertical
•
Wave
Brush mybrush1 =
new HatchBrush(HatchStyle.DiagonalCross, Color.Green);
Brush mybrush1 =
new HatchBrush(HatchStyle.DiagonalCross, Color.Green, Color.White);
g.FillEllipse(mybrush1, 152, 112, 150, 80);
Brush mybrush2 =
new HatchBrush(HatchStyle.ZigZag, Color.Red);
g.FillEllipse(mybrush2, 10, 11, 120, 80);
Beispiel HatchBrush mit FillEllipse
Methoden:
LinearGradientBrush: Version3
Brush mybrush =
new LinearGradientBrush(
point,
point,
Color,
Color
);
Point p1 = new Point(0, 0);
Point p2 = new Point(200, 100); // Größe des Schemas mybrush = new LinearGradientBrush(
p1, // absolute Koordinaten p2, // absolute Koordinaten Color.Red,
Color.Blue );
g.FillEllipse(mybrush, 50, 50, 300, 200);
Beispiel mit LinearGradientBrush
Beispiele mit DrawString
Font drawFont1 = new Font("Arial", 16);
Font drawFont2 = new Font("Courier New", 16, FontStyle.Bold | FontStyle.Underline );
SolidBrush drawBrush = new SolidBrush(Color.Black);
PointF drawPoint = new PointF(50.0F, 55.0F);
g.DrawString("Hier ist ein Text", drawFont1, drawBrush, drawPoint);
g.DrawString("Hier ist zweiter Text", drawFont2, drawBrush,
•
Graphics g = this.CreateGraphics();
•
SizeF sf = g.MeasureString("FFFFFF:F", drawFont1);
•
sf = g.MeasureString("MMMM", drawFont1);
•
leftMargin = (int)sf.Width;
•
rightMargin = (int) sf.Width;
•
int zeilenhoehe = (int) sf.Height;
Beispiele mit DrawString, Abmessungen
Erstellen eines neuen Projektes
o
Name: Grafik2
Eintragen eines Panels
Rechte Maustaste, Properties Eintrag „paint“
Doppelklick
2. Beispiel:
Beispiele mit DrawImage und DrawPolygon
Pen blackPen = new Pen(Color.Black, 3);
Point point1 = new Point(50, 50); Point point2 = new Point(100, 25);
Point point3 = new Point(200, 5); Point point4 = new Point(250, 50);
Point point5 = new Point(300, 100); Point point6 = new Point(350, 200);
Point point7 = new Point(250, 250);
Point[] curvePoints = { point1, point2, point3, point4, point5, point6, point7 };
Image newImage = Image.FromFile("c:\\daten\\fotos\\bsp1.jpg");
// Punkt oben / links des Bildes Point ulCorner = new Point(10, 10);
// Draw image to screen.
g.DrawImage(newImage, ulCorner);
g.DrawPolygon(blackPen, curvePoints);
Erstellen eines neuen Projektes
o