绘图矩形 [英] drawing rectangle

查看:116
本文介绍了绘图矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不丢失第一个矩形的情况下在表单上绘制多个矩形.换句话说,我需要将每个矩形都保留在内存中,但是如何保留呢?

how can i draw multiple rectangles on a form without loosing the first one. In other words I need to keep each and every rectangle in the memory but how?

推荐答案

基督教徒是正确的,但也许有点钝!
为了绘制任何持久性,您必须在每次需要绘制控件(表单是控件,面板也是如此)时,或者认为可能需要绘制时才绘制.

它通过Paint事件来执行此操作.如果处理,则每次绘制时都会绘制任何内容.因此,如果一个不同的应用程序移到您的顶部,则当另一个应用程序显示它的一部分时,您的应用程序将被重画.
Christian is correct, but perhaps a little blunt!

In order to draw anything persistant, you have to draw it every time the control (and forms are controls, so are panels) needs to be drawn, or thinks it might need to be drawn.

It does this by means of the Paint event. If you handle it, then whatever you draw gets drawn every time it is needed. So if a different app moves over the top of yours, yours gets redrawn when the other app reveals a part of it.
List<Rectangle> listOfRectangles = new List<Rectangle>();
...
private void MyControl_Paint(object sender, PaintEventArgs e)
    {
    Graphics g = e.Graphics;
    foreach (Rectangle r in listOfRectangles)
        {
        g.FillRectangle(brush, r);
        }
    }



要强制重画,请使用Invalidate方法-每个控件都有一个.

目前,忽略CreateGraphics方法-它包含一整袋装满蠕虫的袋子,直到您了解自己在做什么!



To force a redraw, use the Invalidate method - every control has one.

For the moment, ignore the CreateGraphics method - it contains a whole bag full of bags of worms until you understand what you are doing!


这仅仅是因为我已经知道像您一样的代码.以后,如果您想让人们修复该问题,请添加邮政编码,

您正在代码中调用creategraphics.除非您想绘制一个临时矩形,否则这是错误的.要绘制永久的任何东西,请处理paint事件并保留一个矩形列表或您要在其中绘制的任何内容.调用invalidate来触发绘画事件.
It''s only because I''ve seen code like yours, that I know. In future, post code if you want people to fix it,

You are calling creategraphics in your code. This is wrong, unless you WANT to draw a temporary rectangle. To draw permenant ANYTHING, handle the paint event and keep a list of rectangles or whatever you want to draw there. Call invalidate to trigger a paint event.


首先,为什么当您仍然有疑问时,为什么将答案标记为已解决问题?

其次,您要在创建矩形之前添加它.您的订单都在这里弄乱了:

First of all, why would you mark an answer as having solved your issue when you still have questions about it?

Secondly, you''re adding a Rectangle before creating it. Your order is all messed up here:

RecList.Add(rect); 
RecList = RecList.Distinct().ToList(); 
rect = new Rectangle(buttcorrs[0], buttcorrs[1], buttcorrs[3] - buttcorrs[1], buttcorrs[2] - buttcorrs[0]); 



如果您不了解基础知识(即必须先创建某些内容,然后才能将其添加到其他内容中),则需要向后退一步并从头开始.

我确实喜欢您确保没有重复的内容.

尝试将最后一行切换到RecList.Add(rect)之前.
实际上,我只是看到您的评论出了什么问题.

您有一个全局声明的变量,称为rect.

那你



If you don''t understand the basics (ie. you have to create something before you can add it to something else) then you need to take some steps backwards and start at the beginning.

I do like that you''re making sure there aren''t any duplicates.

Try switching the last line to before RecList.Add(rect).

Actually, I just saw what the problem was with your comment.

You have a globally declared variable called rect.

Then, you do

foreach (Rectangle rect in...)



您不能声明与全局变量同名的另一个变量.尝试使用其他名称.



you can''t declare another variable with the same name as one that''s global. try a different name.


这篇关于绘图矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆