如何在windows窗体应用程序中删除一个已存在的矩形
How to delete one of existed rectangles In windows forms application
我在pictureBox中创建了一些矩形,我想通过单击另一个按钮删除其中一个矩形,而不影响其他矩形。我使用"g->DrawRectangle()"来绘制矩形,但我不能删除其中一个。我尝试了pitureBox1->Refresh(),但它删除了我所有的矩形。我只想删掉其中一个。
我该怎么做呢?下面是代码:
private: System::Void button4_Click(System::Object^ sender, System::EventArgs^ e) {
{
int x;
int y;
int length;
int width;
Color b;
Graphics^ g = pictureBox1->CreateGraphics();
b = Color::FromArgb(Convert::ToInt32(textBox7->Text),Convert::ToInt32(textBox8->Text),Convert::ToInt32(textBox9->Text));
Pen^ myPen = gcnew Pen(b);
myPen->Width = 2.0F;
x = Convert::ToInt32(textBox10->Text);
y = Convert::ToInt32(textBox13->Text);
length = Convert::ToInt32(textBox11->Text);
width = Convert::ToInt32(textBox12->Text);
//Rectangle
hh = Rectangle(x, y, length, width);
g->DrawRectangle(myPen,hh);
}
您需要保留一个矩形列表来绘制,要删除一个只需停止绘制。
的例子:
std::vector<Rectangle> rectangles;
void YourButtonClick(...){
// do your stuff
hh = Rectangle(x, y, length, width);
rectangles.push_back(hh);
draw();
}
void draw()
{
pictureBox1->Refresh()
Graphics^ g = pictureBox1->CreateGraphics();
for(int i = 0, max = rectangles.size(); i<max; i++){
g->DrawRectangle(pen, rectangles[i]);
}
}
void deleteRectangle(int index){
Rectangle* rect = rectangles[index];
rectangles.erase(rectangles.begin()+index);
draw();
}
相关文章:
- 我们可以访问一个不存在的联盟的成员吗
- std::find,返回所有找到的值的替代方法,而不仅仅是存在重复的向量的第一个值
- 如何巧妙地编写两个函数——一个用于检查是否存在解决方案,另一个用于获取所有解决方案
- 我正在尝试制作一个自平衡机器人,但编译时存在错误。我不知道如何解决它
- C++ 删除存在于另一个矢量中的矢量项,同时保留顺序
- 在类的第一个/最后一个实例存在之前/之后调用一对函数.有没有更好的方法?
- 是否存在一个范围::视图::group_by对应项,它将所有元素都考虑在内,而不是只考虑连续的元素
- 带有 QSharedMemory 的 IPC,如果其中一个进程挂起,则存在风险
- 一个对象的两个指针.删除了一个指针,对象仍然存在
- SFINAE :知道一个函数是否已经存在
- 在一个VS项目中使用多个工具集 - 是否存在风险
- C ++如何创建一个即使对象被覆盖也会持续存在的指针?
- 在另一个对象 B 中创建对象 A 时,对象 A 是否是对象 B 的本地对象,对象 A 是否会存在于对象 B 的实例化之外?
- C++ 如何检查另一个向量中是否存在矢量的内容
- CreateFileWindows XP和7中存在一个问题
- 如果路径的每个元素不存在,请为其创建一个目录
- C++编译器在封装行为上存在分歧 - 哪一个做对了?
- 在已经存在的类中调用线程,而不是创建一个线程
- 控制一个字符串是否存在于另一个字符串中(以 C++ 为单位)
- typecasting和check是一个存在于向量中的实例