如何在windows窗体应用程序中删除一个已存在的矩形

How to delete one of existed rectangles In windows forms application

本文关键字:一个 存在 windows 窗体 应用程序 删除      更新时间:2023-10-16

我在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();
}

相关文章: