2012-05-26 29 views
9

मेरे .net C# प्रोग्राम में मैं टेक्स्ट बॉक्स से मूल्यों का उपयोग करके कुछ पंक्तियां खींचता हूं (मैं DrawLine फ़ंक्शन का उपयोग करता हूं)। मैं इस लाइन में से एक को क्लिक द्वारा स्थानांतरित करने और माउस के साथ इस लाइन को स्थानांतरित करने में सक्षम होना चाहता हूं - क्या यह संभव है?ग्राफ़िक - ड्रॉलाइन - रेखा खींचें और इसे

+2

। निश्चित रूप से आप एक बेहतर सवाल के बारे में सोच सकते हैं? –

+0

तो यह कैसे करें? :) मेरे पास इस तरह कुछ है http://www.youtube.com/watch?v=EiByTu7aa0k और मैं इस लाइन में से किसी एक पर क्लिक करना चाहता हूं और इसकी स्थिति – Juss

+2

अच्छी तरह से बदलना चाहता हूं, आपने अभी तक क्या किया है? क्या आपने कम से कम लाइनों की एक सूची बनाई है और क्या आपने एक माउसडाउन इवेंट हैंडलर लिखा है ताकि यह जांच सके कि उपयोगकर्ता ने लाइन पर क्लिक किया है या नहीं? आप वास्तव में अटक गए थे? –

उत्तर

37

line moving

public class LineMover : Form 
{ 
    public LineMover() 
    { 

    this.DoubleBuffered = true; 

    this.Paint += new PaintEventHandler(LineMover_Paint); 
    this.MouseMove += new MouseEventHandler(LineMover_MouseMove); 
    this.MouseDown += new MouseEventHandler(LineMover_MouseDown); 
    this.MouseUp += new MouseEventHandler(LineMover_MouseUp); 

    this.Lines = new List<GraphLine>() 
    { 
     new GraphLine (10, 10, 100, 200), 
     new GraphLine (10, 150, 120, 40), 
    }; 
    } 

    void LineMover_MouseUp(object sender, MouseEventArgs e) 
    { 
    if (Moving != null) 
    { 
     this.Capture = false; 
     Moving = null; 
    } 
    RefreshLineSelection(e.Location); 

    } 

    void LineMover_MouseDown(object sender, MouseEventArgs e) 
    { 
    RefreshLineSelection(e.Location); 
    if (this.SelectedLine != null && Moving == null) 
    { 
     this.Capture = true; 
     Moving = new MoveInfo 
     { 
      Line = this.SelectedLine, 
      StartLinePoint = SelectedLine.StartPoint, 
      EndLinePoint = SelectedLine.EndPoint, 
      StartMoveMousePoint = e.Location 
     }; 
    } 
    RefreshLineSelection(e.Location); 
    } 

    void LineMover_Paint(object sender, PaintEventArgs e) 
    { 
    e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; 
    e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 
    foreach (var line in Lines) 
    { 
     var color = line == SelectedLine ? Color.Red : Color.Black; 
     var pen = new Pen(color, 2); 
     e.Graphics.DrawLine(pen, line.StartPoint, line.EndPoint); 
    } 
    } 
    void LineMover_MouseMove(object sender, MouseEventArgs e) 
    { 
    if (Moving != null) 
    { 
     Moving.Line.StartPoint = new PointF(Moving.StartLinePoint.X + e.X - Moving.StartMoveMousePoint.X, Moving.StartLinePoint.Y + e.Y - Moving.StartMoveMousePoint.Y); 
     Moving.Line.EndPoint = new PointF(Moving.EndLinePoint.X + e.X - Moving.StartMoveMousePoint.X, Moving.EndLinePoint.Y + e.Y - Moving.StartMoveMousePoint.Y); 
    } 
    RefreshLineSelection(e.Location); 
    } 

    private void RefreshLineSelection(Point point) 
    { 
    var selectedLine = FindLineByPoint(Lines, point); 
    if (selectedLine != this.SelectedLine) 
    { 
     this.SelectedLine = selectedLine; 
     this.Invalidate(); 
    } 
    if (Moving != null) 
     this.Invalidate(); 

    this.Cursor = 
     Moving != null ? Cursors.Hand : 
     SelectedLine != null ? Cursors.SizeAll : 
      Cursors.Default; 

    } 



    public List<GraphLine> Lines = new List<GraphLine>(); 
    GraphLine SelectedLine = null; 
    MoveInfo Moving = null; 


    static GraphLine FindLineByPoint(List<GraphLine> lines, Point p) 
    { 
    var size = 10; 
    var buffer = new Bitmap(size * 2, size * 2); 
    foreach (var line in lines) 
    { 
     //draw each line on small region around current point p and check pixel in point p 

     using (var g = Graphics.FromImage(buffer)) 
     { 
     g.Clear(Color.Black); 
     g.DrawLine(new Pen(Color.Green, 3), line.StartPoint.X - p.X + size, line.StartPoint.Y - p.Y + size, line.EndPoint.X - p.X + size, line.EndPoint.Y - p.Y + size); 
     } 

     if (buffer.GetPixel(size, size).ToArgb() != Color.Black.ToArgb()) 
     return line; 
    } 
    return null; 
    } 

    public static void Main() 
    { 
    Application.Run(new LineMover()); 
    } 
} 

public class MoveInfo 
{ 
    public GraphLine Line; 
    public PointF StartLinePoint; 
    public PointF EndLinePoint; 
    public Point StartMoveMousePoint; 
} 
public class GraphLine 
{ 
    public GraphLine(float x1, float y1, float x2, float y2) 
    { 
    this.StartPoint = new PointF(x1, y1); 
    this.EndPoint = new PointF(x2, y2); 
    } 
    public PointF StartPoint; 
    public PointF EndPoint; 
} 
पाठ्यक्रम संभव है कि के
+1

एक महान कामकाजी समाधान के लिए +1, हालांकि मुझे 'चेकलाइन चयन()' में तर्क समझ में नहीं आता है। – ja72

+0

@ ja72 वर्तमान संस्करण की जांच करें –

+0

वास्तव में अच्छा काम !!! – Alexandr