本文介绍了计算面板C#winform应用程序中某个区域(指定区域)中的对象移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 大家好。 我在面板上调用了地图图像作为背景图像,并在其上指定了一个区域,方法是在其上绘制矩形框。因为稍后我将使用此区域框来计算对象的移动。 我遇到的问题一旦对象进入这个矩形框,计数器变量继续增加它的值 我想实现一个代码,一旦一个对象进入这个矩形框,然后它只增加一次计数器直到它保留在矩形框内 我尝试了什么: 我正在分享我的代码以便更好地了解我的情况 Hi everyone I called a map image on panel as background image, and I specify a zone on it by drawing the rectangular box on it. because later I will use this zone box to count the movement of object. the problem I am facing that once the object enters into this rectangle box the counter variable continues increment its valueI want to implement the code in which once an object entered into this rectangle box then it increments the counter only once until it remain inside the rectangle boxWhat I have tried:I am sharing my code for better understand my situationpublic partial class Form1 : Form { Pen Z1; Rectangle r1, r2; Image i1; Double t1x, t1y; Boolean draw1 = false; Graphics g; int count = 0; public Form1() { InitializeComponent(); Z1 = new Pen(Color.LightCyan, 3); r1 = new Rectangle(0, 0, 200, 200); // SIZE OF THE RECTANGLE STARTING FROM (0,0) OF THE IMAGE i1 = new Bitmap("Childr.png"); } private void Form1_Load(object sender, EventArgs e) { Thread Data_Read1 = new Thread(new ThreadStart(data_read1)); Data_Read1.Start(); typeof(Panel).InvokeMember("DoubleBuffered", BindingFlags.SetProperty | BindingFlags.Instance | BindingFlags.NonPublic, null, panel1, new object[] { true }); } private void panel_paint(object sender, PaintEventArgs e) { g = e.Graphics; g.DrawRectangle(Z1, r1);// DRAW RECTANGLE FOR ZONE 1 if (draw1 == true) { g.DrawImage(i1, Convert.ToInt32(t1y), Convert.ToInt32(t1x), 18, 21); } } public void data_read1() { UdpClient UdpServer = new UdpClient(1001); IPEndPoint Sender = new IPEndPoint(IPAddress.Any, 0); while (true) { Byte[] data1 = UdpServer.Receive(ref Sender); String line1 = Encoding.ASCII.GetString(data1); var part = line1.Split('\t'); if (Double.Parse(part[0]) == 1) { t1x = Math.Round((Double.Parse(part[1]) / 0.0347) + 75); t1y = Math.Round((Double.Parse(part[2]) / 0.0335) + 133); zone1_t1(); // method calling } if (data1 != null) { draw1 = true; panel1.Invoke(new Action(() => { panel1.Invalidate(); })); } else { draw1 = false; } } } private void zone1_t1() { if (r1.Contains(Convert.ToInt32(t1x), Convert.ToInt32(t1y))) // r1 is the rectangle box { count += 1; // counter variable to count the entire of an object label1.Invoke(new Action(() => { label1.Text = "COUNT: " + count.ToString(); })); } } }} 推荐答案 在zone1_t1中检查是否r1在矩形中,然后更新您的计数器。你感兴趣的是r1是否在矩形中,而之前它不是。 你可以创建一个字段previousInRectangle然后像这样使用它: In zone1_t1 you check if r1 is in the rectangle and then update your counter. What you are interested in is whether r1 is in the rectangle, while previously it wasn't.You can make a field previouslyInRectangle and then use it like so:bool previouslyInRectangle;private void zone1_t1(){ if (r1.Contains(Convert.ToInt32(t1x), Convert.ToInt32(t1y))) { // r1 is in the rectangle now if(!previouslyInRectangle) { // And it wasn't previously. count += 1; // counter check label1.Invoke(new Action(() => { label1.Text = "COUNT: " + count.ToString(); })); } previouslyInRectangle = true; } else { previouslyInRectangle = false; }} 这篇关于计算面板C#winform应用程序中某个区域(指定区域)中的对象移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-22 02:06