如何在PictureBox中绘制带有间隔的椭圆
本文关键字:PictureBox 绘制 | 更新日期: 2023-09-27 18:18:56
我想用椭圆做一个简单的模拟。在用户界面,将有一个按钮。用户将按下按钮,然后以1秒的间隔,将椭圆绘制到pictureBox。就像交通灯一样…
我想我需要使用定时器事件,但不知道如何。我怎样才能实现它?
My best wishes…
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;
}
}
}
我不太明白你问的目的,但我猜你可以通过阅读这些链接来接近。
第一部分将介绍你需要了解的关于定时器控制的大部分内容。
第二章会教你如何画出省略号
关于如何构建你的代码,简单地说,我会让按钮启动计时器,然后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);
}
不要忘记您需要将计时器拖到设计上,然后双击属性窗格上的Tick事件。
这个答案认为DrawCell类没有任何问题。
[edit2]
我在想我发布了什么,这将创建省略号,直到计时器停止。所以你可能想要添加一个while
语句来控制要画多少个椭圆。点击此链接查看其他实现方法。
这种方式可以设置要绘制的椭圆的数量,这是一个例子,但有很多方法可以实现。
//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);
}
}