如何将具有间隔的椭圆绘制到PictureBox中

时间:2011-12-27 14:35:29

标签: c# .net winforms picturebox

我想用椭圆进行简单的模拟。在用户界面中,将有一个按钮。用户将按下按钮,然后以1秒的间隔,椭圆将被绘制到pictureBox。像交通信号灯......

我想我需要使用Timer Event,但不知道怎么做。 我怎样才能实现它?

我最诚挚的问候......

CodeWork

public void Button1_Click(Object sender, EventArgs e)
{
   DrawMap ortamcizdir = new DrawMap(p_box_map, bmp, ZoomControl, panel1);
   DrawCell hucrecizdir = new DrawCell (p_box_map, bmp, a, ZoomControl, ZoomKontrolBolen);
}

class DrawCell
{
   for (int z = 0; z < Ortamlar.HucreSayisi; z++)
   {
         Pen Kalem = new Pen(Brushes.CadetBlue);
         Rectangle rect = new Rectangle(((Ortamlar.HUCREKorX[z] / ScaleFactor - Ortamlar.HUCREYaricap[z] / ScaleFactor)
                        * ZoomKontrolCarpan) / ZoomKontrolBolen, (Ortamlar.HUCREKorY[z] / ScaleFactor - Ortamlar.HUCREYaricap[z] / ScaleFactor) *
                        ZoomKontrolCarpan / ZoomKontrolBolen, (2 * Ortamlar.HUCREYaricap[z] / ScaleFactor) * ZoomKontrolCarpan / ZoomKontrolBolen
                        , (2 * Ortamlar.HUCREYaricap[z] / ScaleFactor) * ZoomKontrolCarpan / ZoomKontrolBolen);

         if (Ortamlar.HUCREYZ[z] == "001:400")
         {
                g.DrawString(Ortamlar.HUCREKB[z].ToString(), Font, Brushes.Black, ((Ortamlar.HUCREKorX[z] / ScaleFactor)
                    * ZoomKontrolCarpan) / ZoomKontrolBolen, 600 - (Ortamlar.HUCREKorY[z] / ScaleFactor) *
                    ZoomKontrolCarpan / ZoomKontrolBolen);
                g.MultiplyTransform(new Matrix(1, 0, 0, -1, 0, 600));
                g.DrawEllipse(Kalem, rect);

                g.FillEllipse(Brushes.Wheat, rect);
                g.ResetTransform();

                picturebox1.Image = bmp;
                bmp1 = (Bitmap)picturebox1.Image;
          }
          if (Ortamlar.HUCREYZ[z] == "401:800")
          {
                        g.DrawString(Ortamlar.HUCREKB[z].ToString(), Font, Brushes.Black, ((Ortamlar.HUCREKorX[z] / ScaleFactor)
                    * ZoomKontrolCarpan) / ZoomKontrolBolen, 600 - (Ortamlar.HUCREKorY[z] / ScaleFactor) *
                    ZoomKontrolCarpan / ZoomKontrolBolen);
                        g.MultiplyTransform(new Matrix(1, 0, 0, -1, 0, 600));
                        g.DrawEllipse(Kalem, rect);

                        g.FillEllipse(Brushes.Yellow, rect);

                        g.ResetTransform();
                        picturebox1.Image = bmp;
                        bmp2 = (Bitmap)picturebox1.Image;
          }
          if (Ortamlar.HUCREYZ[z] == "801:120")
          {
                        g.DrawString(Ortamlar.HUCREKB[z].ToString(), Font, Brushes.Black, ((Ortamlar.HUCREKorX[z] / ScaleFactor)
                    * ZoomKontrolCarpan) / ZoomKontrolBolen, 600 - (Ortamlar.HUCREKorY[z] / ScaleFactor) *
                    ZoomKontrolCarpan / ZoomKontrolBolen);
                        g.MultiplyTransform(new Matrix(1, 0, 0, -1, 0, 600));
                        g.DrawEllipse(Kalem, rect);

                        g.FillEllipse(Brushes.Green, rect);
                        g.ResetTransform();

                        picturebox1.Image = bmp;
                        bmp3 = (Bitmap)picturebox1.Image;
          }
          if (Ortamlar.HUCREYZ[z] == "121:161")
          {
                        g.DrawString(Ortamlar.HUCREKB[z].ToString(), Font, Brushes.Black, ((Ortamlar.HUCREKorX[z] / ScaleFactor)
                    * ZoomKontrolCarpan) / ZoomKontrolBolen, 600 - (Ortamlar.HUCREKorY[z] / ScaleFactor) *
                        ZoomKontrolCarpan / ZoomKontrolBolen);
                        g.MultiplyTransform(new Matrix(1, 0, 0, -1, 0, 600));
                        g.DrawEllipse(Kalem, rect);

                        g.FillEllipse(Brushes.Blue, rect);

                        g.ResetTransform();
                        picturebox1.Image = bmp;
                        bmp4 = (Bitmap)picturebox1.Image;
          }
    }
}

1 个答案:

答案 0 :(得分:1)

我不太明白你问的目的,但我猜你可以通过阅读这些链接来了解。

first将指导您完成有关计时器控制的大部分知识。

second会让您了解绘制省略号的方法。

关于如何构建代码,简单地说,我得到启动计时器的按钮,然后timerElapsed事件将控制图片框中每隔1秒创建一个椭圆。

有关更多解释,我认为最好将一些代码放在一起,以便我们更好地了解您的意图。然后我们可以提供更多帮助。希望这会有所帮助。

<强> [EDIT1]

这将创建省略号,直到计时器停止,你可以添加一个按钮来停止timer1:

public void Button1_Click(Object sender, EventArgs e)
{
    timer1.Interval = 1000;
    timer1.Start();
}

private void timer1_Tick(object sender, EventArgs e)
{
    DrawMap ortamcizdir = new DrawMap(p_box_map, bmp, ZoomControl, panel1);
    DrawCell hucrecizdir = new DrawCell (p_box_map, bmp, a, ZoomControl, ZoomKontrolBolen);
}

不要忘记您需要将计时器拖到设计上,然后双击属性窗格上的勾选事件。

这个答案认为DrawCell类没有任何问题。

<强> [EDIT2]

我正在考虑我发布的内容,这将创建省略号,直到计时器停止。因此,您可能希望添加while语句来控制要绘制的椭圆数。检查this link以查看实施该方法的其他方法。

通过这种方式,您可以设置要绘制的省略号,这是一个示例,但有很多方法可以将它拉下来。

//create this field and property before the constructor
public int counter;
public int Counter
{
    get { return counter; }
    set { counter = value; }
}

//button click sets the interval to 1sec, starts timer and sets the int Counter to 0
private void button1_Click(object sender, EventArgs e)
{
    timer1.Interval = 1000;
    timer1.Start();
    Counter = 0;
}

//the tick event iterates the Counter property +1 everytime the timer advances 1sec and while Counter is under 5 for example it creates those objects
private void timer1_Tick(object sender, EventArgs e)
{
    Counter++;
    while (counter < 5)
    {
        DrawMap ortamcizdir = new DrawMap(p_box_map, bmp, ZoomControl, panel1);
        DrawCell hucrecizdir = new DrawCell (p_box_map, bmp, a, ZoomControl, ZoomKontrolBolen);
    }
}