2015-10-15 91 views
0

我寫了一個代碼來使用鼠標繪製線條。Zedgraph用鼠標繪製線條

在鼠標向下我保存用戶點擊的地方。

bool mZgc_MouseDownEvent(ZedGraphControl sender, MouseEventArgs e) 
    { 
     GraphPane graphPane = mZgc.GraphPane; 
     graphPane.ReverseTransform(e.Location, out mMouseDownX, out mMouseDownY); 
     return false; 
    } 

在鼠標彈起我劃清界線:

bool zgc_MouseUpEvent(ZedGraphControl sender, MouseEventArgs e) 
    { 
     GraphPane graphPane = mZgc.GraphPane; 
     double x, y; 

     graphPane.ReverseTransform(e.Location, out x, out y); 
     LineObj threshHoldLine = new LineObj(Color.Red, mMouseDownX, mMouseDownY, x, y); 
     graphPane.GraphObjList.Add(threshHoldLine); 
     mZgc.Refresh(); 

     return false; 
    } 

的問題是,當鼠標下來,用戶看不到線(因爲我畫它只能在「漲」事件)。

我該如何解決它?

從技術上講,我可以使用「懸停」並每秒從圖形中繪製/刪除線條並刷新圖形,但它有點瘋狂。

有沒有一種「正常」的方式來做到這一點?

謝謝

回答

0

好吧,我已經解決了這個問題,我會爲後代發表答案。

首先,我們需要對zedgraph代碼做一些小改動,以允許我們在創建後更改線條,它可能會打破創作者嘗試實現的一些「不可變」範例,但如果您想避免每次鼠標移動時都會創建和刪除線條。

在文件Location.cs編輯X2,Y2屬性(集失蹤):

public double X2 
    { 
     get { return _x+_width; } 
     set { _width = value-_x; } 
    } 

    public double Y2 
    { 
     get { return _y+_height; } 
     set { _height = value-_y; } 
    } 

從這裏很容易,我不會發布的所有代碼,但會解釋步驟:

  1. 成員添加到您的類:private LineObj mCurrentLine;
  2. 在鼠標點下創建一個行mCurrentLine = new LineObj(Color.Red, mMouseDownX, mMouseDownY, mMouseDownX, mMouseDownY);
  3. 在鼠標移動改變線X2,Y2座標mCurrentLine.Location.X2 = x;
  4. 在鼠標彈起只是停止拉絲工藝(以避免改變線「鼠標移動」

如果有人確實要使用它,需要更好的解釋,只是評論。