You're here: Snippet Directory » Microsoft .NET » C# (33)
Language:

Hilfslinien zur Laufzeit / SnapLines at runtime

Language: Deutsch
Programming Language: C#
Published by: LupoWolf
Last Update: 7/25/2008
Views: 1


Description

Mit dieser Klasse ist es möglich, Hilfslinien beim Verschieben von Controls per Laufzeit bereitzustellen.
Die Klasse SnapLines ist noch nicht 100%-ig getestet und kann daher noch bugs enthalten, oder die Funktionalität noch nicht komplett erfüllen. Für meine Zwecke war sie aber ausreichend.
Voraussetzung, um diese Klasse zu nutzen ist, dass man Controls zur Laufzeit verschieben kann (werde ich noch in einem separaten Snippet bereitsstellen)

Code

1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 using System.Windows.Forms; 5 using System.ComponentModel; 6 using System.Drawing; 7 8 namespace MyTools 9 { 10 class SnapLines 11 { 12 List<Control> myControls; 13 Dictionary<Control, bool> myControlsDict; 14 BackgroundWorker myThread; 15 Control myControl; 16 17 public SnapLines(Control aControlToPaintOn) 18 { 19 myControl = aControlToPaintOn; 20 myControls = new List<Control>(); 21 myControlsDict = new Dictionary<Control, bool>(); 22 myThread = new BackgroundWorker(); 23 myThread.WorkerSupportsCancellation = true; 24 myThread.DoWork += new DoWorkEventHandler(myThread_DoWork); 25 } 26 27 void myThread_DoWork(object sender, DoWorkEventArgs e) 28 { 29 Control myTest = new Control(); 30 while (!myThread.CancellationPending) 31 { 32 for (int i = 0; i < myControls.Count; i++) 33 { 34 if (myControlsDict[myControls[i]]) 35 { 36 for (int j = 0; j < myControls.Count; j++) 37 { 38 if (myControls[i] != myControls[j]) 39 { 40 if (myControls[i].Left == myControls[j].Left && myControls[i].Left > 0) 41 { 42 Pen myPen = new Pen(Color.Blue, 2); 43 Graphics g = myControl.CreateGraphics(); 44 if (g != null) 45 { 46 g.DrawLine(myPen, new Point(myControls[i].Left - 3, myControls[i].Top), new Point(myControls[j].Left - 3, myControls[j].Top)); 47 myControl.Invalidate(); 48 } 49 } 50 51 if (myControls[i].Right == myControls[j].Right && myControls[i].Right > 0) 52 { 53 Pen myPen = new Pen(Color.YellowGreen, 2); 54 Graphics g = myControl.CreateGraphics(); 55 if (g != null) 56 { 57 g.DrawLine(myPen, new Point(myControls[i].Right + 3, myControls[i].Top), new Point(myControls[j].Right + 3, myControls[j].Top)); 58 myControl.Invalidate(); 59 } 60 } 61 62 if (myControls[i].Top == myControls[j].Top && myControls[i].Top > 0) 63 { 64 Pen myPen = new Pen(Color.Red, 2); 65 Graphics g = myControl.CreateGraphics(); 66 if (g != null) 67 { 68 g.DrawLine(myPen, new Point(myControls[i].Left, myControls[i].Top - 3), new Point(myControls[j].Left, myControls[j].Top - 3)); 69 myControl.Invalidate(); 70 } 71 } 72 73 if (myControls[i].Bottom == myControls[j].Bottom && myControls[i].Bottom > 0) 74 { 75 Pen myPen = new Pen(Color.Yellow, 2); 76 Graphics g = myControl.CreateGraphics(); 77 if (g != null) 78 { 79 g.DrawLine(myPen, new Point(myControls[i].Left, myControls[i].Bottom + 3), new Point(myControls[j].Left, myControls[j].Bottom + 3)); 80 myControl.Invalidate(); 81 } 82 } 83 } 84 } 85 } 86 } 87 } 88 } 89 90 public void AddControl(Control aControl) 91 { 92 this.myThread.CancelAsync(); 93 myControls.Add(aControl); 94 myControlsDict.Add(aControl, false); 95 aControl.MouseDown += new MouseEventHandler(aControl_MouseDown); 96 aControl.MouseUp += new MouseEventHandler(aControl_MouseUp); 97 } 98 99 void aControl_MouseUp(object sender, MouseEventArgs e) 100 { 101 Control myC = (Control)sender; 102 myControlsDict[myC] = false; 103 Stop(); 104 } 105 106 void aControl_MouseDown(object sender, MouseEventArgs e) 107 { 108 Control myC = (Control)sender; 109 myControlsDict[myC] = true; 110 Start(); 111 } 112 113 private void Stop() 114 { 115 myThread.CancelAsync(); 116 } 117 118 private void Start() 119 { 120 if (!myThread.IsBusy) 121 { 122 myThread.RunWorkerAsync(); 123 } 124 } 125 126 } 127 } 128 129 130 Verwendung: 131 132 public partial class frmBasis : Form 133 { 134 ... 135 SnapLines mySnapLine; 136 ... 137 // Hier wird pictureBox1 als Control übergeben, auf dem die Hilfslinien gezeichnet werden sollen 138 mySnapLine = new SnapLines(pictureBox1); 139 ... 140 // Hier werden die Controls hinzugefügt, die die Hilfslinien benötigen 141 mySnapLine.AddControl(button1); 142 mySnapLine.AddControl(button2); 143 ... 144 } 145 146

No comments avaiable

Add a comment

Name *  

Email (won't be displayed) *    

Website  

Comment *  

Sicherheitscode Security Code *    

RSS