鼠标从右向左拖动时,在面板上显示画线

时间:2014-03-15 18:20:11

标签: c# winforms drawing

    private void panel1_Paint(object sender, PaintEventArgs e)
    {
        Graphics g = panel1.CreateGraphics();
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
        using (Pen p = new Pen(Color.White))
        {

            for (int i = panel1.Width; i >= 0; i -= 40)
            {
                g.DrawLine(p, i, 0, i, panel1.Height);

            }
        }

        using (Pen p = new Pen(Color.White))
        {
            // Draw all visible, vertical gridlines                     
            for (int i = panel1.Height; i >= 0; i -= 40)
            {
                g.DrawLine(p, 0, i, panel1.Width, i);

            }
        }

        Pen pen = new Pen(Color.Red, 6);
        g.DrawLine(pen, new Point(50,80), new Point(8050,  80));

    }

输出: enter image description here

对我来说很好。 但我想当我点击并向右拖动鼠标示例(10px)时,面板显示右下一个10px线形式(隐藏)并从左侧隐藏10px。

我怎么能这样做......  请有人帮助我。

1 个答案:

答案 0 :(得分:0)

DonBoinott是对的。您可能应该创建一个线对象类来为您想要的行为建模。不知道它最终会变得多么复杂,我们只能建议计划增长。以下是多条水平线的简单解决方案:

 //..
 // draw all my lines
   foreach(myLine aLine in myLines)
       using (Pen p = new Pen(aLine.LineColor, aLine.Stroke))
       {
           p.Alignment = System.Drawing.Drawing2D.PenAlignment.Inset;
           g.DrawLine(p, aLine.X, aLine.Y, aLine.X + aLine.Length, aLine.Y);
       }



}

// keeping state of the mouse movement..
int lastX = 0; int currentX = 0; myLine hitLine = null;
// a collection of lines
List<myLine> myLines;
// a simple horizontal line class
public class myLine
{
    private int x;
    public int X
    {
        get { return x; }
        set { x = value; Rect = new Rectangle(x, Y-2, Length, Stroke+4); }
    }
    public int Y { get; set; }
    public int Length { get; set; }
    public int Stroke { get; set; }
    public Color LineColor { get; set; }
    public Rectangle Rect { get; set; }
    public string Name { get; set; }

    public myLine(int x, int y, int length, string name, Color color, int stroke)
    {
        X = x; Y = y; Length = length; Name = name; LineColor = color; Stroke = stroke;
        Rect = new Rectangle(x, Y - 2, Length, Stroke + 4);
    }
}

// create a few lines
private void initLines()
{
    myLines = new List<myLine>();
    myLines.Add(new myLine(100, 80, 400, "Tom", Color.Red, 6));
    myLines.Add(new myLine(100, 130, 400, "Jerry", Color.Orange, 8));
    myLines.Add(new myLine(100, 180, 400, "Barbera", Color.Blue, 10));
}

private void panel1_MouseDown(object sender, MouseEventArgs e)
{
   // hittest, show line name in a panel (optional) 
   foreach (myLine aLine in myLines)
       if (aLine.Rect.Contains(e.Location)) { hitLine = aLine; label1.Text = hitLine.Name;  continue; }
   lastX = currentX;
   currentX = e.X;
}

private void panel1_MouseUp(object sender, MouseEventArgs e)
{
   hitLine = null;
}

private void panel1_MouseMove(object sender, MouseEventArgs e)
{
   if (e.Button == System.Windows.Forms.MouseButtons.Left)
   {
       lastX = currentX;
       currentX = e.X;
       if (hitLine != null)
       {
           hitLine.X += (currentX - lastX);
           // invalidate a bit more than really needed..
           panel1.Invalidate(new Rectangle(0, hitLine.Y - hitLine.Stroke, panel1.Width, hitLine.Y + hitLine.Stroke));
       }
   }
}