我在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);
}
答案 0 :(得分:1)
您需要保留一个要绘制的矩形列表,删除一个只需停止绘制它。
示例:
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();
}