2012-05-05 10 views
1

パケットをルーティングするためのネットワークのトポロジを作成するフォーム上にパネルがあります。私はパネルにトポロジーを描くことができます。しかし、今、小さな四角形をボタンクリックで1つの地点から目的地まで移動させたい。パネルでこれを行うにはどうすればいいですか?ボタンをクリックしても何も起こりません。誰かが私を助けることができれば感謝します。これは私のコードの一部です:panel2_Paintと仮定するとパネル上の四角形をアニメーション化する

public Form1() 
{    
    InitializeComponent(); 

    this.SetStyle(ControlStyles.UserPaint 
      | ControlStyles.AllPaintingInWmPaint 
      | ControlStyles.DoubleBuffer, true); 

    //Initialize the point to start in the top left corner 
    this.SetStyle(ControlStyles.UserPaint 
      | ControlStyles.AllPaintingInWmPaint 
      | ControlStyles.DoubleBuffer, true); 

    //Initialize the point to start in the top left corner 
    mPoint = new Point(mRect.Location.X, mRect.Location.X); 

    //Set up the function to call when the timer fires 
    TimerCallback timercb = new TimerCallback(TimerCB); 

    //Set up the timer to fire at the set animation rate 
    mTimer = new System.Threading.Timer(timercb, 
     null, ANIMATION_RATE, ANIMATION_RATE);  
} 

private void panel2_Paint(object sender, PaintEventArgs e) 
{ 
    if(sendbut) { 
     Graphics dc = panel2.CreateGraphics(); 
     //Fill the background of the client area 
     dc.FillRectangle(SystemBrushes.Control, this.ClientRectangle); 

     //Draw the big rectangle track 
     dc.DrawRectangle(Pens.Black, mRect); 

     //Clone the point 
     Point p = new Point(mPoint.X, mPoint.Y); 

     //Offset it by half the width and height of the desired rectangle 
     p.Offset(-(RECT_WIDTH/2), -(RECT_HEIGHT/2)); 

     //Draw the little moving rectangle 
     dc.FillRectangle(Brushes.Black, 
      new Rectangle(p, new Size(RECT_WIDTH, RECT_HEIGHT))); 
    } 
} 

protected void TimerCB(object o) 
{ 
    //Move the rectangle 
    MovePoint(); 

    //Redraw the form 
    Invalidate(); 
} 

private void button3_Click(object sender, EventArgs e) 
{   
    sendbutt = true; 
} 
+0

なぜあなたとはうまくいかないのか分かりませんが、 'panel2.CreateGraphics()'の代わりに 'e.Graphics'を使ってみてください。多分それが問題の一部です。 – BeemerGuy

+1

はい、間違っています。また、 "sendbutt"と "sendbut"。 –

答えて

0

は塗料が発生した後に、そのメソッドが呼び出されますpanel2.Paintイベントのハンドラです。その代わりに、Panelから継承したカスタムコントロールを作成し、OnPaintメソッドをオーバーライドします。 hereを参照してください。

関連する問題