2013-08-25 112 views
1

我想在面板上畫一條線,因爲我在按住右鍵單擊按鈕的同時移動鼠標。這是我迄今爲止所嘗試的:在移動鼠標並按住右鍵單擊按鈕時繪製線條

private void panel1_MouseDown(object sender, MouseEventArgs e) 
{ 
    Point point = panel1.PointToClient(Cursor.Position); 
    Point cordinatedClikedPoint = new Point(xPanelPosition, yPanelPositon); 
    DrawPoint((point.X), (point.Y), Color.Navy);     
} 

public void DrawPoint(int x, int y, Color color) 
{ 
    Graphics g = this.panel1.CreateGraphics(); 
    Pen pen = new Pen(color); 
    g.DrawRectangle(pen, x, y, 3, 3); 
} 
+2

請標記此問題,WinForms或WPF或Whatever。單獨的C#不會畫線。 –

+0

是否有理由使用'PointToClient(Cursor.Location)'而不是'MouseEventArgs'提供的'e.Location'? – dreamlax

回答

1

試試這個,你需要在每個平局上使面板無效。

private IList<Point> _pointList = new List<Point>(); 
private void panel1_MouseMove(object sender, MouseEventArgs e) 
{  
    _pointList.Add(e.Location); 
    panel1.Invalidate(); //force a repaint 
} 

private void panel1_Paint(object sender, PaintEventArgs e) 
{ 
    e.Graphics.DrawLines(Pens.Black, _pointList); 
} 
+0

謝謝你Mayank,但e.Graphics.DrawLines(Pens.Black,_pointList);不接受列表作爲其第二個參數,它抱怨說它不能將system.Collections.Generic.List 轉換爲System.Drawing.Point [] – user2277918

+0

只是調用'ToArray()'將其轉換爲數組 – Mayank

0

據我所知,當您按下按鈕時,MouseDown事件會一次性觸發。和WindowsForms面板/窗體或任何有什麼是每隔幾毫秒重新繪製。因此,您需要將該代碼改爲OnPaint方法。

它應該是這個樣子的是:

//global variable 
bool isDragging = false; 

private void panel1_MouseDown(object sender, MouseEventArgs e) 
{ 
      isDragging = true;    
} 

private void panel1_MouseUp(object sender, MouseEventArgs e) 
{ 
      isDragging = false;    
} 

private void form_Paint(object sender, EventArgs e) 
{ 
      if(isDragging) 
      { 
       Point point = panel1.PointToClient(Cursor.Position); 
       Point cordinatedClikedPoint = new Point(xPanelPosition, yPanelPositon); 
       DrawPoint((point.X), (point.Y), Color.Navy); 
      } 
} 
3

只需使用MouseDownMouseUp事件來設置一個布爾變量,請在圖形中的MouseMove事件處理程序。由於您直接調用圖形對象,面板上的任何失效或重繪操作都會導致它被清除,另外兩個答案解決了這個問題。

public partial class Form1 : Form 
{ 
    bool mouseIsDown; 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void panel1_MouseDown(object sender, MouseEventArgs e) 
    { 
     if (e.Button== System.Windows.Forms.MouseButtons.Right) 
      mouseIsDown = true; 
    } 

    private void panel1_MouseMove(object sender, MouseEventArgs e) 
    { 
     if (mouseIsDown) 
     { 
      Point point = panel1.PointToClient(Cursor.Position); 
      DrawPoint((point.X), (point.Y), Color.Navy); 
     } 
    } 

    private void panel1_MouseUp(object sender, MouseEventArgs e) 
    { 
     mouseIsDown = false; 
    } 

    public void DrawPoint(int x, int y, Color color) 
    { 
     Graphics g = this.panel1.CreateGraphics(); 
     Pen pen = new Pen(color); 
     g.DrawRectangle(pen, x, y, 3, 3); 
    } 
} 
0

我覺得這是你正在嘗試做一個完整的解決方案:

public partial class Form1 : Form 
{ 
    private Graphics g; 
    private readonly Pen pen = new Pen(Color.Navy, 7); 
    private Point oldCoords; 
    private readonly GraphicsPath graphicsPaths = new GraphicsPath(); 

    public Form1() 
    { 
     InitializeComponent(); 
     g = panel1.CreateGraphics(); 
    } 

    private void panel1_MouseMove(object sender, MouseEventArgs e) 
    { 
     if (e.Button == MouseButtons.Left) 
     { 
      if (oldCoords.IsEmpty) 
       graphicsPaths.StartFigure(); 
      else 
      { 
       graphicsPaths.AddLine(oldCoords, new Point(e.X, e.Y)); 
       g.DrawPath(pen, graphicsPaths); 
      } 
      oldCoords= new Point(e.X,e.Y); 
     } 
     else 
      oldCoords = Point.Empty; 
    } 

    private void panel1_Paint(object sender, PaintEventArgs e) 
    { 
     g.DrawPath(pen, graphicsPaths); 
    } 
} 

乾杯:)

+0

Btw ..如果你想對右鼠標按鈕作出反應,而不是左改變行:if(e.Button == MouseButtons 。對) –

2

另外一...使用ControlPaint.DrawReversibleLine()這個時間:

public partial class Form1 : Form 
{ 

    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private Point StartPoint, EndPoint; 

    private void panel1_MouseDown(object sender, MouseEventArgs e) 
    { 
     if (e.Button == System.Windows.Forms.MouseButtons.Right) 
     { 
      Point pt = Cursor.Position; 
      StartPoint = pt; 
      EndPoint = pt; 
      ControlPaint.DrawReversibleLine(StartPoint, EndPoint, Color.Black); 
     } 
    } 

    private void panel1_MouseMove(object sender, MouseEventArgs e) 
    { 
     if (e.Button == System.Windows.Forms.MouseButtons.Right) 
     { 
      ControlPaint.DrawReversibleLine(StartPoint, EndPoint, Color.Black); // erase previous line 
      EndPoint = Cursor.Position; 
      ControlPaint.DrawReversibleLine(StartPoint, EndPoint, Color.Black); // draw new line 
     } 
    } 

    private void panel1_MouseUp(object sender, MouseEventArgs e) 
    { 
     if (e.Button == System.Windows.Forms.MouseButtons.Right) 
     { 
      ControlPaint.DrawReversibleLine(StartPoint, EndPoint, Color.Black); // erase previous line 

      // ... do something with StartPont and EndPoint in here ... 

      // possibly add them to a class level structure to use in the Paint() event to make it persistent? 

     } 
    } 

} 
相關問題