Freihand auf eine Form zeichnen mit der GraphicsPath Klasse aus dem System.Drawing.Drawing2D Namespace. Kleine Kritzeleien sind nun problemlos möglich, bis zum CAD-Programm ist es aber noch ein Stück.
Hier das Beispiel
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | using System.Drawing; using System.Windows.Forms; using System.Drawing.Drawing2D; namespace Auf_Form_Zeichnen { public partial class Form1 : Form { GraphicsPath pfad; bool bMouseDown; Point ptStart; public Form1() { InitializeComponent(); pfad = new GraphicsPath(); } private void Form1_MouseDown(object sender, MouseEventArgs e) { //Gezeichnet wird mit gedrückter Linker Maustaste if (e.Button != MouseButtons.Left) return; //Startpunkt für spätere Linie merken ptStart = new Point(e.X, e.Y); bMouseDown = true; pfad.StartFigure(); } private void Form1_MouseMove(object sender, MouseEventArgs e) { if (!bMouseDown) return; //Aktuelle Mauskoordinaten Point ptNew = new Point(e.X, e.Y); //Grafikobjekt erzeugen Graphics grfx = CreateGraphics(); //Linie vom Start zum Endpunkt ziehen grfx.DrawLine(new Pen(ForeColor), ptStart, ptNew); grfx.Dispose(); //Alle Linien werden gespeichert pfad.AddLine(ptStart, ptNew); //Neuen Startpunkt setzen ptStart = ptNew; } private void Form1_MouseUp(object sender, MouseEventArgs e) { //Maus wurde losgelassen bMouseDown = false; } protected override void OnPaint(PaintEventArgs e) { //Wichtig für Neuzeichnungen, da sonst alles verschwindet. e.Graphics.DrawPath(new Pen(ForeColor), pfad); } } } |
Tags: cad, grafik, GraphicsPath, malen