1

我在一个表单上有一个面板,它创建了用于路由数据包的网络拓扑。我能够在面板上绘制拓扑。但是,现在我希望通过单击按钮将小方块从一个点移动到目的地。我怎样才能在面板上做到这一点?当我点击按钮时没有任何反应。如果有人可以帮助我,我将不胜感激。这是我的代码的一部分:

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;
}
4

1 回答 1

0

假设panel2_Paintpanel2.Paint事件的处理程序,该方法将在绘制发生后调用。相反,您要做的是创建一个从 Panel 继承的自定义控件并覆盖 OnPaint 方法。见这里

于 2012-05-05T05:46:46.463 回答