Strange things happen in my Win Forms App. I create a Custom LineControl, which simply draws full transparent control with line inside. Line is creating dynamically by User (with preview like in Win Paint). When i create new Line ones for a while it daws a dashed line. Like on picture above. I've drawn 4 line and suddenly 5th is dashed. I cant understard that. Thickness of dashed line is different. Always 1px.

Pen p and SolidBrush b used to draw Line are instantiated in constructor
Code |
OnPaint method looks like that:
C# |
Any ideas?

FroglegRank #1,020 · 1.9K reputationPosted Jan 23, 2011, 9:48 AM EST
waserek5 reputationPosted Jan 23, 2011, 12:41 PM EST
// based on BitLineControl public partial class LineControl : ShapeControl { private Point StartPoint; private Point EndPoint; private Point[] _points = new Point[4]; public LineControl(int x1,int y1, int x2, int y2, int zoom):base (x1,y1,x2-x1,y2-y1,zoom) { this.StartPoint = new Point(x1, y1); this.EndPoint = new Point(x2, y2); col = Color; thick = Thickness; double currZoom = Utility.zoomFactor[zoom]; this.origanalValues = new int[] { (int)(x1 / currZoom), (int)(y1 / currZoom), (int)(x2 / currZoom), (int)(y2 / currZoom), (int)(thick / currZoom) }; p = new Pen(col, thick); p.DashStyle = DashStyle.Solid; b = new SolidBrush(col); drawLine(StartPoint, EndPoint); String s = this.Equation; InitializeComponent(); } public void updateThickness (int thickness){ this.thick = thickness; this.drawLine(this.StartPoint, this.EndPoint); } private void drawLine(Point firstpoint, Point secondpoint) { this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw | ControlStyles.UserPaint | ControlStyles.SupportsTransparentBackColor, true); this.UpdateStyles(); path= new GraphicsPath(); _points[0] = firstpoint; _points[1] = secondpoint; double c = Math.Sqrt(Math.Pow((secondpoint.Y - firstpoint.Y),2) + Math.Pow((secondpoint.X - firstpoint.X),2)); double cos = Math.Abs(secondpoint.Y - firstpoint.Y) / c; double sin = Math.Abs(secondpoint.X - firstpoint.X) / c; int b = (int)(sin * thick); int a = (int)(cos * thick); _points[2] = new Point(secondpoint.X + a, secondpoint.Y + b); _points[3] = new Point(firstpoint.X + a, firstpoint.Y + b); _points = setInitialPoints(_points); path.StartFigure(); path.AddPolygon(_points); path.CloseFigure(); base.Width += 2; } // updates Base.Top, Base.Left to determine size of control private Point[] setInitialPoints(Point[] oldPoints) { int theXvalue = 0; int theYvalue = 0; int maxX = 0; int maxY = 0; GraphicsPath gp = new GraphicsPath(); Point[] newPoint = new Point[oldPoints.Length]; Point oldValue = new Point(); foreach (Point p in oldPoints) { if (oldValue.IsEmpty) { theXvalue = p.X; theYvalue = p.Y; maxX = p.X; maxY = p.Y; } else { if (theXvalue > p.X) theXvalue = p.X; if (theYvalue > p.Y) theYvalue = p.Y; if (maxX < p.X) maxX = p.X; if (maxY < p.Y) maxY = p.Y; } oldValue = p; } base.Left = (int)theXvalue; base.Top = (int)theYvalue; base.Width = (int)(maxX - theXvalue); base.Height = (int)(maxY - theYvalue); //base.Width += 5; //base.Height += 10; if (base.Height <= 0) base.Height = 5; if (base.Width <= 0) base.Width = 5; if (theXvalue != 0) theXvalue = -(theXvalue); if (theYvalue != 0) theYvalue = -(theYvalue); int i = 0; foreach (Point p in oldPoints) { newPoint[i].X = p.X + theXvalue; newPoint[i].Y = p.Y + theYvalue; i++; } return newPoint; } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); e.Graphics.SmoothingMode = SmoothingMode.HighQuality; e.Graphics.FillPath(b, this.path); e.Graphics.DrawPath(p, this.path); this.Region = new Region(this.path); } protected override void OnLocationChanged(EventArgs e) { base.OnLocationChanged(e); this.Invalidate(); } protected override void OnResize(EventArgs e) { this.Region = new Region(path); this.Refresh(); base.OnResize(e); }//onResize }This is how it works: The control calculate 2 other point to draw polygon on GraphicalPath base on the thickness of control. I determine that dashed line is draw only with specific angle. When direction factor on line m= (EndPoint.y-StartPoint.y)/(EndPoint.x-StartPoint.x) is between 0.6 and 1.2. Its crazy wright? I dont see the connection.
waserek5 reputationPosted Jan 24, 2011, 9:00 AM EST